Skip to content

Commit

Permalink
Add retries to ec2_vpc_egress_igw (ansible-collections#421)
Browse files Browse the repository at this point in the history
* ec2_vpc_egress_igw - use connection rather than 'conn' for consistency

* ec2_vpc_egress_igw - enable retry decorator

* changelog
  • Loading branch information
tremble authored and danielcotton committed Nov 23, 2021
1 parent 31bd3f2 commit 63b8038
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/421-ec2_vpc_egress_igw-retry.yml
Original file line number Diff line number Diff line change
@@ -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).
29 changes: 19 additions & 10 deletions plugins/modules/ec2_vpc_egress_igw.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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")

Expand All @@ -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')
Expand Down

0 comments on commit 63b8038

Please sign in to comment.