From d5776fa5829558725a11e0f965e807fb389417fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A9ri=20Le=20Bouder?= Date: Fri, 16 Sep 2022 16:04:54 -0400 Subject: [PATCH] ec2_placement_group: Handle a potential race creation during create (#1477) ec2_placement_group: Handle a potential race creation during create Address a race condition during the creation of a new Placement Group. The consequence was a "placement_group": null in output after a successful new creation. e.g: https://af0ac3e5f4e1620a8d63-ed3785e7f94a59162d05eede0959ab4b.ssl.cf5.rackcdn.com/1459/71e1a28c8ab7c12471b7f1cce5a37846ff642439/check/integration-community.aws-12/aa426fe/job-output.txt Reviewed-by: Mark Chappell This commit was initially merged in https://github.com/ansible-collections/community.aws See: https://github.com/ansible-collections/community.aws/commit/d4eb13b1041872e3d88d1f15beaf055ff38c0a25 --- plugins/modules/ec2_placement_group.py | 29 ++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/plugins/modules/ec2_placement_group.py b/plugins/modules/ec2_placement_group.py index 9ca3bb02ab9..4b90baf57f4 100644 --- a/plugins/modules/ec2_placement_group.py +++ b/plugins/modules/ec2_placement_group.py @@ -112,7 +112,10 @@ @AWSRetry.exponential_backoff() -def get_placement_group_details(connection, module): +def search_placement_group(connection, module): + """ + Check if a placement group exists. + """ name = module.params.get("name") try: response = connection.describe_placement_groups( @@ -136,6 +139,22 @@ def get_placement_group_details(connection, module): } +@AWSRetry.exponential_backoff(catch_extra_error_codes=['InvalidPlacementGroup.Unknown']) +def get_placement_group_information(connection, name): + """ + Retrieve information about a placement group. + """ + response = connection.describe_placement_groups( + GroupNames=[name] + ) + placement_group = response['PlacementGroups'][0] + return { + "name": placement_group['GroupName'], + "state": placement_group['State'], + "strategy": placement_group['Strategy'], + } + + @AWSRetry.exponential_backoff() def create_placement_group(connection, module): name = module.params.get("name") @@ -167,9 +186,7 @@ def create_placement_group(connection, module): msg="Couldn't create placement group [%s]" % name) module.exit_json(changed=True, - placement_group=get_placement_group_details( - connection, module - )) + placement_group=get_placement_group_information(connection, name)) @AWSRetry.exponential_backoff() @@ -205,7 +222,7 @@ def main(): state = module.params.get("state") if state == 'present': - placement_group = get_placement_group_details(connection, module) + placement_group = search_placement_group(connection, module) if placement_group is None: create_placement_group(connection, module) else: @@ -223,7 +240,7 @@ def main(): strategy)) elif state == 'absent': - placement_group = get_placement_group_details(connection, module) + placement_group = search_placement_group(connection, module) if placement_group is None: module.exit_json(changed=False) else: