Skip to content

Commit 6bf97cb

Browse files
authored
Add machine_user_info module (#224)
Signed-off-by: rsuplina <rsuplina@cloudera.com>
1 parent 56072ed commit 6bf97cb

File tree

4 files changed

+550
-0
lines changed

4 files changed

+550
-0
lines changed

plugins/module_utils/cdp_iam.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ def get_user(self, user_id: Optional[str] = None) -> Dict[str, Any]:
342342
response = self.api_client.post(
343343
"/api/v1/iam/getUser",
344344
json_data=json_data,
345+
squelch={404: {}},
345346
)
346347

347348
return response.get("user", {})
@@ -1016,3 +1017,39 @@ def unassign_group_resource_role(
10161017
"/api/v1/iam/unassignGroupResourceRole",
10171018
json_data=json_data,
10181019
)
1020+
1021+
@CdpClient.paginated()
1022+
def list_machine_users(
1023+
self,
1024+
machine_user_names: Optional[List[str]] = None,
1025+
pageToken: Optional[str] = None,
1026+
pageSize: Optional[int] = None,
1027+
) -> Dict[str, Any]:
1028+
"""
1029+
List IAM machine users with automatic pagination.
1030+
1031+
Args:
1032+
machine_user_names: Optional list of machine user names or CRNs to filter by
1033+
pageToken: Token for pagination (automatically handled by decorator)
1034+
pageSize: Page size for pagination (automatically handled by decorator)
1035+
1036+
Returns:
1037+
Response with automatic pagination handling containing machineUsers list
1038+
"""
1039+
json_data: Dict[str, Any] = {}
1040+
1041+
# Add machine user names filter if provided
1042+
if machine_user_names is not None:
1043+
json_data["machineUserNames"] = machine_user_names
1044+
1045+
# Add pagination parameters if provided
1046+
if pageToken is not None:
1047+
json_data["startingToken"] = pageToken
1048+
if pageSize is not None:
1049+
json_data["pageSize"] = pageSize
1050+
1051+
return self.api_client.post(
1052+
"/api/v1/iam/listMachineUsers",
1053+
json_data=json_data,
1054+
squelch={404: {}},
1055+
)
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
# Copyright 2025 Cloudera, Inc. All Rights Reserved.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
DOCUMENTATION = r"""
19+
module: iam_machine_user_info
20+
short_description: Gather information about CDP Public IAM machine users
21+
description:
22+
- Gather information about CDP Public IAM machine user or machine users
23+
author:
24+
- "Ronald Suplina (@rsuplina)"
25+
version_added: "3.2.0"
26+
options:
27+
name:
28+
description:
29+
- A list of machine user names or CRNs or a single machine user name/CRN.
30+
- If no machine user name or CRN is provided, all machine users are returned.
31+
type: list
32+
elements: str
33+
required: False
34+
aliases:
35+
- machine_user_name
36+
extends_documentation_fragment:
37+
- cloudera.cloud.cdp_client
38+
"""
39+
40+
EXAMPLES = r"""
41+
# Note: These examples do not set authentication details.
42+
43+
- name: Gather information about all Machine Users
44+
cloudera.cloud.iam_machine_user_info:
45+
46+
- name: Gather information about a named Machine User
47+
cloudera.cloud.iam_machine_user_info:
48+
name: example-machine-user-01
49+
50+
- name: Gather information about several named Machine Users
51+
cloudera.cloud.iam_machine_user_info:
52+
name:
53+
- example-machine-user-01
54+
- example-machine-user-02
55+
- example-machine-user-03
56+
"""
57+
58+
RETURN = r"""
59+
machine_users:
60+
description:
61+
- Returns a list of machine user records.
62+
- Each record represents a CDP IAM machine user and its details.
63+
type: list
64+
returned: always
65+
elements: dict
66+
contains:
67+
creation_date:
68+
description: The date when this machine user record was created.
69+
returned: on success
70+
type: str
71+
sample: 2020-07-06T12:24:05.531000+00:00
72+
crn:
73+
description: The CRN of the machine user.
74+
returned: on success
75+
type: str
76+
machine_user_name:
77+
description: The machine user name.
78+
returned: on success
79+
type: str
80+
sample: example-machine-user-01
81+
status:
82+
description: The current status of the machine user (ACTIVE or CONTROL_PLANE_LOCKED_OUT).
83+
returned: when supported
84+
type: str
85+
sample: ACTIVE
86+
workload_password_details:
87+
description: Information about the workload password for the machine user.
88+
returned: when supported
89+
type: dict
90+
workload_username:
91+
description: The username used in all the workload clusters of the machine user.
92+
returned: when supported
93+
type: str
94+
sdk_out:
95+
description: Returns the captured API HTTP log.
96+
returned: when supported
97+
type: str
98+
sdk_out_lines:
99+
description: Returns a list of each line of the captured API HTTP log.
100+
returned: when supported
101+
type: list
102+
elements: str
103+
"""
104+
105+
from typing import Any
106+
107+
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
108+
109+
from ansible_collections.cloudera.cloud.plugins.module_utils.common import (
110+
ServicesModule,
111+
)
112+
from ansible_collections.cloudera.cloud.plugins.module_utils.cdp_iam import (
113+
CdpIamClient,
114+
)
115+
116+
117+
class IAMMachineUserInfo(ServicesModule):
118+
def __init__(self):
119+
super().__init__(
120+
argument_spec=dict(
121+
name=dict(
122+
required=False,
123+
type="list",
124+
elements="str",
125+
aliases=["machine_user_name"],
126+
),
127+
),
128+
supports_check_mode=True,
129+
)
130+
131+
# Set parameters
132+
self.name = self.get_param("name")
133+
134+
# Initialize the return values
135+
self.machine_users = []
136+
137+
def process(self):
138+
client = CdpIamClient(api_client=self.api_client)
139+
result = client.list_machine_users(machine_user_names=self.name)
140+
self.machine_users = [
141+
camel_dict_to_snake_dict(machine_user)
142+
for machine_user in result.get("machineUsers", [])
143+
]
144+
145+
146+
def main():
147+
result = IAMMachineUserInfo()
148+
149+
output: dict[str, Any] = dict(
150+
changed=False,
151+
machine_users=result.machine_users,
152+
)
153+
154+
if result.debug_log:
155+
output.update(
156+
sdk_out=result.log_out,
157+
sdk_out_lines=result.log_lines,
158+
)
159+
160+
result.module.exit_json(**output)
161+
162+
163+
if __name__ == "__main__":
164+
main()

0 commit comments

Comments
 (0)