From 8ce4e2a97c93043cc68bd6789742e1dd7a3cfedc Mon Sep 17 00:00:00 2001 From: Anupam Date: Thu, 5 Mar 2026 10:36:05 +0530 Subject: [PATCH] feat: kzb-01 enhance code --- python/gcp-compute/get_compute_engine_list.py | 6 ++-- python/gcp-compute/list_vms.py | 31 +++++++++++++++++++ python/gcp-compute/requirements.txt | 2 +- 3 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 python/gcp-compute/list_vms.py diff --git a/python/gcp-compute/get_compute_engine_list.py b/python/gcp-compute/get_compute_engine_list.py index ba35602..cff1e3d 100644 --- a/python/gcp-compute/get_compute_engine_list.py +++ b/python/gcp-compute/get_compute_engine_list.py @@ -3,9 +3,9 @@ def get_compute_instances(project_id): """ Prints all the google compute instances of a project """ - + instance_client = compute_v1.InstancesClient() - + request = compute_v1.AggregatedListInstancesRequest() request.project = project_id request.max_results = 50 @@ -24,4 +24,4 @@ def main(): get_compute_instances(args.project_id) if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/python/gcp-compute/list_vms.py b/python/gcp-compute/list_vms.py new file mode 100644 index 0000000..a4119e2 --- /dev/null +++ b/python/gcp-compute/list_vms.py @@ -0,0 +1,31 @@ +from google.cloud import compute_v1 + +def list_vms(project_id, instance_zone): + """ Prints all the google compute instances of a project """ + + # Empty list to store instance names + instance_names = [] + + # Create a client + client = compute_v1.InstancesClient() + + # Initialize request argument(s) + request = compute_v1.ListInstancesRequest( + project=project_id, + zone=instance_zone, + ) + + # Make the request + page_result = client.list(request=request) + + # Handle the response + for response in page_result: + print(response.name) + instance_names.append(response.name) + + return instance_names + +if __name__ == "__main__": + project_id = "tidal-outlook-488409-e6" + instance_zone = "us-central1-c" + list_vms(project_id, instance_zone) diff --git a/python/gcp-compute/requirements.txt b/python/gcp-compute/requirements.txt index 3fb67a8..7d97871 100644 --- a/python/gcp-compute/requirements.txt +++ b/python/gcp-compute/requirements.txt @@ -1,3 +1,3 @@ requests google-cloud-compute==1.11.0 -google-cloud-resource-manager==1.10.1 \ No newline at end of file +google-cloud-resource-manager==1.10.1