Skip to content

Commit

Permalink
Rework route53 tagging logic a little
Browse files Browse the repository at this point in the history
  • Loading branch information
tremble authored and alinabuzachis committed Sep 26, 2022
1 parent 8672035 commit 82c51ed
Showing 1 changed file with 43 additions and 29 deletions.
72 changes: 43 additions & 29 deletions plugins/module_utils/route53.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,58 @@
from __future__ import absolute_import, division, print_function
__metaclass__ = type

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 compare_aws_tags


def manage_tags(module, client, resource_type, resource_spec, resource_id):
tagset = client.list_tags_for_resource(
ResourceType=resource_type,
ResourceId=resource_id,
)
old_tags = boto3_tag_list_to_ansible_dict(tagset['ResourceTagSet']['Tags'])
new_tags = {}
if resource_spec['tags']:
new_tags = resource_spec['tags']
tags_to_set, tags_to_delete = compare_aws_tags(
old_tags, new_tags,
purge_tags=resource_spec['purge_tags'],
)

if not tags_to_set and not tags_to_delete:
try:
import botocore
except ImportError:
pass # caught by AnsibleAWSModule

from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_code
from ansible_collections.amazon.aws.plugins.module_utils.tagging import ansible_dict_to_boto3_tag_list
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict
from ansible_collections.amazon.aws.plugins.module_utils.tagging import compare_aws_tags


def manage_tags(module, client, resource_type, resource_id, new_tags, purge_tags):
old_tags = get_tags(module, client, resource_type, resource_id)
tags_to_set, tags_to_delete = compare_aws_tags(old_tags, new_tags, purge_tags=purge_tags)

change_params = dict()
if tags_to_set:
change_params['AddTags'] = ansible_dict_to_boto3_tag_list(tags_to_set)
if tags_to_delete:
change_params['RemoveTagKeys'] = tags_to_delete

if not change_params:
return False

if module.check_mode:
return True

# boto3 does not provide create/remove functions for tags in Route 53,
# neither it works with empty values as parameters to change_tags_for_resource,
# so we need to call the change function twice
if tags_to_set:
try:
client.change_tags_for_resource(
ResourceType=resource_type,
ResourceId=resource_id,
AddTags=ansible_dict_to_boto3_tag_list(tags_to_set),
**change_params
)
if tags_to_delete:
client.change_tags_for_resource(
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e:
module.fail_json_aws(e, msg='Failed to update tags on {0}'.format(resource_type),
resource_id=resource_id, change_params=change_params)
return True


def get_tags(module, client, resource_type, resource_id):
try:
tagset = client.list_tags_for_resource(
ResourceType=resource_type,
ResourceId=resource_id,
RemoveTagKeys=tags_to_delete,
)
return True
except is_boto3_error_code('NoSuchHealthCheck'):
return {}
except is_boto3_error_code('NoSuchHostedZone'): # pylint: disable=duplicate-except
return {}
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg='Failed to fetch tags on {0}'.format(resource_type),
resource_id=resource_id)

tags = boto3_tag_list_to_ansible_dict(tagset['ResourceTagSet']['Tags'])
return tags

0 comments on commit 82c51ed

Please sign in to comment.