Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ec2_asg_lifecycle_hook: Add check_mode support #1060

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'