From 5b894913e1eaa6d0cf2d38081a8bb70c7c450175 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sun, 1 Nov 2026 17:08:10 +0100 Subject: [PATCH] refactor: the cluster shapes in clusters.py, a shorter provisioning docstring models.py is over the file size limit and may not grow, and the declaration of create_vm_from_cloud_init() may not get longer: the four cluster shapes move to napalm_device_types/clusters.py (exported from the package as before), and the docstring says the same in fewer lines. Refs NetOrk/netork#484 --- napalm_device_types/__init__.py | 10 +++--- napalm_device_types/clusters.py | 57 +++++++++++++++++++++++++++++++ napalm_device_types/hypervisor.py | 22 ++++-------- napalm_device_types/models.py | 51 --------------------------- 4 files changed, 70 insertions(+), 70 deletions(-) create mode 100644 napalm_device_types/clusters.py diff --git a/napalm_device_types/__init__.py b/napalm_device_types/__init__.py index f455983..743e0d5 100644 --- a/napalm_device_types/__init__.py +++ b/napalm_device_types/__init__.py @@ -87,6 +87,12 @@ from napalm_device_types.channel import ( open_stream_on_transport, run_on_transport, ) +from napalm_device_types.clusters import ( + ClusterDict, + ClusterNetworkDict, + ClusterNodeDict, + ClusterStorageDict, +) from napalm_device_types.config_lifecycle import ConfigLifecycleMixin from napalm_device_types.config_sync import ConfigSyncMixin from napalm_device_types.container_engine import ( @@ -135,10 +141,6 @@ from napalm_device_types.management_controller import ManagementControllerDriver from napalm_device_types.media import MediaDriver from napalm_device_types.models import ( POWER_ACTIONS, - ClusterDict, - ClusterNetworkDict, - ClusterNodeDict, - ClusterStorageDict, ConfigSyncDict, DhcpHaDict, DhcpHaPeerDict, diff --git a/napalm_device_types/clusters.py b/napalm_device_types/clusters.py new file mode 100644 index 0000000..c5e136e --- /dev/null +++ b/napalm_device_types/clusters.py @@ -0,0 +1,57 @@ +"""The shapes of a hypervisor's clusters (``HypervisorDriver.get_clusters()``). + +Apart from ``models.py``, which is over the file size limit. +""" + +from typing import NotRequired, TypedDict + + +class ClusterNodeDict(TypedDict): + """One host of a cluster, as its cluster sees it. A value the platform does not + report is None; it is never guessed.""" + + name: str # the node's name in its cluster, as get_vms() reports it in ``node`` + status: str # "online", "offline" or "unknown" + local: bool # the node this connection talks to; False on a manager (vCenter) + maintenance: bool | None # in maintenance mode + cpu_threads: int | None # logical CPUs + cpu_cores: int | None # physical cores + cpu_model: str | None + cpu_flags: list[str] | None # cpuinfo flags; None where the platform does not say + cpu_usage: float | None # 0.0–1.0 + memory_total: int | None # bytes + memory_used: int | None # bytes + + +class ClusterStorageDict(TypedDict): + """A storage of a cluster and the nodes that see it.""" + + name: str # usable as create_vm_from_cloud_init(storage=...) on each of ``nodes`` + type: str # backend type: "dir", "lvmthin", "zfspool", "nfs", "rbd", "vmfs", ... + shared: bool # the same storage on every node in ``nodes``, so a VM on it can move + content: list[str] # what it holds: "images", "iso", "snippets", ... + nodes: list[str] # the nodes it is available on + total: int | None # bytes; None where the platform does not report it + used: int | None # bytes + + +class ClusterNetworkDict(TypedDict): + """A network a VM's NIC may attach to, and the nodes that carry it.""" + + name: str # usable as NICConfigDict.bridge on each of ``nodes`` + kind: str # "bridge", "vnet" or "portgroup", as in NetworkTargetDict + vlan_aware: bool # True if a NICConfigDict.vlan_tag may be set on top of it + fixed_vlan_tag: NotRequired[int | None] # vnet/portgroup: the VLAN baked into it + nodes: list[str] + + +class ClusterDict(TypedDict): + """A cluster the connection can see. A standalone host is a cluster of one, + with no name.""" + + id: str | None # stable across connections where the platform has one; None otherwise + name: str | None # the cluster's name; None for a standalone host + quorate: bool | None # None where the platform has no quorum (standalone, vSphere) + nodes: list[ClusterNodeDict] + storages: list[ClusterStorageDict] + networks: list[ClusterNetworkDict] diff --git a/napalm_device_types/hypervisor.py b/napalm_device_types/hypervisor.py index 655a709..3618c22 100644 --- a/napalm_device_types/hypervisor.py +++ b/napalm_device_types/hypervisor.py @@ -13,9 +13,9 @@ Usage:: from typing import TYPE_CHECKING, Any from napalm_device_types.base import DeviceTypeDriver +from napalm_device_types.clusters import ClusterDict from napalm_device_types.health_metrics import HealthMetricsMixin from napalm_device_types.models import ( - ClusterDict, HypervisorHostDict, NetworkTargetDict, NICConfigDict, @@ -478,9 +478,7 @@ class HypervisorDriver(PackageManagementMixin, HealthMetricsMixin, DeviceTypeDri configuration via a storage snippet or similar mechanism. The resulting VM is left in a running state. - Implementations should cache downloaded images by URL/filename on the - hypervisor so repeated provisioning from the same image does not - re-download it every time. + Implementations cache downloaded images on the hypervisor by URL/filename. Args: name (string) - new VM display name @@ -499,10 +497,8 @@ class HypervisorDriver(PackageManagementMixin, HealthMetricsMixin, DeviceTypeDri image_checksum (string | None) - expected checksum of the downloaded image (e.g. "sha256:"). If given, verified after download; mismatch raises RuntimeError. If None, no verification is performed. - ssh_public_keys (list[str] | None) - SSH public keys to inject into guest. - If None or empty, no SSH key injection is performed. - disk_resize_gb (int | None) - resize root disk to this size in GB. - If None, disk remains the downloaded image's native size. Default None. + ssh_public_keys (list[str] | None) - SSH public keys to inject (None: none). + disk_resize_gb (int | None) - resize root disk to this size in GB (None: keep). storage (string | None) - name of the storage pool to place the root disk on (a name returned by ``get_image_storages()``). If None, the driver auto-detects the first enabled, node-available storage @@ -521,13 +517,9 @@ class HypervisorDriver(PackageManagementMixin, HealthMetricsMixin, DeviceTypeDri network-config itself (``provisioning.network_config``). A guest_os the driver does not list raises ValueError before anything is created. Default ``"linux"``. - node (string | None) - the cluster node to create the VM on, a - node ``name`` from ``get_clusters()``. None leaves it to the - driver: the node this connection talks to, or, on a manager of - several hosts, a host of the driver's choosing. A driver that - can only create on its own node raises ValueError for any other - name, before creating anything. ``storage`` and the NICs' - bridges must then be ones that node sees. + node (string | None) - a node ``name`` from ``get_clusters()`` to create + the VM on; None leaves it to the driver. One that only creates on its + own node raises ValueError for another, before creating anything. download_timeout (int) - maximum seconds to wait for the image download (skipped entirely if already cached on the hypervisor). Default 300. timeout (int) - maximum seconds to wait for the remaining provisioning diff --git a/napalm_device_types/models.py b/napalm_device_types/models.py index 3819fa1..112a57c 100644 --- a/napalm_device_types/models.py +++ b/napalm_device_types/models.py @@ -1195,57 +1195,6 @@ class HypervisorHostDict(TypedDict): managed_by: NotRequired[str | None] # the address of a central manager, if any -class ClusterNodeDict(TypedDict): - """One host of a cluster, as its cluster sees it. A value the platform does not - report is None; it is never guessed.""" - - name: str # the node's name in its cluster, as get_vms() reports it in ``node`` - status: str # "online", "offline" or "unknown" - local: bool # the node this connection talks to; False on a manager (vCenter) - maintenance: bool | None # in maintenance mode - cpu_threads: int | None # logical CPUs - cpu_cores: int | None # physical cores - cpu_model: str | None - cpu_flags: list[str] | None # cpuinfo flags; None where the platform does not say - cpu_usage: float | None # 0.0–1.0 - memory_total: int | None # bytes - memory_used: int | None # bytes - - -class ClusterStorageDict(TypedDict): - """A storage of a cluster and the nodes that see it.""" - - name: str # usable as create_vm_from_cloud_init(storage=...) on each of ``nodes`` - type: str # backend type: "dir", "lvmthin", "zfspool", "nfs", "rbd", "vmfs", ... - shared: bool # the same storage on every node in ``nodes``, so a VM on it can move - content: list[str] # what it holds: "images", "iso", "snippets", ... - nodes: list[str] # the nodes it is available on - total: int | None # bytes; None where the platform does not report it - used: int | None # bytes - - -class ClusterNetworkDict(TypedDict): - """A network a VM's NIC may attach to, and the nodes that carry it.""" - - name: str # usable as NICConfigDict.bridge on each of ``nodes`` - kind: str # "bridge", "vnet" or "portgroup", as in NetworkTargetDict - vlan_aware: bool # True if a NICConfigDict.vlan_tag may be set on top of it - fixed_vlan_tag: NotRequired[int | None] # vnet/portgroup: the VLAN baked into it - nodes: list[str] - - -class ClusterDict(TypedDict): - """A cluster the connection can see. A standalone host is a cluster of one, - with no name.""" - - id: str | None # stable across connections where the platform has one; None otherwise - name: str | None # the cluster's name; None for a standalone host - quorate: bool | None # None where the platform has no quorum (standalone, vSphere) - nodes: list[ClusterNodeDict] - storages: list[ClusterStorageDict] - networks: list[ClusterNetworkDict] - - class FixStepOutcomeDict(TypedDict): """What one step of a fix routine reports (see ``napalm_device_types.fixes``)."""