Skip to content

Commit

Permalink
Merge pull request ansible-collections#599 from mediafellows/sns_add_…
Browse files Browse the repository at this point in the history
…type_param

Allow specifying topic type for SNS module.

SUMMARY
Adding a topic_type param to SNS module (simillar to SQS).
ISSUE TYPE

Feature Pull Request

COMPONENT NAME
sns_topic module
ADDITIONAL INFORMATION
Simillar to SQS queues, SNS topics also allow specifying a type, that can either be 'Standard' or 'FIFO'. Furthermore if you have a FIFO SQS queue and want to subscribe that to an SNS topic, that one has to be FIFO too. Hence adding this flag is important and is actually just another option for the creat_topic action in Boto, see https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sns.html#SNS.Client.create_topic

Reviewed-by: Stefan Horning <None>
Reviewed-by: Alina Buzachis <None>
Reviewed-by: Mark Chappell <None>
  • Loading branch information
ansible-zuul[bot] committed Jul 12, 2021
2 parents 2463775 + f043f00 commit 9ab9c77
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 13 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/sns_topic_type.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- sns_topic - Added ``topic_type`` parameter to select type of SNS topic (either FIFO or Standard) (https://github.com/ansible-collections/community.aws/pull/599).
23 changes: 22 additions & 1 deletion plugins/modules/sns_topic.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
- The name or ARN of the SNS topic to manage.
required: true
type: str
topic_type:
description:
- The type of topic that should be created. Either Standard for FIFO (first-in, first-out)
choices: ['standard', 'fifo']
default: 'standard'
type: str
version_added: 2.0.0
state:
description:
- Whether to create or destroy an SNS topic.
Expand Down Expand Up @@ -228,6 +235,7 @@ class SnsTopicManager(object):
def __init__(self,
module,
name,
topic_type,
state,
display_name,
policy,
Expand All @@ -239,6 +247,7 @@ def __init__(self,
self.connection = module.client('sns')
self.module = module
self.name = name
self.topic_type = topic_type
self.state = state
self.display_name = display_name
self.policy = policy
Expand Down Expand Up @@ -285,9 +294,17 @@ def _topic_arn_lookup(self):
return topic

def _create_topic(self):
attributes = {'FifoTopic': 'false'}
tags = []

if self.topic_type == 'fifo':
attributes['FifoTopic'] = 'true'
if not self.name.endswith('.fifo'):
self.name = self.name + '.fifo'

if not self.check_mode:
try:
response = self.connection.create_topic(Name=self.name)
response = self.connection.create_topic(Name=self.name, Attributes=attributes, Tags=tags)
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
self.module.fail_json_aws(e, msg="Couldn't create topic %s" % self.name)
self.topic_arn = response['TopicArn']
Expand Down Expand Up @@ -456,6 +473,7 @@ def ensure_gone(self):
def get_info(self):
info = {
'name': self.name,
'topic_type': self.topic_type,
'state': self.state,
'subscriptions_new': self.subscriptions,
'subscriptions_existing': self.subscriptions_existing,
Expand All @@ -479,6 +497,7 @@ def get_info(self):
def main():
argument_spec = dict(
name=dict(required=True),
topic_type=dict(type='str', default='standard', choices=['standard', 'fifo']),
state=dict(default='present', choices=['present', 'absent']),
display_name=dict(),
policy=dict(type='dict'),
Expand All @@ -491,6 +510,7 @@ def main():
supports_check_mode=True)

name = module.params.get('name')
topic_type = module.params.get('topic_type')
state = module.params.get('state')
display_name = module.params.get('display_name')
policy = module.params.get('policy')
Expand All @@ -501,6 +521,7 @@ def main():

sns_topic = SnsTopicManager(module,
name,
topic_type,
state,
display_name,
policy,
Expand Down
3 changes: 0 additions & 3 deletions tests/integration/targets/sns_topic/aliases
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
# reason: missing-policy
unsupported

cloud/aws
12 changes: 7 additions & 5 deletions tests/integration/targets/sns_topic/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
sns_topic_topic_name: "{{ resource_prefix }}-topic"
# we hash the resource_prefix to get a shorter, unique string
unique_id: "{{ resource_prefix | hash('md5') }}"

sns_topic_topic_name: "ansible-test-{{ unique_id }}-topic"
sns_topic_subscriptions:
- endpoint: "{{ sns_topic_subscriber_arn }}"
protocol: "lambda"
sns_topic_third_party_topic_arn: "arn:aws:sns:us-east-1:806199016981:AmazonIpSpaceChanged"
sns_topic_third_party_region: "{{ sns_topic_third_party_topic_arn.split(':')[3] }}"

# additional test resource namings
sns_topic_lambda_function: "sns_topic_lambda"
sns_topic_lambda_name: "{{ resource_prefix }}-{{ sns_topic_lambda_function }}"
# IAM role names have to be less than 64 characters
# we hash the resource_prefix to get a shorter, unique string
unique_id: "{{ resource_prefix | hash('md5') }}"
sns_topic_lambda_name: "ansible-test-{{ unique_id }}-{{ sns_topic_lambda_function }}"
sns_topic_lambda_role: "ansible-test-{{ unique_id }}-sns-lambda"
Loading

0 comments on commit 9ab9c77

Please sign in to comment.