Skip to content

Commit

Permalink
[ec2_vpc_net] Add retries to describe_vpc_attribute call (ansible#39256)
Browse files Browse the repository at this point in the history
* [ec2_vpc_net] Add retries to describe_vpc_attribute call

* Use new AnsibleAWSModule client-based waiters
  • Loading branch information
s-hertel authored and ryansb committed Apr 30, 2018
1 parent cc06f4c commit ec9c59f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
9 changes: 8 additions & 1 deletion lib/ansible/module_utils/aws/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ def _gather_versions(self):


class _RetryingBotoClientWrapper(object):
__never_wait = (
'get_paginator', 'can_paginate',
'get_waiter', 'generate_presigned_url',
)

def __init__(self, client, retry):
self.client = client
self.retry = retry
Expand All @@ -212,7 +217,9 @@ def deciding_wrapper(aws_retry=False, *args, **kwargs):

def __getattr__(self, name):
unwrapped = getattr(self.client, name)
if callable(unwrapped):
if name in self.__never_wait:
return unwrapped
elif callable(unwrapped):
wrapped = self._create_optional_retry_wrapper_function(unwrapped)
setattr(self, name, wrapped)
return wrapped
Expand Down
20 changes: 11 additions & 9 deletions lib/ansible/modules/cloud/amazon/ec2_vpc_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@

from time import sleep, time
from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.ec2 import (boto3_conn, get_aws_connection_info, ec2_argument_spec, camel_dict_to_snake_dict,
ansible_dict_to_boto3_tag_list, boto3_tag_list_to_ansible_dict, AWSRetry)
from ansible.module_utils.ec2 import (AWSRetry, camel_dict_to_snake_dict,
ansible_dict_to_boto3_tag_list, boto3_tag_list_to_ansible_dict)
from ansible.module_utils.six import string_types


Expand Down Expand Up @@ -312,8 +312,7 @@ def wait_for_vpc_attribute(connection, module, vpc_id, attribute, expected_value


def main():
argument_spec = ec2_argument_spec()
argument_spec.update(dict(
argument_spec = dict(
name=dict(required=True),
cidr_block=dict(type='list', required=True),
tenancy=dict(choices=['default', 'dedicated'], default='default'),
Expand All @@ -325,7 +324,6 @@ def main():
multi_ok=dict(type='bool', default=False),
purge_cidrs=dict(type='bool', default=False),
)
)

module = AnsibleAWSModule(
argument_spec=argument_spec,
Expand All @@ -345,8 +343,12 @@ def main():

changed = False

region, ec2_url, aws_connect_params = get_aws_connection_info(module, boto3=True)
connection = boto3_conn(module, conn_type='client', resource='ec2', region=region, endpoint=ec2_url, **aws_connect_params)
connection = module.client(
'ec2',
retry_decorator=AWSRetry.jittered_backoff(
retries=8, delay=3, catch_extra_error_codes=['InvalidVpcID.NotFound']
)
)

if dns_hostnames and not dns_support:
module.fail_json(msg='In order to enable DNS Hostnames you must also enable DNS support')
Expand Down Expand Up @@ -396,8 +398,8 @@ def main():
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="Failed to update tags")

current_dns_enabled = connection.describe_vpc_attribute(Attribute='enableDnsSupport', VpcId=vpc_id)['EnableDnsSupport']['Value']
current_dns_hostnames = connection.describe_vpc_attribute(Attribute='enableDnsHostnames', VpcId=vpc_id)['EnableDnsHostnames']['Value']
current_dns_enabled = connection.describe_vpc_attribute(Attribute='enableDnsSupport', VpcId=vpc_id, aws_retry=True)['EnableDnsSupport']['Value']
current_dns_hostnames = connection.describe_vpc_attribute(Attribute='enableDnsHostnames', VpcId=vpc_id, aws_retry=True)['EnableDnsHostnames']['Value']
if current_dns_enabled != dns_support:
changed = True
if not module.check_mode:
Expand Down

0 comments on commit ec9c59f

Please sign in to comment.