Skip to content

Commit

Permalink
Add dump stat for GCE
Browse files Browse the repository at this point in the history
  • Loading branch information
asmorodskyi committed Jul 24, 2024
1 parent e227f91 commit d2b6109
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
32 changes: 32 additions & 0 deletions ocw/lib/dump_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down
19 changes: 19 additions & 0 deletions ocw/lib/gce.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
2 changes: 2 additions & 0 deletions ocw/lib/influx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
32 changes: 32 additions & 0 deletions tests/test_gce.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d2b6109

Please sign in to comment.