Skip to content

Commit

Permalink
ec2_asg_lifecycle_hook: Add check_mode support (#1060)
Browse files Browse the repository at this point in the history
ec2_asg_lifecycle_hook: Add check_mode support

SUMMARY

Add check_mode support to ec2_asg_lifecycle_hook.

ISSUE TYPE


Feature Pull Request

COMPONENT NAME

ec2_asg_lifecycle_hook

Reviewed-by: Markus Bergholz <git@osuv.de>
Reviewed-by: Joseph Torcasso <None>
  • Loading branch information
mandar242 committed Apr 13, 2022
1 parent 0e54134 commit 1d2605b
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- ec2_asg_lifecycle_hook - Added check_mode support (https://github.com/ansible-collections/community.aws/pull/1060).
14 changes: 12 additions & 2 deletions plugins/modules/ec2_asg_lifecycle_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ def create_lifecycle_hook(connection, module):

if not existing_hook:
try:
if module.check_mode:
module.exit_json(changed=True, msg="Would have created AutoScalingGroup Lifecycle Hook if not in check_mode.")
return_object['changed'] = True
connection.put_lifecycle_hook(**lch_params)
return_object['lifecycle_hook_info'] = connection.describe_lifecycle_hooks(
Expand All @@ -196,6 +198,8 @@ def create_lifecycle_hook(connection, module):
added, removed, modified, same = dict_compare(lch_params, existing_hook[0])
if modified:
try:
if module.check_mode:
module.exit_json(changed=True, msg="Would have modified AutoScalingGroup Lifecycle Hook if not in check_mode.")
return_object['changed'] = True
connection.put_lifecycle_hook(**lch_params)
return_object['lifecycle_hook_info'] = connection.describe_lifecycle_hooks(
Expand Down Expand Up @@ -245,6 +249,8 @@ def delete_lifecycle_hook(connection, module):
}

try:
if module.check_mode:
module.exit_json(changed=True, msg="Would have deleted AutoScalingGroup Lifecycle Hook if not in check_mode.")
connection.delete_lifecycle_hook(**lch_params)
return_object['changed'] = True
return_object['lifecycle_hook_removed'] = {'LifecycleHookName': lch_name, 'AutoScalingGroupName': asg_name}
Expand All @@ -269,8 +275,12 @@ def main():
state=dict(default='present', choices=['present', 'absent'])
)

module = AnsibleAWSModule(argument_spec=argument_spec,
required_if=[['state', 'present', ['transition']]])
module = AnsibleAWSModule(
argument_spec=argument_spec,
supports_check_mode=True,
required_if=[['state', 'present', ['transition']]],
)

state = module.params.get('state')

connection = module.client('autoscaling')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@

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

- name: Create lifecycle hook - check_mode
community.aws.ec2_asg_lifecycle_hook:
region: "{{ aws_region }}"
autoscaling_group_name: "{{ resource_prefix }}-asg"
lifecycle_hook_name: "{{ resource_prefix }}-test-hook"
transition: autoscaling:EC2_INSTANCE_LAUNCHING
heartbeat_timeout: 7000
default_result: ABANDON
state: present
check_mode: true
register: output

- assert:
that:
- output is changed
- output is not failed
- '"lifecycle_hook_info" not in output'
- '"Would have created AutoScalingGroup Lifecycle Hook if not in check_mode" in output.msg'

- name: Create lifecycle hook
community.aws.ec2_asg_lifecycle_hook:
region: "{{ aws_region }}"
Expand Down Expand Up @@ -76,6 +95,43 @@
- output is not failed
- '"lifecycle_hook_info" not in output'

- name: Create lifecycle hook - check_mode (Idempotency)
community.aws.ec2_asg_lifecycle_hook:
region: "{{ aws_region }}"
autoscaling_group_name: "{{ resource_prefix }}-asg"
lifecycle_hook_name: "{{ resource_prefix }}-test-hook"
transition: autoscaling:EC2_INSTANCE_LAUNCHING
heartbeat_timeout: 7000
default_result: ABANDON
state: present
check_mode: true
register: output

- assert:
that:
- output is not changed
- output is not failed
- '"lifecycle_hook_info" not in output'

- name: Update lifecycle hook - check_mode
community.aws.ec2_asg_lifecycle_hook:
region: "{{ aws_region }}"
autoscaling_group_name: "{{ resource_prefix }}-asg"
lifecycle_hook_name: "{{ resource_prefix }}-test-hook"
transition: autoscaling:EC2_INSTANCE_LAUNCHING
heartbeat_timeout: 6000
default_result: ABANDON
state: present
check_mode: true
register: output

- assert:
that:
- output is changed
- output is not failed
- '"lifecycle_hook_info" not in output'
- '"Would have modified AutoScalingGroup Lifecycle Hook if not in check_mode." in output.msg'

- name: Update lifecycle hook
community.aws.ec2_asg_lifecycle_hook:
region: "{{ aws_region }}"
Expand Down Expand Up @@ -111,6 +167,40 @@
- output is not failed
- '"lifecycle_hook_info" not in output'

- name: Update lifecycle hook - check_mode (Idempotency)
community.aws.ec2_asg_lifecycle_hook:
region: "{{ aws_region }}"
autoscaling_group_name: "{{ resource_prefix }}-asg"
lifecycle_hook_name: "{{ resource_prefix }}-test-hook"
transition: autoscaling:EC2_INSTANCE_LAUNCHING
heartbeat_timeout: 6000
default_result: ABANDON
state: present
check_mode: true
register: output

- assert:
that:
- output is not changed
- output is not failed
- '"lifecycle_hook_info" not in output'

- name: Delete lifecycle hook - check_mode
community.aws.ec2_asg_lifecycle_hook:
region: "{{ aws_region }}"
autoscaling_group_name: "{{ resource_prefix }}-asg"
lifecycle_hook_name: "{{ resource_prefix }}-test-hook"
state: absent
check_mode: true
register: output

- assert:
that:
- output is changed
- output is not failed
- '"lifecycle_hook_removed" not in output'
- '"Would have deleted AutoScalingGroup Lifecycle Hook if not in check_mode." in output.msg'

- name: Delete lifecycle hook
community.aws.ec2_asg_lifecycle_hook:
region: "{{ aws_region }}"
Expand Down Expand Up @@ -138,3 +228,18 @@
- output is not changed
- output is not failed
- '"lifecycle_hook_removed" not in output'

- name: Delete lifecycle hook - check_mode (Idempotency)
community.aws.ec2_asg_lifecycle_hook:
region: "{{ aws_region }}"
autoscaling_group_name: "{{ resource_prefix }}-asg"
lifecycle_hook_name: "{{ resource_prefix }}-test-hook"
state: absent
check_mode: true
register: output

- assert:
that:
- output is not changed
- output is not failed
- '"lifecycle_hook_removed" not in output'

0 comments on commit 1d2605b

Please sign in to comment.