Debian Codegraph @

ansible_collections/community/proxmox/plugins/modules/proxmox_user_info.py

raw
1 #!/usr/bin/python
2 #
3 # Copyright Tristan Le Guern <tleguern at bouledef.eu>
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7
8 DOCUMENTATION = r"""
9 module: proxmox_user_info
10 short_description: Retrieve information about one or more Proxmox VE users
11 description:
12 - Retrieve information about one or more Proxmox VE users.
13 options:
14 domain:
15 description:
16 - Restrict results to a specific authentication realm.
17 aliases: ['realm']
18 type: str
19 user:
20 description:
21 - Restrict results to a specific user.
22 aliases: ['name']
23 type: str
24 userid:
25 description:
26 - Restrict results to a specific user ID, which is a concatenation of a user and domain parts.
27 type: str
28 author: Tristan Le Guern (@tleguern)
29 extends_documentation_fragment:
30 - community.proxmox.proxmox.actiongroup_proxmox
31 - community.proxmox.proxmox.documentation
32 - community.proxmox.attributes
33 - community.proxmox.attributes.info_module
34 """
35
36 EXAMPLES = r"""
37 - name: List existing users
38 community.proxmox.proxmox_user_info:
39 register: proxmox_users
40
41 - name: List existing users in the pve authentication realm
42 community.proxmox.proxmox_user_info:
43 domain: pve
44 register: proxmox_users_pve
45
46 - name: Retrieve information about admin@pve
47 community.proxmox.proxmox_user_info:
48 userid: admin@pve
49 register: proxmox_user_admin
50
51 - name: Alternative way to retrieve information about admin@pve
52 community.proxmox.proxmox_user_info:
53 user: admin
54 domain: pve
55 register: proxmox_user_admin
56 """
57
58
59 RETURN = r"""
60 proxmox_users:
61 description: List of users.
62 returned: always, but can be empty
63 type: list
64 elements: dict
65 contains:
66 comment:
67 description: Short description of the user.
68 returned: on success
69 type: str
70 domain:
71 description: User's authentication realm, also the right part of the user ID.
72 returned: on success
73 type: str
74 email:
75 description: User's email address.
76 returned: on success
77 type: str
78 enabled:
79 description: User's account state.
80 returned: on success
81 type: bool
82 expire:
83 description: Expiration date in seconds since EPOCH. Zero means no expiration.
84 returned: on success
85 type: int
86 firstname:
87 description: User's first name.
88 returned: on success
89 type: str
90 groups:
91 description: List of groups which the user is a member of.
92 returned: on success
93 type: list
94 elements: str
95 keys:
96 description: User's two factor authentication keys.
97 returned: on success
98 type: str
99 lastname:
100 description: User's last name.
101 returned: on success
102 type: str
103 tokens:
104 description: List of API tokens associated to the user.
105 returned: on success
106 type: list
107 elements: dict
108 contains:
109 comment:
110 description: Short description of the token.
111 returned: on success
112 type: str
113 expire:
114 description: Expiration date in seconds since EPOCH. Zero means no expiration.
115 returned: on success
116 type: int
117 privsep:
118 description: Describe if the API token is further restricted with ACLs or is fully privileged.
119 returned: on success
120 type: bool
121 tokenid:
122 description: Token name.
123 returned: on success
124 type: str
125 user:
126 description: User's login name, also the left part of the user ID.
127 returned: on success
128 type: str
129 userid:
130 description: Proxmox user ID, represented as user@realm.
131 returned: on success
132 type: str
133 """
134
135
136 import traceback
137
138 from ansible_collections.community.proxmox.plugins.module_utils.proxmox import (
139 ProxmoxAnsible,
140 create_proxmox_module,
141 proxmox_to_ansible_bool,
142 )
143
144 try:
145 import proxmoxer
146 except ImportError:
147 PROXMOXER_LIBRARY = False
148 PROXMOXER_LIBRARY_IMPORT_ERROR = traceback.format_exc()
149 else:
150 PROXMOXER_LIBRARY = True
151 PROXMOXER_LIBRARY_IMPORT_ERROR = None
152
153
154 def module_args():
155 return dict(
156 domain=dict(type="str", aliases=["realm"]),
157 user=dict(type="str", aliases=["name"]),
158 userid=dict(type="str"),
159 )
160
161
162 def module_options():
163 return dict(mutually_exclusive=[("user", "userid"), ("domain", "userid")])
164
165
166 class ProxmoxUserInfoAnsible(ProxmoxAnsible):
167 def get_user(self, userid):
168 try:
169 user = self.proxmox_api.access.users.get(userid)
170 except proxmoxer.core.ResourceException:
171 result = dict(changed=False)
172 result["proxmox_users"] = []
173 self.module.exit_json(**result)
174 except Exception as e:
175 self.module.fail_json(msg=f"Getting specific user '{userid}' failed: {e}")
176 user["userid"] = userid
177 return ProxmoxUser(user)
178
179 def get_users(self, domain=None):
180 users = self.proxmox_api.access.users.get(full=1)
181 users = [ProxmoxUser(user) for user in users]
182 if domain:
183 return [user for user in users if user.user["domain"] == domain]
184 return users
185
186
187 class ProxmoxUser:
188 def __init__(self, user):
189 self.user = dict()
190 # Data representation is not the same depending on API calls
191 for k, v in user.items():
192 if k == "enable":
193 self.user["enabled"] = proxmox_to_ansible_bool(user["enable"])
194 elif k == "userid":
195 self.user["user"] = user["userid"].split("@")[0]
196 self.user["domain"] = user["userid"].split("@")[1]
197 self.user[k] = v
198 elif k in ["groups", "tokens"] and (v == "" or v is None):
199 self.user[k] = []
200 elif k == "groups" and isinstance(v, str):
201 self.user["groups"] = v.split(",")
202 elif k == "tokens" and isinstance(v, list):
203 for token in v:
204 if "privsep" in token:
205 token["privsep"] = proxmox_to_ansible_bool(token["privsep"])
206 self.user["tokens"] = v
207 elif k == "tokens" and isinstance(v, dict):
208 self.user["tokens"] = list()
209 for tokenid, tokenvalues in v.items():
210 t = tokenvalues
211 t["tokenid"] = tokenid
212 if "privsep" in tokenvalues:
213 t["privsep"] = proxmox_to_ansible_bool(tokenvalues["privsep"])
214 self.user["tokens"].append(t)
215 else:
216 self.user[k] = v
217
218
219 def main():
220 module = create_proxmox_module(module_args(), **module_options())
221 proxmox = ProxmoxUserInfoAnsible(module)
222
223 result = dict(changed=False)
224
225 domain = module.params["domain"]
226 user = module.params["user"]
227 if user and domain:
228 userid = user + "@" + domain
229 else:
230 userid = module.params["userid"]
231
232 if userid:
233 users = [proxmox.get_user(userid=userid)]
234 else:
235 users = proxmox.get_users(domain=domain)
236 result["proxmox_users"] = [user.user for user in users]
237
238 module.exit_json(**result)
239
240
241 if __name__ == "__main__":
242 main()