feat(clusters): name the backup server behind a pbs storage (#55) #57

Merged
christianmanivong merged 1 commits from fix/pbs-storage-ref into main 2026-11-03 04:13:53 +00:00
3 changed files with 97 additions and 0 deletions

No files matched your search

+7
View File
@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- `get_clusters()` names the backup server behind each `pbs` storage
(`ClusterStorageDict.backup_server`: server, port, datastore, namespace, fingerprint,
read from `/storage`; never its password or encryption key), so an application can tie a
Proxmox Backup Server's groups to the cluster's guests (#55). An unreadable `/storage`
leaves it `None`; every other storage carries no such key.
## [0.9.0] - 2026-11-02
### Added
+31
View File
@@ -36,6 +36,7 @@ if TYPE_CHECKING:
# Type-only: the cluster models are newer than the napalm_device_types floor
# in pyproject.toml, and nothing here needs them at runtime.
from napalm_device_types import (
BackupServerRefDict,
ClusterDict,
ClusterNetworkDict,
ClusterNodeDict,
@@ -94,6 +95,35 @@ def _storages(resources: list[_JsonDict]) -> list[ClusterStorageDict]:
return [_storage(name, rows) for name, rows in groups.items()]
def _backup_server_ref(config: _JsonDict) -> BackupServerRefDict:
"""Which server and datastore a ``pbs`` storage writes to (``/storage``); never
its password or encryption key."""
port = config.get("port")
return {
"server": str(config.get("server") or ""),
"port": int(port) if port else None,
"datastore": str(config.get("datastore") or ""),
"namespace": config.get("namespace") or None,
"fingerprint": config.get("fingerprint") or None,
}
def _name_backup_servers(storages: list[ClusterStorageDict], api: Any) -> None:
"""Give each ``pbs`` storage in *storages* its ``backup_server`` (#55); None for
all of them when ``/storage`` cannot be read."""
pbs = [s for s in storages if s["type"] == "pbs"]
if not pbs:
return
try:
configs = {str(c.get("storage")): c for c in api.storage.get() or []}
except Exception:
logger.debug("/storage could not be read; the backup servers stay unnamed", exc_info=True)
configs = None
for storage in pbs:
config = configs.get(storage["name"]) if configs is not None else None
storage["backup_server"] = _backup_server_ref(config) if config else None
def _merge_bridges(per_node: list[tuple[str, list[_JsonDict]]]) -> list[ClusterNetworkDict]:
merged: dict[str, ClusterNetworkDict] = {}
for node, network in per_node:
@@ -148,6 +178,7 @@ class ProxmoxClusterMixin:
head = next((s for s in status if s.get("type") == "cluster"), None)
members = self._members(status if head else [])
storages = _storages(resources)
_name_backup_servers(storages, self._api)
return [
{
"id": None,
+59
View File
@@ -226,6 +226,7 @@ def _api(
api.cluster.ha.status.current.get.return_value = []
api.cluster.ha.rules.get.return_value = []
api.cluster.backup.get.return_value = []
api.storage.get.return_value = []
return api
@@ -505,3 +506,61 @@ class TestCreateOnAnotherNode:
api.cluster.nextid.get.assert_not_called()
for res in [api.nodes(n) for n in ("pve1", "pve2")]:
assert not [c for c in res.mock_calls if "post" in c[0]]
# /storage: what a pbs storage writes to (napalm-device-types 4.25, #55). The password
# and the encryption key are never in this answer's use.
PBS_ROW = _storage_row("pve1", "pbs-01", "pbs", 1, "backup", 1000 * GIB, 30 * GIB)
STORAGE_CONFIG = [
{"storage": "local", "type": "dir", "path": "/var/lib/vz", "content": "iso,backup"},
{
"storage": "pbs-01",
"type": "pbs",
"server": "192.0.2.80",
"port": 8007,
"datastore": "backup",
"namespace": "lab",
"fingerprint": "aa:bb",
"username": "root@pam",
"content": "backup",
},
]
class TestBackupServerRef:
def _storages(self, config):
api = _api(resources=[*CLUSTER_RESOURCES, PBS_ROW])
if isinstance(config, Exception):
api.storage.get.side_effect = config
else:
api.storage.get.return_value = config
return _by_name(_driver(api).get_clusters()[0]["storages"])
def test_a_pbs_storage_names_its_server(self):
assert self._storages(STORAGE_CONFIG)["pbs-01"]["backup_server"] == {
"server": "192.0.2.80",
"port": 8007,
"datastore": "backup",
"namespace": "lab",
"fingerprint": "aa:bb",
}
def test_what_it_does_not_set_is_none(self):
bare = [{"storage": "pbs-01", "type": "pbs", "server": "pbs.example.com", "datastore": "b"}]
assert self._storages(bare)["pbs-01"]["backup_server"] == {
"server": "pbs.example.com",
"port": None,
"datastore": "b",
"namespace": None,
"fingerprint": None,
}
def test_other_storages_carry_no_reference(self):
storages = self._storages(STORAGE_CONFIG)
assert "backup_server" not in storages["local-lvm"]
assert "backup_server" not in storages["ceph-vm"]
def test_an_unreadable_storage_config_says_none(self):
storages = self._storages(PermissionError("403 Forbidden"))
assert storages["pbs-01"]["backup_server"] is None
assert "backup_server" not in storages["local-lvm"]