Skip to content

Commit

Permalink
Use pagination and add more retry wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
tremble committed Feb 20, 2021
1 parent 21c6ee3 commit b98e4d9
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions plugins/modules/ec2_vpc_nat_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,18 @@
DRY_RUN_MSGS = 'DryRun Mode:'


@AWSRetry.jittered_backoff(retries=10)
def _describe_addresses(client, **params):
paginator = client.get_paginator('describe_addresses')
return paginator.paginate(**kwargs).build_full_result()['Addresses']


@AWSRetry.jittered_backoff(retries=10)
def _describe_nat_gateways(client, **params):
paginator = client.get_paginator('describe_nat_gateways')
return paginator.paginate(**kwargs).build_full_result()['NatGateways']


def get_nat_gateways(client, subnet_id=None, nat_gateway_id=None,
states=None, check_mode=False):
"""Retrieve a list of NAT Gateways
Expand Down Expand Up @@ -339,7 +351,7 @@ def get_nat_gateways(client, subnet_id=None, nat_gateway_id=None,

try:
if not check_mode:
gateways = client.describe_nat_gateways(**params)['NatGateways']
gateways = _describe_nat_gateways(client, **params)
if gateways:
for gw in gateways:
existing_gateways.append(camel_dict_to_snake_dict(gw))
Expand Down Expand Up @@ -534,7 +546,7 @@ def get_eip_allocation_id_by_address(client, eip_address, check_mode=False):
err_msg = ""
try:
if not check_mode:
allocations = client.describe_addresses(**params)['Addresses']
allocations = _describe_addresses(client, **params)
if len(allocations) == 1:
allocation = allocations[0]
else:
Expand Down Expand Up @@ -597,7 +609,7 @@ def allocate_eip_address(client, check_mode=False):
)
new_eip = 'eipalloc-{0}'.format(random_numbers)
else:
new_eip = client.allocate_address(**params)['AllocationId']
new_eip = client.allocate_address(aws_retry=True, **params)['AllocationId']
ip_allocated = True
err_msg = 'eipalloc id {0} created'.format(new_eip)

Expand Down Expand Up @@ -632,14 +644,14 @@ def release_address(client, allocation_id, check_mode=False):

ip_released = False
try:
client.describe_addresses(AllocationIds=[allocation_id])
_describe_addresses(client, aws_retry=True, AllocationIds=[allocation_id])
except botocore.exceptions.ClientError as e:
# IP address likely already released
# Happens with gateway in 'deleted' state that
# still lists associations
return True, str(e)
try:
client.release_address(AllocationId=allocation_id)
client.release_address(aws_retry=True, AllocationId=allocation_id)
ip_released = True
except botocore.exceptions.ClientError as e:
err_msg = str(e)
Expand Down Expand Up @@ -712,7 +724,7 @@ def create(client, module, subnet_id, allocation_id, tags, purge_tags, client_to

try:
if not check_mode:
result = camel_dict_to_snake_dict(client.create_nat_gateway(**params)["NatGateway"])
result = camel_dict_to_snake_dict(client.create_nat_gateway(aws_retry=True, **params)["NatGateway"])
else:
result = DRY_RUN_GATEWAYS[0]
result['create_time'] = datetime.datetime.utcnow()
Expand Down Expand Up @@ -939,7 +951,7 @@ def remove(client, nat_gateway_id, wait=False, wait_timeout=0,
if exist and len(gw) == 1:
results = gw[0]
if not check_mode:
client.delete_nat_gateway(**params)
client.delete_nat_gateway(aws_retry=True, **params)

allocation_id = (
results['nat_gateway_addresses'][0]['allocation_id']
Expand Down

0 comments on commit b98e4d9

Please sign in to comment.