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_vol: Add check_mode support to ec2_vol #509

Merged
3 changes: 3 additions & 0 deletions changelogs/fragments/509-ec2_vol_add_check_mode_support.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minor_changes:
- ec2_vol - add check_mode support
(https://github.com/ansible-collections/amazon.aws/pull/509).
12 changes: 12 additions & 0 deletions plugins/modules/ec2_vol.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,8 @@ def update_volume(module, ec2_conn, volume):
changed = iops_changed or size_changed or type_changed or throughput_changed or multi_attach_changed

if changed:
if module.check_mode:
module.exit_json(changed=True, msg='Would have updated volume if not in check mode')
response = ec2_conn.modify_volume(**req_obj)

volume['size'] = response.get('VolumeModification').get('TargetSize')
Expand All @@ -457,6 +459,9 @@ def create_volume(module, ec2_conn, zone):

volume = get_volume(module, ec2_conn)

if module.check_mode:
module.exit_json(changed=True, msg='Would have created a volume if not in check mode', volume_type=volume_type)

if volume is None:

try:
Expand Down Expand Up @@ -605,6 +610,8 @@ def get_attachment_data(volume_dict, wanted_state=None):


def detach_volume(module, ec2_conn, volume_dict):
if module.check_mode:
module.exit_json(changed=True, msg='Would have detached volume if not in check mode')
changed = False

attachment_data = get_attachment_data(volume_dict, wanted_state='attached')
Expand Down Expand Up @@ -662,6 +669,8 @@ def get_mapped_block_device(instance_dict=None, device_name=None):


def ensure_tags(module, connection, res_id, res_type, tags, purge_tags):
if module.check_mode:
return {}, True
changed = ensure_ec2_tags(connection, module, res_id, res_type, tags, purge_tags, ['InvalidVolume.NotFound'])
final_tags = describe_ec2_tags(connection, module, res_id, res_type)

Expand Down Expand Up @@ -696,6 +705,7 @@ def main():
['volume_type', 'io1', ['iops']],
['volume_type', 'io2', ['iops']],
],
supports_check_mode=True,
)

param_id = module.params.get('id')
Expand Down Expand Up @@ -837,6 +847,8 @@ def main():
if not name and not param_id:
module.fail_json('A volume name or id is required for deletion')
if volume:
if module.check_mode:
module.exit_json(changed=True, msg='Would have deleted volume if not in check mode')
detach_volume(module, ec2_conn, volume_dict=volume)
changed = delete_volume(module, ec2_conn, volume_id=volume['volume_id'])
module.exit_json(changed=changed)
Expand Down
85 changes: 85 additions & 0 deletions tests/integration/targets/ec2_vol/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,19 @@
that:
- not changed_gp3_volume.changed

- name: change existing volume to gp3 (check_mode)
ec2_vol:
id: "{{ new_vol_attach_result.volume_id }}"
zone: "{{ availability_zone }}"
volume_type: gp3
modify_volume: yes
check_mode: true
register: changed_gp3_volume_check_mode

- assert:
that:
- changed_gp3_volume_check_mode is changed

- name: change existing volume to gp3
ec2_vol:
id: "{{ new_vol_attach_result.volume_id }}"
Expand Down Expand Up @@ -714,6 +727,78 @@
- 'test_instance.instance_ids[0] in vol_attach_result.volume.attachment_set | map(attribute="instance_id") | list'
- 'test_instance_2.instance_ids[0] in vol_attach_result.volume.attachment_set | map(attribute="instance_id") | list'

# ==== Assert check mode ==================================================

- name: Create a volume (check_mode)
ec2_vol:
volume_size: 2
zone: "{{ availability_zone }}"
tags:
ResourcePrefix: "{{ resource_prefix }}"
check_mode: true
register: check_create_result_vol

- assert:
that:
- check_create_result_vol is changed

- name: Get instance info before check mode attach
ec2_instance_info:
instance_ids:
- "{{ item }}"
ignore_errors: yes
with_items:
- "{{ test_instance.instance_ids[0] }}"
register: check_instance_info_pre

- name: Attach a volume (check_mode)
ec2_vol:
instance: "{{ test_instance.instance_ids[0] }}"
volume_size: 2
volume_type: gp2
device_name: /dev/xvdf
check_mode: true
register: check_vol_attach_result

- name: Get instance info after check mode attach
ec2_instance_info:
instance_ids:
- "{{ item }}"
ignore_errors: yes
with_items:
- "{{ test_instance.instance_ids[0] }}"
register: check_instance_info_post

- assert:
that:
- check_vol_attach_result is changed
- volume_count_after == volume_count_before
vars:
volume_count_before: "{{ check_instance_info_pre.results[0].instances[0].block_device_mappings | length }}"
volume_count_after: "{{ check_instance_info_post.results[0].instances[0].block_device_mappings | length }}"

- name: Detach a volume (check_mode)
ec2_vol:
id: "{{ new_vol_attach_result.volume_id }}"
instance: ""
check_mode: true
register: check_mode_detach_result

- assert:
that:
- check_mode_detach_result is changed

- name: Delete a volume (check_mode)
ec2_vol:
id: "{{ new_vol_attach_result.volume_id}}"
state: absent
check_mode: true
register: check_mode_delete_result

- assert:
that:
- check_mode_delete_result is changed

# ==== Cleanup ============================================================

always:
Expand Down