Debian Codegraph @

ansible_collections/community/aws/plugins/modules/mq_user_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_user_info
10 version_added: 6.0.0
11 short_description: List users of an Amazon MQ broker
12 description:
13 - List users for the specified broker ID.
14 - Pending creations and deletions can be skipped by options.
15 author:
16 - FCO (@fotto)
17 options:
18 broker_id:
19 description:
20 - The ID of the MQ broker to work on.
21 type: str
22 required: true
23 max_results:
24 description:
25 - The maximum number of results to return.
26 type: int
27 default: 100
28 skip_pending_create:
29 description:
30 - Will skip pending creates from the result set.
31 type: bool
32 default: false
33 skip_pending_delete:
34 description:
35 - Will skip pending deletes from the result set.
36 type: bool
37 default: false
38 as_dict:
39 description:
40 - Convert result into lookup table by username.
41 type: bool
42 default: false
43
44 extends_documentation_fragment:
45 - amazon.aws.boto3
46 - amazon.aws.common.modules
47 - amazon.aws.region.modules
48 """
49
50
51 EXAMPLES = r"""
52 - name: get all users as list - relying on environment for API credentials
53 community.aws.mq_user_info:
54 broker_id: "aws-mq-broker-id"
55 max_results: 50
56 register: result
57
58 - name: get users as dict - explicitly specifying all credentials
59 community.aws.mq_user_info:
60 broker_id: "aws-mq-broker-id"
61 register: result
62
63 - name: get list of users to decide which may need to be deleted
64 community.aws.mq_user_info:
65 broker_id: "aws-mq-broker-id"
66 skip_pending_delete: true
67
68 - name: get list of users to decide which may need to be created
69 community.aws.mq_user_info:
70 broker_id: "aws-mq-broker-id"
71 skip_pending_create: true
72 """
73
74 RETURN = r"""
75 users:
76 type: dict
77 returned: success
78 description:
79 - dict key is username
80 - each entry is the record for a user as returned by API but converted to snake yaml
81 """
82
83 try:
84 import botocore
85 except ImportError:
86 # handled by AnsibleAWSModule
87 pass
88
89 from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
90
91 from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
92
93 DEFAULTS = {"max_results": 100, "skip_pending_create": False, "skip_pending_delete": False, "as_dict": True}
94
95
96 def get_user_info(conn, module):
97 try:
98 response = conn.list_users(BrokerId=module.params["broker_id"], MaxResults=module.params["max_results"])
99 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
100 if module.check_mode:
101 # return empty set for unknown broker in check mode
102 if DEFAULTS["as_dict"]:
103 return {}
104 return []
105 module.fail_json_aws(e, msg="Failed to describe users")
106 #
107 if not module.params["skip_pending_create"] and not module.params["skip_pending_delete"]:
108 # we can simply return the sub-object from the response
109 records = response["Users"]
110 else:
111 records = []
112 for record in response["Users"]:
113 if "PendingChange" in record:
114 if record["PendingChange"] == "CREATE" and module.params["skip_pending_create"]:
115 continue
116 if record["PendingChange"] == "DELETE" and module.params["skip_pending_delete"]:
117 continue
118 #
119 records.append(record)
120 #
121 if DEFAULTS["as_dict"]:
122 user_records = {}
123 for record in records:
124 user_records[record["Username"]] = record
125 #
126 return camel_dict_to_snake_dict(user_records, ignore_list=["Tags"])
127
128 return camel_dict_to_snake_dict(records, ignore_list=["Tags"])
129
130
131 def main():
132 argument_spec = dict(
133 broker_id=dict(required=True, type="str"),
134 max_results=dict(required=False, type="int", default=DEFAULTS["max_results"]),
135 skip_pending_create=dict(required=False, type="bool", default=DEFAULTS["skip_pending_create"]),
136 skip_pending_delete=dict(required=False, type="bool", default=DEFAULTS["skip_pending_delete"]),
137 as_dict=dict(required=False, type="bool", default=False),
138 )
139
140 module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True)
141
142 connection = module.client("mq")
143
144 try:
145 user_records = get_user_info(connection, module)
146 except botocore.exceptions.ClientError as e:
147 module.fail_json_aws(e)
148
149 module.exit_json(users=user_records)
150
151
152 if __name__ == "__main__":
153 main()