Skip to content

Commit

Permalink
Add count of EC2 VPCs
Browse files Browse the repository at this point in the history
  • Loading branch information
asmorodskyi committed Jul 24, 2024
1 parent d1211b4 commit e227f91
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ocw/lib/dump_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ def dump_state():
namespace,
EC2(namespace).count_all_volumes
)
Influx().dump_resource(
ProviderChoice.EC2.value,
Influx.VPC_QUANTITY,
namespace,
EC2(namespace).count_all_vpc
)
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
1 change: 1 addition & 0 deletions ocw/lib/influx.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Influx:
DISK_QUANTITY: str = "disk_quantity"
VOLUMES_QUANTITY: str = "volumes_quanity"
IMAGE_VERSION_QUANTITY: str = "img_version_quantity"
VPC_QUANTITY: str = "vpc_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

0 comments on commit e227f91

Please sign in to comment.