From 4de3f2461f194734c4f9f5f3c49616d341c3b9f2 Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Fri, 19 Feb 2021 00:54:50 +0100 Subject: [PATCH] Add retries to ec2_vpc_egress_igw (#421) * ec2_vpc_egress_igw - use connection rather than 'conn' for consistency * ec2_vpc_egress_igw - enable retry decorator * changelog --- .../421-ec2_vpc_egress_igw-retry.yml | 2 ++ plugins/modules/ec2_vpc_egress_igw.py | 29 ++++++++++++------- 2 files changed, 21 insertions(+), 10 deletions(-) create mode 100644 changelogs/fragments/421-ec2_vpc_egress_igw-retry.yml diff --git a/changelogs/fragments/421-ec2_vpc_egress_igw-retry.yml b/changelogs/fragments/421-ec2_vpc_egress_igw-retry.yml new file mode 100644 index 00000000000..31c03c833c2 --- /dev/null +++ b/changelogs/fragments/421-ec2_vpc_egress_igw-retry.yml @@ -0,0 +1,2 @@ +minor_changes: +- ec2_vpc_egress_igw - Add retries on common AWS failures (https://github.com/ansible-collections/community.aws/pull/421). diff --git a/plugins/modules/ec2_vpc_egress_igw.py b/plugins/modules/ec2_vpc_egress_igw.py index 0026ade65ad..23c2f86abd0 100644 --- a/plugins/modules/ec2_vpc_egress_igw.py +++ b/plugins/modules/ec2_vpc_egress_igw.py @@ -66,20 +66,24 @@ from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_code +from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry -def delete_eigw(module, conn, eigw_id): +def delete_eigw(module, connection, eigw_id): """ Delete EIGW. module : AnsibleAWSModule object - conn : boto3 client connection object + connection : boto3 client connection object eigw_id : ID of the EIGW to delete """ changed = False try: - response = conn.delete_egress_only_internet_gateway(DryRun=module.check_mode, EgressOnlyInternetGatewayId=eigw_id) + response = connection.delete_egress_only_internet_gateway( + aws_retry=True, + DryRun=module.check_mode, + EgressOnlyInternetGatewayId=eigw_id) except is_boto3_error_code('DryRunOperation'): changed = True except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except @@ -91,19 +95,22 @@ def delete_eigw(module, conn, eigw_id): return changed -def create_eigw(module, conn, vpc_id): +def create_eigw(module, connection, vpc_id): """ Create EIGW. module : AnsibleAWSModule object - conn : boto3 client connection object + connection : boto3 client connection object vpc_id : ID of the VPC we are operating on """ gateway_id = None changed = False try: - response = conn.create_egress_only_internet_gateway(DryRun=module.check_mode, VpcId=vpc_id) + response = connection.create_egress_only_internet_gateway( + aws_retry=True, + DryRun=module.check_mode, + VpcId=vpc_id) except is_boto3_error_code('DryRunOperation'): # When boto3 method is run with DryRun=True it returns an error on success # We need to catch the error and return something valid @@ -128,18 +135,19 @@ def create_eigw(module, conn, vpc_id): return changed, gateway_id -def describe_eigws(module, conn, vpc_id): +def describe_eigws(module, connection, vpc_id): """ Describe EIGWs. module : AnsibleAWSModule object - conn : boto3 client connection object + connection : boto3 client connection object vpc_id : ID of the VPC we are operating on """ gateway_id = None try: - response = conn.describe_egress_only_internet_gateways() + response = connection.describe_egress_only_internet_gateways( + aws_retry=True) except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: module.fail_json_aws(e, msg="Could not get list of existing Egress-Only Internet Gateways") @@ -159,7 +167,8 @@ def main(): module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) - connection = module.client('ec2') + retry_decorator = AWSRetry.jittered_backoff(retries=10) + connection = module.client('ec2', retry_decorator=retry_decorator) vpc_id = module.params.get('vpc_id') state = module.params.get('state')