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

Allow specifying topic type for SNS module. #599

Merged
Merged
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
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