From e6767dfb3c01bbe827f69ca4ee4018a1d88926e9 Mon Sep 17 00:00:00 2001 From: Matt Schurenko Date: Thu, 18 May 2023 18:19:53 -0700 Subject: [PATCH 1/6] add wait_for_modify_volume_complete option --- plugins/modules/ec2_vol.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/plugins/modules/ec2_vol.py b/plugins/modules/ec2_vol.py index 981510c8470..64e784b5560 100644 --- a/plugins/modules/ec2_vol.py +++ b/plugins/modules/ec2_vol.py @@ -439,6 +439,22 @@ def update_volume(module, ec2_conn, volume): volume["multi_attach_enabled"] = response.get("VolumeModification").get("TargetMultiAttachEnabled") volume["throughput"] = response.get("VolumeModification").get("TargetThroughput") + if module.params.get("wait_for_modify_volume_complete"): + mod_state = "" + _max_attempts = 100 + _sleep_secs = 10 + _attempt = 0 + while mod_state != "completed": + _attempt += 1 + mod_response = ec2_conn.describe_volumes_modifications(VolumeIds=[volume["volume_id"]]) + mod_state = mod_response.get("VolumesModifications")[0].get("ModificationState") + # this can take a long time, depending on how much bigger the requested volume size is + if _attempt > _max_attempts: + module.fail_json( + msg=f"Volume {volume['volume_id']} modification state has not reached 'completed' after {_max_attempts * _sleep_secs} seconds." + ) + time.sleep(_sleep_secs) + return volume, changed @@ -714,6 +730,7 @@ def main(): outpost_arn=dict(type="str"), purge_tags=dict(type="bool", default=True), multi_attach=dict(type="bool"), + wait_for_modify_volume_complete=dict(default=False, type="bool") ) module = AnsibleAWSModule( @@ -738,6 +755,9 @@ def main(): volume_type = module.params.get("volume_type") throughput = module.params.get("throughput") multi_attach = module.params.get("multi_attach") + modify_volume = module.params.get("modify_volume") + wait_for_modify_volume_complete = module.params.get("wait_for_modify_volume_complete") + # Ensure we have the zone or can get the zone if instance is None and zone is None and state == "present": @@ -769,6 +789,9 @@ def main(): if multi_attach is True and volume_type not in ("io1", "io2"): module.fail_json(msg="multi_attach is only supported for io1 and io2 volumes.") + if wait_for_modify_volume_complete is True and modify_volume is False: + module.fail_json(msg="wait_for_modify_volume_complete does nothing if modify_volume is False.") + # Set changed flag changed = False From 7bb7ac81ef1d066054c3a508a461ef331bad24e2 Mon Sep 17 00:00:00 2001 From: Matt Schurenko Date: Mon, 24 Jul 2023 11:55:29 -0700 Subject: [PATCH 2/6] increasing polling interval;adding doc;check for failed --- plugins/modules/ec2_vol.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/plugins/modules/ec2_vol.py b/plugins/modules/ec2_vol.py index 64e784b5560..89df7760fad 100644 --- a/plugins/modules/ec2_vol.py +++ b/plugins/modules/ec2_vol.py @@ -104,6 +104,11 @@ - If set, allows to create volume in an Outpost. type: str version_added: 3.1.0 + wait_for_modify_volume_complete: + description: + - Wait for volume modification to complete + type: bool + version_added: 6.3.0 author: - "Lester Wade (@lwade)" notes: @@ -441,13 +446,19 @@ def update_volume(module, ec2_conn, volume): if module.params.get("wait_for_modify_volume_complete"): mod_state = "" - _max_attempts = 100 - _sleep_secs = 10 + _max_attempts = 200 + _sleep_secs = 30 _attempt = 0 while mod_state != "completed": _attempt += 1 mod_response = ec2_conn.describe_volumes_modifications(VolumeIds=[volume["volume_id"]]) mod_state = mod_response.get("VolumesModifications")[0].get("ModificationState") + + if mod_state == "failed": + module.fail_json( + msg=f"Volume {volume['volume_id']} modification has failed." + ) + # this can take a long time, depending on how much bigger the requested volume size is if _attempt > _max_attempts: module.fail_json( From 6d28b3c28e1fd484438f9b92599d24644ffb07ca Mon Sep 17 00:00:00 2001 From: Matt Schurenko Date: Tue, 25 Jul 2023 12:38:58 -0700 Subject: [PATCH 3/6] adding changelog fragment --- .../1558-ec2_vol-add-wait-for-modifyvolume-complete.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/1558-ec2_vol-add-wait-for-modifyvolume-complete.yml diff --git a/changelogs/fragments/1558-ec2_vol-add-wait-for-modifyvolume-complete.yml b/changelogs/fragments/1558-ec2_vol-add-wait-for-modifyvolume-complete.yml new file mode 100644 index 00000000000..1f8c90f0bc0 --- /dev/null +++ b/changelogs/fragments/1558-ec2_vol-add-wait-for-modifyvolume-complete.yml @@ -0,0 +1,2 @@ +minor_changes: +- ec2_vol - added ``wait_for_modify_volume_complete`` option (https://github.com/ansible-collections/amazon.aws/pull/1558). From d48dcf9cf23cf9129a5e4c486525b3ef85f9381d Mon Sep 17 00:00:00 2001 From: Matt Schurenko Date: Tue, 25 Jul 2023 13:14:34 -0700 Subject: [PATCH 4/6] fix linting --- plugins/modules/ec2_vol.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/plugins/modules/ec2_vol.py b/plugins/modules/ec2_vol.py index 89df7760fad..f7e18060cae 100644 --- a/plugins/modules/ec2_vol.py +++ b/plugins/modules/ec2_vol.py @@ -455,9 +455,7 @@ def update_volume(module, ec2_conn, volume): mod_state = mod_response.get("VolumesModifications")[0].get("ModificationState") if mod_state == "failed": - module.fail_json( - msg=f"Volume {volume['volume_id']} modification has failed." - ) + module.fail_json(msg=f"Volume {volume['volume_id']} modification has failed.") # this can take a long time, depending on how much bigger the requested volume size is if _attempt > _max_attempts: @@ -741,7 +739,7 @@ def main(): outpost_arn=dict(type="str"), purge_tags=dict(type="bool", default=True), multi_attach=dict(type="bool"), - wait_for_modify_volume_complete=dict(default=False, type="bool") + wait_for_modify_volume_complete=dict(default=False, type="bool"), ) module = AnsibleAWSModule( @@ -769,7 +767,6 @@ def main(): modify_volume = module.params.get("modify_volume") wait_for_modify_volume_complete = module.params.get("wait_for_modify_volume_complete") - # Ensure we have the zone or can get the zone if instance is None and zone is None and state == "present": module.fail_json(msg="You must specify either instance or zone") From 30bd554a34e65b66a243911e9b5c324f246519f5 Mon Sep 17 00:00:00 2001 From: Matt Schurenko Date: Tue, 25 Jul 2023 13:43:24 -0700 Subject: [PATCH 5/6] fix sanity test --- plugins/modules/ec2_vol.py | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/modules/ec2_vol.py b/plugins/modules/ec2_vol.py index f7e18060cae..f8c6a867d6c 100644 --- a/plugins/modules/ec2_vol.py +++ b/plugins/modules/ec2_vol.py @@ -108,6 +108,7 @@ description: - Wait for volume modification to complete type: bool + default: false version_added: 6.3.0 author: - "Lester Wade (@lwade)" From d0be10ea4d7bc90ff18e5a5102821ae7f88e71ec Mon Sep 17 00:00:00 2001 From: Matt Schurenko Date: Tue, 1 Aug 2023 12:07:17 -0700 Subject: [PATCH 6/6] updating params --- ...ol-add-wait-for-modify-volume-complete.yml | 2 + ...vol-add-wait-for-modifyvolume-complete.yml | 2 - plugins/modules/ec2_vol.py | 43 +++++++++++-------- 3 files changed, 27 insertions(+), 20 deletions(-) create mode 100644 changelogs/fragments/1558-ec2_vol-add-wait-for-modify-volume-complete.yml delete mode 100644 changelogs/fragments/1558-ec2_vol-add-wait-for-modifyvolume-complete.yml diff --git a/changelogs/fragments/1558-ec2_vol-add-wait-for-modify-volume-complete.yml b/changelogs/fragments/1558-ec2_vol-add-wait-for-modify-volume-complete.yml new file mode 100644 index 00000000000..d0ea6c9cc0e --- /dev/null +++ b/changelogs/fragments/1558-ec2_vol-add-wait-for-modify-volume-complete.yml @@ -0,0 +1,2 @@ +minor_changes: +- ec2_vol - added ``wait`` and ``wait_timeout`` options (https://github.com/ansible-collections/amazon.aws/pull/1558). diff --git a/changelogs/fragments/1558-ec2_vol-add-wait-for-modifyvolume-complete.yml b/changelogs/fragments/1558-ec2_vol-add-wait-for-modifyvolume-complete.yml deleted file mode 100644 index 1f8c90f0bc0..00000000000 --- a/changelogs/fragments/1558-ec2_vol-add-wait-for-modifyvolume-complete.yml +++ /dev/null @@ -1,2 +0,0 @@ -minor_changes: -- ec2_vol - added ``wait_for_modify_volume_complete`` option (https://github.com/ansible-collections/amazon.aws/pull/1558). diff --git a/plugins/modules/ec2_vol.py b/plugins/modules/ec2_vol.py index f8c6a867d6c..583078a47fa 100644 --- a/plugins/modules/ec2_vol.py +++ b/plugins/modules/ec2_vol.py @@ -104,12 +104,18 @@ - If set, allows to create volume in an Outpost. type: str version_added: 3.1.0 - wait_for_modify_volume_complete: + wait: description: - - Wait for volume modification to complete + - Wait for volume modification to complete. type: bool default: false version_added: 6.3.0 + wait_timeout: + description: + - How long before wait gives up, in seconds. + type: int + default: 900 + version_added: 6.3.0 author: - "Lester Wade (@lwade)" notes: @@ -445,25 +451,25 @@ def update_volume(module, ec2_conn, volume): volume["multi_attach_enabled"] = response.get("VolumeModification").get("TargetMultiAttachEnabled") volume["throughput"] = response.get("VolumeModification").get("TargetThroughput") - if module.params.get("wait_for_modify_volume_complete"): + if module.params.get("wait"): mod_state = "" - _max_attempts = 200 - _sleep_secs = 30 - _attempt = 0 - while mod_state != "completed": - _attempt += 1 + _wait_till = module.params.get("wait_timeout") + time.time() + while _wait_till > time.time(): mod_response = ec2_conn.describe_volumes_modifications(VolumeIds=[volume["volume_id"]]) mod_state = mod_response.get("VolumesModifications")[0].get("ModificationState") + if mod_state == "completed": + break + if mod_state == "failed": module.fail_json(msg=f"Volume {volume['volume_id']} modification has failed.") - # this can take a long time, depending on how much bigger the requested volume size is - if _attempt > _max_attempts: - module.fail_json( - msg=f"Volume {volume['volume_id']} modification state has not reached 'completed' after {_max_attempts * _sleep_secs} seconds." - ) - time.sleep(_sleep_secs) + time.sleep(30) + + else: + module.fail_json( + msg=f"Volume {volume['volume_id']} modification state has not reached 'completed' after {module.params.get('wait_timeout')} seconds." + ) return volume, changed @@ -740,7 +746,8 @@ def main(): outpost_arn=dict(type="str"), purge_tags=dict(type="bool", default=True), multi_attach=dict(type="bool"), - wait_for_modify_volume_complete=dict(default=False, type="bool"), + wait=dict(default=False, type="bool"), + wait_timeout=dict(type="int", default=900), ) module = AnsibleAWSModule( @@ -766,7 +773,7 @@ def main(): throughput = module.params.get("throughput") multi_attach = module.params.get("multi_attach") modify_volume = module.params.get("modify_volume") - wait_for_modify_volume_complete = module.params.get("wait_for_modify_volume_complete") + wait = module.params.get("wait") # Ensure we have the zone or can get the zone if instance is None and zone is None and state == "present": @@ -798,8 +805,8 @@ def main(): if multi_attach is True and volume_type not in ("io1", "io2"): module.fail_json(msg="multi_attach is only supported for io1 and io2 volumes.") - if wait_for_modify_volume_complete is True and modify_volume is False: - module.fail_json(msg="wait_for_modify_volume_complete does nothing if modify_volume is False.") + if wait is True and modify_volume is False: + module.fail_json(msg="wait does nothing if modify_volume is False.") # Set changed flag changed = False