"""The node's past APT transactions (``UpdateMixin.get_update_history``, NetOrk/netork#2728). PVE 9.3 reads apt's history log for ``GET /nodes/{node}/apt/history``, newest first, also the transactions netOrk did not start (an operator's ``apt``, unattended-upgrades). A transaction is named by its ``start-time`` and its ``start-index`` among those that started in the same second of the same log file. An older node has no such endpoint: the driver raises ``NotImplementedError`` and parses no log over SSH. """ from __future__ import annotations from typing import TYPE_CHECKING, Any, cast from napalm_device_types import UpdateHistoryAction, UpdateHistoryDict, UpdateHistoryPackageDict from napalm_proxmox.pve_values import PVE_9_3 _JsonDict = dict[str, Any] def _text(value: Any) -> str | None: return str(value) if value else None def _package(row: _JsonDict) -> UpdateHistoryPackageDict: return { "name": str(row.get("package") or ""), "action": cast(UpdateHistoryAction, row.get("action")), "old_version": _text(row.get("old-version")), "new_version": _text(row.get("new-version")), } def _transaction(row: _JsonDict) -> UpdateHistoryDict: start = int(row["start-time"]) end = row.get("end-time") return { "id": f"{start}-{int(row.get('start-index') or 0)}", "start": start, "end": None if end is None else int(end), "packages": [_package(p) for p in row.get("packages") or []], "requested_by": _text(row.get("requested-by")), "command": _text(row.get("command-line")), "error": _text(row.get("error")), } class ProxmoxUpdateHistoryMixin: """Relies on the driver's ``_node_api`` and ``_version_at_least``.""" _node_name: str if TYPE_CHECKING: # pragma: no cover - the driver's (napalm_proxmox._base) def _node_api(self) -> Any: ... def _version_at_least(self, major: int, minor: int) -> bool: ... def get_update_history(self, since: int | None = None) -> list[UpdateHistoryDict]: """The node's APT transactions, newest first, from ``since`` (unix seconds) on. :raises NotImplementedError: before PVE 9.3, which has no history endpoint. :raises RuntimeError: the history could not be read; never an empty list. """ if not self._version_at_least(*PVE_9_3): raise NotImplementedError( f"Proxmox VE on {self._node_name} keeps no APT history its API reports " "(from 9.3 on)" ) params = {} if since is None else {"since": since} try: rows = self._node_api().apt.history.get(**params) or [] except Exception as exc: raise RuntimeError(f"Cannot read the APT history of {self._node_name}: {exc}") from exc return [_transaction(row) for row in rows]