Skip to content

Commit

Permalink
Add check_mode support
Browse files Browse the repository at this point in the history
  • Loading branch information
mandar242 committed Mar 29, 2022
1 parent 3015013 commit 818d057
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
10 changes: 10 additions & 0 deletions plugins/modules/ec2_asg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,9 @@ def create_autoscaling_group(connection):
ResourceType='auto-scaling-group',
ResourceId=group_name))
if not as_groups:
if module.check_mode:
module.exit_json(changed=True, msg="Would have created AutoScalingGroup if not in check_mode.")

if not vpc_zone_identifier and not availability_zones:
availability_zones = module.params['availability_zones'] = [zone['ZoneName'] for
zone in ec2_connection.describe_availability_zones()['AvailabilityZones']]
Expand Down Expand Up @@ -1206,6 +1209,9 @@ def create_autoscaling_group(connection):
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="Failed to create Autoscaling Group.")
else:
if module.check_mode:
module.exit_json(changed=True, msg="Would have modified AutoScalingGroup if not in check_mode.")

as_group = as_groups[0]
initial_asg_properties = get_properties(as_group)
changed = False
Expand Down Expand Up @@ -1401,6 +1407,8 @@ def delete_autoscaling_group(connection):
del_notification_config(connection, group_name, notification_topic)
groups = describe_autoscaling_groups(connection, group_name)
if groups:
if module.check_mode:
module.exit_json(changed=True, msg="Would have deleted AutoScalingGroup if not in check_mode.")
wait_timeout = time.time() + wait_timeout
if not wait_for_instances:
delete_asg(connection, group_name, force_delete=True)
Expand Down Expand Up @@ -1456,6 +1464,7 @@ def replace(connection):
min_size = module.params.get('min_size')
desired_capacity = module.params.get('desired_capacity')
launch_config_name = module.params.get('launch_config_name')

# Required to maintain the default value being set to 'true'
if launch_config_name:
lc_check = module.params.get('lc_check')
Expand Down Expand Up @@ -1891,6 +1900,7 @@ def main():
global module
module = AnsibleAWSModule(
argument_spec=argument_spec,
supports_check_mode=True,
mutually_exclusive=[
['replace_all_instances', 'replace_instances'],
['replace_all_instances', 'detach_instances'],
Expand Down
69 changes: 69 additions & 0 deletions tests/integration/targets/ec2_asg/tasks/instance_detach.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@
- '"autoscaling:CreateLaunchConfiguration" in create_lc.resource_actions'

#----------------------------------------------------------------------

- name: create a AutoScalingGroup to be used for instance_detach test
ec2_asg:
name: "{{ resource_prefix }}-asg-detach-test"
launch_config_name: "{{ resource_prefix }}-lc-detach-test"
health_check_period: 60
health_check_type: ELB
replace_all_instances: yes
min_size: 3
max_size: 6
desired_capacity: 3
region: "{{ aws_region }}"
register: create_asg
check_mode: true

- assert:
that:
- create_asg is changed
- create_asg is not failed
- '"autoscaling:CreateAutoScalingGroup" not in create_asg.resource_actions'

- name: create a AutoScalingGroup to be used for instance_detach test
ec2_asg:
name: "{{ resource_prefix }}-asg-detach-test"
Expand Down Expand Up @@ -68,6 +89,28 @@

#----------------------------------------------------------------------

- name: detach 2 instance from the asg and replace with other instances
ec2_asg:
name: "{{ resource_prefix }}-asg-detach-test"
launch_config_name: "{{ resource_prefix }}-lc-detach-test"
health_check_period: 60
health_check_type: ELB
min_size: 3
max_size: 3
desired_capacity: 3
region: "{{ aws_region }}"
detach_instances:
- '{{ init_instance_1 }}'
- '{{ init_instance_2 }}'
register: detach_result
check_mode: true

- assert:
that:
- detach_result is changed
- detach_result is not failed
- '"autoscaling:DetachInstances" not in detach_result.resource_actions'

- name: detach 2 instance from the asg and replace with other instances
ec2_asg:
name: "{{ resource_prefix }}-asg-detach-test"
Expand Down Expand Up @@ -189,6 +232,19 @@
- "{{ instance_replace_2 }}"
- "{{ instance_replace_3 }}"

- name: kill asg created in this test - check_mode
ec2_asg:
name: "{{ resource_prefix }}-asg-detach-test"
state: absent
register: removed
check_mode: true

- assert:
that:
- removed is changed
- removed is not failed
- '"autoscaling:DeleteAutoScalingGroup" not in removed.resource_actions'

- name: kill asg created in this test
ec2_asg:
name: "{{ resource_prefix }}-asg-detach-test"
Expand All @@ -198,6 +254,19 @@
ignore_errors: yes
retries: 10

- name: kill asg created in this test - check_mode (idempotent)
ec2_asg:
name: "{{ resource_prefix }}-asg-detach-test"
state: absent
register: removed
check_mode: true

- assert:
that:
- removed is not changed
- removed is not failed
- '"autoscaling:DeleteAutoScalingGroup" not in removed.resource_actions'

- name: remove launch config created in this test
ec2_lc:
name: "{{ resource_prefix }}-lc-detach-test"
Expand Down
24 changes: 24 additions & 0 deletions tests/integration/targets/ec2_asg/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,30 @@
groups:
- "{{ sg.group_id }}"

- name: update autoscaling group with mixed-instances policy with mixed instances types
ec2_asg:
name: "{{ resource_prefix }}-asg"
launch_template:
launch_template_name: "{{ resource_prefix }}-lt"
desired_capacity: 1
min_size: 1
max_size: 1
vpc_zone_identifier: "{{ testing_subnet.subnet.id }}"
state: present
mixed_instances_policy:
instance_types:
- t3.micro
- t2.nano
wait_for_instances: yes
register: output
check_mode: true

- assert:
that:
- output is changed
- output is not failed
- '"autoscaling:CreateOrUpdateTags" not in output.resource_actions'

- name: update autoscaling group with mixed-instances policy with mixed instances types
ec2_asg:
name: "{{ resource_prefix }}-asg"
Expand Down
17 changes: 17 additions & 0 deletions tests/integration/targets/ec2_asg/tasks/tag_operations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@
that:
- info_result.results[0].tags | length == 0

- name: Tag asg
ec2_asg:
name: "{{ resource_prefix }}-asg-tag-test"
tags:
- tag_a: 'value 1'
propagate_at_launch: no
- tag_b: 'value 2'
propagate_at_launch: yes
register: output
check_mode: true

- assert:
that:
- output is changed
- output is not failed
- '"autoscaling:CreateOrUpdateTags" not in output.resource_actions'

- name: Tag asg
ec2_asg:
name: "{{ resource_prefix }}-asg-tag-test"
Expand Down

0 comments on commit 818d057

Please sign in to comment.