Skip to content

Commit

Permalink
ec2_placement_group: Handle a potential race creation during create (a…
Browse files Browse the repository at this point in the history
…nsible-collections#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 <None>
  • Loading branch information
goneri authored Sep 16, 2022
1 parent a0ca3c6 commit d917438
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions 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 d917438

Please sign in to comment.