From 1755f7ad6f996004a01af13838f1a09e431d8384 Mon Sep 17 00:00:00 2001 From: Mauricio Teixeira <1847440+badnetmask@users.noreply.github.com> Date: Mon, 3 May 2021 19:21:46 -0400 Subject: [PATCH 1/7] implement adding tags to brand new zones This commit was initially merged in https://github.com/ansible-collections/community.aws See: https://github.com/ansible-collections/community.aws/commit/8c20df05c2c2909ca8a718d9db6ec4d9459c08b1 --- plugins/module_utils/route53.py | 42 +++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 plugins/module_utils/route53.py diff --git a/plugins/module_utils/route53.py b/plugins/module_utils/route53.py new file mode 100644 index 00000000000..8830332c0cd --- /dev/null +++ b/plugins/module_utils/route53.py @@ -0,0 +1,42 @@ +# This file is part of Ansible +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +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'], + ) + # 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: + client.change_tags_for_resource( + ResourceType=resource_type, + ResourceId=resource_id, + AddTags=ansible_dict_to_boto3_tag_list(tags_to_set), + ) + if tags_to_delete: + client.change_tags_for_resource( + ResourceType=resource_type, + ResourceId=resource_id, + RemoveTagKeys=tags_to_delete, + ) + if tags_to_set or tags_to_delete: + return True + return False From 8672035458d59e2ea4fb5b80a4d7e75f8da74b46 Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Tue, 12 Oct 2021 10:56:09 +0200 Subject: [PATCH 2/7] Make sure we don't update tags in check_mode This commit was initially merged in https://github.com/ansible-collections/community.aws See: https://github.com/ansible-collections/community.aws/commit/55fc45fc0e4b6dc50311f88972eb684b59b920f1 --- plugins/module_utils/route53.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/plugins/module_utils/route53.py b/plugins/module_utils/route53.py index 8830332c0cd..15b9f63cc3e 100644 --- a/plugins/module_utils/route53.py +++ b/plugins/module_utils/route53.py @@ -22,6 +22,13 @@ def manage_tags(module, client, resource_type, resource_spec, resource_id): old_tags, new_tags, purge_tags=resource_spec['purge_tags'], ) + + if not tags_to_set and not tags_to_delete: + 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 @@ -37,6 +44,4 @@ def manage_tags(module, client, resource_type, resource_spec, resource_id): ResourceId=resource_id, RemoveTagKeys=tags_to_delete, ) - if tags_to_set or tags_to_delete: - return True - return False + return True From 82c51ed556bce483a0ba5b1d2792908ea7da9bdf Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Tue, 12 Oct 2021 13:52:39 +0200 Subject: [PATCH 3/7] Rework route53 tagging logic a little This commit was initially merged in https://github.com/ansible-collections/community.aws See: https://github.com/ansible-collections/community.aws/commit/26bebba5074378d88c138d5c954245a091a40e2f --- plugins/module_utils/route53.py | 72 ++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/plugins/module_utils/route53.py b/plugins/module_utils/route53.py index 15b9f63cc3e..3d21d8b0802 100644 --- a/plugins/module_utils/route53.py +++ b/plugins/module_utils/route53.py @@ -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 From 7e011fc730002e864269afc42c184cde1140add8 Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Mon, 18 Oct 2021 11:24:09 +0200 Subject: [PATCH 4/7] route53_health_check - add tagging support (#765) route53_health_check - add tagging support SUMMARY add tagging support to route53_health_check ISSUE TYPE Feature Pull Request COMPONENT NAME route53_health_check ADDITIONAL INFORMATION Reviewed-by: Alina Buzachis Reviewed-by: None This commit was initially merged in https://github.com/ansible-collections/community.aws See: https://github.com/ansible-collections/community.aws/commit/324801611f014a0b8fd9ed4d0b3154eb3b56c41c --- plugins/module_utils/route53.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plugins/module_utils/route53.py b/plugins/module_utils/route53.py index 3d21d8b0802..3e2940a5311 100644 --- a/plugins/module_utils/route53.py +++ b/plugins/module_utils/route53.py @@ -16,6 +16,9 @@ def manage_tags(module, client, resource_type, resource_id, new_tags, purge_tags): + if new_tags is None: + return False + 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) From 0a147228ed063081b342e72286dd7b6cb4f40418 Mon Sep 17 00:00:00 2001 From: Alina Buzachis Date: Mon, 26 Sep 2022 12:40:54 +0200 Subject: [PATCH 5/7] Update FQDN Signed-off-by: Alina Buzachis --- plugins/modules/route53_health_check.py | 4 ++-- plugins/modules/route53_zone.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/modules/route53_health_check.py b/plugins/modules/route53_health_check.py index 14f1b44e235..890857d47f7 100644 --- a/plugins/modules/route53_health_check.py +++ b/plugins/modules/route53_health_check.py @@ -268,8 +268,8 @@ from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_code from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry -from ansible_collections.community.aws.plugins.module_utils.route53 import get_tags -from ansible_collections.community.aws.plugins.module_utils.route53 import manage_tags +from ansible_collections.amazon.aws.plugins.module_utils.route53 import get_tags +from ansible_collections.amazon.aws.plugins.module_utils.route53 import manage_tags def _list_health_checks(**params): diff --git a/plugins/modules/route53_zone.py b/plugins/modules/route53_zone.py index 7e6abd92a4b..e5901386161 100644 --- a/plugins/modules/route53_zone.py +++ b/plugins/modules/route53_zone.py @@ -141,8 +141,8 @@ import time from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry -from ansible_collections.community.aws.plugins.module_utils.route53 import manage_tags -from ansible_collections.community.aws.plugins.module_utils.route53 import get_tags +from ansible_collections.amazon.aws.plugins.module_utils.route53 import manage_tags +from ansible_collections.amazon.aws.plugins.module_utils.route53 import get_tags try: from botocore.exceptions import BotoCoreError, ClientError From d92306fc0ae0b10cb692883a5b1378c9268f7e14 Mon Sep 17 00:00:00 2001 From: Alina Buzachis Date: Mon, 26 Sep 2022 12:49:06 +0200 Subject: [PATCH 6/7] Add changelog fragment Signed-off-by: Alina Buzachis --- changelogs/fragments/migrate_route53_module_utils.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/migrate_route53_module_utils.yml diff --git a/changelogs/fragments/migrate_route53_module_utils.yml b/changelogs/fragments/migrate_route53_module_utils.yml new file mode 100644 index 00000000000..000b97a16bd --- /dev/null +++ b/changelogs/fragments/migrate_route53_module_utils.yml @@ -0,0 +1,2 @@ +trivial: +- route53 - Migrate plugins/module_utils/route53.py to amazon.aws (https://github.com/ansible-collections/amazon.aws/pull/1066). From f1b6831c6bd151625b5145a42e47562f50c3b18c Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Mon, 26 Sep 2022 13:27:06 +0200 Subject: [PATCH 7/7] Add module_utils_route53 as alias for route53 integration tests --- tests/integration/targets/route53/aliases | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/integration/targets/route53/aliases b/tests/integration/targets/route53/aliases index 6db61c383f3..c6a08294495 100644 --- a/tests/integration/targets/route53/aliases +++ b/tests/integration/targets/route53/aliases @@ -1,3 +1,4 @@ cloud/aws route53_info +module_utils_route53