Skip to content

Commit

Permalink
ec2_placement_group: Handle a potential race creation during create
Browse files Browse the repository at this point in the history
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
  • Loading branch information
goneri committed Sep 16, 2022
1 parent a52608b commit fe3a568
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/ec2_placement_group_race_on_create.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
bugfixes:
- "ec2_placement_group - Handle a potential race creation during the creation of a new Placement Group (https://github.com/ansible-collections/community.aws/pull/1477)."
29 changes: 23 additions & 6 deletions plugins/modules/ec2_placement_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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")
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit fe3a568

Please sign in to comment.