Skip to content

Commit ca053b8

Browse files
authored
proxmox_zone - new module (#176)
* proxmox_zone: new module for proxmox_zones - List zones * proxmox_zone: Create new zone * proxmox_zone: Keep common parameter for all conditions * proxmox_zone: Implement locking * proxmox_zone: Added update_zone() * proxmox_zone: added zone_absent() * proxmox_zone: Added document and examples * proxmox_zone: add missing exception handling for zone_preent() * proxmox_zone: validate params * proxmox_zone and proxmox module_utils - Move sdn locking functions to proxmox module_utils * proxmox_zone: Fix sanity issues - Fix PEP8 issues - Fix Doc issues for dnszone - Add workaround for author string - Add attribute check_mode and diff_mode - Add no_log=False for lock_token as this is not a secret - Add proxmox_zone to runtime.yml * proxmox_zone: fix minor issues found during testing * proxmox_zone: Add unit tests * proxmox_zone: Add missing boolean conversion * 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 * Create seprate proxmox_zone_info - Split proxmox_zone and create proxmox_zone_info - Rename variable type to zone_type * proxmox_zone: Merge statepresent and update * proxmox_zone: Get lock just before making changes - also fix minor issues in proxmox_zone,proxmox_zone_info, proxmox_sdn. * Update unit tests for proxmox_zone & proxmox_zone_info
1 parent a0f0955 commit ca053b8

File tree

6 files changed

+928
-0
lines changed

6 files changed

+928
-0
lines changed

meta/runtime.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ action_groups:
3636
- proxmox_user
3737
- proxmox_user_info
3838
- proxmox_vm_info
39+
- proxmox_zone
40+
- proxmox_zone_info
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
from typing import List, Dict
12+
13+
from ansible_collections.community.proxmox.plugins.module_utils.proxmox import (
14+
ansible_to_proxmox_bool,
15+
ProxmoxAnsible
16+
)
17+
18+
19+
class ProxmoxSdnAnsible(ProxmoxAnsible):
20+
"""Base Class for All Proxmox SDN Classes"""
21+
22+
def __init__(self, module):
23+
super(ProxmoxSdnAnsible, self).__init__(module)
24+
self.module = module
25+
26+
def get_global_sdn_lock(self) -> str:
27+
"""Acquire global SDN lock. Needed for any changes under SDN.
28+
29+
:return: lock-token
30+
"""
31+
try:
32+
return self.proxmox_api.cluster().sdn().lock().post()
33+
except Exception as e:
34+
self.module.fail_json(
35+
msg=f'Failed to acquire global sdn lock {e}'
36+
)
37+
38+
def apply_sdn_changes_and_release_lock(self, lock: str, release_lock: bool = True) -> None:
39+
"""Apply all SDN changes done under a lock token.
40+
41+
:param lock: Global SDN lock token
42+
:param release_lock: if True release lock after successfully applying changes
43+
"""
44+
lock_params = {
45+
'lock-token': lock,
46+
'release-lock': ansible_to_proxmox_bool(release_lock)
47+
}
48+
try:
49+
self.proxmox_api.cluster().sdn().put(**lock_params)
50+
except Exception as e:
51+
self.rollback_sdn_changes_and_release_lock(lock)
52+
self.module.fail_json(
53+
msg=f'Failed to apply sdn changes {e}. Rolling back all pending changes.'
54+
)
55+
56+
def rollback_sdn_changes_and_release_lock(self, lock: str, release_lock: bool = True) -> None:
57+
"""Rollback all changes done under a lock token.
58+
59+
:param lock: Global SDN lock token
60+
:param release_lock: if True release lock after successfully rolling back changes
61+
"""
62+
lock_params = {
63+
'lock-token': lock,
64+
'release-lock': ansible_to_proxmox_bool(release_lock)
65+
}
66+
try:
67+
self.proxmox_api.cluster().sdn().rollback().post(**lock_params)
68+
except Exception as e:
69+
self.module.fail_json(
70+
msg=f'Rollback attempt failed - {e}. Manually clear lock by deleting /etc/pve/sdn/.lock'
71+
)
72+
73+
def release_lock(self, lock: str, force: bool = False) -> None:
74+
"""Release Global SDN lock
75+
76+
:param lock: Global SDN lock token
77+
:param force: if true, allow releasing lock without providing the token
78+
"""
79+
lock_params = {
80+
'lock-token': lock,
81+
'force': ansible_to_proxmox_bool(force)
82+
}
83+
try:
84+
self.proxmox_api.cluster().sdn().lock().delete(**lock_params)
85+
except Exception as e:
86+
self.module.fail_json(
87+
msg=f'Failed to release lock - {e}. Manually clear lock by deleting /etc/pve/sdn/.lock'
88+
)
89+
90+
def get_zones(self, zone_type: str = None) -> List[Dict]:
91+
"""Get Proxmox SDN zones
92+
93+
:param zone_type: Filter zones based on type.
94+
:return: list of all zones and their properties.
95+
"""
96+
try:
97+
return self.proxmox_api.cluster().sdn().zones().get(type=zone_type)
98+
except Exception as e:
99+
self.module.fail_json(
100+
msg=f'Failed to retrieve zone information from cluster: {e}'
101+
)

0 commit comments

Comments
 (0)