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 committed Oct 12, 2021
1 parent 55fc45f commit 26bebba
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 34 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
14 changes: 9 additions & 5 deletions plugins/modules/route53_zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
import time
from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.community.aws.plugins.module_utils.route53 import manage_tags
from ansible_collections.community.aws.plugins.module_utils.route53 import get_tags

try:
from botocore.exceptions import BotoCoreError, ClientError
Expand Down Expand Up @@ -197,15 +198,21 @@ def create(module, client, matching_zones):
'name': zone_in,
'delegation_set_id': delegation_set_id,
'zone_id': None,
'tags': tags,
'purge_tags': purge_tags,
}

if private_zone:
changed, result = create_or_update_private(module, client, matching_zones, record)
else:
changed, result = create_or_update_public(module, client, matching_zones, record)

zone_id = result.get('zone_id')
if zone_id:
if tags is not None:
changed |= manage_tags(module, client, 'hostedzone', zone_id, tags, purge_tags)
result['tags'] = get_tags(module, client, 'hostedzone', zone_id)
else:
result['tags'] = tags

return changed, result


Expand Down Expand Up @@ -322,9 +329,6 @@ def create_or_update_public(module, client, matching_zones, record):
record['name'] = zone_details['Name']
record['delegation_set_id'] = zone_delegation_set_details.get('Id', '').replace('/delegationset/', '')

if record['tags'] or record['purge_tags']:
changed = manage_tags(module, client, 'hostedzone', record, zone_details['Id'].replace('/hostedzone/', ''))

return changed, record


Expand Down
2 changes: 2 additions & 0 deletions tests/integration/targets/route53_zone/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
comment: updated comment
state: present
purge_tags: true
tags: {}
register: output

- assert:
Expand All @@ -132,6 +133,7 @@
comment: updated comment for check
state: present
purge_tags: true
tags: {}
register: output
check_mode: yes

Expand Down

0 comments on commit 26bebba

Please sign in to comment.