From b1b67a259fac94f2d48ea32cfd94488c99a48618 Mon Sep 17 00:00:00 2001 From: Pascal Walter <79926629+Pascal-Architrave@users.noreply.github.com> Date: Tue, 14 Mar 2023 13:06:39 +0100 Subject: [PATCH] sns: Add parameters for fifo topics (#1733) 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 --- .../1733-sns-publish-to-fifo-topics.yml | 2 + plugins/modules/sns.py | 47 +++++++++++++++++-- .../integration/targets/sns/defaults/main.yml | 1 + tests/integration/targets/sns/tasks/main.yml | 27 +++++++++++ 4 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/1733-sns-publish-to-fifo-topics.yml diff --git a/changelogs/fragments/1733-sns-publish-to-fifo-topics.yml b/changelogs/fragments/1733-sns-publish-to-fifo-topics.yml new file mode 100644 index 00000000000..d0b74749ec6 --- /dev/null +++ b/changelogs/fragments/1733-sns-publish-to-fifo-topics.yml @@ -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). diff --git a/plugins/modules/sns.py b/plugins/modules/sns.py index 6f4338954f8..96f5b72e70e 100644 --- a/plugins/modules/sns.py +++ b/plugins/modules/sns.py @@ -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 @@ -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""" @@ -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 @@ -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: @@ -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'] } @@ -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__': diff --git a/tests/integration/targets/sns/defaults/main.yml b/tests/integration/targets/sns/defaults/main.yml index 59ef656491d..f81d04e9deb 100644 --- a/tests/integration/targets/sns/defaults/main.yml +++ b/tests/integration/targets/sns/defaults/main.yml @@ -1 +1,2 @@ sns_topic_name: "{{ resource_prefix }}-topic" +sns_fifo_topic_name: "{{ resource_prefix }}-fifo-topic" diff --git a/tests/integration/targets/sns/tasks/main.yml b/tests/integration/targets/sns/tasks/main.yml index bc39b9ce7a4..42ef9b1904d 100644 --- a/tests/integration/targets/sns/tasks/main.yml +++ b/tests/integration/targets/sns/tasks/main.yml @@ -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