Retry creation of tags when DHCP optionset is not found (#38528)
This commit is contained in:
parent
361437b042
commit
56cd8f2d48
1 changed files with 18 additions and 12 deletions
|
@ -201,6 +201,18 @@ def get_resource_tags(vpc_conn, resource_id):
|
||||||
return dict((t.name, t.value) for t in vpc_conn.get_all_tags(filters={'resource-id': resource_id}))
|
return dict((t.name, t.value) for t in vpc_conn.get_all_tags(filters={'resource-id': resource_id}))
|
||||||
|
|
||||||
|
|
||||||
|
def retry_not_found(to_call, *args, **kwargs):
|
||||||
|
start_time = time()
|
||||||
|
while time() < start_time + 300:
|
||||||
|
try:
|
||||||
|
return to_call(*args, **kwargs)
|
||||||
|
except EC2ResponseError as e:
|
||||||
|
if e.error_code == 'InvalidDhcpOptionID.NotFound':
|
||||||
|
sleep(3)
|
||||||
|
continue
|
||||||
|
raise e
|
||||||
|
|
||||||
|
|
||||||
def ensure_tags(module, vpc_conn, resource_id, tags, add_only, check_mode):
|
def ensure_tags(module, vpc_conn, resource_id, tags, add_only, check_mode):
|
||||||
try:
|
try:
|
||||||
cur_tags = get_resource_tags(vpc_conn, resource_id)
|
cur_tags = get_resource_tags(vpc_conn, resource_id)
|
||||||
|
@ -209,11 +221,11 @@ def ensure_tags(module, vpc_conn, resource_id, tags, add_only, check_mode):
|
||||||
|
|
||||||
to_delete = dict((k, cur_tags[k]) for k in cur_tags if k not in tags)
|
to_delete = dict((k, cur_tags[k]) for k in cur_tags if k not in tags)
|
||||||
if to_delete and not add_only:
|
if to_delete and not add_only:
|
||||||
vpc_conn.delete_tags(resource_id, to_delete, dry_run=check_mode)
|
retry_not_found(vpc_conn.delete_tags, resource_id, to_delete, dry_run=check_mode)
|
||||||
|
|
||||||
to_add = dict((k, tags[k]) for k in tags if k not in cur_tags)
|
to_add = dict((k, tags[k]) for k in tags if k not in cur_tags)
|
||||||
if to_add:
|
if to_add:
|
||||||
vpc_conn.create_tags(resource_id, to_add, dry_run=check_mode)
|
retry_not_found(vpc_conn.create_tags, resource_id, to_add, dry_run=check_mode)
|
||||||
|
|
||||||
latest_tags = get_resource_tags(vpc_conn, resource_id)
|
latest_tags = get_resource_tags(vpc_conn, resource_id)
|
||||||
return {'changed': True, 'tags': latest_tags}
|
return {'changed': True, 'tags': latest_tags}
|
||||||
|
@ -363,16 +375,10 @@ def main():
|
||||||
# wait for dhcp option to be accessible
|
# wait for dhcp option to be accessible
|
||||||
found_dhcp_opt = False
|
found_dhcp_opt = False
|
||||||
start_time = time()
|
start_time = time()
|
||||||
while time() < start_time + 300:
|
try:
|
||||||
try:
|
found_dhcp_opt = retry_not_found(connection.get_all_dhcp_options, dhcp_options_ids=[dhcp_option.id])
|
||||||
found_dhcp_opt = connection.get_all_dhcp_options(dhcp_options_ids=[dhcp_option.id])
|
except EC2ResponseError as e:
|
||||||
except EC2ResponseError as e:
|
module.fail_json(msg="Failed to describe DHCP options", exception=traceback.format_exc)
|
||||||
if e.error_code == 'InvalidDhcpOptionID.NotFound':
|
|
||||||
sleep(3)
|
|
||||||
else:
|
|
||||||
module.fail_json(msg="Failed to describe DHCP options", exception=traceback.format_exc)
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
if not found_dhcp_opt:
|
if not found_dhcp_opt:
|
||||||
module.fail_json(msg="Failed to wait for {0} to be available.".format(dhcp_option.id))
|
module.fail_json(msg="Failed to wait for {0} to be available.".format(dhcp_option.id))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue