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
@@ -1,6 +1,7 @@
|
||||
"""Import virtual machines, interfaces, IP addresses and prefixes into NetBox."""
|
||||
|
||||
import ipaddress
|
||||
import re
|
||||
|
||||
from netbox_sync.core.netbox import assign_mac, related_id, related_name
|
||||
from netbox_sync.core.virtualization import ip_with_prefix, valid_guest_ip
|
||||
@@ -22,6 +23,21 @@ def suggested_prefixlen(interface):
|
||||
return 24 if interface.version == 4 else interface.network.prefixlen
|
||||
|
||||
|
||||
def cluster_key(vm):
|
||||
"""Identifies the hypervisor cluster of a VM across sources."""
|
||||
return f"{vm.get('source_id', vm.get('source', ''))}:{vm.get('cluster', '')}"
|
||||
|
||||
|
||||
def _compact(name):
|
||||
return re.sub(r"[^0-9a-z]", "", (name or "").casefold())
|
||||
|
||||
|
||||
CLUSTER_TYPES = {
|
||||
"VMware": ("VMware vSphere", "vmware-vsphere"),
|
||||
"Proxmox": ("Proxmox VE", "proxmox-ve"),
|
||||
}
|
||||
|
||||
|
||||
def prefix_choices(host_address, detected_prefixlen):
|
||||
host = ipaddress.ip_address(host_address)
|
||||
if host.version == 4:
|
||||
@@ -107,6 +123,48 @@ class VMSyncEngine:
|
||||
self.log(f"VRF erstellt: {vrf_name}", "success")
|
||||
return vrf.id
|
||||
|
||||
def match_cluster(self, source_cluster, remembered=None):
|
||||
"""Find the NetBox cluster for a hypervisor cluster.
|
||||
|
||||
Returns ``(netbox_cluster_name, how)`` where ``how`` is ``remembered``,
|
||||
``name`` or ``similar``; ``(None, None)`` if nothing matches uniquely.
|
||||
"""
|
||||
if remembered and remembered in self.clusters:
|
||||
return remembered, "remembered"
|
||||
if not source_cluster:
|
||||
return None, None
|
||||
wanted = source_cluster.casefold()
|
||||
exact = [name for name in self.clusters if name.casefold() == wanted]
|
||||
if len(exact) == 1:
|
||||
return exact[0], "name"
|
||||
compact = _compact(source_cluster)
|
||||
same = [name for name in self.clusters if _compact(name) == compact]
|
||||
if len(same) == 1:
|
||||
return same[0], "name"
|
||||
if len(compact) >= 3:
|
||||
similar = [name for name in self.clusters
|
||||
if compact in _compact(name) or (len(_compact(name)) >= 3 and _compact(name) in compact)]
|
||||
if len(similar) == 1:
|
||||
return similar[0], "similar"
|
||||
return None, None
|
||||
|
||||
def create_cluster(self, name, source_type, tenant_id=None):
|
||||
"""Create a NetBox cluster (and its cluster type if necessary)."""
|
||||
if name in self.clusters:
|
||||
return self.clusters[name]
|
||||
type_name, slug = CLUSTER_TYPES.get(source_type, (f"{source_type}", _compact(source_type) or "virtualization"))
|
||||
cluster_type = self.nb.virtualization.cluster_types.get(slug=slug)
|
||||
if not cluster_type:
|
||||
cluster_type = self.nb.virtualization.cluster_types.create({"name": type_name, "slug": slug})
|
||||
self.log(f"Cluster-Typ erstellt: {type_name}", "success")
|
||||
payload = {"name": name, "type": cluster_type.id, "status": "active"}
|
||||
if tenant_id:
|
||||
payload["tenant"] = tenant_id
|
||||
cluster = self.nb.virtualization.clusters.create(payload)
|
||||
self.clusters[name] = cluster.id
|
||||
self.log(f"Cluster erstellt: {name}", "success")
|
||||
return cluster.id
|
||||
|
||||
def get_platform_id(self, guest_os):
|
||||
guest_os = (guest_os or "").lower()
|
||||
if not guest_os:
|
||||
|
||||
Reference in new issue
Block a user