diff --git a/ocw/lib/dump_state.py b/ocw/lib/dump_state.py index dacb023..0d7bbcb 100644 --- a/ocw/lib/dump_state.py +++ b/ocw/lib/dump_state.py @@ -4,6 +4,7 @@ from webui.PCWConfig import PCWConfig from ocw.lib.azure import Azure from ocw.lib.ec2 import EC2 +from ocw.lib.gce import GCE from ocw.enums import ProviderChoice from ocw.lib.influx import Influx @@ -71,6 +72,37 @@ def dump_state(): namespace, EC2(namespace).count_all_vpc ) + if ProviderChoice.GCE in providers: + Influx().dump_resource( + ProviderChoice.GCE.value, + Influx.VMS_QUANTITY, + namespace, + GCE(namespace).count_all_instances + ) + Influx().dump_resource( + ProviderChoice.GCE.value, + Influx.IMAGES_QUANTITY, + namespace, + GCE(namespace).count_all_images + ) + Influx().dump_resource( + ProviderChoice.GCE.value, + Influx.DISK_QUANTITY, + namespace, + GCE(namespace).count_all_disks + ) + Influx().dump_resource( + ProviderChoice.GCE.value, + Influx.BLOB_QUANTITY, + namespace, + GCE(namespace).count_all_blobs + ) + Influx().dump_resource( + ProviderChoice.GCE.value, + Influx.NETWORK_QUANTITY, + namespace, + GCE(namespace).count_all_networks + ) except Exception: logger.exception( "[%s] Dump state failed!: \n %s", namespace, traceback.format_exc() diff --git a/ocw/lib/gce.py b/ocw/lib/gce.py index 8924971..1cb1bee 100644 --- a/ocw/lib/gce.py +++ b/ocw/lib/gce.py @@ -264,3 +264,22 @@ def cleanup_networks(self) -> None: self._delete_resource( self.compute_client().networks, network["name"], project=self.project, network=network["name"] ) + + def count_all_instances(self) -> int: + return len(self.list_all_instances()) + + def count_all_images(self) -> int: + return len(self._paginated(self.compute_client().images, project=self.project)) + + def count_all_disks(self) -> int: + all_disks = 0 + for region in self.list_regions(): + for zone in self.list_zones(region): + all_disks += len(self._paginated(self.compute_client().disks, project=self.project, zone=zone)) + return all_disks + + def count_all_blobs(self) -> int: + return len(self._paginated(self.storage_client().objects, bucket=self.__bucket)) + + def count_all_networks(self) -> int: + return len(self._paginated(self.compute_client().networks, project=self.project)) diff --git a/ocw/lib/influx.py b/ocw/lib/influx.py index 5e5f48f..a24e50d 100644 --- a/ocw/lib/influx.py +++ b/ocw/lib/influx.py @@ -17,9 +17,11 @@ class Influx: VMS_QUANTITY: str = "vms_quantity" IMAGES_QUANTITY: str = "images_quantity" DISK_QUANTITY: str = "disk_quantity" + BLOB_QUANTITY: str = "blob_quantity" VOLUMES_QUANTITY: str = "volumes_quanity" IMAGE_VERSION_QUANTITY: str = "img_version_quantity" VPC_QUANTITY: str = "vpc_quantity" + NETWORK_QUANTITY: str = "network_quantity" NAMESPACE_TAG: str = "namespace" def __init__(self) -> None: diff --git a/tests/test_gce.py b/tests/test_gce.py index baf3f6d..f33ff82 100644 --- a/tests/test_gce.py +++ b/tests/test_gce.py @@ -253,3 +253,35 @@ def __init__(self, content) -> None: assert GCE.get_error_reason(MockHttpError({'error': {'errors': []}})) == "unknown" assert GCE.get_error_reason(MockHttpError({'error': {'errors': [{}]}})) == "unknown" assert GCE.get_error_reason(MockHttpError({'error': {'errors': [{'reason': 'aaa'}]}})) == "aaa" + + +def test_count_all_instances(gce): + with ( + patch.object(gce, 'list_regions', return_value=['region1']), + patch.object(gce, 'list_zones', return_value=['zone1']), + ): + assert gce.count_all_instances() == 2 + + +def test_count_all_images(gce): + with (patch.object(gce, '_paginated', return_value=[1, 2, 3, 4])): + assert gce.count_all_images() == 4 + + +def test_count_all_disks(gce): + with ( + patch.object(gce, 'list_regions', return_value=['region1']), + patch.object(gce, 'list_zones', return_value=['zone1']), + patch.object(gce, '_paginated', return_value=[1, 2, 3, 4]), + ): + assert gce.count_all_disks() == 4 + + +def test_count_all_blobs(gce): + with (patch.object(gce, '_paginated', return_value=[1, 2, 3, 4])): + assert gce.count_all_blobs() == 4 + + +def test_count_all_networks(gce): + with (patch.object(gce, '_paginated', return_value=[1, 2, 3, 4])): + assert gce.count_all_networks() == 4