Skip to content

Commit

Permalink
add check for target groups per alb (#18)
Browse files Browse the repository at this point in the history
* add check for target groups per alb and format
  • Loading branch information
mgreschl authored Apr 30, 2021
1 parent 2599886 commit bd35344
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

- new check: elb_target_groups_per_alb

### Fixed

- fix wrong number of EBS snapshots
Expand Down
83 changes: 67 additions & 16 deletions aws_quota/check/elb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@


def get_albs(session: boto3.Session):
return list(filter(lambda lb: lb['Type'] == 'application', session.client('elbv2').describe_load_balancers()['LoadBalancers']))
return list(
filter(
lambda lb: lb['Type'] == 'application',
session.client('elbv2').describe_load_balancers()['LoadBalancers'],
)
)


def get_nlbs(session: boto3.Session):
return list(filter(lambda lb: lb['Type'] == 'network', session.client('elbv2').describe_load_balancers()['LoadBalancers']))
return list(
filter(
lambda lb: lb['Type'] == 'network',
session.client('elbv2').describe_load_balancers()['LoadBalancers'],
)
)


class ClassicLoadBalancerCountCheck(QuotaCheck):
Expand All @@ -21,7 +31,9 @@ class ClassicLoadBalancerCountCheck(QuotaCheck):

@property
def current(self):
return len(self.boto_session.client('elb').describe_load_balancers()['LoadBalancerDescriptions'])
return len(
self.boto_session.client('elb').describe_load_balancers()['LoadBalancerDescriptions']
)


class ListenerPerClassicLoadBalancerCountCheck(InstanceQuotaCheck):
Expand All @@ -33,14 +45,19 @@ class ListenerPerClassicLoadBalancerCountCheck(InstanceQuotaCheck):

@staticmethod
def get_all_identifiers(session: boto3.Session) -> typing.List[str]:
return [lb['LoadBalancerName'] for lb in session.client('elb').describe_load_balancers()['LoadBalancerDescriptions']]
return [
lb['LoadBalancerName']
for lb in session.client('elb').describe_load_balancers()['LoadBalancerDescriptions']
]

@property
def current(self):
try:
return len(self.boto_session.client('elb').describe_load_balancers(
LoadBalancerNames=[self.instance_id]
)['LoadBalancerDescriptions'][0]['ListenerDescriptions'])
return len(
self.boto_session.client('elb').describe_load_balancers(
LoadBalancerNames=[self.instance_id]
)['LoadBalancerDescriptions'][0]['ListenerDescriptions']
)
except self.boto_session.client('elb').exceptions.AccessPointNotFoundException as e:
raise InstanceWithIdentifierNotFound(self) from e

Expand All @@ -54,7 +71,14 @@ class NetworkLoadBalancerCountCheck(QuotaCheck):

@property
def current(self):
return len(list(filter(lambda lb: lb['Type'] == 'network', self.boto_session.client('elbv2').describe_load_balancers()['LoadBalancers'])))
return len(
list(
filter(
lambda lb: lb['Type'] == 'network',
self.boto_session.client('elbv2').describe_load_balancers()['LoadBalancers'],
)
)
)


class ListenerPerNetworkLoadBalancerCountCheck(InstanceQuotaCheck):
Expand All @@ -66,14 +90,16 @@ class ListenerPerNetworkLoadBalancerCountCheck(InstanceQuotaCheck):

@staticmethod
def get_all_identifiers(session: boto3.Session) -> typing.List[str]:
return [alb['LoadBalancerArn']
for alb in get_nlbs(session)]
return [alb['LoadBalancerArn'] for alb in get_nlbs(session)]

@property
def current(self):
try:
return len(self.boto_session.client('elbv2').describe_listeners(
LoadBalancerArn=self.instance_id)['Listeners'])
return len(
self.boto_session.client('elbv2').describe_listeners(
LoadBalancerArn=self.instance_id
)['Listeners']
)
except self.boto_session.client('elbv2').exceptions.LoadBalancerNotFoundException as e:
raise InstanceWithIdentifierNotFound(self) from e

Expand All @@ -99,14 +125,16 @@ class ListenerPerApplicationLoadBalancerCountCheck(InstanceQuotaCheck):

@staticmethod
def get_all_identifiers(session: boto3.Session) -> typing.List[str]:
return [alb['LoadBalancerArn']
for alb in get_albs(session)]
return [alb['LoadBalancerArn'] for alb in get_albs(session)]

@property
def current(self) -> int:
try:
return len(self.boto_session.client('elbv2').describe_listeners(
LoadBalancerArn=self.instance_id)['Listeners'])
return len(
self.boto_session.client('elbv2').describe_listeners(
LoadBalancerArn=self.instance_id
)['Listeners']
)
except self.boto_session.client('elbv2').exceptions.LoadBalancerNotFoundException as e:
raise InstanceWithIdentifierNotFound(self) from e

Expand All @@ -121,3 +149,26 @@ class TargetGroupCountCheck(QuotaCheck):
@property
def current(self):
return len(self.boto_session.client('elbv2').describe_target_groups()['TargetGroups'])


class TargetGroupsPerApplicationLoadBalancerCountCheck(InstanceQuotaCheck):
key = "elb_target_groups_per_alb"
description = "Target groups per Application Load Balancer"
service_code = 'elasticloadbalancing'
quota_code = 'L-822D1B1B'
instance_id = 'Load Balancer ARN'

@staticmethod
def get_all_identifiers(session: boto3.Session) -> typing.List[str]:
return [alb['LoadBalancerArn'] for alb in get_albs(session)]

@property
def current(self) -> int:
try:
return len(
self.boto_session.client('elbv2').describe_target_groups(
LoadBalancerArn=self.instance_id
)['TargetGroups']
)
except self.boto_session.client('elbv2').exceptions.LoadBalancerNotFoundException as e:
raise InstanceWithIdentifierNotFound(self) from e

0 comments on commit bd35344

Please sign in to comment.