Repository: ansible-collections/community.aws

Issue:
community.aws.sns_topic_info module missing
##### SUMMARY

community.aws.sns_topic_info module missing

##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
community.aws.sns_topic exists, however the equivelent _info module is not yet developed.

The _info module would be useful to detect the current state of sns_topics.

##### ANSIBLE VERSION

```
$ ansible --version
ansible 2.10.8
  config file = /home/nobody/src/mobile-ansible-core/ansible.cfg
  configured module search path = ['/home/nobody/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/nobody/.cache/venv/mobile-ansible-core-3.8/lib/python3.8/site-packages/ansible
  executable location = /home/nobody/.cache/venv/mobile-ansible-core-3.8/bin/ansible
  python version = 3.8.2 (default, Jan  1 1970, 00:00:01) [GCC 7.5.0]
```
and using

```
- name: community.aws
  version: 1.5.0
```



Discussion:
Files identified in the description:
* [`plugins/modules/sns_topic.py`](https://github.com/['ansible-collections/amazon.aws', 'ansible-collections/community.aws', 'ansible-collections/community.vmware']/blob/main/plugins/modules/sns_topic.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
@divansantana: Greetings! Thanks for taking the time to open this issue. In order for the community to handle your issue effectively, we need a bit more information.

Here are the items we could not find in your description:
- ansible version

Please set the description of this issue with this template:
https://raw.githubusercontent.com/ansible/ansible/devel/.github/ISSUE_TEMPLATE/bug_report.md

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: issue_missing_data --->
cc @jillr @joelthompson @nand0p @s-hertel @tremble @wimnat
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
cc @markuman
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/plugins/modules/sns_topic_info.py b/plugins/modules/sns_topic_info.py
new file mode 100644
--- /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()


Test patch:
diff --git a/tests/integration/targets/sns_topic/aliases b/tests/integration/targets/sns_topic/aliases
--- 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
--- 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 }}'