Google Cloud Compute Engine is an Infrastructure-as-a-Service (IaaS) offering from GCP that allows you to run virtual machines (VMs), manage disks, networks, firewalls, snapshots, and images. This handler (GcpComputeHandler) provides methods to collect detailed information about Compute resources for governance, auditing, and monitoring purposes. It authenticates using a GCP service account and leverages the Google Cloud SDK (google-cloud-compute) to fetch data.

Example

To create the GcpComputeHandler object with your service account:
import os
import json
from superagentx_handlers.gcp.compute import GcpComputeHandler

# Load service account credentials
with open("service_account.json", "r") as f:
    service_account_info = json.load(f)

compute_handler = GcpComputeHandler(service_account_info=service_account_info)
Collect Instances:
Retrieve details of all VM instances in a project or specific zone.
instances = await compute_handler.collect_instances(project_id="my-project-id", zone="us-central1-a")
print(instances[:2])  # preview first 2 VMs
Collect Disks:
Fetch persistent disk information across all zones or a specific zone.
disks = await compute_handler.collect_disks(project_id="my-project-id")
print(disks[:2])  # preview first 2 disks
Collect Networks:
Get all VPC networks within a project.
networks = await compute_handler.collect_networks(project_id="my-project-id")
print(networks)
Collect Subnetworks:
Retrieve subnetworks across all regions or a specific region.
subnets = await compute_handler.collect_subnetworks(project_id="my-project-id", region="us-central1")
print(subnets[:2])
Collect Firewall Rules:
Fetch firewall rules that define ingress/egress traffic control for resources.
firewalls = await compute_handler.collect_firewall_rules(project_id="my-project-id")
print(firewalls[:2])
Collect Images:
Retrieve custom images created in the project.
images = await compute_handler.collect_images(project_id="my-project-id")
print(images)
Collect Snapshots:
Fetch disk snapshots for backup and disaster recovery.
snapshots = await compute_handler.collect_snapshots(project_id="my-project-id")
print(snapshots)
Collect All Compute Info:
Retrieve a comprehensive dictionary with all compute resources (instances, disks, networks, subnetworks, firewalls, images, and snapshots).
all_compute_info = await compute_handler.collect_all_compute_info(project_id="my-project-id")
print(all_compute_info.keys())