Skip to content

Commit 4693b66

Browse files
committed
module_utils: Create new base class ProxmoxSdnAnsible()
- Move SDN locking methods from ProxmoxAnsible class to ProxmoxSdnAnsible - Add ge_zones() method to ProxmoxSdnAnsible As we will be splitting zone module into zone and zone_info
1 parent 7b24750 commit 4693b66

File tree

2 files changed

+112
-64
lines changed

2 files changed

+112
-64
lines changed

plugins/module_utils/proxmox.py

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -245,67 +245,3 @@ def get_storage_content(self, node, storage, content=None, vmid=None):
245245
msg="Unable to list content on %s, %s for %s and %s: %s"
246246
% (node, storage, content, vmid, e)
247247
)
248-
249-
def get_global_sdn_lock(self):
250-
"""Acquire global SDN lock. Needed for any changes under SDN.
251-
252-
@return: lock-token
253-
"""
254-
try:
255-
return self.proxmox_api.cluster().sdn().lock().post()
256-
except Exception as e:
257-
self.module.fail_json(
258-
msg=f'Failed to acquire global sdn lock {e}'
259-
)
260-
261-
def apply_sdn_changes_and_release_lock(self, lock, release_lock=True):
262-
"""Apply all SDN changes done under a lock token.
263-
264-
@param lock: Global SDN lock token
265-
@param release_lock: if True release lock after successfully applying changes
266-
"""
267-
lock_params = {
268-
'lock-token': lock,
269-
'release-lock': ansible_to_proxmox_bool(release_lock)
270-
}
271-
try:
272-
self.proxmox_api.cluster().sdn().put(**lock_params)
273-
except Exception as e:
274-
self.rollback_sdn_changes_and_release_lock(lock)
275-
self.module.fail_json(
276-
msg=f'Failed to apply sdn changes {e}. Rolling back all pending changes.'
277-
)
278-
279-
def rollback_sdn_changes_and_release_lock(self, lock, release_lock=True):
280-
"""Rollback all changes done under a lock token.
281-
282-
@param lock: Global SDN lock token
283-
@param release_lock: if True release lock after successfully rolling back changes
284-
"""
285-
lock_params = {
286-
'lock-token': lock,
287-
'release-lock': ansible_to_proxmox_bool(release_lock)
288-
}
289-
try:
290-
self.proxmox_api.cluster().sdn().rollback().post(**lock_params)
291-
except Exception as e:
292-
self.module.fail_json(
293-
msg=f'Rollback attempt failed - {e}. Manually clear lock by deleting /etc/pve/sdn/.lock'
294-
)
295-
296-
def release_lock(self, lock, force=False):
297-
"""Release lock
298-
299-
@param lock: Global SDN lock token
300-
@param force: if true, allow releasing lock without providing the token
301-
"""
302-
lock_params = {
303-
'lock-token': lock,
304-
'force': ansible_to_proxmox_bool(force)
305-
}
306-
try:
307-
self.proxmox_api.cluster().sdn().lock().delete(**lock_params)
308-
except Exception as e:
309-
self.module.fail_json(
310-
msg=f'Failed to release lock - {e}. Manually clear lock by deleting /etc/pve/sdn/.lock'
311-
)
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# -*- coding: utf-8 -*-
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+
from __future__ import absolute_import, division, print_function
8+
9+
__metaclass__ = type
10+
11+
import traceback
12+
from typing import List, Dict
13+
14+
PROXMOXER_IMP_ERR = None
15+
try:
16+
from proxmoxer import ProxmoxAPI
17+
from proxmoxer import __version__ as proxmoxer_version
18+
19+
HAS_PROXMOXER = True
20+
except ImportError:
21+
HAS_PROXMOXER = False
22+
PROXMOXER_IMP_ERR = traceback.format_exc()
23+
24+
from ansible_collections.community.proxmox.plugins.module_utils.proxmox import (
25+
ansible_to_proxmox_bool,
26+
ProxmoxAnsible
27+
)
28+
29+
30+
class ProxmoxSdnAnsible(ProxmoxAnsible):
31+
"""Base Class for All Proxmox SDN Classes"""
32+
33+
def __init__(self, module):
34+
super(ProxmoxSdnAnsible, self).__init__(module)
35+
self.module = module
36+
37+
def get_global_sdn_lock(self) -> str:
38+
"""Acquire global SDN lock. Needed for any changes under SDN.
39+
40+
:return: lock-token
41+
"""
42+
try:
43+
return self.proxmox_api.cluster().sdn().lock().post()
44+
except Exception as e:
45+
self.module.fail_json(
46+
msg=f'Failed to acquire global sdn lock {e}'
47+
)
48+
49+
def apply_sdn_changes_and_release_lock(self, lock: str, release_lock: bool = True) -> None:
50+
"""Apply all SDN changes done under a lock token.
51+
52+
:param lock: Global SDN lock token
53+
:param release_lock: if True release lock after successfully applying changes
54+
"""
55+
lock_params = {
56+
'lock-token': lock,
57+
'release-lock': ansible_to_proxmox_bool(release_lock)
58+
}
59+
try:
60+
self.proxmox_api.cluster().sdn().put(**lock_params)
61+
except Exception as e:
62+
self.rollback_sdn_changes_and_release_lock(lock)
63+
self.module.fail_json(
64+
msg=f'Failed to apply sdn changes {e}. Rolling back all pending changes.'
65+
)
66+
67+
def rollback_sdn_changes_and_release_lock(self, lock: str, release_lock: bool = True) -> None:
68+
"""Rollback all changes done under a lock token.
69+
70+
:param lock: Global SDN lock token
71+
:param release_lock: if True release lock after successfully rolling back changes
72+
"""
73+
lock_params = {
74+
'lock-token': lock,
75+
'release-lock': ansible_to_proxmox_bool(release_lock)
76+
}
77+
try:
78+
self.proxmox_api.cluster().sdn().rollback().post(**lock_params)
79+
except Exception as e:
80+
self.module.fail_json(
81+
msg=f'Rollback attempt failed - {e}. Manually clear lock by deleting /etc/pve/sdn/.lock'
82+
)
83+
84+
def release_lock(self, lock: str, force: bool = False) -> None:
85+
"""Release Global SDN lock
86+
87+
:param lock: Global SDN lock token
88+
:param force: if true, allow releasing lock without providing the token
89+
"""
90+
lock_params = {
91+
'lock-token': lock,
92+
'force': ansible_to_proxmox_bool(force)
93+
}
94+
try:
95+
self.proxmox_api.cluster().sdn().lock().delete(**lock_params)
96+
except Exception as e:
97+
self.module.fail_json(
98+
msg=f'Failed to release lock - {e}. Manually clear lock by deleting /etc/pve/sdn/.lock'
99+
)
100+
101+
def get_zones(self, zone_type: str = None) -> List[Dict]:
102+
"""Get Proxmox SDN zones
103+
104+
:param zone_type: Filter zones based on type.
105+
:return: list of all zones and their properties.
106+
"""
107+
try:
108+
return self.proxmox_api.cluster().sdn().zones().get(type=zone_type)
109+
except Exception as e:
110+
self.module.fail_json(
111+
msg=f'Failed to retrieve zone information from cluster: {e}'
112+
)

0 commit comments

Comments
 (0)