From 2082f712a261ac881e43d93ea7aef9ccba364340 Mon Sep 17 00:00:00 2001 From: Anupam Date: Thu, 5 Mar 2026 09:38:03 +0530 Subject: [PATCH] refactor: kzb-01 fldr renamed --- .../get_compute_engine_list.py | 27 ------- .../gcp-compute-engine/get_gce_metadata.py | 34 --------- .../gcp-compute-engine/get_gce_properties.py | 34 --------- .../get_instances_with_eips.py | 25 ------- .../get_unused_compute_disks.py | 20 ----- .../get_unused_external_ips_1.py | 26 ------- .../get_unused_external_ips_2.py | 74 ------------------- .../gcp-compute-engine/requirements.txt | 3 - .../stop_compute_instance.py | 44 ----------- python-samples/gcp-key-management/.gitkeep | 0 .../gcp-key-management/get_key_details.py | 22 ------ .../gcp-key-management/requirements.txt | 2 - .../gcp-resource-manager/get_all_projects.py | 40 ---------- .../gcp-resource-manager/requirements.txt | 1 - 14 files changed, 352 deletions(-) delete mode 100644 python-samples/gcp-compute-engine/get_compute_engine_list.py delete mode 100644 python-samples/gcp-compute-engine/get_gce_metadata.py delete mode 100644 python-samples/gcp-compute-engine/get_gce_properties.py delete mode 100644 python-samples/gcp-compute-engine/get_instances_with_eips.py delete mode 100644 python-samples/gcp-compute-engine/get_unused_compute_disks.py delete mode 100644 python-samples/gcp-compute-engine/get_unused_external_ips_1.py delete mode 100644 python-samples/gcp-compute-engine/get_unused_external_ips_2.py delete mode 100644 python-samples/gcp-compute-engine/requirements.txt delete mode 100644 python-samples/gcp-compute-engine/stop_compute_instance.py delete mode 100644 python-samples/gcp-key-management/.gitkeep delete mode 100644 python-samples/gcp-key-management/get_key_details.py delete mode 100644 python-samples/gcp-key-management/requirements.txt delete mode 100644 python-samples/gcp-resource-manager/get_all_projects.py delete mode 100644 python-samples/gcp-resource-manager/requirements.txt diff --git a/python-samples/gcp-compute-engine/get_compute_engine_list.py b/python-samples/gcp-compute-engine/get_compute_engine_list.py deleted file mode 100644 index ba35602..0000000 --- a/python-samples/gcp-compute-engine/get_compute_engine_list.py +++ /dev/null @@ -1,27 +0,0 @@ -import argparse -from google.cloud import compute_v1 - -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 - - agg_list = instance_client.aggregated_list(request=request) - - for zone, response in agg_list: - if response.instances: - for instance in response.instances: - print(f"Instance Name: {instance.name}, Instance ID: {instance.id}, Instance Zone: {zone}") - -def main(): - parser = argparse.ArgumentParser() - parser.add_argument("project_id", help="Your Google Cloud project ID.") - args = parser.parse_args() - get_compute_instances(args.project_id) - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/python-samples/gcp-compute-engine/get_gce_metadata.py b/python-samples/gcp-compute-engine/get_gce_metadata.py deleted file mode 100644 index 6986cdf..0000000 --- a/python-samples/gcp-compute-engine/get_gce_metadata.py +++ /dev/null @@ -1,34 +0,0 @@ -# Import the required python libraries -import requests - -def get_instance_metadata(root_url, request_header, metadata_list): - """ - This function returns the google compute instance metadata. - - Note: You can query the contents of the metadata server by making a request to the root URLs from within - a virtual machine instance. - Use the http://metadata.google.internal/computeMetadata/v1/ root URL to make requests to metadata server. - - Ref: https://cloud.google.com/compute/docs/metadata/overview#querying - """ - - for metadata in metadata_list: - try: - response = requests.get(root_url + metadata, headers=request_header) - print(response.text) - except Exception as error: - print(f'Error occurred: {error}') - -def main(): - """ - This main function calls the `get_instance_metadata` function to get google compute instance metadata. - """ - - root_url = "http://metadata.google.internal/computeMetadata/v1/" - request_header = {"Metadata-Flavor": "Google"} - metadata_list = ["instance/hostname", "instance/network-interfaces/0/ip"] - - get_instance_metadata(root_url, request_header, metadata_list) - -if __name__ == "__main__": - main() diff --git a/python-samples/gcp-compute-engine/get_gce_properties.py b/python-samples/gcp-compute-engine/get_gce_properties.py deleted file mode 100644 index 1582f0f..0000000 --- a/python-samples/gcp-compute-engine/get_gce_properties.py +++ /dev/null @@ -1,34 +0,0 @@ -# Import the required python libraries -from google.cloud import compute_v1 - -def get_instance_properties(project_id, zone, instance_name): - """ - This function returns the google compute instance properties. - """ - - instances_client = compute_v1.InstancesClient() - - try: - instance_properties = instances_client.get(project=project_id, zone=zone, instance=instance_name) - if instance_properties: - print(f"Instance Name: {instance_properties.name}") - print(f"Instance ID: {instance_properties.id}") - print(f"Instance Status: {instance_properties.status}") - print(f"Instance Machine Type: {instance_properties.machine_type}") - print(f"Instance Creation Time: {instance_properties.creation_timestamp}") - except Exception as error: - print(f"Error retrieving instance properties: {error}") - -def main(): - """ - This main function calls the `get_instance_properties` function to get google compute instance properties. - """ - - project_id = "UPDATE_ME" - zone = "UPDATE_ME" - instance_name = "UPDATE_ME" - - get_instance_properties(project_id, zone, instance_name) - -if __name__ == "__main__": - main() diff --git a/python-samples/gcp-compute-engine/get_instances_with_eips.py b/python-samples/gcp-compute-engine/get_instances_with_eips.py deleted file mode 100644 index bdba7bf..0000000 --- a/python-samples/gcp-compute-engine/get_instances_with_eips.py +++ /dev/null @@ -1,25 +0,0 @@ -import argparse -from google.cloud import compute_v1 - -def get_gce_with_eips(project_id): - """ Prints all the instances with external IP in any of its network interface """ - - instance_client = compute_v1.InstancesClient() - agg_list = instance_client.aggregated_list(project=project_id) - - for zone, response in agg_list: - if response.instances: - for instance in response.instances: - for interface in instance.network_interfaces: - if interface.access_configs: - print(f"Instance Name: {instance.name}, Instance ID: {instance.id}, Instance Zone: {zone}") - break - -def main(): - parser = argparse.ArgumentParser() - parser.add_argument("project_id", help="Your Google Cloud project ID.") - args = parser.parse_args() - get_gce_with_eips(args.project_id) - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/python-samples/gcp-compute-engine/get_unused_compute_disks.py b/python-samples/gcp-compute-engine/get_unused_compute_disks.py deleted file mode 100644 index 57e8a9f..0000000 --- a/python-samples/gcp-compute-engine/get_unused_compute_disks.py +++ /dev/null @@ -1,20 +0,0 @@ -from google.cloud import compute_v1 - -def get_unused_compute_disks(project_id): - """ - Prints the unused regional/zonal compute disks of a project. - """ - - addresses_client = compute_v1.DisksClient() - for zone, response in addresses_client.aggregated_list(project=project_id): - if response.disks: - for disk in response.disks: - if len(disk.users) == 0: - print(f"Disk Name: {disk.name}, Disk Size: {disk.size_gb}, Disk Location: {zone}") - -def main(project_id): - get_unused_compute_disks(project_id) - -if __name__ == "__main__": - project_id = "UPDATE_ME" - main(project_id) \ No newline at end of file diff --git a/python-samples/gcp-compute-engine/get_unused_external_ips_1.py b/python-samples/gcp-compute-engine/get_unused_external_ips_1.py deleted file mode 100644 index 91cbdcd..0000000 --- a/python-samples/gcp-compute-engine/get_unused_external_ips_1.py +++ /dev/null @@ -1,26 +0,0 @@ -# from google.oauth2 import service_account -from google.cloud import compute_v1 - -# Use this, if authentication to be done via service account -# credentials = service_account.Credentials.from_service_account_file('sa-key.json') -# addresses_client = compute_v1.AddressesClient(credentials=credentials) - -def get_reserved_ips(project_ids): - addresses_client = compute_v1.AddressesClient() - - for project_id in project_ids: - request = compute_v1.AggregatedListAddressesRequest() - request.project = project_id - - for region, response in addresses_client.aggregated_list(request=request): - if response.addresses: - for address in response.addresses: - if(address.address_type == "EXTERNAL" and address.status == "RESERVED"): - print("External IP:", address.name, "Region:", region) - -def main(): - project_ids = ["UPDATE_ME"] - get_reserved_ips(project_ids) - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/python-samples/gcp-compute-engine/get_unused_external_ips_2.py b/python-samples/gcp-compute-engine/get_unused_external_ips_2.py deleted file mode 100644 index 9b81732..0000000 --- a/python-samples/gcp-compute-engine/get_unused_external_ips_2.py +++ /dev/null @@ -1,74 +0,0 @@ -"""Get Unsed External IP Addresses - -This script prints all the unused external IP addresses of all the projects -present under a parent(folder/organization) along with details like (region, -external IP name, project ID) - -This script requires that Cloud Client Libraries of gcp services -`google-cloud-compute` and `google-cloud-resource-manager` be installed -within the Python environment you are running this script in. - -This file can also be imported as a module and contains the following -functions: - - * get_gcp_projects - returns the list of projects under a parent - * get_reserved_ips - prints the unused eips along with details - * main - the main function of the script -""" - -from google.cloud import compute_v1, resourcemanager_v3 - -def get_gcp_projects(parent): - """Returns the list of projects under a parent (folder/org) - - Args: - parent (str): the id of the parent whose projects to be returned - - Returns: - list: a list of strings representing the projects under a parent - """ - - project_ids = [] - - # Create a client - client = resourcemanager_v3.ProjectsClient() - # Initialize request argument(s) - request = resourcemanager_v3.ListProjectsRequest(parent=parent) - # Make the request - page_result = client.list_projects(request=request) - # Handle the response - for response in page_result: - project_ids.append(response.project_id) - return project_ids - -def get_reserved_ips(project_ids): - """Prints the list of unused external IPs of a GCP Project - - Args: - project_ids (list): the list of the projects whose unused external - IP addresses to be printed - - Returns: - it returns None as the function doesn't have any return statement - but it prints the list of unused external IPs of a GCP Project - """ - - addresses_client = compute_v1.AddressesClient() - - for project_id in project_ids: - request = compute_v1.AggregatedListAddressesRequest() - request.project = project_id - - for region, response in addresses_client.aggregated_list(request=request): - if response.addresses: - for address in response.addresses: - if(address.address_type == "EXTERNAL" and address.status == "RESERVED"): - print(f"Project ID: {project_id} External IP: {address.name} Region: {region}") - -def main(): - parent="organizations/UPDATE_ME" # For folder, use "folders/UPDATE_ME" - project_ids = get_gcp_projects(parent) - get_reserved_ips(project_ids) - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/python-samples/gcp-compute-engine/requirements.txt b/python-samples/gcp-compute-engine/requirements.txt deleted file mode 100644 index 3fb67a8..0000000 --- a/python-samples/gcp-compute-engine/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -requests -google-cloud-compute==1.11.0 -google-cloud-resource-manager==1.10.1 \ No newline at end of file diff --git a/python-samples/gcp-compute-engine/stop_compute_instance.py b/python-samples/gcp-compute-engine/stop_compute_instance.py deleted file mode 100644 index 682fc83..0000000 --- a/python-samples/gcp-compute-engine/stop_compute_instance.py +++ /dev/null @@ -1,44 +0,0 @@ -from google.cloud import compute_v1 - -def get_compute_instances(project_id): - """ - Gets the details list of targetted Google Compute Engine instances. - """ - - instance_list = [] # Initialize an empty list to store instance details - - instance_client = compute_v1.InstancesClient() - agg_list = instance_client.aggregated_list(project=project_id) - - for zone, response in agg_list: - if response.instances: - for instance in response.instances: - if ("purpose", "test") in instance.labels.items(): - temp_instance_dict = {} # Initialize a dictionary to temporary hold and dump values in a dict - temp_instance_dict.update({"instance_name" : instance.name}) - temp_instance_dict.update({"instance_zone" : zone.split("/")[1]}) - temp_instance_dict.update({"instance_project" : project_id}) - instance_list.append(temp_instance_dict) - return instance_list - -def stop_compute_instances(instance_list): - """ - Stops running Google Compute Engine instances. - """ - instance_client = compute_v1.InstancesClient() - - for instance in instance_list: - operation = instance_client.stop( - project=instance["instance_project"], zone=instance["instance_zone"], instance=instance["instance_name"] - ) - result = operation.result(timeout=300) - - print("Stop operation completed for targetted compute instances.") - -def main(project_id): - instance_list = get_compute_instances(project_id) - stop_compute_instances(instance_list) - -if __name__ == "__main__": - project_id = "UPDATE_ME" - main(project_id) \ No newline at end of file diff --git a/python-samples/gcp-key-management/.gitkeep b/python-samples/gcp-key-management/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/python-samples/gcp-key-management/get_key_details.py b/python-samples/gcp-key-management/get_key_details.py deleted file mode 100644 index ac95f0e..0000000 --- a/python-samples/gcp-key-management/get_key_details.py +++ /dev/null @@ -1,22 +0,0 @@ -from google.cloud import kms_v1 - -def get_crypto_key(crypto_key_name): - client = kms_v1.KeyManagementServiceClient() - - # request = kms_v1.GetCryptoKeyRequest( - # name=crypto_key_name, - # ) - # response = client.get_crypto_key(request=request) - - response = client.get_crypto_key(name=crypto_key_name) - if response: - if(response.rotation_period.days >= 90): - print("Key rotation period is good.") - -def main(): - crypto_key_name = "projects/UPDATE_ME/locations/UPDATE_ME/keyRings/UPDATE_ME/cryptoKeys/UPDATE_ME" - get_crypto_key(crypto_key_name) - -# Executing as standalone script -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/python-samples/gcp-key-management/requirements.txt b/python-samples/gcp-key-management/requirements.txt deleted file mode 100644 index d330145..0000000 --- a/python-samples/gcp-key-management/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -google-cloud-resource-manager==1.10.1 -google-cloud-kms==2.17.0 \ No newline at end of file diff --git a/python-samples/gcp-resource-manager/get_all_projects.py b/python-samples/gcp-resource-manager/get_all_projects.py deleted file mode 100644 index 369f00a..0000000 --- a/python-samples/gcp-resource-manager/get_all_projects.py +++ /dev/null @@ -1,40 +0,0 @@ -from google.cloud import resourcemanager_v3 - -def list_containers(parent): - all_project_ids = [] - all_folder_ids = [] - folder_ids = [] - - prj_client = resourcemanager_v3.ProjectsClient() - prj_result = prj_client.list_projects(parent=parent) - if prj_result: - for prj_response in prj_result: - all_project_ids.append(prj_response.project_id) - - fldr_client = resourcemanager_v3.FoldersClient() - fldr_result = fldr_client.list_folders(parent=parent) - if fldr_result: - for fldr_response in fldr_result: - all_folder_ids.append(fldr_response.name) - folder_ids.append(fldr_response.name) - - while all_folder_ids: - folder_id = all_folder_ids.pop() - subfldr_result = fldr_client.list_folders(parent=folder_id) - if subfldr_result: - for subfldr_response in subfldr_result: - all_folder_ids.append(subfldr_response.name) - folder_ids.append(subfldr_response.name) - - project_under_folder_result = prj_client.list_projects(parent=folder_id) - if project_under_folder_result: - for prj_under_fldr_response in project_under_folder_result: - all_project_ids.append(prj_under_fldr_response.project_id) - return(f"Project IDs:{all_project_ids}, Folder IDs:{folder_ids}") - -def main(): - parent="organizations/UPDATE_ME" - print(list_containers(parent)) - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/python-samples/gcp-resource-manager/requirements.txt b/python-samples/gcp-resource-manager/requirements.txt deleted file mode 100644 index 0814a65..0000000 --- a/python-samples/gcp-resource-manager/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -google-cloud-resource-manager==1.10.1 \ No newline at end of file