From 1e7d2bd318503289a42da21b81975d34e810b2d1 Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Wed, 1 Jun 2022 13:11:59 +0200 Subject: [PATCH] Tagging fragment - use fragment and remove default empty dict where purge_tags default is True (#1183) Tagging - remove default empty dict where purge_tags default is True Depends-On: ansible-collections/amazon.aws#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 This commit was initially merged in https://github.com/ansible-collections/community.aws See: https://github.com/ansible-collections/community.aws/commit/7d98adc70de8aeee77a074cf57bb17497eaa099e --- plugins/modules/ec2_transit_gateway.py | 94 +++++--------------------- 1 file changed, 16 insertions(+), 78 deletions(-) diff --git a/plugins/modules/ec2_transit_gateway.py b/plugins/modules/ec2_transit_gateway.py index 4237376203b..be1082768fa 100644 --- a/plugins/modules/ec2_transit_gateway.py +++ b/plugins/modules/ec2_transit_gateway.py @@ -44,11 +44,6 @@ - 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. @@ -56,10 +51,6 @@ 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. @@ -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 = ''' @@ -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): @@ -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 @@ -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']) @@ -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'),