Skip to content

Commit 2083e18

Browse files
authored
Update iam_group_info module (#217)
Signed-off-by: rsuplina <rsuplina@cloudera.com>
1 parent 8073015 commit 2083e18

File tree

3 files changed

+273
-68
lines changed

3 files changed

+273
-68
lines changed

plugins/module_utils/cdp_iam.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Copyright 2025 Cloudera, Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""
18+
A REST client for the Cloudera on Cloud Platform (CDP) IAM API
19+
"""
20+
21+
from typing import Any, Dict, List, Optional
22+
23+
from ansible_collections.cloudera.cloud.plugins.module_utils.cdp_client import (
24+
RestClient,
25+
CdpClient,
26+
)
27+
28+
29+
class CdpIamClient(CdpClient):
30+
"""CDP IAM API client."""
31+
32+
def __init__(self, api_client: RestClient):
33+
"""
34+
Initialize CDP IAM client.
35+
36+
Args:
37+
api_client: RestClient instance for managing HTTP method calls
38+
"""
39+
super().__init__(api_client=api_client)
40+
41+
@RestClient.paginated()
42+
def list_groups(
43+
self,
44+
group_names: Optional[List[str]] = None,
45+
pageToken: Optional[str] = None,
46+
pageSize: Optional[int] = None,
47+
) -> Dict[str, Any]:
48+
"""
49+
List IAM groups with automatic pagination.
50+
51+
Args:
52+
group_names: Optional list of group names or CRNs to filter by
53+
pageToken: Token for pagination (automatically handled by decorator)
54+
pageSize: Page size for pagination (automatically handled by decorator)
55+
56+
Returns:
57+
Response with automatic pagination handling containing groups list
58+
"""
59+
json_data: Dict[str, Any] = {}
60+
61+
# Add group names filter if provided
62+
if group_names is not None:
63+
json_data["groupNames"] = group_names
64+
65+
# Add pagination parameters if provided
66+
if pageToken is not None:
67+
json_data["startingToken"] = pageToken
68+
if pageSize is not None:
69+
json_data["pageSize"] = pageSize
70+
71+
return self.post(
72+
"/api/v1/iam/listGroups",
73+
json_data=json_data,
74+
)

plugins/modules/iam_group_info.py

Lines changed: 67 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
author:
2424
- "Webster Mudge (@wmudge)"
2525
- "Dan Chaffelson (@chaffelson)"
26+
- "Ronald Suplina (@rsuplina)"
2627
version_added: "1.0.0"
2728
options:
2829
name:
@@ -36,21 +37,26 @@
3637
aliases:
3738
- group_name
3839
extends_documentation_fragment:
39-
- cloudera.cloud.cdp_sdk_options
40-
- cloudera.cloud.cdp_auth_options
40+
- cloudera.cloud.cdp_client
4141
"""
4242

4343
EXAMPLES = r"""
4444
# Note: These examples do not set authentication details.
4545
46-
# Gather information about all Groups
47-
- cloudera.cloud.iam_group_info:
46+
- name: Gather information about all Groups
47+
cloudera.cloud.iam_group_info:
4848
49-
# Gather information about a named Group
50-
- cloudera.cloud.iam_group_info:
51-
name: example-01
49+
- name: Gather information about a named Group
50+
cloudera.cloud.iam_group_info:
51+
name: example-01
52+
53+
- name: Gather information about several named Groups
54+
cloudera.cloud.iam_group_info:
55+
name:
56+
- example-01
57+
- example-02
58+
- example-03
5259
53-
# Gather information about several named Groups
5460
- cloudera.cloud.iam_group_info:
5561
name:
5662
- example-01
@@ -60,12 +66,15 @@
6066

6167
RETURN = r"""
6268
groups:
63-
description: The information about the named Group or Groups
69+
description:
70+
- Returns a list of group records.
71+
- Each record represents a CDP IAM group and its details.
6472
type: list
6573
returned: always
6674
elements: dict
6775
contains:
6876
creationDate:
77+
# creation_date:
6978
description: The date when this group record was created.
7079
returned: on success
7180
type: str
@@ -75,96 +84,86 @@
7584
returned: on success
7685
type: str
7786
groupName:
87+
# group_name:
7888
description: The group name.
7989
returned: on success
8090
type: str
8191
sample: example-01
82-
users:
83-
description: List of User CRNs which are members of the group.
84-
returned: on success
85-
type: list
86-
elements: str
87-
roles:
88-
description: List of Role CRNs assigned to the group.
89-
returned: on success
90-
type: list
91-
elements: str
92-
resource_roles:
93-
description: List of Resource-to-Role assignments, by CRN, that are associated with the group.
94-
returned: on success
95-
type: list
96-
elements: dict
97-
contains:
98-
resourceCrn:
99-
description: The CRN of the resource granted the rights of the role.
100-
returned: on success
101-
type: str
102-
resourceRoleCrn:
103-
description: The CRN of the CDP Role.
104-
returned: on success
105-
type: str
10692
syncMembershipOnUserLogin:
107-
description: Flag indicating whether group membership is synced when a user logs in. The default is to sync group
108-
membership.
93+
# sync_membership_on_user_login:
94+
description: Flag indicating whether group membership is synced when a user logs in. The default is to sync group membership.
10995
returned: when supported
11096
type: bool
11197
sdk_out:
112-
description: Returns the captured CDP SDK log.
98+
description: Returns the captured API HTTP log.
11399
returned: when supported
114100
type: str
115101
sdk_out_lines:
116-
description: Returns a list of each line of the captured CDP SDK log.
102+
description: Returns a list of each line of the captured API HTTP log.
117103
returned: when supported
118104
type: list
119105
elements: str
120106
"""
121107

122-
from ansible.module_utils.basic import AnsibleModule
123-
from ansible_collections.cloudera.cloud.plugins.module_utils.cdp_common import CdpModule
108+
from typing import Any
124109

110+
# from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
125111

126-
class IAMGroupInfo(CdpModule):
127-
def __init__(self, module):
128-
super(IAMGroupInfo, self).__init__(module)
112+
from ansible_collections.cloudera.cloud.plugins.module_utils.common import (
113+
ServicesModule,
114+
)
115+
from ansible_collections.cloudera.cloud.plugins.module_utils.cdp_iam import (
116+
CdpIamClient,
117+
)
129118

130-
# Set variables
131-
self.name = self._get_param("name")
132119

133-
# Initialize the return values
134-
self.info = []
120+
class IAMGroupInfo(ServicesModule):
121+
def __init__(self):
122+
super().__init__(
123+
argument_spec=dict(
124+
name=dict(
125+
required=False,
126+
type="list",
127+
elements="str",
128+
aliases=["group_name"],
129+
),
130+
),
131+
supports_check_mode=True,
132+
)
135133

136-
# Execute logic process
137-
self.process()
134+
# Set parameters
135+
self.name = self.get_param("name")
136+
137+
# Initialize the return values
138+
self.groups = []
138139

139-
@CdpModule._Decorators.process_debug
140140
def process(self):
141-
self.info = self.cdpy.iam.gather_groups(self.name)
141+
client = CdpIamClient(api_client=self.api_client)
142+
result = client.list_groups(group_names=self.name)
143+
self.groups = result.get("groups", [])
142144

143145

144-
def main():
145-
module = AnsibleModule(
146-
argument_spec=CdpModule.argument_spec(
147-
name=dict(
148-
required=False,
149-
type="list",
150-
elements="str",
151-
aliases=["group_name"],
152-
),
153-
),
154-
supports_check_mode=True,
155-
)
146+
# NOTE: Snake_case conversion deferred until 4.0 to maintain backward compatibility.
147+
# self.groups = [
148+
# camel_dict_to_snake_dict(group) for group in result.get("groups", [])
149+
# ]
150+
156151

157-
result = IAMGroupInfo(module)
152+
def main():
153+
result = IAMGroupInfo()
158154

159-
output = dict(
155+
output: dict[str, Any] = dict(
160156
changed=False,
161-
groups=result.info,
157+
groups=result.groups,
162158
)
163159

164-
if result.debug:
165-
output.update(sdk_out=result.log_out, sdk_out_lines=result.log_lines)
160+
if result.debug_log:
161+
output.update(
162+
sdk_out=result.log_out,
163+
sdk_out_lines=result.log_lines,
164+
)
166165

167-
module.exit_json(**output)
166+
result.module.exit_json(**output)
168167

169168

170169
if __name__ == "__main__":

0 commit comments

Comments
 (0)