Skip to content

Commit

Permalink
AWS ELB: Return empty list when no load balancer name was found (ansi…
Browse files Browse the repository at this point in the history
…ble-collections#215)

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 <[email protected]>
  • Loading branch information
2 people authored and abikouo committed Sep 18, 2023
1 parent 8cd3a2d commit 3e42eb8
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions elb_classic_lb_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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', [])
Expand All @@ -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'])
Expand All @@ -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'))
Expand Down

0 comments on commit 3e42eb8

Please sign in to comment.