Debian Codegraph @

ansible_collections/dellemc/openmanage/plugins/modules/ome_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) 2019-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 from __future__ import (absolute_import, division, print_function)
13 __metaclass__ = type
14
15 DOCUMENTATION = r'''
16 ---
17 module: ome_user_info
18 short_description: Retrieves details of all accounts or a specific account on OpenManage Enterprise
19 version_added: "2.0.0"
20 description:
21 - "This module retrieves the list and basic details of all accounts or details of a specific account on
22 OpenManage Enterprise."
23 extends_documentation_fragment:
24 - dellemc.openmanage.ome_auth_options
25 options:
26 account_id:
27 description: Unique Id of the account.
28 type: int
29 system_query_options:
30 description: Options for filtering the output.
31 type: dict
32 suboptions:
33 filter:
34 description: Filter records for the supported values.
35 type: str
36 requirements:
37 - "python >= 3.9.6"
38 author: "Jagadeesh N V (@jagadeeshnv)"
39 notes:
40 - Run this module from a system that has direct access to Dell OpenManage Enterprise.
41 - This module supports C(check_mode).
42 '''
43
44 EXAMPLES = r'''
45 ---
46 - name: Retrieve basic details of all accounts
47 dellemc.openmanage.ome_user_info:
48 hostname: "192.168.0.1"
49 username: "username"
50 password: "password"
51 ca_path: "/path/to/ca_cert.pem"
52
53 - name: Retrieve details of a specific account identified by its account ID
54 dellemc.openmanage.ome_user_info:
55 hostname: "192.168.0.1"
56 username: "username"
57 password: "password"
58 ca_path: "/path/to/ca_cert.pem"
59 account_id: 1
60
61 - name: Get filtered user info based on user name
62 dellemc.openmanage.ome_user_info:
63 hostname: "192.168.0.1"
64 username: "username"
65 password: "password"
66 ca_path: "/path/to/ca_cert.pem"
67 system_query_options:
68 filter: "UserName eq 'test'"
69 '''
70
71 RETURN = r'''
72 ---
73 msg:
74 type: str
75 description: Over all status of fetching user facts.
76 returned: on error
77 sample: "Unable to retrieve the account details."
78 user_info:
79 type: dict
80 description: Details of the user.
81 returned: success
82 sample: {
83 "192.168.0.1": {
84 "Id": "1814",
85 "UserTypeId": 1,
86 "DirectoryServiceId": 0,
87 "Description": "user name description",
88 "Name": "user_name",
89 "Password": null,
90 "UserName": "user_name",
91 "RoleId": "10",
92 "Locked": false,
93 "IsBuiltin": true,
94 "Enabled": true
95 }
96 }
97 '''
98
99 import json
100 from ssl import SSLError
101 from ansible_collections.dellemc.openmanage.plugins.module_utils.ome import RestOME, OmeAnsibleModule
102 from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError
103 from ansible.module_utils.urls import ConnectionError, SSLValidationError
104
105
106 def _get_query_parameters(module_params):
107 """Builds query parameter.
108
109 :return: dict
110 :example: {"$filter": UserName eq 'user name'}
111 """
112 system_query_param = module_params.get("system_query_options")
113 query_param = {}
114 if system_query_param:
115 query_param = dict([("$" + k, v) for k, v in system_query_param.items() if v is not None])
116 return query_param
117
118
119 def main():
120 specs = {
121 "account_id": {"type": 'int', "required": False},
122 "system_query_options": {"required": False, "type": 'dict', "options": {
123 "filter": {"type": 'str', "required": False},
124 }},
125 }
126
127 module = OmeAnsibleModule(
128 argument_spec=specs,
129 mutually_exclusive=[
130 ('account_id', 'system_query_options')
131 ],
132 supports_check_mode=True
133 )
134 account_uri = "AccountService/Accounts"
135 query_param = None
136 try:
137 with RestOME(module.params, req_session=True) as rest_obj:
138 if module.params.get("account_id") is not None:
139 # Fetch specific account
140 account_id = module.params.get("account_id")
141 account_path = "{0}('{1}')".format(account_uri, account_id)
142 elif module.params.get("system_query_options") is not None:
143 # Fetch all the user based on UserName
144 query_param = _get_query_parameters(module.params)
145 account_path = account_uri
146 else:
147 # Fetch all users
148 account_path = account_uri
149 resp = rest_obj.invoke_request('GET', account_path, query_param=query_param)
150 user_facts = resp.json_data
151 user_exists = True
152 if "value" in user_facts and len(user_facts["value"]) == 0:
153 user_exists = False
154 # check for 200 status as GET only returns this for success
155 if resp.status_code == 200 and user_exists:
156 module.exit_json(user_info={module.params["hostname"]: user_facts})
157 else:
158 module.fail_json(msg="Unable to retrieve the account details.")
159 except HTTPError as err:
160 module.fail_json(msg=json.load(err))
161 except URLError as err:
162 module.exit_json(msg=str(err), unreachable=True)
163 except (SSLValidationError, ConnectionError, TypeError, ValueError, OSError, SSLError) as err:
164 module.fail_json(msg=str(err))
165
166
167 if __name__ == '__main__':
168 main()