Skip to content

Commit

Permalink
IGW use module_util functions for tag management (ansible-collections…
Browse files Browse the repository at this point in the history
…#523)

IGW use module_util functions for tag management

SUMMARY


Internet Gateway use module_util functions for tag management
Cleaning


ISSUE TYPE


Feature Pull Request

COMPONENT NAME

ec2_vpc_igw

Reviewed-by: Mark Chappell <None>
Reviewed-by: None <None>
  • Loading branch information
alinabuzachis authored Oct 4, 2021
1 parent b9f0bda commit d2ebd2e
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 143 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/523-ec2_vpc_igw-tagging.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- ec2_vpc_igw - use module_util helper for tagging (https://github.com/ansible-collections/amazon.aws/pull/523).
113 changes: 35 additions & 78 deletions plugins/modules/ec2_vpc_igw.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,23 @@
except ImportError:
pass # caught by AnsibleAWSModule

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.waiters import get_waiter
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ansible_dict_to_boto3_filter_list
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ansible_dict_to_boto3_tag_list
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import boto3_tag_list_to_ansible_dict
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import camel_dict_to_snake_dict
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import compare_aws_tags
from ..module_utils.core import AnsibleAWSModule
from ..module_utils.waiters import get_waiter
from ..module_utils.ec2 import AWSRetry
from ..module_utils.ec2 import ansible_dict_to_boto3_filter_list
from ..module_utils.ec2 import boto3_tag_list_to_ansible_dict
from ..module_utils.ec2 import camel_dict_to_snake_dict
from ..module_utils.ec2 import ensure_ec2_tags


class AnsibleEc2Igw(object):
class AnsibleEc2Igw():

def __init__(self, module, results):
self._module = module
self._results = results
self._connection = self._module.client('ec2', retry_decorator=AWSRetry.jittered_backoff())
self._connection = self._module.client(
'ec2', retry_decorator=AWSRetry.jittered_backoff()
)
self._check_mode = self._module.check_mode

def process(self):
Expand All @@ -144,74 +145,19 @@ def get_matching_igw(self, vpc_id):
igw = None
if len(igws) > 1:
self._module.fail_json(
msg='EC2 returned more than one Internet Gateway for VPC {0}, aborting'.format(vpc_id))
msg='EC2 returned more than one Internet Gateway for VPC {0}, aborting'
.format(vpc_id))
elif igws:
igw = camel_dict_to_snake_dict(igws[0])

return igw

def ensure_tags(self, igw_id, tags, purge_tags):
final_tags = []

filters = ansible_dict_to_boto3_filter_list({'resource-id': igw_id, 'resource-type': 'internet-gateway'})
cur_tags = None
try:
cur_tags = self._connection.describe_tags(aws_retry=True, Filters=filters)
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
self._module.fail_json_aws(e, msg="Couldn't describe tags")

if tags is None:
return boto3_tag_list_to_ansible_dict(cur_tags.get('Tags'))

to_update, to_delete = compare_aws_tags(boto3_tag_list_to_ansible_dict(cur_tags.get('Tags')), tags, purge_tags)
final_tags = boto3_tag_list_to_ansible_dict(cur_tags.get('Tags'))

if to_update:
try:
if self._check_mode:
final_tags.update(to_update)
else:
self._connection.create_tags(
aws_retry=True,
Resources=[igw_id],
Tags=ansible_dict_to_boto3_tag_list(to_update)
)

self._results['changed'] = True
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
self._module.fail_json_aws(e, msg="Couldn't create tags")

if to_delete:
try:
if self._check_mode:
for key in to_delete:
del final_tags[key]
else:
tags_list = []
for key in to_delete:
tags_list.append({'Key': key})

self._connection.delete_tags(aws_retry=True, Resources=[igw_id], Tags=tags_list)

self._results['changed'] = True
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
self._module.fail_json_aws(e, msg="Couldn't delete tags")

if not self._check_mode and (to_update or to_delete):
try:
response = self._connection.describe_tags(aws_retry=True, Filters=filters)
final_tags = boto3_tag_list_to_ansible_dict(response.get('Tags'))
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
self._module.fail_json_aws(e, msg="Couldn't describe tags")

return final_tags

@staticmethod
def get_igw_info(igw):
def get_igw_info(igw, vpc_id):
return {
'gateway_id': igw['internet_gateway_id'],
'tags': igw['tags'],
'vpc_id': igw['vpc_id']
'tags': boto3_tag_list_to_ansible_dict(igw['tags']),
'vpc_id': vpc_id
}

def ensure_igw_absent(self, vpc_id):
Expand All @@ -225,8 +171,15 @@ def ensure_igw_absent(self, vpc_id):

try:
self._results['changed'] = True
self._connection.detach_internet_gateway(aws_retry=True, InternetGatewayId=igw['internet_gateway_id'], VpcId=vpc_id)
self._connection.delete_internet_gateway(aws_retry=True, InternetGatewayId=igw['internet_gateway_id'])
self._connection.detach_internet_gateway(
aws_retry=True,
InternetGatewayId=igw['internet_gateway_id'],
VpcId=vpc_id
)
self._connection.delete_internet_gateway(
aws_retry=True,
InternetGatewayId=igw['internet_gateway_id']
)
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
self._module.fail_json_aws(e, msg="Unable to delete Internet Gateway")

Expand All @@ -249,18 +202,22 @@ def ensure_igw_present(self, vpc_id, tags, purge_tags):
waiter.wait(InternetGatewayIds=[response['InternetGateway']['InternetGatewayId']])

igw = camel_dict_to_snake_dict(response['InternetGateway'])
self._connection.attach_internet_gateway(aws_retry=True, InternetGatewayId=igw['internet_gateway_id'], VpcId=vpc_id)
self._connection.attach_internet_gateway(
aws_retry=True,
InternetGatewayId=igw['internet_gateway_id'],
VpcId=vpc_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')

igw['vpc_id'] = vpc_id

igw['tags'] = self.ensure_tags(igw_id=igw['internet_gateway_id'], tags=tags, purge_tags=purge_tags)

igw_info = self.get_igw_info(igw)
self._results['changed'] |= ensure_ec2_tags(
self._connection, self._module, igw['internet_gateway_id'],
resource_type='internet-gateway', tags=tags, purge_tags=purge_tags
)
igw_info = self.get_igw_info(self.get_matching_igw(vpc_id), vpc_id)
self._results.update(igw_info)

return self._results
Expand Down
Loading

0 comments on commit d2ebd2e

Please sign in to comment.