Files
napalm-vmware/napalm_vmware/parse/hosts.py
T
Christian Manivong 06b446934f fix: what bandit and the remaining lint rules found
- zip() in provisioning is strict (B905): the OVF asks for one adapter
  per requested NIC, so the two lists match. A VM that came back with
  fewer adapters used to get a seed that left a NIC unconfigured; it now
  fails, and create_vm_from_cloud_init discards it as on any other error
  after the import. A test covers it.
- A failed abort of the import lease is logged at debug level instead of
  passed over (B110); the original error is still the one raised.
- nosec for false positives, each with its reason: the 0.0.0.0 entries in
  the parsers are addresses vSphere reports, not binds (B104); qemu-img
  runs with a fixed argv and no shell (B404, B603); xml.sax.saxutils only
  escapes the OVF descriptor the driver writes, nothing is parsed (B406),
  so defusedxml would not apply.
2026-10-23 08:38:41 +02:00

41 lines
1.5 KiB
Python

"""The hosts a driver sees, as facts (``HypervisorDriver.get_hypervisor_hosts``).
Whether a host in maintenance mode or one managed by a vCenter is a problem is
decided by the caller, never here.
"""
from __future__ import annotations
from typing import Any
from napalm_device_types.models import HypervisorHostDict
_CONNECTED = "connected"
# Addresses vSphere reports for "no managing vCenter"; data, not a bind.
_UNMANAGED = {None, "", "0.0.0.0"} # nosec B104
def _host(host: dict[str, Any], standalone: bool) -> HypervisorHostDict:
manager = host.get("summary.managementServerIp")
return {
"name": host.get("name", host.get("_moref", "")),
# A disconnected host could not have answered the ESXi driver.
"connected": None
if standalone
else host.get("runtime.connectionState", _CONNECTED) == _CONNECTED,
"maintenance": bool(host.get("runtime.inMaintenanceMode")),
"config_issues": [
i["fullFormattedMessage"]
for i in host.get("configIssue") or []
if i.get("fullFormattedMessage")
],
# Under a vCenter every host names that vCenter; only a host the ESXi
# driver reached directly can be managed by someone else.
"managed_by": manager if standalone and manager not in _UNMANAGED else None,
}
def hypervisor_hosts(hosts: list[dict[str, Any]], *, standalone: bool) -> list[HypervisorHostDict]:
"""``standalone`` is True for the ESXi driver, False for vCenter."""
return [_host(host, standalone) for host in hosts]