Skip to content

Commit

Permalink
Tagging fragment - use fragment and remove default empty dict where p…
Browse files Browse the repository at this point in the history
…urge_tags default is True (ansible-collections#1183)

Tagging - remove default empty dict where purge_tags default is True

Depends-On: ansible-collections#844
SUMMARY

Move modules over to the new tagging fragment
Update modules to remove default tags of {} and use None instead, so that purging tags only happens if someone explicitly passes the tags parameter

ISSUE TYPE

Docs Pull Request
Feature Pull Request

COMPONENT NAME
plugins/modules/ec2_transit_gateway.py
plugins/modules/efs.py
plugins/modules/eks_fargate_profile.py
plugins/modules/elb_target_group.py
ADDITIONAL INFORMATION

Reviewed-by: Alina Buzachis <None>

This commit was initially merged in https://github.com/ansible-collections/community.aws
See: ansible-collections/community.aws@7d98adc
  • Loading branch information
tremble authored and mandar242 committed Oct 24, 2024
1 parent 6e6b347 commit 1e7d2bd
Showing 1 changed file with 16 additions and 78 deletions.
94 changes: 16 additions & 78 deletions plugins/modules/ec2_transit_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,13 @@
- Whether to enable AWS DNS support.
default: true
type: bool
purge_tags:
description:
- Whether to purge existing tags not included with tags argument.
default: true
type: bool
state:
description:
- C(present) to ensure resource is created.
- C(absent) to remove resource.
default: present
choices: [ "present", "absent"]
type: str
tags:
description:
- A dictionary of resource tags
type: dict
transit_gateway_id:
description:
- The ID of the transit gateway.
Expand All @@ -80,11 +71,12 @@
default: 300
type: int
author: "Bob Boldin (@BobBoldin)"
author:
- "Bob Boldin (@BobBoldin)"
extends_documentation_fragment:
- amazon.aws.aws
- amazon.aws.ec2
- amazon.aws.aws
- amazon.aws.ec2
- amazon.aws.tags
'''

EXAMPLES = '''
Expand Down Expand Up @@ -226,15 +218,11 @@

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from time import sleep, time
from ansible.module_utils._text import to_text
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import (
ansible_dict_to_boto3_tag_list,
ansible_dict_to_boto3_filter_list,
AWSRetry,
boto3_tag_list_to_ansible_dict,
camel_dict_to_snake_dict,
compare_aws_tags
)
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 AWSRetry
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 ensure_ec2_tags


class AnsibleEc2Tgw(object):
Expand Down Expand Up @@ -412,57 +400,6 @@ def delete_tgw(self, tgw_id):

return result

def ensure_tags(self, tgw_id, tags, purge_tags):
"""
Ensures tags are applied to the transit gateway. Optionally will remove any
existing tags not in the tags argument if purge_tags is set to true
:param tgw_id: The AWS id of the transit gateway
:param tags: list of tags to apply to the transit gateway.
:param purge_tags: when true existing tags not in tags parms are removed
:return: true if tags were updated
"""
tags_changed = False
filters = ansible_dict_to_boto3_filter_list({'resource-id': tgw_id})
try:
cur_tags = self._connection.describe_tags(Filters=filters)
except (ClientError, BotoCoreError) as e:
self._module.fail_json_aws(e, msg="Couldn't describe tags")

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

if to_update:
try:
if not self._check_mode:
AWSRetry.exponential_backoff()(self._connection.create_tags)(
Resources=[tgw_id],
Tags=ansible_dict_to_boto3_tag_list(to_update)
)
self._results['changed'] = True
tags_changed = True
except (ClientError, BotoCoreError) as e:
self._module.fail_json_aws(e, msg="Couldn't create tags {0} for resource {1}".format(
ansible_dict_to_boto3_tag_list(to_update), tgw_id))

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

AWSRetry.exponential_backoff()(self._connection.delete_tags)(
Resources=[tgw_id],
Tags=tags_list
)
self._results['changed'] = True
tags_changed = True
except (ClientError, BotoCoreError) as e:
self._module.fail_json_aws(e, msg="Couldn't delete tags {0} for resource {1}".format(
ansible_dict_to_boto3_tag_list(to_delete), tgw_id))

return tags_changed

def ensure_tgw_present(self, tgw_id=None, description=None):
"""
Will create a tgw if no match to the tgw_id or description are found
Expand All @@ -488,10 +425,11 @@ def ensure_tgw_present(self, tgw_id=None, description=None):
except (BotoCoreError, ClientError) as e:
self._module.fail_json_aws(e, msg='Unable to create Transit Gateway')

if self._module.params.get('tags') != tgw.get('tags'):
stringed_tags_dict = dict((to_text(k), to_text(v)) for k, v in self._module.params.get('tags').items())
if self.ensure_tags(tgw['transit_gateway_id'], stringed_tags_dict, self._module.params.get('purge_tags')):
self._results['changed'] = True
self._results['changed'] |= ensure_ec2_tags(
self._connection, self._module, tgw['transit_gateway_id'],
tags=self._module.params.get('tags'),
purge_tags=self._module.params.get('purge_tags'),
)

self._results['transit_gateway'] = self.get_matching_tgw(tgw_id=tgw['transit_gateway_id'])

Expand Down Expand Up @@ -539,7 +477,7 @@ def setup_module_object():
dns_support=dict(type='bool', default='yes'),
purge_tags=dict(type='bool', default='yes'),
state=dict(default='present', choices=['present', 'absent']),
tags=dict(default=dict(), type='dict'),
tags=dict(type='dict', aliases=['resource_tags']),
transit_gateway_id=dict(type='str'),
vpn_ecmp_support=dict(type='bool', default='yes'),
wait=dict(type='bool', default='yes'),
Expand Down

0 comments on commit 1e7d2bd

Please sign in to comment.