Skip to content

Commit

Permalink
sns: Add parameters for fifo topics (#1733)
Browse files Browse the repository at this point in the history
sns: Add parameters for fifo topics

SUMMARY
Implements #1718. Adding message_group_id and message_deduplication_id to support publishing of messages to fifo topics.
ISSUE TYPE

Feature Pull Request

COMPONENT NAME
sns

Reviewed-by: Mark Chappell
  • Loading branch information
Pascal-Architrave authored Mar 14, 2023
1 parent c822f55 commit b1b67a2
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/1733-sns-publish-to-fifo-topics.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- sns - Add support for ``message_group_id`` and ``message_deduplication_id`` (https://github.com/ansible-collections/community.aws/pull/1733).
47 changes: 44 additions & 3 deletions plugins/modules/sns.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,28 @@
message_structure:
description:
- The payload format to use for the message.
- This must be 'json' to support protocol-specific messages (C(http), C(https), C(email), C(sms), C(sqs)).
- It must be 'string' to support I(message_attributes).
- This must be C(json) to support protocol-specific messages (C(http), C(https), C(email), C(sms), C(sqs)).
- It must be C(string) to support I(message_attributes).
default: json
choices: ['json', 'string']
type: str
message_group_id:
description:
- A tag which is used to process messages that belong to the same group in a FIFO manner.
- Has to be included when publishing a message to a fifo topic.
- Can contain up to 128 alphanumeric characters and punctuation.
type: str
version_added: 5.4.0
message_deduplication_id:
description:
- Only in connection with the message_group_id.
- Overwrites the auto generated MessageDeduplicationId.
- Can contain up to 128 alphanumeric characters and punctuation.
- Messages with the same deduplication id getting recognized as the same message.
- Gets overwritten by an auto generated token, if the topic has ContentBasedDeduplication set.
type: str
version_added: 5.4.0
extends_documentation_fragment:
- amazon.aws.region.modules
- amazon.aws.common.modules
Expand Down Expand Up @@ -108,6 +125,14 @@
data_type: String
string_value: "green"
delegate_to: localhost
- name: Send message to a fifo topic
community.aws.sns:
topic: "deploy"
msg: "Message with message group id"
subject: Deploy complete!
message_group_id: "deploy-1"
delegate_to: localhost
"""

RETURN = r"""
Expand All @@ -121,6 +146,10 @@
returned: when success
type: str
sample: 2f681ef0-6d76-5c94-99b2-4ae3996ce57b
sequence_number:
description: A 128 bits long sequence number which gets assigned to the message in fifo topics
returned: when success
type: str
"""

import json
Expand Down Expand Up @@ -154,6 +183,8 @@ def main():
topic=dict(required=True),
message_attributes=dict(type='dict'),
message_structure=dict(choices=['json', 'string'], default='json'),
message_group_id=dict(),
message_deduplication_id=dict(),
)

for p in protocols:
Expand All @@ -172,6 +203,11 @@ def main():
module.fail_json(msg='message_attributes is only supported when the message_structure is "string".')
sns_kwargs['MessageAttributes'] = module.params['message_attributes']

if module.params["message_group_id"]:
sns_kwargs["MessageGroupId"] = module.params["message_group_id"]
if module.params["message_deduplication_id"]:
sns_kwargs["MessageDeduplicationId"] = module.params["message_deduplication_id"]

dict_msg = {
'default': sns_kwargs['Message']
}
Expand Down Expand Up @@ -202,7 +238,12 @@ def main():
except (BotoCoreError, ClientError) as e:
module.fail_json_aws(e, msg='Failed to publish message')

module.exit_json(msg='OK', message_id=result['MessageId'])
sns_result = dict(msg="OK", message_id=result["MessageId"])

if module.params["message_group_id"]:
sns_result["sequence_number"] = result["SequenceNumber"]

module.exit_json(**sns_result)


if __name__ == '__main__':
Expand Down
1 change: 1 addition & 0 deletions tests/integration/targets/sns/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
sns_topic_name: "{{ resource_prefix }}-topic"
sns_fifo_topic_name: "{{ resource_prefix }}-fifo-topic"
27 changes: 27 additions & 0 deletions tests/integration/targets/sns/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,36 @@
subject: Second test message
msg: Simple test message

- name: Create an FIFO SNS topic
sns_topic:
name: "{{ sns_fifo_topic_name }}"
display_name: "Test fifo topic"
topic_type: "fifo"
register: sns_fifo_topic

- name: Publish to the fifo topic
sns:
topic: "{{ sns_fifo_topic.sns_arn }}"
subject: Fifo test message
msg: Simple test message
message_group_id: group-id
message_deduplication_id: deduplication-id
register: result

- name: Check for expected result structure
assert:
that:
- result is not changed
- "'sequence_number' in result"
always:
- name: Remove topic
sns_topic:
name: "{{ sns_topic_name }}"
state: absent
ignore_errors: yes

- name: Remove fifo topic
sns_topic:
name: "{{ sns_fifo_topic_name }}"
state: absent
ignore_errors: yes

0 comments on commit b1b67a2

Please sign in to comment.