Skip to content

Commit

Permalink
sns_topic - Add tags and purge_tags options (ansible-collections#972)
Browse files Browse the repository at this point in the history
sns_topic - Add tags and purge_tags options

SUMMARY

sns_topic - Add tags and purge_tags options
Closes ansible-collections#964

ISSUE TYPE


Feature Pull Request

COMPONENT NAME

sns_topic

Reviewed-by: Mark Woolley <[email protected]>
Reviewed-by: Mark Chappell <None>
Reviewed-by: Alina Buzachis <None>
  • Loading branch information
alinabuzachis authored and abikouo committed Sep 18, 2023
1 parent 46d2826 commit 13cde6a
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions sns_topic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
short_description: Manages AWS SNS topics and subscriptions
version_added: 1.0.0
description:
- The M(community.aws.sns_topic) module allows you to create, delete, and manage subscriptions for AWS SNS topics.
- As of 2.6, this module can be use to subscribe and unsubscribe to topics outside of your AWS account.
- The M(community.aws.sns_topic) module allows you to create, delete, and manage subscriptions for AWS SNS topics.
author:
- "Joel Thompson (@joelthompson)"
- "Fernando Jose Pando (@nand0p)"
Expand Down Expand Up @@ -149,10 +148,13 @@
Blame Amazon."
default: true
type: bool
notes:
- Support for I(tags) and I(purge_tags) was added in release 5.3.0.
extends_documentation_fragment:
- amazon.aws.aws
- amazon.aws.ec2
- amazon.aws.boto3
- amazon.aws.common.modules
- amazon.aws.region.modules
- amazon.aws.tags.modules
- amazon.aws.boto3
'''

EXAMPLES = r"""
Expand Down Expand Up @@ -328,12 +330,14 @@
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.core import scrub_none_parameters
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import compare_policies
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ansible_dict_to_boto3_tag_list
from ansible_collections.community.aws.plugins.module_utils.sns import list_topics
from ansible_collections.community.aws.plugins.module_utils.sns import topic_arn_lookup
from ansible_collections.community.aws.plugins.module_utils.sns import compare_delivery_policies
from ansible_collections.community.aws.plugins.module_utils.sns import list_topic_subscriptions
from ansible_collections.community.aws.plugins.module_utils.sns import canonicalize_endpoint
from ansible_collections.community.aws.plugins.module_utils.sns import get_info
from ansible_collections.community.aws.plugins.module_utils.sns import update_tags


class SnsTopicManager(object):
Expand All @@ -349,6 +353,8 @@ def __init__(self,
delivery_policy,
subscriptions,
purge_subscriptions,
tags,
purge_tags,
check_mode):

self.connection = module.client('sns')
Expand All @@ -371,6 +377,8 @@ def __init__(self,
self.topic_deleted = False
self.topic_arn = None
self.attributes_set = []
self.tags = tags
self.purge_tags = purge_tags

def _create_topic(self):
attributes = {}
Expand All @@ -383,6 +391,9 @@ def _create_topic(self):
if not self.name.endswith('.fifo'):
self.name = self.name + '.fifo'

if self.tags:
tags = ansible_dict_to_boto3_tag_list(self.tags)

if not self.check_mode:
try:
response = self.connection.create_topic(Name=self.name,
Expand Down Expand Up @@ -542,12 +553,13 @@ def ensure_ok(self):
elif self.display_name or self.policy or self.delivery_policy:
self.module.fail_json(msg="Cannot set display name, policy or delivery policy for SNS topics not owned by this account")
changed |= self._set_topic_subs()

self._init_desired_subscription_attributes()
if self.topic_arn in list_topics(self.connection, self.module):
changed |= self._set_topic_subs_attributes()
elif any(self.desired_subscription_attributes.values()):
self.module.fail_json(msg="Cannot set subscription attributes for SNS topics not owned by this account")
# Check tagging
changed |= update_tags(self.connection, self.module, self.topic_arn)

return changed

Expand Down Expand Up @@ -600,6 +612,8 @@ def main():
delivery_policy=dict(type='dict', options=delivery_args),
subscriptions=dict(default=[], type='list', elements='dict'),
purge_subscriptions=dict(type='bool', default=True),
tags=dict(type='dict', aliases=['resource_tags']),
purge_tags=dict(type='bool', default=True),
)

module = AnsibleAWSModule(argument_spec=argument_spec,
Expand All @@ -614,6 +628,8 @@ def main():
subscriptions = module.params.get('subscriptions')
purge_subscriptions = module.params.get('purge_subscriptions')
check_mode = module.check_mode
tags = module.params.get('tags')
purge_tags = module.params.get('purge_tags')

sns_topic = SnsTopicManager(module,
name,
Expand All @@ -624,6 +640,8 @@ def main():
delivery_policy,
subscriptions,
purge_subscriptions,
tags,
purge_tags,
check_mode)

if state == 'present':
Expand Down

0 comments on commit 13cde6a

Please sign in to comment.