-
Notifications
You must be signed in to change notification settings - Fork 396
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement adding tags to brand new zones
- Loading branch information
1 parent
e3a50c4
commit 8c20df0
Showing
2 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters