feat: map hypervisor clusters to NetBox clusters automatically
- Read the vCenter/ESXi compute resource and the Proxmox cluster name (node name when standalone) for every VM - Match each hypervisor cluster to a NetBox cluster by name, normalized name or unique similarity; remember manual choices per profile and offer to create missing clusters (with cluster type) - Import wizard: per-cluster mapping table instead of a single cluster, tenant prefilled from the mapped clusters, review shows the mapping - Show the hypervisor cluster in the VM table fix: graphic glitches - Empty state text no longer clipped by the buttons - Combo boxes open a plain drop-down list instead of the overlay popup - Status bar badges keep their height and no longer get cut off Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
4878275f2a
commit
c3b5fb547e
12 files changed
+300
-50
No files matched your search
@@ -69,7 +69,7 @@ def load_vmware_source(source, log=_noop):
|
||||
type=vim.VirtualMachine, all=False, pathSet=[
|
||||
"name", "config.hardware.memoryMB", "config.hardware.numCPU",
|
||||
"config.hardware.device", "config.guestFullName", "config.template",
|
||||
"runtime.powerState", "guest.net",
|
||||
"runtime.powerState", "runtime.host", "guest.net",
|
||||
],
|
||||
)
|
||||
filter_spec = vim.PropertyCollector.FilterSpec(objectSet=[object_spec], propSet=[property_spec])
|
||||
@@ -80,11 +80,15 @@ def load_vmware_source(source, log=_noop):
|
||||
while result and result.token:
|
||||
result = content.propertyCollector.ContinueRetrievePropertiesEx(result.token)
|
||||
objects.extend(result.objects or [])
|
||||
host_clusters = _vmware_host_clusters(content, vim)
|
||||
normalized = []
|
||||
for item in objects:
|
||||
properties = {prop.name: prop.val for prop in (item.propSet or [])}
|
||||
if properties.get("name") and not properties.get("config.template"):
|
||||
normalized.append(_normalize_vmware_vm(properties, source, vim))
|
||||
vm = _normalize_vmware_vm(properties, source, vim)
|
||||
host = properties.get("runtime.host")
|
||||
vm["cluster"] = host_clusters.get(getattr(host, "_moId", None), "")
|
||||
normalized.append(vm)
|
||||
return normalized
|
||||
finally:
|
||||
view.Destroy()
|
||||
@@ -92,6 +96,20 @@ def load_vmware_source(source, log=_noop):
|
||||
Disconnect(service_instance)
|
||||
|
||||
|
||||
def _vmware_host_clusters(content, vim):
|
||||
"""Map ESXi host IDs to their cluster name (or the host name when standalone)."""
|
||||
result = {}
|
||||
view = content.viewManager.CreateContainerView(content.rootFolder, [vim.ComputeResource], True)
|
||||
try:
|
||||
for resource in view.view:
|
||||
name = resource.name
|
||||
for host in resource.host or []:
|
||||
result[host._moId] = name
|
||||
finally:
|
||||
view.Destroy()
|
||||
return result
|
||||
|
||||
|
||||
def _normalize_vmware_vm(properties, source, vim):
|
||||
disks = sum(
|
||||
round(device.capacityInKB / 1024)
|
||||
@@ -176,6 +194,7 @@ def load_proxmox_source(source, log=_noop):
|
||||
resources = _proxmox_resources_by_node(session, source)
|
||||
log(f"{source['name']}: Node-Abfrage meldet {len(resources)} Ressourcen")
|
||||
|
||||
cluster_name = _proxmox_cluster_name(session, source)
|
||||
result = []
|
||||
for resource in resources:
|
||||
kind = resource.get("type")
|
||||
@@ -184,7 +203,9 @@ def load_proxmox_source(source, log=_noop):
|
||||
if kind == "lxc" and not source.get("include_lxc", True):
|
||||
continue
|
||||
try:
|
||||
result.append(_normalize_proxmox_guest(session, source, resource))
|
||||
guest = _normalize_proxmox_guest(session, source, resource)
|
||||
guest["cluster"] = cluster_name or resource.get("node", "")
|
||||
result.append(guest)
|
||||
except Exception as error: # noqa: BLE001 - one broken guest must not stop the source
|
||||
log(f"{source['name']} / VMID {resource.get('vmid')}: {error}", "warning")
|
||||
if not result:
|
||||
@@ -195,6 +216,15 @@ def load_proxmox_source(source, log=_noop):
|
||||
return result
|
||||
|
||||
|
||||
def _proxmox_cluster_name(session, source):
|
||||
"""Name of the Proxmox cluster, or ``""`` for a standalone node (the node name is used then)."""
|
||||
try:
|
||||
status = _proxmox_get(session, source["url"], "/cluster/status") or []
|
||||
except (requests.RequestException, PermissionError, ValueError):
|
||||
return ""
|
||||
return next((item.get("name", "") for item in status if item.get("type") == "cluster"), "")
|
||||
|
||||
|
||||
def _proxmox_resources_by_node(session, source):
|
||||
resources = []
|
||||
kinds = ["qemu"] + (["lxc"] if source.get("include_lxc", True) else [])
|
||||
|
||||
Reference in new issue
Block a user