Debian Codegraph @

ansible_collections/amazon/aws/plugins/module_utils/route53.py

raw
1 # -*- coding: utf-8 -*-
2
3 # This file is part of Ansible
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import annotations
7
8 import typing
9
10 if typing.TYPE_CHECKING:
11 from ansible_collections.amazon.aws.plugins.module_utils.botocore import ClientType
12
13 # pylint: disable-next=unused-import
14 from ansible_collections.amazon.aws.plugins.module_utils._route53.common import AnsibleRoute53Error
15 from ansible_collections.amazon.aws.plugins.module_utils._route53.common import Route53ErrorHandler
16 from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry
17 from ansible_collections.amazon.aws.plugins.module_utils.tagging import ansible_dict_to_boto3_tag_list
18 from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict
19 from ansible_collections.amazon.aws.plugins.module_utils.tagging import compare_aws_tags
20
21
22 def manage_tags(
23 client: ClientType,
24 resource_type: str,
25 resource_id: str,
26 new_tags: dict,
27 purge_tags: bool,
28 check_mode: bool,
29 ) -> bool:
30 if new_tags is None:
31 return False
32
33 old_tags = get_tags(client, resource_type, resource_id)
34 tags_to_set, tags_to_delete = compare_aws_tags(old_tags, new_tags, purge_tags=purge_tags)
35
36 change_params = {}
37 if tags_to_set:
38 change_params["AddTags"] = ansible_dict_to_boto3_tag_list(tags_to_set)
39 if tags_to_delete:
40 change_params["RemoveTagKeys"] = tags_to_delete
41
42 if not change_params:
43 return False
44
45 if check_mode:
46 return True
47
48 _change_tags_for_resource(client, resource_type, resource_id, **change_params)
49 return True
50
51
52 @Route53ErrorHandler.common_error_handler("change tags for resource")
53 @AWSRetry.jittered_backoff()
54 def _change_tags_for_resource(client: ClientType, resource_type: str, resource_id: str, **kwargs) -> dict:
55 return client.change_tags_for_resource(
56 ResourceType=resource_type,
57 ResourceId=resource_id,
58 **kwargs,
59 )
60
61
62 @Route53ErrorHandler.list_error_handler("list tags for resource", {})
63 @AWSRetry.jittered_backoff()
64 def get_tags(client: ClientType, resource_type: str, resource_id: str) -> dict:
65 tagset = client.list_tags_for_resource(
66 ResourceType=resource_type,
67 ResourceId=resource_id,
68 )
69 tags = boto3_tag_list_to_ansible_dict(tagset["ResourceTagSet"]["Tags"])
70 return tags