From 58b3c687171646d7322a93042cde8d73d36c05db Mon Sep 17 00:00:00 2001 From: Alina Buzachis Date: Mon, 28 Mar 2022 12:01:37 +0200 Subject: [PATCH] sns_topic_info new module (#673) sns_topic_info new module SUMMARY sns_topic_info - new module allowing to get all AWS SNS topics or properties of a specific AWS SNS topic. Fixes #601 Requires #879 ISSUE TYPE New Module Pull Request COMPONENT NAME sns_topic_info Reviewed-by: Joseph Torcasso Reviewed-by: Alina Buzachis Reviewed-by: Mark Woolley Reviewed-by: Markus Bergholz --- meta/runtime.yml | 1 + plugins/modules/sns_topic_info.py | 167 ++++++++++++++++++ tests/integration/targets/sns_topic/aliases | 2 + .../targets/sns_topic/tasks/main.yml | 57 +++++- 4 files changed, 225 insertions(+), 2 deletions(-) create mode 100644 plugins/modules/sns_topic_info.py diff --git a/meta/runtime.yml b/meta/runtime.yml index 66a50a2da56..93cbf3e92fe 100644 --- a/meta/runtime.yml +++ b/meta/runtime.yml @@ -176,6 +176,7 @@ action_groups: - s3_website - sns - sns_topic + - sns_topic_info - sqs_queue - sts_assume_role - sts_session_token diff --git a/plugins/modules/sns_topic_info.py b/plugins/modules/sns_topic_info.py new file mode 100644 index 00000000000..380d712820b --- /dev/null +++ b/plugins/modules/sns_topic_info.py @@ -0,0 +1,167 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# Copyright: Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + + +DOCUMENTATION = r''' +module: sns_topic_info +short_description: sns_topic_info module +version_added: 3.2.0 +description: +- The M(community.aws.sns_topic_info) module allows to get all AWS SNS topics or properties of a specific AWS SNS topic. +author: +- "Alina Buzachis (@alinabuzachis)" +options: + topic_arn: + description: The ARN of the AWS SNS topic for which you wish to find subscriptions or list attributes. + required: false + type: str +extends_documentation_fragment: +- amazon.aws.aws +- amazon.aws.ec2 +''' + +EXAMPLES = r''' +- name: list all the topics + community.aws.sns_topic_info: + register: sns_topic_list + +- name: get info on specific topic + community.aws.sns_topic_info: + topic_arn: "{{ sns_arn }}" + register: sns_topic_info +''' + +RETURN = r''' +result: + description: + - The result contaning the details of one or all AWS SNS topics. + returned: success + type: list + contains: + sns_arn: + description: The ARN of the topic. + type: str + returned: always + sample: "arn:aws:sns:us-east-2:111111111111:my_topic_name" + sns_topic: + description: Dict of sns topic details. + type: complex + returned: always + contains: + delivery_policy: + description: Delivery policy for the SNS topic. + returned: when topic is owned by this AWS account + type: str + sample: > + {"http":{"defaultHealthyRetryPolicy":{"minDelayTarget":20,"maxDelayTarget":20,"numRetries":3,"numMaxDelayRetries":0, + "numNoDelayRetries":0,"numMinDelayRetries":0,"backoffFunction":"linear"},"disableSubscriptionOverrides":false}} + display_name: + description: Display name for SNS topic. + returned: when topic is owned by this AWS account + type: str + sample: My topic name + owner: + description: AWS account that owns the topic. + returned: when topic is owned by this AWS account + type: str + sample: '111111111111' + policy: + description: Policy for the SNS topic. + returned: when topic is owned by this AWS account + type: str + sample: > + {"Version":"2012-10-17","Id":"SomePolicyId","Statement":[{"Sid":"ANewSid","Effect":"Allow","Principal":{"AWS":"arn:aws:iam::111111111111:root"}, + "Action":"sns:Subscribe","Resource":"arn:aws:sns:us-east-2:111111111111:ansible-test-dummy-topic","Condition":{"StringEquals":{"sns:Protocol":"email"}}}]} + subscriptions: + description: List of subscribers to the topic in this AWS account. + returned: always + type: list + sample: [] + subscriptions_added: + description: List of subscribers added in this run. + returned: always + type: list + sample: [] + subscriptions_confirmed: + description: Count of confirmed subscriptions. + returned: when topic is owned by this AWS account + type: str + sample: '0' + subscriptions_deleted: + description: Count of deleted subscriptions. + returned: when topic is owned by this AWS account + type: str + sample: '0' + subscriptions_existing: + description: List of existing subscriptions. + returned: always + type: list + sample: [] + subscriptions_new: + description: List of new subscriptions. + returned: always + type: list + sample: [] + subscriptions_pending: + description: Count of pending subscriptions. + returned: when topic is owned by this AWS account + type: str + sample: '0' + subscriptions_purge: + description: Whether or not purge_subscriptions was set. + returned: always + type: bool + sample: true + topic_arn: + description: ARN of the SNS topic (equivalent to sns_arn). + returned: when topic is owned by this AWS account + type: str + sample: arn:aws:sns:us-east-2:111111111111:ansible-test-dummy-topic + topic_type: + description: The type of topic. + type: str + sample: "standard" +''' + + +try: + import botocore +except ImportError: + pass # handled by AnsibleAWSModule + +from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule +from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry +from ansible_collections.community.aws.plugins.module_utils.sns import list_topics +from ansible_collections.community.aws.plugins.module_utils.sns import get_info + + +def main(): + argument_spec = dict( + topic_arn=dict(type='str', required=False), + ) + + module = AnsibleAWSModule(argument_spec=argument_spec, + supports_check_mode=True) + + topic_arn = module.params.get('topic_arn') + + try: + connection = module.client('sns', retry_decorator=AWSRetry.jittered_backoff()) + except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: + module.fail_json_aws(e, msg='Failed to connect to AWS.') + + if topic_arn: + results = dict(sns_arn=topic_arn, sns_topic=get_info(connection, module, topic_arn)) + else: + results = list_topics(connection, module) + + module.exit_json(result=results) + + +if __name__ == '__main__': + main() diff --git a/tests/integration/targets/sns_topic/aliases b/tests/integration/targets/sns_topic/aliases index 4ef4b2067d0..b1656d7e39c 100644 --- a/tests/integration/targets/sns_topic/aliases +++ b/tests/integration/targets/sns_topic/aliases @@ -1 +1,3 @@ cloud/aws + +sns_topic_info diff --git a/tests/integration/targets/sns_topic/tasks/main.yml b/tests/integration/targets/sns_topic/tasks/main.yml index ab245490cbd..b8c426a459e 100644 --- a/tests/integration/targets/sns_topic/tasks/main.yml +++ b/tests/integration/targets/sns_topic/tasks/main.yml @@ -4,8 +4,7 @@ aws_access_key: '{{ aws_access_key }}' security_token: '{{ security_token|default(omit) }}' region: '{{ aws_region }}' - collections: - - community.general + block: - name: create minimal lambda role (needed for subscription test further down) @@ -22,6 +21,25 @@ seconds: 10 when: iam_role is changed + - name: list all the topics (check_mode) + sns_topic_info: + check_mode: true + register: sns_topic_list + + - name: assert success + assert: + that: + - sns_topic_list is successful + + - name: list all the topics + sns_topic_info: + register: sns_topic_list + + - name: assert success + assert: + that: + - sns_topic_list is successful + - name: create standard SNS topic sns_topic: name: '{{ sns_topic_topic_name }}' @@ -37,6 +55,41 @@ set_fact: sns_arn: '{{ sns_topic_create.sns_arn }}' + - name: get info on specific topic (check_mode) + sns_topic_info: + topic_arn: "{{ sns_arn }}" + check_mode: true + register: sns_topic_info + + - name: assert success + assert: + that: + - sns_topic_info is successful + - "'result' in sns_topic_info" + - sns_topic_info.result["sns_arn"] == "{{ sns_arn }}" + - "'sns_topic' in sns_topic_info.result" + - "'display_name' in sns_topic_info.result['sns_topic']" + - sns_topic_info.result["sns_topic"]["display_name"] == "My topic name" + - "'owner' in sns_topic_info.result['sns_topic']" + - "'policy' in sns_topic_info.result['sns_topic']" + + - name: get info on specific topic + sns_topic_info: + topic_arn: "{{ sns_arn }}" + register: sns_topic_info + + - name: assert success + assert: + that: + - sns_topic_info is successful + - "'result' in sns_topic_info" + - sns_topic_info.result["sns_arn"] == "{{ sns_arn }}" + - "'sns_topic' in sns_topic_info.result" + - "'display_name' in sns_topic_info.result['sns_topic']" + - sns_topic_info.result["sns_topic"]["display_name"] == "My topic name" + - "'owner' in sns_topic_info.result['sns_topic']" + - "'policy' in sns_topic_info.result['sns_topic']" + - name: create topic again (expect changed=False) sns_topic: name: '{{ sns_topic_topic_name }}'