Debian Codegraph @

ansible_collections/community/aws/plugins/modules/mq_broker_info.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: Contributors to the Ansible project
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 DOCUMENTATION = r"""
8 ---
9 module: mq_broker_info
10 version_added: 6.0.0
11 short_description: Retrieve MQ Broker details
12 description:
13 - Get details about a broker.
14 author:
15 - FCO (@fotto)
16 options:
17 broker_id:
18 description: Get details for broker with specified ID.
19 type: str
20 broker_name:
21 description:
22 - Get details for broker with specified Name.
23 - Is ignored if I(broker_id) is specified.
24 type: str
25 extends_documentation_fragment:
26 - amazon.aws.boto3
27 - amazon.aws.common.modules
28 - amazon.aws.region.modules
29 """
30
31
32 EXAMPLES = r"""
33 - name: get current broker settings by id
34 community.aws.mq_broker_info:
35 broker_id: "aws-mq-broker-id"
36 register: broker_info
37
38 - name: get current broker settings by name setting all credential parameters explicitly
39 community.aws.mq_broker_info:
40 broker_name: "aws-mq-broker-name"
41 register: broker_info
42 """
43
44 RETURN = r"""
45 broker:
46 description: API response of describe_broker() converted to snake yaml.
47 type: dict
48 returned: success
49 """
50
51 try:
52 import botocore
53 except ImportError:
54 # handled by AnsibleAWSModule
55 pass
56
57 from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
58
59 from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule
60
61
62 def get_broker_id(conn, module):
63 try:
64 broker_name = module.params["broker_name"]
65 broker_id = None
66 response = conn.list_brokers(MaxResults=100)
67 for broker in response["BrokerSummaries"]:
68 if broker["BrokerName"] == broker_name:
69 broker_id = broker["BrokerId"]
70 break
71 return broker_id
72 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
73 module.fail_json_aws(e, msg="Couldn't list broker brokers.")
74
75
76 def get_broker_info(conn, module, broker_id):
77 try:
78 return conn.describe_broker(BrokerId=broker_id)
79 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
80 if module.check_mode:
81 module.exit_json(broker={"broker_id": broker_id, "broker_name": "fakeName"})
82 else:
83 module.fail_json_aws(e, msg="Couldn't get broker details.")
84
85
86 def main():
87 argument_spec = dict(broker_id=dict(type="str"), broker_name=dict(type="str"))
88 required_one_of = (
89 (
90 "broker_name",
91 "broker_id",
92 ),
93 )
94
95 module = AnsibleAWSModule(
96 argument_spec=argument_spec,
97 required_one_of=required_one_of,
98 supports_check_mode=True,
99 )
100 broker_id = module.params["broker_id"]
101 broker_name = module.params["broker_name"]
102
103 connection = module.client("mq")
104
105 try:
106 if not broker_id:
107 broker_id = get_broker_id(connection, module)
108 if not broker_id:
109 if module.check_mode:
110 module.exit_json(
111 broker={"broker_id": "fakeId", "broker_name": broker_name if broker_name else "fakeName"}
112 )
113 result = get_broker_info(connection, module, broker_id)
114 except botocore.exceptions.ClientError as e:
115 module.fail_json_aws(e)
116 #
117 module.exit_json(broker=camel_dict_to_snake_dict(result, ignore_list=["Tags"]))
118
119
120 if __name__ == "__main__":
121 main()