Debian Codegraph @

ansible_collections/community/general/plugins/modules/one_vnet.py

raw
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2024, Alexander Bakanovskii <[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 # Make coding more python3-ish
8 from __future__ import annotations
9
10 DOCUMENTATION = r"""
11 module: one_vnet
12 short_description: Manages OpenNebula virtual networks
13 version_added: 9.4.0
14 author: "Alexander Bakanovskii (@abakanovskii)"
15 requirements:
16 - pyone
17 description:
18 - Manages virtual networks in OpenNebula.
19 attributes:
20 check_mode:
21 support: partial
22 details:
23 - Note that check mode always returns RV(ignore:changed=true) for existing networks, even if the network would not actually
24 change.
25 diff_mode:
26 support: none
27 options:
28 id:
29 description:
30 - A O(id) of the network you would like to manage.
31 - If not set then a new network is created with the given O(name).
32 type: int
33 name:
34 description:
35 - A O(name) of the network you would like to manage. If a network with the given name does not exist it, then is created,
36 otherwise it is managed by this module.
37 type: str
38 template:
39 description:
40 - A string containing the network template contents.
41 type: str
42 state:
43 description:
44 - V(present) - state that is used to manage the network.
45 - V(absent) - delete the network.
46 choices: ["present", "absent"]
47 default: present
48 type: str
49
50 extends_documentation_fragment:
51 - community.general._opennebula
52 - community.general._attributes
53 """
54
55 EXAMPLES = r"""
56 - name: Make sure the network is present by ID
57 community.general.one_vnet:
58 id: 0
59 state: present
60 register: result
61
62 - name: Make sure the network is present by name
63 community.general.one_vnet:
64 name: opennebula-bridge
65 state: present
66 register: result
67
68 - name: Create a new or update an existing network
69 community.general.one_vnet:
70 name: bridge-network
71 template: |
72 VN_MAD = "bridge"
73 BRIDGE = "br0"
74 BRIDGE_TYPE = "linux"
75 AR=[
76 TYPE = "IP4",
77 IP = 192.0.2.50,
78 SIZE = "20"
79 ]
80 DNS = 192.0.2.1
81 GATEWAY = 192.0.2.1
82
83 - name: Delete the network by ID
84 community.general.one_vnet:
85 id: 0
86 state: absent
87 """
88
89 RETURN = r"""
90 id:
91 description: The network ID.
92 type: int
93 returned: when O(state=present)
94 sample: 153
95 name:
96 description: The network name.
97 type: str
98 returned: when O(state=present)
99 sample: app1
100 template:
101 description: The parsed network template.
102 type: dict
103 returned: when O(state=present)
104 sample:
105 BRIDGE: onebr.1000
106 BRIDGE_TYPE: linux
107 DESCRIPTION: sampletext
108 PHYDEV: eth0
109 SECURITY_GROUPS: 0
110 VLAN_ID: 1000
111 VN_MAD: 802.1Q
112 user_id:
113 description: The network's user name.
114 type: int
115 returned: when O(state=present)
116 sample: 1
117 user_name:
118 description: The network's user ID.
119 type: str
120 returned: when O(state=present)
121 sample: oneadmin
122 group_id:
123 description: The network's group ID.
124 type: int
125 returned: when O(state=present)
126 sample: 1
127 group_name:
128 description: The network's group name.
129 type: str
130 returned: when O(state=present)
131 sample: one-users
132 owner_id:
133 description: The network's owner ID.
134 type: int
135 returned: when O(state=present)
136 sample: 143
137 owner_name:
138 description: The network's owner name.
139 type: str
140 returned: when O(state=present)
141 sample: ansible-test
142 permissions:
143 description: The network's permissions.
144 type: dict
145 returned: when O(state=present)
146 contains:
147 owner_u:
148 description: The network's owner USAGE permissions.
149 type: str
150 sample: 1
151 owner_m:
152 description: The network's owner MANAGE permissions.
153 type: str
154 sample: 0
155 owner_a:
156 description: The network's owner ADMIN permissions.
157 type: str
158 sample: 0
159 group_u:
160 description: The network's group USAGE permissions.
161 type: str
162 sample: 0
163 group_m:
164 description: The network's group MANAGE permissions.
165 type: str
166 sample: 0
167 group_a:
168 description: The network's group ADMIN permissions.
169 type: str
170 sample: 0
171 other_u:
172 description: The network's other users USAGE permissions.
173 type: str
174 sample: 0
175 other_m:
176 description: The network's other users MANAGE permissions.
177 type: str
178 sample: 0
179 other_a:
180 description: The network's other users ADMIN permissions.
181 type: str
182 sample: 0
183 sample:
184 owner_u: 1
185 owner_m: 0
186 owner_a: 0
187 group_u: 0
188 group_m: 0
189 group_a: 0
190 other_u: 0
191 other_m: 0
192 other_a: 0
193 clusters:
194 description: The network's clusters.
195 type: list
196 returned: when O(state=present)
197 sample: [0, 100]
198 bridge:
199 description: The network's bridge interface.
200 type: str
201 returned: when O(state=present)
202 sample: br0
203 bridge_type:
204 description: The network's bridge type.
205 type: str
206 returned: when O(state=present)
207 sample: linux
208 parent_network_id:
209 description: The network's parent network ID.
210 type: int
211 returned: when O(state=present)
212 sample: 1
213 vn_mad:
214 description: The network's VN_MAD.
215 type: str
216 returned: when O(state=present)
217 sample: bridge
218 phydev:
219 description: The network's physical device (NIC).
220 type: str
221 returned: when O(state=present)
222 sample: eth0
223 vlan_id:
224 description: The network's VLAN tag.
225 type: int
226 returned: when O(state=present)
227 sample: 1000
228 outer_vlan_id:
229 description: The network's outer VLAN tag.
230 type: int
231 returned: when O(state=present)
232 sample: 1000
233 vrouters:
234 description: The network's list of virtual routers IDs.
235 type: list
236 returned: when O(state=present)
237 sample: [0, 1]
238 ar_pool:
239 description: The network's list of ar_pool.
240 type: list
241 returned: when O(state=present)
242 sample:
243 - ar_id: 0
244 ip: 192.0.2.1
245 mac: 6c:1e:46:01:cd:d1
246 size: 20
247 type: IP4
248 - ar_id: 1
249 allocated: 0
250 ip: 198.51.100.1
251 mac: 5d:9b:c0:9e:f6:e5
252 size: 20
253 type: IP4
254 """
255
256
257 from ansible_collections.community.general.plugins.module_utils._opennebula import OpenNebulaModule
258
259
260 class NetworksModule(OpenNebulaModule):
261 def __init__(self):
262 argument_spec = dict(
263 id=dict(type="int"),
264 name=dict(type="str"),
265 state=dict(type="str", choices=["present", "absent"], default="present"),
266 template=dict(type="str"),
267 )
268
269 mutually_exclusive = [["id", "name"]]
270
271 required_one_of = [("id", "name")]
272
273 required_if = [["state", "present", ["template"]]]
274
275 OpenNebulaModule.__init__(
276 self,
277 argument_spec,
278 supports_check_mode=True,
279 mutually_exclusive=mutually_exclusive,
280 required_one_of=required_one_of,
281 required_if=required_if,
282 )
283
284 def run(self, one, module, result):
285 params = module.params
286 id = params.get("id")
287 name = params.get("name")
288 desired_state = params.get("state")
289 template_data = params.get("template")
290
291 self.result = {}
292
293 template = self.get_template_instance(id, name)
294 needs_creation = False
295 if not template and desired_state != "absent":
296 if id:
297 module.fail_json(msg=f"There is no template with id={id}")
298 else:
299 needs_creation = True
300
301 if desired_state == "absent":
302 self.result = self.delete_template(template)
303 else:
304 if needs_creation:
305 self.result = self.create_template(name, template_data)
306 else:
307 self.result = self.update_template(template, template_data)
308
309 self.exit()
310
311 def get_template(self, predicate):
312 # -2 means "Resources belonging to all users"
313 # the other two parameters are used for pagination, -1 for both essentially means "return all"
314 pool = self.one.vnpool.info(-2, -1, -1)
315
316 for template in pool.VNET:
317 if predicate(template):
318 return template
319
320 return None
321
322 def get_template_by_id(self, template_id):
323 return self.get_template(lambda template: template_id == template.ID)
324
325 def get_template_by_name(self, name):
326 return self.get_template(lambda template: name == template.NAME)
327
328 def get_template_instance(self, requested_id, requested_name):
329 if requested_id:
330 return self.get_template_by_id(requested_id)
331 else:
332 return self.get_template_by_name(requested_name)
333
334 def get_networks_ar_pool(self, template):
335 ar_pool = []
336 template_pool = template.AR_POOL.AR
337 for ar in range(len(template_pool)):
338 template_param = template_pool[ar]
339 ar_pool.append(
340 {
341 # These params will always be present
342 "ar_id": template_param.AR_ID,
343 "mac": template_param.MAC,
344 "size": template_param.SIZE,
345 "type": template_param.TYPE,
346 # These are optional so firstly check for presence
347 # and if not present set value to Null
348 "allocated": getattr(template_param, "ALLOCATED", "Null"),
349 "ip": getattr(template_param, "IP", "Null"),
350 "global_prefix": getattr(template_param, "GLOBAL_PREFIX", "Null"),
351 "parent_network_ar_id": getattr(template_param, "PARENT_NETWORK_AR_ID", "Null"),
352 "ula_prefix": getattr(template_param, "ULA_PREFIX", "Null"),
353 "vn_mad": getattr(template_param, "VN_MAD", "Null"),
354 }
355 )
356 return ar_pool
357
358 def get_template_info(self, template):
359 info = {
360 "id": template.ID,
361 "name": template.NAME,
362 "template": template.TEMPLATE,
363 "user_name": template.UNAME,
364 "user_id": template.UID,
365 "group_name": template.GNAME,
366 "group_id": template.GID,
367 "permissions": {
368 "owner_u": template.PERMISSIONS.OWNER_U,
369 "owner_m": template.PERMISSIONS.OWNER_M,
370 "owner_a": template.PERMISSIONS.OWNER_A,
371 "group_u": template.PERMISSIONS.GROUP_U,
372 "group_m": template.PERMISSIONS.GROUP_M,
373 "group_a": template.PERMISSIONS.GROUP_A,
374 "other_u": template.PERMISSIONS.OTHER_U,
375 "other_m": template.PERMISSIONS.OTHER_M,
376 "other_a": template.PERMISSIONS.OTHER_A,
377 },
378 "clusters": template.CLUSTERS.ID,
379 "bridge": template.BRIDGE,
380 "bride_type": template.BRIDGE_TYPE,
381 "parent_network_id": template.PARENT_NETWORK_ID,
382 "vn_mad": template.VN_MAD,
383 "phydev": template.PHYDEV,
384 "vlan_id": template.VLAN_ID,
385 "outer_vlan_id": template.OUTER_VLAN_ID,
386 "used_leases": template.USED_LEASES,
387 "vrouters": template.VROUTERS.ID,
388 "ar_pool": self.get_networks_ar_pool(template),
389 }
390
391 return info
392
393 def create_template(self, name, template_data):
394 if not self.module.check_mode:
395 # -1 means that network won't be added to any cluster which happens by default
396 self.one.vn.allocate(f'NAME = "{name}"\n{template_data}', -1)
397
398 result = self.get_template_info(self.get_template_by_name(name))
399 result["changed"] = True
400
401 return result
402
403 def update_template(self, template, template_data):
404 if not self.module.check_mode:
405 # 0 = replace the whole template
406 self.one.vn.update(template.ID, template_data, 0)
407
408 result = self.get_template_info(self.get_template_by_id(template.ID))
409 if self.module.check_mode:
410 # Unfortunately it is not easy to detect if the template would have changed, therefore always report a change here.
411 result["changed"] = True
412 else:
413 # if the previous parsed template data is not equal to the updated one, this has changed
414 result["changed"] = result["template"] != template.TEMPLATE
415
416 return result
417
418 def delete_template(self, template):
419 if not template:
420 return {"changed": False}
421
422 if not self.module.check_mode:
423 self.one.vn.delete(template.ID)
424
425 return {"changed": True}
426
427
428 def main():
429 NetworksModule().run_module()
430
431
432 if __name__ == "__main__":
433 main()