"""``get_clusters``: clusters, their hosts, and the datastores and port groups they see. A vCenter runs two kinds of compute resource: a ``ClusterComputeResource``, and a plain ``ComputeResource`` for a host added to a datacenter directly. Each is one cluster here; the plain one has no name, since the contract keeps names for clusters an operator made. An ESXi host is a cluster of one. A cluster's datastores and port groups are the ones its hosts list, each with the hosts that list it. vSphere has no cluster quorum and no cpuinfo flags, so ``quorate`` and ``cpu_flags`` are always None. """ from __future__ import annotations from dataclasses import dataclass from typing import Any from napalm_device_types import ( ClusterDict, ClusterNetworkDict, ClusterNodeDict, ClusterStorageDict, ) from napalm_vmware.parse.networks import dv_vlan, is_uplink Row = dict[str, Any] _MB = 1024 * 1024 _STATUS = {"connected": "online", "disconnected": "offline", "notResponding": "offline"} #: What a datastore can hold for netOrk: VM disks and the cloud-init seed ISO. _CONTENT = ["images", "iso"] #: A standard port group's "VLAN" 4095 trunks every VLAN to the guest. _TRUNK = 4095 @dataclass class ClusterSources: """The inventory rows every cluster draws from.""" hosts: list[Row] datastores: list[Row] networks: list[Row] # ``Network`` objects, distributed port groups included dv_portgroups: list[Row] def manager_clusters( compute_resources: list[Row], cluster_refs: set[str], src: ClusterSources, *, instance_uuid: str | None, ) -> list[ClusterDict]: """vCenter: one cluster per compute resource; ``cluster_refs`` are the real clusters. The id is ``/``: a MoRef alone repeats across vCenters. """ hosts = {h["_moref"]: h for h in src.hosts} clusters = [] for cr in compute_resources: members = [hosts[ref] for ref in cr.get("host") or [] if ref in hosts] name = cr.get("name") if cr["_moref"] in cluster_refs else None ident = f"{instance_uuid}/{cr['_moref']}" if instance_uuid else cr["_moref"] clusters.append(_cluster(ident, name, members, src, local=False)) return sorted(clusters, key=lambda c: (c["name"] is None, c["name"] or _first_node(c))) def host_cluster(src: ClusterSources) -> ClusterDict: """ESXi: the host itself, a cluster of one with no name.""" if not src.hosts: raise RuntimeError("The inventory shows no host") host = src.hosts[0] ident = (host.get("hardware.systemInfo") or {}).get("uuid") return _cluster(ident, None, [host], src, local=True) def _first_node(cluster: ClusterDict) -> str: return cluster["nodes"][0]["name"] if cluster["nodes"] else "" def _cluster( ident: str | None, name: str | None, hosts: list[Row], src: ClusterSources, *, local: bool ) -> ClusterDict: hosts = sorted(hosts, key=_host_name) return { "id": ident, "name": name, "quorate": None, "nodes": [_node(h, local) for h in hosts], "storages": _storages(hosts, src.datastores), "networks": _networks(hosts, src), } def _host_name(host: Row) -> str: return str(host.get("name") or host["_moref"]) # -- nodes --------------------------------------------------------------------- def _node(host: Row, local: bool) -> ClusterNodeDict: status = _STATUS.get(host.get("runtime.connectionState") or "", "unknown") hardware: Row = {} stats: Row = {} # A host vCenter cannot reach reports what it last knew, if anything. if status != "offline": hardware = host.get("summary.hardware") or {} stats = host.get("summary.quickStats") or {} memory_used = stats.get("overallMemoryUsage") return { "name": _host_name(host), "status": status, "local": local, "maintenance": host.get("runtime.inMaintenanceMode"), "cpu_threads": hardware.get("numCpuThreads"), "cpu_cores": hardware.get("numCpuCores"), "cpu_model": hardware.get("cpuModel"), "cpu_flags": None, "cpu_usage": _cpu_usage(hardware, stats), "memory_total": hardware.get("memorySize"), "memory_used": memory_used * _MB if memory_used is not None else None, } def _cpu_usage(hardware: Row, stats: Row) -> float | None: """Used MHz over the host's total MHz (``cpuMhz`` per core).""" used = stats.get("overallCpuUsage") total = (hardware.get("cpuMhz") or 0) * (hardware.get("numCpuCores") or 0) if used is None or not total: return None return min(1.0, float(used) / total) # -- storages ------------------------------------------------------------------ def _mounts(hosts: list[Row], key: str) -> dict[str, list[str]]: """MoRef -> names of the hosts whose ``key`` list holds it, in host order.""" seen: dict[str, list[str]] = {} for host in hosts: for ref in host.get(key) or []: seen.setdefault(ref, []).append(_host_name(host)) return seen def _storages(hosts: list[Row], datastores: list[Row]) -> list[ClusterStorageDict]: mounts = _mounts(hosts, "datastore") result: list[ClusterStorageDict] = [] for ds in datastores: summary = ds.get("summary") or {} if ds["_moref"] not in mounts or not summary.get("accessible"): continue total = summary.get("capacity") free = summary.get("freeSpace") result.append( { "name": ds.get("name") or summary.get("name") or ds["_moref"], "type": str(summary.get("type", "")).lower(), "shared": bool(summary.get("multipleHostAccess")), "content": list(_CONTENT), "nodes": mounts[ds["_moref"]], "total": total, "used": total - free if total is not None and free is not None else None, } ) return sorted(result, key=lambda s: s["name"]) # -- networks ------------------------------------------------------------------ def _networks(hosts: list[Row], src: ClusterSources) -> list[ClusterNetworkDict]: return [ { "name": name, "kind": "portgroup", # The port group fixes the VLAN; a NIC cannot add a tag of its own. "vlan_aware": False, "fixed_vlan_tag": _fixed_tag(vlans), "nodes": nodes, } for name, (nodes, vlans) in sorted(_seen_networks(hosts, src).items()) ] def _seen_networks( hosts: list[Row], src: ClusterSources ) -> dict[str, tuple[list[str], set[int | None]]]: """Port group name -> (the hosts that have it, the VLAN it carries on each).""" names = {n["_moref"]: n.get("name") or n["_moref"] for n in src.networks} dv = {pg["_moref"]: pg for pg in src.dv_portgroups} seen: dict[str, tuple[list[str], set[int | None]]] = {} for host in hosts: for ref in _vm_networks(host, dv): name = names.get(ref, ref) nodes, vlans = seen.setdefault(name, ([], set())) if _host_name(host) not in nodes: nodes.append(_host_name(host)) vlans.add(_vlan(ref, name, host, dv)) return seen def _vm_networks(host: Row, dv: dict[str, Row]) -> list[str]: """The host's networks a VM can use: a vDS's uplink port group carries none.""" return [ref for ref in host.get("network") or [] if not (ref in dv and is_uplink(dv[ref]))] def _vlan(ref: str, name: str, host: Row, dv: dict[str, Row]) -> int | None: """The VLAN ``ref`` carries on ``host``; None where it cannot be read.""" if ref in dv: return dv_vlan(dv[ref].get("config.defaultPortConfig")) for pg in host.get("config.network.portgroup") or []: spec = pg.get("spec") or {} if spec.get("name") == name: return int(spec.get("vlanId", 0)) return None def _fixed_tag(vlans: set[int | None]) -> int | None: """One VLAN on every host, and a single tag at that; else None.""" if len(vlans) != 1: return None (vlan,) = vlans return vlan if vlan not in (None, 0, _TRUNK) else None