Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2025, Jana Hoch <[email protected]>
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_vnet_info
10 short_description: Retrieve information about one or more Proxmox VE SDN vnets.
11 version_added: "1.4.0"
12 description:
13 - Retrieve information about one or more Proxmox VE SDN vnets.
14 author: 'Jana Hoch <[email protected]> (!UNKNOWN)'
15 options:
16 vnet:
17 description:
18 - Restrict results to a specific vnet.
19 type: str
20
21 extends_documentation_fragment:
22 - community.proxmox.proxmox.actiongroup_proxmox
23 - community.proxmox.proxmox.documentation
24 - community.proxmox.attributes
25 - community.proxmox.attributes.info_module
26 """
27
28 EXAMPLES = r"""
29 - name: Get all vnet details
30 community.proxmox.proxmox_vnet_info:
31
32 - name: Get details for vnet - test
33 community.proxmox.proxmox_vnet_info:
34 vnet: test
35 """
36
37 RETURN = r"""
38 vnets:
39 description: List of vnets.
40 returned: on success
41 type: list
42 elements: dict
43 sample:
44 [
45 {
46 "digest": "01505201eb33919888fb0cacba27d3aae803f6d2",
47 "firewall_rules": [],
48 "subnets": [
49 {
50 "cidr": "10.10.100.0/24",
51 "dhcp-range": [],
52 "digest": "47684c511d9b67e8eb41b93bc5c0b078786b0ee3",
53 "id": "lab-10.10.100.0-24",
54 "mask": "24",
55 "network": "10.10.100.0",
56 "snat": 1,
57 "subnet": "lab-10.10.100.0-24",
58 "type": "subnet",
59 "vnet": "lab",
60 "zone": "lab"
61 }
62 ],
63 "tag": 100,
64 "type": "vnet",
65 "vnet": "lab",
66 "zone": "lab"
67 },
68 {
69 "digest": "01505201eb33919888fb0cacba27d3aae803f6d2",
70 "firewall_rules": [
71 {
72 "action": "ACCEPT",
73 "dest": "+sdn/test2-gateway",
74 "digest": "36016a02a5387d4c1171d29be966d550216bc500",
75 "enable": 1,
76 "log": "nolog",
77 "macro": "DNS",
78 "pos": 0,
79 "type": "forward"
80 },
81 {
82 "action": "ACCEPT",
83 "digest": "36016a02a5387d4c1171d29be966d550216bc500",
84 "enable": 1,
85 "log": "nolog",
86 "macro": "DHCPfwd",
87 "pos": 1,
88 "type": "forward"
89 }
90 ],
91 "subnets": [
92 {
93 "cidr": "10.10.0.0/24",
94 "dhcp-range": [
95 {
96 "end-address": "10.10.0.50",
97 "start-address": "10.10.0.5"
98 }
99 ],
100 "digest": "47684c511d9b67e8eb41b93bc5c0b078786b0ee3",
101 "gateway": "10.10.0.1",
102 "id": "test1-10.10.0.0-24",
103 "mask": "24",
104 "network": "10.10.0.0",
105 "subnet": "test1-10.10.0.0-24",
106 "type": "subnet",
107 "vnet": "test2",
108 "zone": "test1"
109 },
110 {
111 "cidr": "10.10.1.0/24",
112 "dhcp-range": [
113 {
114 "end-address": "10.10.1.50",
115 "start-address": "10.10.1.5"
116 }
117 ],
118 "digest": "47684c511d9b67e8eb41b93bc5c0b078786b0ee3",
119 "gateway": "10.10.1.0",
120 "id": "test1-10.10.1.0-24",
121 "mask": "24",
122 "network": "10.10.1.0",
123 "subnet": "test1-10.10.1.0-24",
124 "type": "subnet",
125 "vnet": "test2",
126 "zone": "test1"
127 }
128 ],
129 "type": "vnet",
130 "vnet": "test2",
131 "zone": "test1"
132 }
133 ]
134 """
135
136 from ansible_collections.community.proxmox.plugins.module_utils.proxmox import (
137 ProxmoxAnsible,
138 create_proxmox_module,
139 )
140
141
142 def module_args():
143 return dict(vnet=dict(type="str", required=False))
144
145
146 def module_options():
147 return {}
148
149
150 class ProxmoxVnetInfoAnsible(ProxmoxAnsible):
151 def get_subnets(self, vnet):
152 try:
153 return self.proxmox_api.cluster().sdn().vnets(vnet).subnets().get()
154 except Exception as e:
155 self.module.fail_json(msg=f"Failed to retrieve subnet information from vnet {vnet}: {e}")
156
157 def get_firewall(self, vnet_name):
158 try:
159 return self.proxmox_api.cluster().sdn().vnets(vnet_name).firewall().rules().get()
160 except Exception as e:
161 self.module.fail_json(msg=f"Failed to retrieve subnet information from vnet {vnet_name}: {e}")
162
163 def get_vnet_detail(self):
164 try:
165 vnets = self.proxmox_api.cluster().sdn().vnets().get()
166 for vnet in vnets:
167 vnet["subnets"] = self.get_subnets(vnet["vnet"])
168 vnet["firewall_rules"] = self.get_firewall(vnet["vnet"])
169 return vnets
170 except Exception as e:
171 self.module.fail_json(msg=f"Failed to retrieve vnet information from cluster: {e}")
172
173
174 def main():
175 module = create_proxmox_module(module_args(), **module_options())
176 proxmox = ProxmoxVnetInfoAnsible(module)
177
178 vnet = module.params["vnet"]
179 vnets = proxmox.get_vnet_detail()
180
181 if vnet:
182 vnets = [vnet_details for vnet_details in vnets if vnet_details["vnet"] == vnet]
183
184 module.exit_json(changed=False, vnets=vnets, msg="Successfully retrieved vnet info")
185
186
187 if __name__ == "__main__":
188 main()