From 1c771776a469e11e9a3de677b9d6bab77575a8d7 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 This commit was initially merged in https://github.com/ansible-collections/community.aws See: https://github.com/ansible-collections/community.aws/commit/f2d48514a4cf318b17e5aa31babb054f14ab9c37 --- plugins/modules/ec2_vpc_egress_igw.py | 29 ++++++++++++++++++--------- 1 file changed, 19 insertions(+), 10 deletions(-) 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')