Skip to content

Commit

Permalink
ec2_vpc_igw: fix NoneType error (ansible-collections#695)
Browse files Browse the repository at this point in the history
`ec2_vpc_igw`: fix NoneType error

SUMMARY

use paginator for describe internet gateways and add retry_codes='InvalidInternetGatewayID.NotFound' (thanks @alinabuzachis)
Fixes ansible-collections#647

ISSUE TYPE

Bugfix Pull Request

COMPONENT NAME
ec2_vpc_igw
ADDITIONAL INFORMATION

previously added InternetGatewayAttached waiter but problem still persists (ansible-collections#691)

Reviewed-by: Abhijeet Kasurde <None>
Reviewed-by: Jill R <None>
  • Loading branch information
jatorcasso authored Mar 4, 2022
1 parent fecc95d commit 1965b05
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- ec2_vpc_igw - use paginator for describe internet gateways and add retry to fix NoneType object is not subscriptable error (https://github.com/ansible-collections/amazon.aws/pull/695).
17 changes: 12 additions & 5 deletions plugins/modules/ec2_vpc_igw.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@
from ..module_utils.tagging import boto3_tag_list_to_ansible_dict


@AWSRetry.jittered_backoff(retries=10, delay=10)
def describe_igws_with_backoff(connection, **params):
paginator = connection.get_paginator('describe_internet_gateways')
return paginator.paginate(**params).build_full_result()['InternetGateways']


class AnsibleEc2Igw():

def __init__(self, module, results):
Expand All @@ -135,10 +141,8 @@ def process(self):

def get_matching_igw(self, vpc_id):
filters = ansible_dict_to_boto3_filter_list({'attachment.vpc-id': vpc_id})
igws = []
try:
response = self._connection.describe_internet_gateways(aws_retry=True, Filters=filters)
igws = response.get('InternetGateways', [])
igws = describe_igws_with_backoff(self._connection, Filters=filters)
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
self._module.fail_json_aws(e)

Expand Down Expand Up @@ -211,17 +215,20 @@ def ensure_igw_present(self, vpc_id, tags, purge_tags):
# Ensure the gateway is attached before proceeding
waiter = get_waiter(self._connection, 'internet_gateway_attached')
waiter.wait(InternetGatewayIds=[igw['internet_gateway_id']])

self._results['changed'] = True
except botocore.exceptions.WaiterError as e:
self._module.fail_json_aws(e, msg="No Internet Gateway exists.")
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
self._module.fail_json_aws(e, msg='Unable to create Internet Gateway')

# Modify tags
self._results['changed'] |= ensure_ec2_tags(
self._connection, self._module, igw['internet_gateway_id'],
resource_type='internet-gateway', tags=tags, purge_tags=purge_tags
resource_type='internet-gateway', tags=tags, purge_tags=purge_tags,
retry_codes='InvalidInternetGatewayID.NotFound'
)

# Update igw
igw = self.get_matching_igw(vpc_id)
igw_info = self.get_igw_info(igw, vpc_id)
self._results.update(igw_info)
Expand Down

0 comments on commit 1965b05

Please sign in to comment.