Skip to content

Commit

Permalink
Merge pull request #397 from SUSE/gce_dump
Browse files Browse the repository at this point in the history
Add dump stat for GCE
  • Loading branch information
asmorodskyi authored Jul 24, 2024
2 parents d1211b4 + d2b6109 commit 7eb98e4
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 0 deletions.
38 changes: 38 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 @@ -65,6 +66,43 @@ def dump_state():
namespace,
EC2(namespace).count_all_volumes
)
Influx().dump_resource(
ProviderChoice.EC2.value,
Influx.VPC_QUANTITY,
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
7 changes: 7 additions & 0 deletions ocw/lib/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,13 @@ def count_all_volumes(self) -> int:
all_volumes_cnt += len(response['Volumes'])
return all_volumes_cnt

def count_all_vpc(self) -> int:
all_vpcs = 0
for region in self.all_regions:
response = self.ec2_client(region).describe_vpcs(Filters=[{'Name': 'isDefault', 'Values': ['false']}])
all_vpcs += len(response['Vpcs'])
return all_vpcs

def cleanup_images(self, valid_period_days: float) -> None:
self.log_dbg('Call cleanup_images')
for region in self.all_regions:
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))
3 changes: 3 additions & 0 deletions ocw/lib/influx.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +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
4 changes: 4 additions & 0 deletions tests/test_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,3 +532,7 @@ def test_count_all_volumes(ec2_patch):
'Tags': [{'Key': 'pcw_ignore', 'Value': '1'}]}, ]
}
assert ec2_patch.count_all_volumes() == 3


def test_count_all_vpcs(ec2_patch_for_vpc):
assert ec2_patch_for_vpc.count_all_vpc() == 1
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 7eb98e4

Please sign in to comment.