Last active
April 24, 2020 04:42
-
-
Save kenazk/f34ef5ba0e043106aab3309b9a2e4b62 to your computer and use it in GitHub Desktop.
Filter Load Balancers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import re | |
from nebula_sdk import Interface, Dynamic as D | |
ni = Interface() | |
if __name__ == '__main__': | |
# For each load balancer => Get target groups => Get targets under target group | |
to_terminate = [] | |
elbs = ni.get(D.loadbalancers) | |
all_target_groups = ni.get(D.targetgroups) | |
all_targets = ni.get(D.targets) | |
if len(elbs) == 0: | |
print('No ELBs found! Exiting ...') | |
exit | |
print('Evaluating the following Elastic Load Balancers: \n') | |
# elb_arns = [i["LoadBalancerArn"] for i in elbs] | |
# print(elb_arns) | |
elb_names = [i["LoadBalancerName"] for i in elbs] | |
print(elb_names) | |
for elb in elbs: | |
terminate = True | |
target_groups = all_target_groups[ elb['LoadBalancerName'] ] | |
print('Evaluating the following Target Groups under {0}'.format(elb['LoadBalancerName'])) | |
for tg in target_groups: | |
print(tg['TargetGroupName']) | |
print(all_targets[tg['TargetGroupName']]) | |
if len(all_targets[ tg['TargetGroupName'] ]) > 0: | |
terminate = False | |
if (terminate): | |
to_terminate.append(elb['LoadBalancerArn']) | |
print('Adding the following Elastic Load Balancers to terminate: {0}'.format(to_terminate)) | |
ni.outputs.set('elb_arns', to_terminate) | |
# target_group_arns = [i['TargetGroupArn'] for i in target_groups['TargetGroups']] | |
# print('For {1}, found the following target groups:\n {0}'.format(target_group_arns, arn)) | |
# # Get targets within target group | |
# for g in target_group_arns: | |
# targets = elbv2.describe_target_health(TargetGroupArn=g) | |
# # If any targets found, don't terminate ELB | |
# if len(targets['TargetHealthDescriptions'])>0: | |
# terminate = False | |
# if (terminate): | |
# to_terminate.append(arn) | |
# ni.outputs.set('elb_arns',to_terminate) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment