From 3e42eb8d063c782f1983209b4c49824223e0f33c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Magalh=C3=A3es?= <4622652+pjrm@users.noreply.github.com> Date: Sun, 14 Mar 2021 14:27:15 +0000 Subject: [PATCH] AWS ELB: Return empty list when no load balancer name was found (#215) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When trying to describe a LoadBalancer that doesn't exist, the module crash. Instead of that behavior, this commit will return an empty list when no load balancer is found, allowing to deal next tasks by reading the output of the module. Co-authored-by: Pedro Magalhães --- elb_classic_lb_info.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/elb_classic_lb_info.py b/elb_classic_lb_info.py index 12a6a43771a..a1a0c39e042 100644 --- a/elb_classic_lb_info.py +++ b/elb_classic_lb_info.py @@ -142,7 +142,7 @@ vpc_id: vpc-c248fda4 ''' -from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule +from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule, is_boto3_error_code from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ( AWSRetry, camel_dict_to_snake_dict, @@ -154,14 +154,17 @@ except ImportError: pass # caught by AnsibleAWSModule +MAX_AWS_RETRIES = 5 +MAX_AWS_DELAY = 5 -@AWSRetry.backoff(tries=5, delay=5, backoff=2.0) -def list_elbs(connection, names): - paginator = connection.get_paginator('describe_load_balancers') - load_balancers = paginator.paginate(LoadBalancerNames=names).build_full_result().get('LoadBalancerDescriptions', []) + +def list_elbs(connection, load_balancer_names): results = [] - for lb in load_balancers: + for load_balancer_name in load_balancer_names: + lb = get_lb(connection, load_balancer_name) + if not lb: + continue description = camel_dict_to_snake_dict(lb) name = lb['LoadBalancerName'] instances = lb.get('Instances', []) @@ -174,13 +177,20 @@ def list_elbs(connection, names): return results -def get_lb_attributes(connection, name): - attributes = connection.describe_load_balancer_attributes(LoadBalancerName=name).get('LoadBalancerAttributes', {}) +def get_lb(connection, load_balancer_name): + try: + return connection.describe_load_balancers(aws_retry=True, LoadBalancerNames=[load_balancer_name])['LoadBalancerDescriptions'][0] + except is_boto3_error_code('LoadBalancerNotFound'): + return [] + + +def get_lb_attributes(connection, load_balancer_name): + attributes = connection.describe_load_balancer_attributes(aws_retry=True, LoadBalancerName=load_balancer_name).get('LoadBalancerAttributes', {}) return camel_dict_to_snake_dict(attributes) def get_tags(connection, load_balancer_name): - tags = connection.describe_tags(LoadBalancerNames=[load_balancer_name])['TagDescriptions'] + tags = connection.describe_tags(aws_retry=True, LoadBalancerNames=[load_balancer_name])['TagDescriptions'] if not tags: return {} return boto3_tag_list_to_ansible_dict(tags[0]['Tags']) @@ -194,14 +204,14 @@ def lb_instance_health(connection, load_balancer_name, instances, state): def main(): argument_spec = dict( - names={'default': [], 'type': 'list', 'elements': 'str'} + names=dict(default=[], type='list', elements='str') ) module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) if module._name == 'elb_classic_lb_facts': module.deprecate("The 'elb_classic_lb_facts' module has been renamed to 'elb_classic_lb_info'", date='2021-12-01', collection_name='community.aws') - connection = module.client('elb') + connection = module.client('elb', retry_decorator=AWSRetry.jittered_backoff(retries=MAX_AWS_RETRIES, delay=MAX_AWS_DELAY)) try: elbs = list_elbs(connection, module.params.get('names'))