From fbac32c5d0255bd8ad8bf4cba965bd60934761b4 Mon Sep 17 00:00:00 2001 From: Brian Scholer Date: Thu, 4 Oct 2018 16:31:16 -0400 Subject: [PATCH] Fix support for SubnetMappings and EIPs in NLB (#42979) * Fix support for SubnetMappings and EIPs in NLB * Fix style failures --- lib/ansible/module_utils/aws/elbv2.py | 40 ++++++++++++++++++--------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/lib/ansible/module_utils/aws/elbv2.py b/lib/ansible/module_utils/aws/elbv2.py index 2261001877c..800267a0fe6 100644 --- a/lib/ansible/module_utils/aws/elbv2.py +++ b/lib/ansible/module_utils/aws/elbv2.py @@ -152,27 +152,31 @@ class ElasticLoadBalancerV2(object): :return: bool True if they match otherwise False """ - subnet_id_list = [] - subnets = [] + subnet_mapping_id_list = [] + subnet_mappings = [] # Check if we're dealing with subnets or subnet_mappings if self.subnets is not None: - # We need to first get the subnet ID from the list - subnets = self.subnets + # Convert subnets to subnet_mappings format for comparison + for subnet in self.subnets: + subnet_mappings.append({'SubnetId': subnet}) if self.subnet_mappings is not None: - # Make a list from the subnet_mappings dict - subnets_from_mappings = [] - for subnet_mapping in self.subnet_mappings: - subnets.append(subnet_mapping['SubnetId']) + # Use this directly since we're comparing as a mapping + subnet_mappings = self.subnet_mappings + # Build a subnet_mapping style struture of what's currently + # on the load balancer for subnet in self.elb['AvailabilityZones']: - subnet_id_list.append(subnet['SubnetId']) + this_mapping = {'SubnetId': subnet['SubnetId']} + for address in subnet['LoadBalancerAddresses']: + if 'AllocationId' in address: + this_mapping['AllocationId'] = address['AllocationId'] + break - if set(subnet_id_list) != set(subnets): - return False - else: - return True + subnet_mapping_id_list.append(this_mapping) + + return set(frozenset(mapping.items()) for mapping in subnet_mapping_id_list) == set(frozenset(mapping.items()) for mapping in subnet_mappings) def modify_subnets(self): """ @@ -359,6 +363,8 @@ class NetworkLoadBalancer(ElasticLoadBalancerV2): # Other parameters if self.subnets is not None: params['Subnets'] = self.subnets + if self.subnet_mappings is not None: + params['SubnetMappings'] = self.subnet_mappings params['Scheme'] = self.scheme if self.tags: params['Tags'] = self.tags @@ -400,6 +406,14 @@ class NetworkLoadBalancer(ElasticLoadBalancerV2): AWSRetry.jittered_backoff()(self.connection.delete_load_balancer)(LoadBalancerArn=self.elb['LoadBalancerArn']) self.module.fail_json_aws(e) + def modify_subnets(self): + """ + Modify elb subnets to match module parameters (unsupported for NLB) + :return: + """ + + self.module.fail_json(msg='Modifying subnets and elastic IPs is not supported for Network Load Balancer') + class ELBListeners(object):