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

This commit was initially merged in https://github.com/ansible-collections/community.aws
See: ansible-collections/community.aws@f2d4851
  • Loading branch information
tremble authored and abikouo committed Oct 15, 2024
1 parent cc22847 commit 1c77177
Showing 1 changed file with 19 additions and 10 deletions.
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 1c77177

Please sign in to comment.