"""get_clusters() for Proxmox VE: quorum votes and the guests the HA manager keeps. Read from: - ``GET /cluster/config/nodes``: corosync's nodelist, ``quorum_votes`` per node; - ``GET /cluster/ha/resources``: each HA resource's ``sid`` (``vm:100``, ``ct:101``), requested ``state`` and, on PVE 8, ``group``; - ``GET /cluster/ha/status/current``: per ``service`` entry the ``node`` and the CRM's ``crm_state`` (only the verbose ``state`` while the CRM has not taken it up yet); - ``GET /cluster/ha/rules`` (PVE 9): each rule's ``rule`` id and its ``resources``, a comma-separated list of sids. PVE 8 has no such endpoint. Each read is best-effort: what cannot be read is None, never a guess. """ from __future__ import annotations import logging from typing import TYPE_CHECKING, Any from napalm_proxmox.pve_values import split_list if TYPE_CHECKING: # Type-only: newer than the napalm_device_types floor in pyproject.toml. from napalm_device_types import HAResourceDict logger = logging.getLogger(__name__) _JsonDict = dict[str, Any] #: HA resource type prefix -> guest kind. _KINDS = {"vm": "qemu", "ct": "lxc"} #: corosync counts a node without ``quorum_votes`` as one vote. _DEFAULT_VOTES = 1 #: What HA keeps a resource at when its config names no state. _DEFAULT_REQUESTED = "started" def read_votes(api: Any) -> dict[str, int] | None: """Quorum votes per node name; None when the nodelist cannot be read.""" try: rows = api.cluster.config.nodes.get() or [] except Exception as exc: logger.debug("Cannot read the corosync nodelist: %s", exc) return None return { str(row.get("node") or row.get("name")): int(row.get("quorum_votes", _DEFAULT_VOTES)) for row in rows if row.get("node") or row.get("name") } def _services(api: Any) -> dict[str, _JsonDict] | None: try: rows = api.cluster.ha.status.current.get() or [] except Exception as exc: logger.debug("Cannot read the HA status: %s", exc) return None return {str(r["sid"]): r for r in rows if r.get("type") == "service" and r.get("sid")} def _rules(api: Any) -> dict[str, list[str]]: """Rule ids per sid. PVE 8 answers with an error: no rules there.""" try: rows = api.cluster.ha.rules.get() or [] except Exception as exc: logger.debug("No HA rules (PVE 8 has none): %s", exc) return {} by_sid: dict[str, list[str]] = {} for rule in rows: for sid in split_list(rule.get("resources")): by_sid.setdefault(sid, []).append(str(rule.get("rule"))) return by_sid def _resource( row: _JsonDict, services: dict[str, _JsonDict] | None, rules: dict[str, list[str]] ) -> HAResourceDict | None: sid = str(row.get("sid") or "") prefix, _, vmid = sid.partition(":") if prefix not in _KINDS or not vmid.isdigit(): return None service = (services or {}).get(sid, {}) return { "vmid": int(vmid), "kind": _KINDS[prefix], "requested": str(row.get("state") or _DEFAULT_REQUESTED), "status": service.get("crm_state") or service.get("state") or None, "node": service.get("node") or None, "group": row.get("group") or None, "rules": rules.get(sid, []), } def read_ha_resources(api: Any) -> list[HAResourceDict] | None: """Every guest HA manages, by vmid; None when the HA resources cannot be read.""" try: rows = api.cluster.ha.resources.get() or [] except Exception as exc: logger.debug("Cannot read the HA resources: %s", exc) return None services, rules = _services(api), _rules(api) resources = [_resource(row, services, rules) for row in rows] return sorted((r for r in resources if r is not None), key=lambda r: r["vmid"])