AWS EC2 ASG - Replace ELBs correctly and remove existing ELBs when specified as an empty list (#27830)

* fixes for edge cases - load_balancers has not been specified - don't want to delete existing elbs, wanted elbs aren't a superset of has_elbs (eg. 1 elb existing, adding another), specifying load_balancers: [] to delete existing elbs
pull/27928/merge
Willem van Ketwich 7 years ago committed by Sloane Hertel
parent 14186af558
commit 82765637b9

@ -800,6 +800,7 @@ def create_autoscaling_group(connection, module):
# Update load balancers if they are specified and one or more already exists # Update load balancers if they are specified and one or more already exists
elif as_group['LoadBalancerNames']: elif as_group['LoadBalancerNames']:
change_load_balancers = load_balancers is not None
# Get differences # Get differences
if not load_balancers: if not load_balancers:
load_balancers = list() load_balancers = list()
@ -807,7 +808,7 @@ def create_autoscaling_group(connection, module):
has_elbs = set(as_group['LoadBalancerNames']) has_elbs = set(as_group['LoadBalancerNames'])
# check if all requested are already existing # check if all requested are already existing
if has_elbs.issuperset(wanted_elbs): if has_elbs - wanted_elbs and change_load_balancers:
# if wanted contains less than existing, then we need to delete some # if wanted contains less than existing, then we need to delete some
elbs_to_detach = has_elbs.difference(wanted_elbs) elbs_to_detach = has_elbs.difference(wanted_elbs)
if elbs_to_detach: if elbs_to_detach:
@ -816,7 +817,7 @@ def create_autoscaling_group(connection, module):
AutoScalingGroupName=group_name, AutoScalingGroupName=group_name,
LoadBalancerNames=list(elbs_to_detach) LoadBalancerNames=list(elbs_to_detach)
) )
if wanted_elbs.issuperset(has_elbs): if wanted_elbs - has_elbs:
# if has contains less than wanted, then we need to add some # if has contains less than wanted, then we need to add some
elbs_to_attach = wanted_elbs.difference(has_elbs) elbs_to_attach = wanted_elbs.difference(has_elbs)
if elbs_to_attach: if elbs_to_attach:

Loading…
Cancel
Save