From ec9c59f52b9cc30345a84ce642b120bc2c2e986b Mon Sep 17 00:00:00 2001 From: Sloane Hertel Date: Mon, 30 Apr 2018 15:27:22 -0400 Subject: [PATCH] [ec2_vpc_net] Add retries to describe_vpc_attribute call (#39256) * [ec2_vpc_net] Add retries to describe_vpc_attribute call * Use new AnsibleAWSModule client-based waiters --- lib/ansible/module_utils/aws/core.py | 9 ++++++++- .../modules/cloud/amazon/ec2_vpc_net.py | 20 ++++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/ansible/module_utils/aws/core.py b/lib/ansible/module_utils/aws/core.py index 91267cc7240..e4199fb14a0 100644 --- a/lib/ansible/module_utils/aws/core.py +++ b/lib/ansible/module_utils/aws/core.py @@ -195,6 +195,11 @@ class AnsibleAWSModule(object): 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 @@ -212,7 +217,9 @@ class _RetryingBotoClientWrapper(object): 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 diff --git a/lib/ansible/modules/cloud/amazon/ec2_vpc_net.py b/lib/ansible/modules/cloud/amazon/ec2_vpc_net.py index 8c74e7201e5..9c7dfc37029 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_vpc_net.py +++ b/lib/ansible/modules/cloud/amazon/ec2_vpc_net.py @@ -166,8 +166,8 @@ except ImportError: 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 @@ -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'), @@ -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, @@ -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') @@ -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: