Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[vpc] enhancements #6

Merged
merged 2 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions aws_quota/check/quota_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class QuotaCheck:
scope: QuotaScope = None
service_code: str = None
quota_code: str = None
warning_threshold: float = None
error_threshold: float = None

def __init__(self, boto_session: boto3.Session) -> None:
super().__init__()
Expand Down
53 changes: 53 additions & 0 deletions aws_quota/check/vpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ def get_vpc_by_id(session: boto3.Session, vpc_id: str) -> dict:
except StopIteration:
raise KeyError

@cachetools.cached(cache=cachetools.TTLCache(1, 60))
def get_vpc_peering_connections(session: boto3.Session) -> typing.List[dict]:
return session.client('ec2').describe_vpc_peering_connections(
Filters=[
{'Name': 'status-code', 'Values': ['active']},
]
)['VpcPeeringConnections']

@cachetools.cached(cache=cachetools.TTLCache(1, 60))
def get_all_sgs(session: boto3.Session) -> typing.List[dict]:
Expand Down Expand Up @@ -80,6 +87,16 @@ class InternetGatewayCountCheck(QuotaCheck):
def current(self):
return len(self.boto_session.client('ec2').describe_internet_gateways()['InternetGateways'])

class VpcEndpointCountCheck(QuotaCheck):
key = "vpc_endpoint"
description = "Gateway VPC endpoints per Region"
scope = QuotaScope.REGION
service_code = 'vpc'
quota_code = 'L-1B52E74A'

@property
def current(self):
return len(self.boto_session.client('ec2').describe_vpc_endpoints()['VpcEndpoints'])

class NetworkInterfaceCountCheck(QuotaCheck):
key = "ni_count"
Expand All @@ -104,6 +121,16 @@ class SecurityGroupCountCheck(QuotaCheck):
def current(self):
return len(self.boto_session.client('ec2').describe_security_groups()['SecurityGroups'])

class NatGatewayCountCheck(QuotaCheck):
key = "nat_count"
description = "NAT gateways per Region"
scope = QuotaScope.REGION
service_code = 'vpc'
quota_code = 'L-FE5A380F'

@property
def current(self):
return len(self.boto_session.client('ec2').describe_nat_gateways()['NatGateways'])

class RulesPerSecurityGroupCheck(InstanceQuotaCheck):
key = "vpc_rules_per_sg"
Expand Down Expand Up @@ -275,3 +302,29 @@ def current(self) -> int:
return len(list(filter(lambda cbas: cbas['Ipv6CidrBlockState']['State'] == 'associated', vpc['Ipv6CidrBlockAssociationSet'])))
except KeyError:
raise InstanceWithIdentifierNotFound(self)

class ActiveVpcPeeringConnectionsPerVpcCheck(InstanceQuotaCheck):
key = "vpc_peering_connections_per_vpc"
description = "Active VPC peering connections per VPC"
service_code = 'vpc'
quota_code = 'L-7E9ECCDB'
instance_id = 'VPC ID'

@staticmethod
def get_all_identifiers(session: boto3.Session) -> typing.List[str]:
return [vpc['VpcId'] for vpc in get_all_vpcs(session)]

@property
def current(self) -> int:
peering_connections_per_vpc = 0
try:
vpc = get_vpc_by_id(self.boto_session, self.instance_id)
vpc_peering_connections = get_vpc_peering_connections(self.boto_session)
for peering_connection in vpc_peering_connections:
for vpc_info in [peering_connection['AccepterVpcInfo'], peering_connection['RequesterVpcInfo']]:
if vpc_info['VpcId'] == vpc['VpcId'] and self.boto_session.region_name == vpc_info['Region']:
peering_connections_per_vpc += 1

return peering_connections_per_vpc
except KeyError:
raise InstanceWithIdentifierNotFound(self)
Loading