Debian Codegraph @

ansible_collections/community/proxmox/plugins/modules/proxmox_group_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_group_info
10 short_description: Retrieve information about one or more Proxmox VE groups
11 description:
12 - Retrieve information about one or more Proxmox VE groups.
13 options:
14 group:
15 description:
16 - Restrict results to a specific group.
17 aliases: ['groupid', 'name']
18 type: str
19 author: Tristan Le Guern (@tleguern)
20 extends_documentation_fragment:
21 - community.proxmox.proxmox.actiongroup_proxmox
22 - community.proxmox.proxmox.documentation
23 - community.proxmox.attributes
24 - community.proxmox.attributes.info_module
25 """
26
27
28 EXAMPLES = r"""
29 - name: List existing groups
30 community.proxmox.proxmox_group_info:
31 register: proxmox_groups
32
33 - name: Retrieve information about the admin group
34 community.proxmox.proxmox_group_info:
35 group: admin
36 register: proxmox_group_admin
37 """
38
39
40 RETURN = r"""
41 proxmox_groups:
42 description: List of groups.
43 returned: always, but can be empty
44 type: list
45 elements: dict
46 contains:
47 comment:
48 description: Short description of the group.
49 returned: on success, can be absent
50 type: str
51 groupid:
52 description: Group name.
53 returned: on success
54 type: str
55 users:
56 description: List of users in the group.
57 returned: on success
58 type: list
59 elements: str
60 """
61
62
63 from ansible_collections.community.proxmox.plugins.module_utils.proxmox import (
64 ProxmoxAnsible,
65 create_proxmox_module,
66 )
67
68
69 def module_args():
70 return dict(
71 group=dict(type="str", aliases=["groupid", "name"]),
72 )
73
74
75 def module_options():
76 return {}
77
78
79 class ProxmoxGroupInfoAnsible(ProxmoxAnsible):
80 def get_group(self, groupid):
81 try:
82 group = self.proxmox_api.access.groups.get(groupid)
83 except Exception:
84 self.module.fail_json(msg=f"Group '{groupid}' does not exist")
85 group["groupid"] = groupid
86 return ProxmoxGroup(group)
87
88 def get_groups(self):
89 groups = self.proxmox_api.access.groups.get()
90 return [ProxmoxGroup(group) for group in groups]
91
92
93 class ProxmoxGroup:
94 def __init__(self, group):
95 self.group = dict()
96 # Data representation is not the same depending on API calls
97 for k, v in group.items():
98 if k == "users" and isinstance(v, str):
99 self.group["users"] = v.split(",")
100 elif k == "members":
101 self.group["users"] = group["members"]
102 else:
103 self.group[k] = v
104
105
106 def main():
107 module = create_proxmox_module(module_args(), **module_options())
108 proxmox = ProxmoxGroupInfoAnsible(module)
109
110 result = dict(changed=False)
111
112 group = module.params["group"]
113
114 if group:
115 groups = [proxmox.get_group(groupid=group)]
116 else:
117 groups = proxmox.get_groups()
118 result["proxmox_groups"] = [group.group for group in groups]
119
120 module.exit_json(**result)
121
122
123 if __name__ == "__main__":
124 main()