Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds tagging option to elasticache module #1970

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelogs/fragments/1970-elasticache-tags.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- elasticache - Add AWS resource tagging option to module (https://github.com/ansible-collections/community.aws/pull/1970)
39 changes: 38 additions & 1 deletion plugins/modules/elasticache.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@
type: list
elements: str
default: []
tags:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're extending the amazon.aws.tags documentation fragment, you do not need to document tags anymore. I would suggest to add a note about the version when tags support has been added.

description:
- A dictionary of tags to apply when creating an Elasticache instance.
type: dict
cache_security_groups:
description:
- A list of cache security group names to associate with this cache cluster.
Expand All @@ -97,6 +101,7 @@
extends_documentation_fragment:
- amazon.aws.common.modules
- amazon.aws.region.modules
- amazon.aws.tags
- amazon.aws.boto3
"""

Expand Down Expand Up @@ -138,6 +143,7 @@
pass # Handled by AnsibleAWSModule

from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code
from ansible_collections.amazon.aws.plugins.module_utils.tagging import ansible_dict_to_boto3_tag_list

from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule

Expand All @@ -161,6 +167,8 @@ def __init__(
cache_subnet_group,
cache_security_groups,
security_group_ids,
tags,
purge_tags,
zone,
wait,
hard_modify,
Expand All @@ -176,6 +184,8 @@ def __init__(
self.cache_subnet_group = cache_subnet_group
self.cache_security_groups = cache_security_groups
self.security_group_ids = security_group_ids
self.tags = ansible_dict_to_boto3_tag_list(tags)
self.purge_tags = purge_tags
self.zone = zone
self.wait = wait
self.hard_modify = hard_modify
Expand Down Expand Up @@ -230,6 +240,8 @@ def create(self):
CacheParameterGroupName=self.cache_parameter_group,
CacheSubnetGroupName=self.cache_subnet_group,
)
if self.tags:
kwargs["Tags"] = self.tags
if self.cache_port is not None:
kwargs["Port"] = self.cache_port
if self.zone is not None:
Expand Down Expand Up @@ -303,11 +315,14 @@ def sync(self):
if self._requires_modification():
self.modify()

if self.tags:
self.apply_tags

def modify(self):
"""Modify the cache cluster. Note it's only possible to modify a few select options."""
nodes_to_remove = self._get_nodes_to_remove()
try:
self.conn.modify_cache_cluster(
cluster = self.conn.modify_cache_cluster(
CacheClusterId=self.name,
NumCacheNodes=self.num_nodes,
CacheNodeIdsToRemove=nodes_to_remove,
Expand All @@ -326,6 +341,22 @@ def modify(self):
if self.wait:
self._wait_for_status("available")

def apply_tags(self):
cluster_arn = self.data["ARN"]
tags = self.tags

resp = self.conn.list_tags_for_resource(ResourceName=cluster_arn)
existing_tags = resp["TagList"]
if existing_tags:
if tags != existing_tags:
if self.purge_tags:
keys = [item["Key"] for item in existing_tags]
# simply remove all existing tags here, as we will re-add tags from module input below anyways
self.conn.remove_tags_from_resource(ResourceName=cluster_arn, TagKeys=keys)
self.conn.add_tags_to_resource(ResourceName=cluster_arn, Tags=tags)
else:
self.conn.add_tags_to_resource(ResourceName=cluster_arn, Tags=tags)

def reboot(self):
"""Reboot the cache cluster"""
if not self.exists():
Expand Down Expand Up @@ -489,6 +520,8 @@ def main():
cache_subnet_group=dict(default=""),
cache_security_groups=dict(default=[], type="list", elements="str"),
security_group_ids=dict(default=[], type="list", elements="str"),
tags=dict(type="dict", aliases=["resource_tags"]),
purge_tags=dict(type="bool", default=True),
zone=dict(),
wait=dict(default=True, type="bool"),
hard_modify=dict(type="bool"),
Expand All @@ -508,6 +541,8 @@ def main():
cache_subnet_group = module.params["cache_subnet_group"]
cache_security_groups = module.params["cache_security_groups"]
security_group_ids = module.params["security_group_ids"]
tags = module.params.get("tags")
purge_tags = module.params.get("purge_tags")
zone = module.params["zone"]
wait = module.params["wait"]
hard_modify = module.params["hard_modify"]
Expand All @@ -531,6 +566,8 @@ def main():
cache_subnet_group,
cache_security_groups,
security_group_ids,
tags,
purge_tags,
zone,
wait,
hard_modify,
Expand Down