Debian Codegraph @

ansible_collections/dellemc/openmanage/plugins/modules/idrac_user_info.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 #
5 # Dell OpenManage Ansible Modules
6 # Version 9.3.0
7 # Copyright (C) 2022-2025 Dell Inc. or its subsidiaries. All Rights Reserved.
8
9 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
10 #
11
12
13 from __future__ import (absolute_import, division, print_function)
14 __metaclass__ = type
15
16 DOCUMENTATION = """
17 ---
18 module: idrac_user_info
19 short_description: Retrieve details of all users or a specific user on iDRAC.
20 version_added: "7.0.0"
21 description:
22 - "This module retrieves the list and basic details of all users or details of a specific user on
23 iDRAC"
24 extends_documentation_fragment:
25 - dellemc.openmanage.idrac_x_auth_options
26 options:
27 user_id:
28 description:
29 - Sequential user id numbers that supports from 1 to 16.
30 - I(user_id) is mutually exclusive with I(username)
31 type: int
32 username:
33 type: str
34 description:
35 - Username of the account that is created in iDRAC local users.
36 - I(username) is mutually exclusive with I(user_id)
37 requirements:
38 - "python >= 3.9.6"
39 author: "Husniya Hameed(@husniya_hameed)"
40 notes:
41 - Run this module on a system that has direct access to Dell iDRAC.
42 - This module supports C(check_mode).
43 """
44
45 EXAMPLES = """
46 ---
47 - name: Retrieve basic details of all user accounts.
48 dellemc.openmanage.idrac_user_info:
49 idrac_ip: 198.162.0.1
50 idrac_user: idrac_user
51 idrac_password: idrac_password
52 ca_path: "/path/to/ca_cert.pem"
53
54 - name: Retrieve user details using user_id
55 dellemc.openmanage.idrac_user_info:
56 idrac_ip: 198.162.0.1
57 idrac_user: idrac_user
58 idrac_password: idrac_password
59 ca_path: "/path/to/ca_cert.pem"
60 user_id: 1
61
62 - name: Retrieve user details using username
63 dellemc.openmanage.idrac_user_info:
64 idrac_ip: 198.162.0.1
65 idrac_user: idrac_user
66 idrac_password: idrac_password
67 ca_path: "/path/to/ca_cert.pem"
68 username: user_name
69 """
70
71 RETURN = r'''
72 ---
73 msg:
74 description: Status of user information retrieval.
75 returned: always
76 type: str
77 sample: "Successfully retrieved the user information."
78 user_info:
79 description: Information about the user.
80 returned: success
81 type: list
82 sample: [{
83 "Description": "User Account",
84 "Enabled": false,
85 "Id": "1",
86 "Locked": false,
87 "Name": "User Account",
88 "Password": null,
89 "RoleId": "None",
90 "UserName": ""
91 }]
92 error_info:
93 description: Details of the HTTP Error.
94 returned: on HTTP error
95 type: dict
96 sample: {
97 "error": {
98 "code": "Base.1.0.GeneralError",
99 "message": "A general error has occurred. See ExtendedInfo for more information.",
100 "@Message.ExtendedInfo": [
101 {
102 "MessageId": "GEN1234",
103 "RelatedProperties": [],
104 "Message": "Unable to process the request because an error occurred.",
105 "MessageArgs": [],
106 "Severity": "Critical",
107 "Resolution": "Retry the operation. If the issue persists, contact your system administrator."
108 }
109 ]
110 }
111 }
112 '''
113
114
115 import json
116 from ssl import SSLError
117 from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError
118 from ansible.module_utils.urls import ConnectionError, SSLValidationError
119 from ansible_collections.dellemc.openmanage.plugins.module_utils.idrac_redfish import iDRACRedfishAPI, IdracAnsibleModule
120 from ansible_collections.dellemc.openmanage.plugins.module_utils.utils import strip_substr_dict
121
122
123 ACCOUNT = "/redfish/v1"
124 SUCCESS_MSG = "Successfully retrieved the information of {0} user(s)."
125 UNSUCCESS_MSG = "Unable to retrieve the user information."
126 INVALID_USERID = "'user_id' is not valid."
127 INVALID_USERNAME = "'username' is not valid."
128 SUCCESSFUL_MSG = "Successfully retrieved the user information."
129
130
131 def get_accounts_uri(idrac):
132 try:
133 account_path = idrac.invoke_request(ACCOUNT, 'GET')
134 account_service = account_path.json_data.get("AccountService").get("@odata.id")
135 accounts = idrac.invoke_request(account_service, "GET")
136 accounts_uri = accounts.json_data.get("Accounts").get("@odata.id")
137 except HTTPError:
138 accounts_uri = "/redfish/v1/AccountService/Accounts"
139 return accounts_uri
140
141
142 def fetch_all_accounts(idrac, accounts_uri):
143 all_accounts = idrac.invoke_request("{0}?$expand=*($levels=1)".format(accounts_uri), 'GET')
144 all_accs = all_accounts.json_data.get("Members")
145 return all_accs
146
147
148 def get_user_id_accounts(idrac, module, accounts_uri, user_id):
149 acc_dets_json_data = {}
150 try:
151 acc_uri = accounts_uri + "/{0}".format(user_id)
152 acc_dets = idrac.invoke_request(acc_uri, "GET")
153 acc_dets_json_data = strip_substr_dict(acc_dets.json_data)
154 if acc_dets_json_data.get("Oem") is not None:
155 acc_dets_json_data["Oem"]["Dell"] = strip_substr_dict(acc_dets_json_data["Oem"]["Dell"])
156 acc_dets_json_data.pop("Links", None)
157 except HTTPError:
158 module.exit_json(msg=INVALID_USERID, failed=True)
159 return acc_dets_json_data
160
161
162 def get_user_name_accounts(idrac, module, accounts_uri, user_name):
163 all_accs = fetch_all_accounts(idrac, accounts_uri)
164 acc_dets_json_data = {}
165 for acc in all_accs:
166 if acc.get("UserName") == user_name:
167 acc.pop("Links", None)
168 acc_dets_json_data = strip_substr_dict(acc)
169 if acc_dets_json_data.get("Oem") is not None:
170 acc_dets_json_data["Oem"]["Dell"] = strip_substr_dict(acc_dets_json_data["Oem"]["Dell"])
171 break
172 if not bool(acc_dets_json_data):
173 module.fail_json(msg=INVALID_USERNAME, failed=True)
174 return acc_dets_json_data
175
176
177 def get_all_accounts(idrac, account_uri):
178 all_accs = fetch_all_accounts(idrac, account_uri)
179 idrac_list = []
180 for acc in all_accs:
181 if acc.get("UserName") != "":
182 acc.pop("Links", None)
183 acc_dets_json_data = strip_substr_dict(acc)
184 if acc_dets_json_data.get("Oem") is not None:
185 acc_dets_json_data["Oem"]["Dell"] = strip_substr_dict(acc_dets_json_data["Oem"]["Dell"])
186 idrac_list.append(acc_dets_json_data)
187 return idrac_list
188
189
190 def main():
191 specs = {
192 "user_id": {"type": 'int'},
193 "username": {"type": 'str'}
194 }
195
196 module = IdracAnsibleModule(
197 argument_spec=specs,
198 mutually_exclusive=[
199 ('user_id', 'username')
200 ],
201 supports_check_mode=True
202 )
203 try:
204 with iDRACRedfishAPI(module.params, req_session=True) as idrac:
205 resp = []
206 msg = SUCCESSFUL_MSG
207 accounts_uri = get_accounts_uri(idrac)
208 user_id = module.params.get("user_id")
209 user_name = module.params.get("username")
210 if user_id is not None:
211 resp.append(get_user_id_accounts(idrac, module, accounts_uri, user_id))
212 elif user_name is not None:
213 resp.append(get_user_name_accounts(idrac, module, accounts_uri, user_name))
214 else:
215 resp.extend(get_all_accounts(idrac, accounts_uri))
216 resp_len = len(resp)
217 msg = SUCCESS_MSG.format(resp_len)
218 if resp:
219 module.exit_json(msg=msg, user_info=resp)
220 else:
221 module.fail_json(msg=UNSUCCESS_MSG, failed=True)
222 except HTTPError as err:
223 module.fail_json(msg=json.load(err))
224 except URLError as err:
225 module.exit_json(msg=str(err), unreachable=True)
226 except (SSLValidationError, ConnectionError, TypeError, ValueError, OSError, SSLError) as err:
227 module.fail_json(msg=str(err))
228
229
230 if __name__ == '__main__':
231 main()