From 07fc7d0dd36da97328e0c007eb56cbb1a9595345 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Thu, 26 Nov 2026 17:26:57 +0100 Subject: [PATCH 1/3] feat: guest uid, credential expiry and update history contracts (NetOrk/netork#2728) VMDict.uid is an identity of the guest that survives a migration, a restore and a changed vmid. CredentialExpiryMixin reads when the credential a driver logs in with expires. UpdateMixin.get_update_history lists the package manager's past transactions. resume_vm documents that the guest clock is set after a resume where the platform can. --- CHANGELOG.md | 19 +++++++ napalm_device_types/__init__.py | 15 ++++++ napalm_device_types/credential_expiry.py | 58 ++++++++++++++++++++ napalm_device_types/hypervisor.py | 9 ++++ napalm_device_types/models.py | 36 +++++++++++++ napalm_device_types/updates.py | 45 +++++++++++++++- tests/test_pve93_contracts.py | 68 ++++++++++++++++++++++++ 7 files changed, 249 insertions(+), 1 deletion(-) create mode 100644 napalm_device_types/credential_expiry.py create mode 100644 tests/test_pve93_contracts.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c4af30..ba58f48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,25 @@ recorded here; their history is the git log. ## [Unreleased] +### Added +- `VMDict.uid`, optional: an identity of the guest that survives a migration, a restore + and a changed `vmid` (NetOrk/netork#2728). Proxmox VE 9.3 gives every guest one, VMware's + instance UUID is one; a driver leaves it out where the platform has none, and a caller + then knows the guest by `vmid` alone. +- `CredentialExpiryMixin.get_credential_expiry()`: the credential the connection logged in + with (`kind` `"api-token"` or `"account"`, its `name`) and when it expires, `None` for + never (NetOrk/netork#2728). `PermissionError` where the platform will not tell the + credential its own expiry. +- `UpdateMixin.get_update_history(since)`: the package manager's past transactions, newest + first, each with a stable `id`, start and end, the packages it changed (`action`, old and + new version), who asked for it, the command line and the error it ended with + (`UpdateHistoryDict`, `UpdateHistoryPackageDict`, NetOrk/netork#2728). + +### Changed +- `HypervisorDriver.resume_vm()`: the guest's clock is set to the host's time after it + resumes, where the platform can (NetOrk/netork#2728). A documented expectation, not a + parameter: no caller wants a guest that runs behind. + ## [4.43.0] - 2026-11-26 ### Added diff --git a/napalm_device_types/__init__.py b/napalm_device_types/__init__.py index 9367ae9..6e3bb45 100644 --- a/napalm_device_types/__init__.py +++ b/napalm_device_types/__init__.py @@ -41,6 +41,7 @@ instead of being restated on every role that happens to need it: * :class:`~napalm_device_types.config_revisions.ConfigRevisionsMixin` * :class:`~napalm_device_types.config_sync.ConfigSyncMixin` * :class:`~napalm_device_types.container_engine.ContainerEngineMixin` +* :class:`~napalm_device_types.credential_expiry.CredentialExpiryMixin` * :class:`~napalm_device_types.dhcp.DhcpServerMixin` * :class:`~napalm_device_types.firewall_rules.FirewallRuleMixin` * :class:`~napalm_device_types.health_metrics.HealthMetricsMixin` @@ -143,6 +144,11 @@ from napalm_device_types.container_engine import ( ContainerEngineMixin, ContainerEngineUnavailable, ) +from napalm_device_types.credential_expiry import ( + CredentialExpiryDict, + CredentialExpiryMixin, + CredentialKind, +) from napalm_device_types.dhcp import DhcpApplyError, DhcpServerMixin, normalize_cidr, normalize_mac from napalm_device_types.dns_forward import DnsForwardMixin from napalm_device_types.firewall import FirewallDriver @@ -226,6 +232,9 @@ from napalm_device_types.models import ( PowerAction, PowerStateDict, SnmpAgentSettingsDict, + UpdateHistoryAction, + UpdateHistoryDict, + UpdateHistoryPackageDict, UpdateProgressDict, ) from napalm_device_types.nat_vpn import NatVpnMixin @@ -406,6 +415,12 @@ __all__ = [ "OspfInterfaceDict", "OspfMixin", "UpdateProgressDict", + "UpdateHistoryAction", + "UpdateHistoryDict", + "UpdateHistoryPackageDict", + "CredentialExpiryDict", + "CredentialExpiryMixin", + "CredentialKind", "FirmwareOfferMixin", "FirmwareUpgradeMixin", "LicenseDict", diff --git a/napalm_device_types/credential_expiry.py b/napalm_device_types/credential_expiry.py new file mode 100644 index 0000000..5c49050 --- /dev/null +++ b/napalm_device_types/credential_expiry.py @@ -0,0 +1,58 @@ +"""When the credential a driver logs in with stops working. + +An API token can be given an expiry, and a platform can require one (Proxmox VE +9.3's datacenter ``token-policy``). Once it has passed, every login fails with +an authentication error, and an application that polls the device learns of it +only then. A driver that can read its own credential's expiry says so here, so +the application can warn before, the way it warns of an expiring certificate. + +Which credential is read is the driver's: the one this connection logged in +with, never another. +""" + +from __future__ import annotations + +from typing import TYPE_CHECKING, Literal + +from typing_extensions import TypedDict + +#: ``"api-token"``: a token of an account; ``"account"``: the account a password logs in to. +CredentialKind = Literal["api-token", "account"] + + +class CredentialExpiryDict(TypedDict): + """The credential this connection logged in with, and when it expires.""" + + kind: CredentialKind + #: The credential as the platform names it, e.g. ``"netork@pve!poll"``. + name: str + #: Unix seconds; ``None`` when it never expires. + expires: int | None + + +class CredentialExpiryMixin: + """Reads the expiry of the credential the driver logged in with. + + Declared under ``if TYPE_CHECKING``: a contract, not a placeholder. + """ + + if TYPE_CHECKING: + + def get_credential_expiry(self) -> CredentialExpiryDict: + """ + The credential this connection logged in with, and when it expires. + + ``expires`` is ``None`` for one that never expires. A credential + whose expiry has passed cannot log in, so a driver that returns at + all reports one that still works. + + :raises PermissionError: The platform will not tell this credential + its own expiry (a token whose privileges do not reach it). Not a + fault of the connection; a caller then knows nothing either way. + :raises RuntimeError: The platform could not be read. + + Example:: + + {"kind": "api-token", "name": "netork@pve!poll", "expires": 1767225600} + """ + ... diff --git a/napalm_device_types/hypervisor.py b/napalm_device_types/hypervisor.py index bc66e94..a56d65b 100644 --- a/napalm_device_types/hypervisor.py +++ b/napalm_device_types/hypervisor.py @@ -82,6 +82,10 @@ class HypervisorDriver(PackageManagementMixin, HealthMetricsMixin, DeviceTypeDri * memory_usage (int) - current RAM usage in megabytes * uptime (int) - uptime in seconds (0 if not running) * node (string) - cluster node this VM lives on (empty string for standalone) + * uid (string, optional) - an identity of the guest that survives a + migration, a restore and a changed ``vmid`` (Proxmox VE 9.3's ``uid``, + VMware's instance UUID). Left out where the platform has none, or has + not given this guest one yet; a caller then knows it by ``vmid`` alone. Example:: @@ -272,6 +276,11 @@ class HypervisorDriver(PackageManagementMixin, HealthMetricsMixin, DeviceTypeDri counterpart of :meth:`suspend_vm`. The method blocks until the hypervisor took the request. + The guest's clock is set to the host's time after it resumes, where the + platform can do so (Proxmox VE: through the guest agent): a guest that + stood still for hours would otherwise run behind. No caller wants + otherwise, so it is the driver's to do, not a choice. + Optional, like every power action: a driver whose platform cannot resume a guest does not implement it, and a caller then offers no resume. A driver that can resume some guests but not others (a diff --git a/napalm_device_types/models.py b/napalm_device_types/models.py index 66667ba..6dec022 100644 --- a/napalm_device_types/models.py +++ b/napalm_device_types/models.py @@ -835,6 +835,9 @@ class VMDict(TypedDict): memory_usage: int uptime: int node: str + # An identity of the guest that survives migration, restore and a changed + # ``vmid``; absent where the platform has none or has not assigned it yet. + uid: NotRequired[str] class VMPassthroughDict(TypedDict): @@ -1141,6 +1144,39 @@ class UpdateProgressDict(TypedDict): log: str # the device's own log of it, whole; a caller follows it by line count +UpdateHistoryAction = Literal[ + "install", "reinstall", "upgrade", "downgrade", "remove", "purge", "disappear" +] + + +class UpdateHistoryPackageDict(TypedDict): + """One package a past transaction changed (``UpdateHistoryDict.packages``).""" + + name: str + action: UpdateHistoryAction # "disappear": dpkg dropped it, its files taken by another + old_version: str | None # None where nothing was installed before + new_version: str | None # None where nothing is installed after + + +class UpdateHistoryDict(TypedDict): + """A past run of the host's package manager (``UpdateMixin.get_update_history``). + + A fact of the host's own log: what it changed, when, and who asked, also for + a run that no application of the caller started. + """ + + #: Stable for the same transaction across reads, unique on the host: apt's + #: start time with its index among those starting in the same second, dnf's + #: transaction ID. + id: str + start: int # unix seconds + end: int | None # None: still running, or interrupted + packages: list[UpdateHistoryPackageDict] + requested_by: str | None # the user behind sudo, pkexec or PackageKit; None when unknown + command: str | None # the command line, where the log keeps it + error: str | None # the error the run ended with; None for none + + class DnsForwardDict(TypedDict): """A domain the device's resolver forwards to another server (``DnsForwardMixin.get_dns_forwards``).""" diff --git a/napalm_device_types/updates.py b/napalm_device_types/updates.py index a24b186..9e70cf5 100644 --- a/napalm_device_types/updates.py +++ b/napalm_device_types/updates.py @@ -14,7 +14,12 @@ from __future__ import annotations from typing import TYPE_CHECKING, Any -from napalm_device_types.models import ApplyUpdatesResultDict, UpdateDict, UpdateProgressDict +from napalm_device_types.models import ( + ApplyUpdatesResultDict, + UpdateDict, + UpdateHistoryDict, + UpdateProgressDict, +) class PackageIndexMissing(RuntimeError): @@ -83,6 +88,44 @@ class UpdateMixin: """ ... + def get_update_history(self, since: int | None = None) -> list[UpdateHistoryDict]: + """ + The transactions the host's package manager ran, newest first: what + each changed, when, and who asked for it -- also those no caller of + this driver started (an operator's ``apt upgrade``, unattended-upgrades). + + :param since: Unix seconds; only transactions that started at or after + it. ``None`` for as far back as the host's log reaches. + + **An empty list means no transaction since.** A reader that cannot + read raises, as :meth:`get_available_updates` does: a caller keeps what + it read before, and an empty list for "don't know" would read as a + host that never changed. ``NotImplementedError`` where the package + manager keeps no history. + + Example:: + + [ + { + "id": "1764158400-0", + "start": 1764158400, + "end": 1764158460, + "packages": [ + { + "name": "openssh-server", + "action": "upgrade", + "old_version": "1:9.2p1-2+deb12u1", + "new_version": "1:9.2p1-2+deb12u2", + }, + ], + "requested_by": "admin", + "command": "apt-get -y upgrade", + "error": None, + }, + ] + """ + ... + def refresh_available_updates(self) -> dict[str, Any]: """ Refreshes the host's package index, so that :meth:`get_available_updates` diff --git a/tests/test_pve93_contracts.py b/tests/test_pve93_contracts.py new file mode 100644 index 0000000..97a512b --- /dev/null +++ b/tests/test_pve93_contracts.py @@ -0,0 +1,68 @@ +"""Contracts Proxmox VE 9.3 made worth having, kept neutral (NetOrk/netork#2728). + +A guest identity that survives a changed ID, the expiry of the credential a +driver logs in with, and what a host's package manager did in the past: each a +shape every driver of its kind can fill, with the platform's own calls left to +the driver. +""" + +from __future__ import annotations + +import typing +from typing import get_type_hints + +from napalm_device_types import ( + CredentialExpiryDict, + CredentialExpiryMixin, + UpdateHistoryDict, + UpdateHistoryPackageDict, + UpdateMixin, +) +from napalm_device_types.models import VMDict + + +def test_a_vm_may_carry_an_identity_that_outlives_its_vmid(): + assert "uid" in get_type_hints(VMDict) + assert "uid" in VMDict.__optional_keys__ + + +def test_a_credential_expiry_names_the_credential_and_when_it_ends(): + assert set(get_type_hints(CredentialExpiryDict)) == {"kind", "name", "expires"} + kinds = typing.get_args(get_type_hints(CredentialExpiryDict)["kind"]) + assert set(kinds) == {"api-token", "account"} + + +def test_credential_expiry_is_a_contract_not_a_placeholder(): + assert not hasattr(CredentialExpiryMixin, "get_credential_expiry") + + +def test_an_update_transaction_is_its_packages_and_who_asked_for_it(): + assert set(get_type_hints(UpdateHistoryDict)) == { + "id", + "start", + "end", + "packages", + "requested_by", + "command", + "error", + } + assert set(get_type_hints(UpdateHistoryPackageDict)) == { + "name", + "action", + "old_version", + "new_version", + } + actions = typing.get_args(get_type_hints(UpdateHistoryPackageDict)["action"]) + assert set(actions) == { + "install", + "reinstall", + "upgrade", + "downgrade", + "remove", + "purge", + "disappear", + } + + +def test_update_history_is_declared_next_to_pending_updates(): + assert not hasattr(UpdateMixin, "get_update_history") From 53bc79098b1fcd74f2d03b93726299b41264129c Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Thu, 26 Nov 2026 18:05:09 +0100 Subject: [PATCH 2/3] chore(release): 4.44.0 --- CHANGELOG.md | 2 ++ pyproject.toml | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba58f48..f72d65e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ recorded here; their history is the git log. ## [Unreleased] +## [4.44.0] - 2026-11-26 + ### Added - `VMDict.uid`, optional: an identity of the guest that survives a migration, a restore and a changed `vmid` (NetOrk/netork#2728). Proxmox VE 9.3 gives every guest one, VMware's diff --git a/pyproject.toml b/pyproject.toml index 003d0bd..ba854c9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "napalm-device-types" -version = "4.43.0" +version = "4.44.0" description = "Abstract device-type base classes for NAPALM drivers" readme = "README.md" requires-python = ">=3.12" From d56104680295d06ce77c8c1adca0ce50d5d23fca Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Thu, 26 Nov 2026 18:11:58 +0100 Subject: [PATCH 3/3] refactor: the update history shapes live with UpdateMixin, within the quality ratchet --- napalm_device_types/__init__.py | 11 +++++---- napalm_device_types/hypervisor.py | 15 +++++++----- napalm_device_types/models.py | 40 ++----------------------------- napalm_device_types/updates.py | 37 ++++++++++++++++++++++++++-- 4 files changed, 53 insertions(+), 50 deletions(-) diff --git a/napalm_device_types/__init__.py b/napalm_device_types/__init__.py index 6e3bb45..d54f9a0 100644 --- a/napalm_device_types/__init__.py +++ b/napalm_device_types/__init__.py @@ -232,9 +232,6 @@ from napalm_device_types.models import ( PowerAction, PowerStateDict, SnmpAgentSettingsDict, - UpdateHistoryAction, - UpdateHistoryDict, - UpdateHistoryPackageDict, UpdateProgressDict, ) from napalm_device_types.nat_vpn import NatVpnMixin @@ -370,7 +367,13 @@ from napalm_device_types.time_sync import ( check_ntp_servers, ) from napalm_device_types.time_sync_posix import PosixTimeSyncMixin, parse_time_sync -from napalm_device_types.updates import PackageIndexMissing, UpdateMixin +from napalm_device_types.updates import ( + PackageIndexMissing, + UpdateHistoryAction, + UpdateHistoryDict, + UpdateHistoryPackageDict, + UpdateMixin, +) from napalm_device_types.virtual_ips import ( VIRTUAL_IPS_COMMAND, VirtualIpsMixin, diff --git a/napalm_device_types/hypervisor.py b/napalm_device_types/hypervisor.py index a56d65b..a38fb56 100644 --- a/napalm_device_types/hypervisor.py +++ b/napalm_device_types/hypervisor.py @@ -41,6 +41,13 @@ class HypervisorDriver(PackageManagementMixin, HealthMetricsMixin, DeviceTypeDri Inherits all standard NAPALM NetworkDriver methods and adds hypervisor-specific operations that concrete drivers must implement. + + A guest is known by its ``vmid``, which a platform may give to another guest + once the first is gone, and where it has one also by its ``uid`` + (``VMDict.uid``): an identity that survives a migration, a restore and a + changed ``vmid`` -- Proxmox VE 9.3's ``uid``, VMware's instance UUID. A driver + leaves it out where the platform has none or has not given the guest one yet; + a caller then knows that guest by ``vmid`` alone. """ #: Stable key netOrk exposes as ``device_class``. The order in which a @@ -69,8 +76,7 @@ class HypervisorDriver(PackageManagementMixin, HealthMetricsMixin, DeviceTypeDri Each entry contains: * name (string) - VM display name - * vmid (string) - hypervisor-internal ID (Proxmox: ``"100"``, - VMware: the instance UUID) + * vmid (string) - hypervisor-internal ID (Proxmox: ``"100"``, VMware: the instance UUID) * status (string) - ``"running"``, ``"stopped"``, ``"paused"``, ``"suspended"``. A guest that is not executing is never ``"running"``: ``"paused"`` while its process still holds its memory (Proxmox's ``qmpstatus`` @@ -82,10 +88,7 @@ class HypervisorDriver(PackageManagementMixin, HealthMetricsMixin, DeviceTypeDri * memory_usage (int) - current RAM usage in megabytes * uptime (int) - uptime in seconds (0 if not running) * node (string) - cluster node this VM lives on (empty string for standalone) - * uid (string, optional) - an identity of the guest that survives a - migration, a restore and a changed ``vmid`` (Proxmox VE 9.3's ``uid``, - VMware's instance UUID). Left out where the platform has none, or has - not given this guest one yet; a caller then knows it by ``vmid`` alone. + * uid (string, optional) - outlives the ``vmid``; absent where none (``VMDict.uid``) Example:: diff --git a/napalm_device_types/models.py b/napalm_device_types/models.py index 6dec022..f6b21cc 100644 --- a/napalm_device_types/models.py +++ b/napalm_device_types/models.py @@ -1,5 +1,4 @@ -""" -TypedDicts for NAPALM device-type-specific return values. +"""TypedDicts for NAPALM device-type-specific return values. These supplement the types defined in napalm.base.models and are used by the abstract device-type driver classes in this package. @@ -835,9 +834,7 @@ class VMDict(TypedDict): memory_usage: int uptime: int node: str - # An identity of the guest that survives migration, restore and a changed - # ``vmid``; absent where the platform has none or has not assigned it yet. - uid: NotRequired[str] + uid: NotRequired[str] # outlives a migration, a restore, a changed vmid (get_vms) class VMPassthroughDict(TypedDict): @@ -1144,39 +1141,6 @@ class UpdateProgressDict(TypedDict): log: str # the device's own log of it, whole; a caller follows it by line count -UpdateHistoryAction = Literal[ - "install", "reinstall", "upgrade", "downgrade", "remove", "purge", "disappear" -] - - -class UpdateHistoryPackageDict(TypedDict): - """One package a past transaction changed (``UpdateHistoryDict.packages``).""" - - name: str - action: UpdateHistoryAction # "disappear": dpkg dropped it, its files taken by another - old_version: str | None # None where nothing was installed before - new_version: str | None # None where nothing is installed after - - -class UpdateHistoryDict(TypedDict): - """A past run of the host's package manager (``UpdateMixin.get_update_history``). - - A fact of the host's own log: what it changed, when, and who asked, also for - a run that no application of the caller started. - """ - - #: Stable for the same transaction across reads, unique on the host: apt's - #: start time with its index among those starting in the same second, dnf's - #: transaction ID. - id: str - start: int # unix seconds - end: int | None # None: still running, or interrupted - packages: list[UpdateHistoryPackageDict] - requested_by: str | None # the user behind sudo, pkexec or PackageKit; None when unknown - command: str | None # the command line, where the log keeps it - error: str | None # the error the run ended with; None for none - - class DnsForwardDict(TypedDict): """A domain the device's resolver forwards to another server (``DnsForwardMixin.get_dns_forwards``).""" diff --git a/napalm_device_types/updates.py b/napalm_device_types/updates.py index 9e70cf5..d730b2d 100644 --- a/napalm_device_types/updates.py +++ b/napalm_device_types/updates.py @@ -12,15 +12,48 @@ this class in can never shadow a working implementation from a sibling base. from __future__ import annotations -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING, Any, Literal + +from typing_extensions import TypedDict from napalm_device_types.models import ( ApplyUpdatesResultDict, UpdateDict, - UpdateHistoryDict, UpdateProgressDict, ) +UpdateHistoryAction = Literal[ + "install", "reinstall", "upgrade", "downgrade", "remove", "purge", "disappear" +] + + +class UpdateHistoryPackageDict(TypedDict): + """One package a past transaction changed (``UpdateHistoryDict.packages``).""" + + name: str + action: UpdateHistoryAction # "disappear": dpkg dropped it, its files taken by another + old_version: str | None # None where nothing was installed before + new_version: str | None # None where nothing is installed after + + +class UpdateHistoryDict(TypedDict): + """A past run of the host's package manager (``UpdateMixin.get_update_history``). + + A fact of the host's own log: what it changed, when, and who asked, also for + a run that no application of the caller started. + """ + + #: Stable for the same transaction across reads, unique on the host: apt's + #: start time with its index among those starting in the same second, dnf's + #: transaction ID. + id: str + start: int # unix seconds + end: int | None # None: still running, or interrupted + packages: list[UpdateHistoryPackageDict] + requested_by: str | None # the user behind sudo, pkexec or PackageKit; None when unknown + command: str | None # the command line, where the log keeps it + error: str | None # the error the run ended with; None for none + class PackageIndexMissing(RuntimeError): """The host holds no package index to read pending updates from.