From d56104680295d06ce77c8c1adca0ce50d5d23fca Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Thu, 26 Nov 2026 18:11:58 +0100 Subject: [PATCH] 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.