"""Where a new VM goes: a pure decision over plain inventory rows. A host qualifies when it is connected, not in maintenance mode, and sees both a usable datastore and every requested network. Among those, the pair with the most free space wins -- or the named datastore, when one is given; a named host narrows the choice to that host and is refused if it does not qualify. The resource pool comes from the host's compute resource and the VM folder from the datacenter above it; a standalone ESXi host has the same shape (``ha-compute-res`` under ``ha-datacenter``), so one path serves both. """ from __future__ import annotations from dataclasses import dataclass, field from typing import Any Row = dict[str, Any] class NodeRefused(ValueError): """The host named for a new VM does not exist or cannot take it.""" @dataclass class Inventory: hosts: list[Row] datastores: list[Row] networks: list[Row] compute_resources: list[Row] folders: list[Row] = field(default_factory=list) datacenters: list[Row] = field(default_factory=list) @dataclass class Placement: host: str host_name: str datastore: str datastore_name: str resource_pool: str folder: str datacenter_name: str networks: list[str] def _usable_datastores(inv: Inventory, storage: str | None) -> dict[str, Row]: usable = { ds["_moref"]: ds for ds in inv.datastores if (ds.get("summary") or {}).get("accessible") and (ds.get("summary") or {}).get("maintenanceMode", "normal") == "normal" and (storage is None or ds.get("name") == storage) } if storage is not None and not usable: raise ValueError(f"There is no usable datastore named {storage!r}") return usable def _host_networks( host: Row, names_by_moref: dict[str, str], wanted: list[str] ) -> list[str] | None: """The host's network moref for each wanted name, or None if one is missing.""" by_name = {names_by_moref.get(m): m for m in host.get("network") or []} found = [by_name.get(name) for name in wanted] return None if None in found else [m for m in found if m] def _datacenter(inv: Inventory, compute_resource: str) -> Row: parents = {row["_moref"]: row.get("parent") for row in inv.compute_resources + inv.folders} datacenters = {dc["_moref"]: dc for dc in inv.datacenters} node: str | None = compute_resource while node is not None and node not in datacenters: node = parents.get(node) if node is None: raise RuntimeError(f"No datacenter above {compute_resource!r}") return datacenters[node] def _refusal(host: Row) -> str | None: """Why ``host`` cannot take a new VM, or None if it can.""" if host.get("runtime.connectionState") != "connected": return "is not connected" if host.get("runtime.inMaintenanceMode"): return "is in maintenance mode" return None def _candidates(inv: Inventory, host_name: str | None) -> list[Row]: """The hosts placement may choose from: every eligible one, or the named one.""" hosts = sorted(inv.hosts, key=lambda h: h.get("name", "")) if host_name is None: return [h for h in hosts if _refusal(h) is None] named = [h for h in hosts if h.get("name") == host_name] if not named: raise NodeRefused(f"There is no host named {host_name!r}") reason = _refusal(named[0]) if reason: raise NodeRefused(f"Host {host_name} {reason}") return named[:1] def _best( hosts: list[Row], usable: dict[str, Row], names: dict[str, str], networks: list[str] ) -> tuple[Row, Row, list[str]] | None: """The host/datastore pair with the most free space, and the host's networks.""" best: tuple[int, Row, Row, list[str]] | None = None for host in hosts: host_nets = _host_networks(host, names, networks) if host_nets is None: continue for ds_ref in host.get("datastore") or []: ds = usable.get(ds_ref) free = int((ds or {}).get("summary", {}).get("freeSpace", 0)) if ds is not None and (best is None or free > best[0]): best = (free, host, ds, host_nets) return best[1:] if best else None def choose_placement( inv: Inventory, storage: str | None, networks: list[str], *, host_name: str | None = None ) -> Placement: """Where a VM on ``storage`` with ``networks`` goes; ``host_name`` pins the host. Raises NodeRefused (a ValueError) when the named host does not exist or cannot take the VM, and ValueError when no host can. """ usable = _usable_datastores(inv, storage) names = {n["_moref"]: n.get("name", "") for n in inv.networks} best = _best(_candidates(inv, host_name), usable, names, networks) wanted = f"datastore {storage or '(any)'} and networks {networks}" if best is None and host_name is not None: raise NodeRefused(f"Host {host_name} does not see {wanted}") if best is None: raise ValueError(f"There is no host that sees {wanted}") host, ds, host_nets = best compute = next(c for c in inv.compute_resources if c["_moref"] == host.get("parent")) dc = _datacenter(inv, compute["_moref"]) return Placement( host=host["_moref"], host_name=host.get("name", ""), datastore=ds["_moref"], datastore_name=ds.get("name", ""), resource_pool=compute["resourcePool"], folder=dc["vmFolder"], datacenter_name=dc.get("name", ""), networks=host_nets, )