Skip to content

Commit

Permalink
Fix target group remove / add logic
Browse files Browse the repository at this point in the history
Add additional integration tests to test linking target groups
to autoscaling groups.
  • Loading branch information
msven committed Mar 23, 2021
1 parent 0412f00 commit b7a7636
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 20 deletions.
35 changes: 16 additions & 19 deletions plugins/modules/ec2_asg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1161,25 +1161,22 @@ def create_autoscaling_group(connection):
# Get differences
wanted_tgs = set(target_group_arns)
has_tgs = set(as_group['TargetGroupARNs'])
# check if all requested are already existing
if has_tgs.issuperset(wanted_tgs):
# if wanted contains less than existing, then we need to delete some
tgs_to_detach = has_tgs.difference(wanted_tgs)
if tgs_to_detach:
changed = True
try:
detach_lb_target_groups(connection, group_name, list(tgs_to_detach))
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="Failed to detach load balancer target groups {0}".format(tgs_to_detach))
if wanted_tgs.issuperset(has_tgs):
# if has contains less than wanted, then we need to add some
tgs_to_attach = wanted_tgs.difference(has_tgs)
if tgs_to_attach:
changed = True
try:
attach_lb_target_groups(connection, group_name, list(tgs_to_attach))
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json(msg="Failed to attach load balancer target groups {0}".format(tgs_to_attach))

tgs_to_detach = has_tgs.difference(wanted_tgs)
if tgs_to_detach:
changed = True
try:
detach_lb_target_groups(connection, group_name, list(tgs_to_detach))
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="Failed to detach load balancer target groups {0}".format(tgs_to_detach))

tgs_to_attach = wanted_tgs.difference(has_tgs)
if tgs_to_attach:
changed = True
try:
attach_lb_target_groups(connection, group_name, list(tgs_to_attach))
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json(msg="Failed to attach load balancer target groups {0}".format(tgs_to_attach))

# check for attributes that aren't required for updating an existing ASG
# check if min_size/max_size/desired capacity have been specified and if not use ASG values
Expand Down
143 changes: 142 additions & 1 deletion tests/integration/targets/ec2_asg/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,134 @@
- "output.mixed_instances_policy[0] == 't3.micro'"
- "output.mixed_instances_policy[1] == 't3a.micro'"

# ============================================================

# Target group names have max length of 32 characters
- set_fact:
tg1_name: "{{ (resource_prefix + '-tg1' ) | regex_search('(.{1,32})$') }}"
tg2_name: "{{ (resource_prefix + '-tg2' ) | regex_search('(.{1,32})$') }}"

- name: create target group 1
elb_target_group:
name: "{{ tg1_name }}"
protocol: tcp
port: 80
health_check_protocol: tcp
health_check_port: 80
healthy_threshold_count: 2
unhealthy_threshold_count: 2
vpc_id: "{{ testing_vpc.vpc.id }}"
state: present
register: out_tg1

- name: create target group 2
elb_target_group:
name: "{{ tg2_name }}"
protocol: tcp
port: 80
health_check_protocol: tcp
health_check_port: 80
healthy_threshold_count: 2
unhealthy_threshold_count: 2
vpc_id: "{{ testing_vpc.vpc.id }}"
state: present
register: out_tg2

- name: update autoscaling group with tg1
ec2_asg:
name: "{{ resource_prefix }}-asg"
launch_template:
launch_template_name: "{{ resource_prefix }}-lt"
target_group_arns:
- "{{ out_tg1.target_group_arn }}"
desired_capacity: 1
min_size: 1
max_size: 1
state: present
wait_for_instances: yes
register: output

- assert:
that:
- output.target_group_arns[0] == out_tg1.target_group_arn

- name: update autoscaling group add tg2
ec2_asg:
name: "{{ resource_prefix }}-asg"
launch_template:
launch_template_name: "{{ resource_prefix }}-lt"
target_group_arns:
- "{{ out_tg1.target_group_arn }}"
- "{{ out_tg2.target_group_arn }}"
desired_capacity: 1
min_size: 1
max_size: 1
state: present
wait_for_instances: yes
register: output

- assert:
that:
- "output.target_group_arns | length == 2"

- name: update autoscaling group remove tg1
ec2_asg:
name: "{{ resource_prefix }}-asg"
launch_template:
launch_template_name: "{{ resource_prefix }}-lt"
target_group_arns:
- "{{ out_tg2.target_group_arn }}"
desired_capacity: 1
min_size: 1
max_size: 1
state: present
wait_for_instances: yes
register: output

- assert:
that:
- "output.target_group_arns | length == 1"
- "output.target_group_arns[0] == out_tg2.target_group_arn"

- name: update autoscaling group remove tg2 and add tg1
ec2_asg:
name: "{{ resource_prefix }}-asg"
launch_template:
launch_template_name: "{{ resource_prefix }}-lt"
target_group_arns:
- "{{ out_tg1.target_group_arn }}"
desired_capacity: 1
min_size: 1
max_size: 1
state: present
wait_for_instances: yes
register: output

- assert:
that:
- "output.target_group_arns | length == 1"
- "output.target_group_arns[0] == out_tg1.target_group_arn"

- name: target group no change
ec2_asg:
name: "{{ resource_prefix }}-asg"
launch_template:
launch_template_name: "{{ resource_prefix }}-lt"
target_group_arns:
- "{{ out_tg1.target_group_arn }}"
desired_capacity: 1
min_size: 1
max_size: 1
state: present
wait_for_instances: yes
register: output

- assert:
that:
- "output.target_group_arns | length == 1"
- "output.target_group_arns[0] == out_tg1.target_group_arn"
- "output.changed == false"

# ============================================================

always:
Expand All @@ -683,6 +811,19 @@

# Remove the testing dependencies

- name: remove target group
elb_target_group:
name: "{{ item }}"
state: absent
register: removed
until: removed is not failed
ignore_errors: yes
retries: 10
register: output
loop:
- "{{ tg1_name }}"
- "{{ tg2_name }}"

- name: remove the load balancer
ec2_elb_lb:
name: "{{ load_balancer_name }}"
Expand Down Expand Up @@ -711,7 +852,7 @@

- name: remove launch configs
ec2_lc:
name: "{{ resource_prefix }}-lc"
name: "{{ item }}"
state: absent
register: removed
until: removed is not failed
Expand Down

0 comments on commit b7a7636

Please sign in to comment.