feat: VLAN labels, automatic port mapping and neighbour review for switches

- Read VLAN membership (untagged and tagged) from the running configuration
  on Aruba CX, ArubaOS-Switch and HPE Comware and write it to the NetBox
  interface label (e.g. "10", "1,20-22", "all")
- Keep only VLAN-related configuration lines in the raw data / offline export
- Map switch ports to NetBox interfaces with differing names automatically:
  equal name, equal port number (1/1/5 <-> 5), then natural order
- New "Review mapping" dialog before the preview: correct port mappings and
  assign each LLDP/CDP neighbour to a device of the switch's tenant group;
  choices are stored per switch in the profile
- Ignore the local Gitea token file

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
MrBlakeandClaude Opus 5.5 committed 2026-10-30 13:16:22 +01:00
1 parent f249c3e1c9
commit 39a29b9230
8 files changed
+753 -32

No files matched your search

+40 -4
View File
@@ -17,6 +17,7 @@ from netbox_sync.core.switches import (
PLATFORMS,
describe_snapshot,
discover_switch,
plan_switch_sync,
suggest_platform,
sync_snapshot_to_netbox,
)
@@ -24,7 +25,7 @@ from netbox_sync.core.virtualization import load_source
from netbox_sync.core.vm_sync import VMSyncEngine
from netbox_sync.ui import widgets
from netbox_sync.ui.client_dialogs import ClientPreviewDialog, ClientTargetDialog
from netbox_sync.ui.dialogs import SyncPreviewDialog
from netbox_sync.ui.dialogs import SwitchMappingDialog, SyncPreviewDialog
from netbox_sync.ui.tasks import area_log, logger
@@ -412,8 +413,44 @@ class Actions:
connect = self._connector()
log = area_log("switch")
def preview(ctx):
def plan(ctx):
nb = connect(ctx)
plans = []
for index, (switch, snapshot) in enumerate(items):
ctx.progress(index, len(items), switch["name"])
ctx.log(f"{switch['name']}: ordne Schnittstellen und Nachbarn zu …")
plans.append((switch, plan_switch_sync(nb, switch, snapshot)))
return nb, plans
def review(result):
nb, plans = result
self.state.set_netbox(nb, "ok", f"Verbunden · NetBox {nb.netbox_version}")
reviewable = [(switch, item) for switch, item in plans if not item["error"]]
needs_review = any(
any(entry["method"] not in ("name", "new") or (entry["method"] == "new" and entry["physical"])
for entry in item["interfaces"]) or (create_cables and item["neighbors"])
for _switch, item in reviewable)
def ask():
# Started after the planning task has finished, so the next
# task is not rejected as "still busy".
if needs_review:
dialog = SwitchMappingDialog(self.window, reviewable, create_cables)
if not dialog.exec():
return
stored = {switch["id"]: switch for switch in self.state.switches}
for switch, _snapshot in items:
choices = dialog.result.get(switch["id"])
if choices:
switch.update(choices)
if switch["id"] in stored:
stored[switch["id"]].update(choices)
self.state.touch()
self.tasks.run(lambda ctx: preview(ctx, nb), title="Änderungen berechnen", area="switch",
on_success=show_preview, on_error=self._netbox_failed)
QTimer.singleShot(0, ask)
def preview(ctx, nb):
reports = []
for index, (switch, snapshot) in enumerate(items):
ctx.progress(index, len(items), switch["name"])
@@ -423,7 +460,6 @@ class Actions:
def show_preview(result):
nb, reports = result
self.state.set_netbox(nb, "ok", f"Verbunden · NetBox {nb.netbox_version}")
def ask():
# Opened after the preview task has fully finished, so the
@@ -433,7 +469,7 @@ class Actions:
self._apply_switch_sync(nb, items, create_cables, log)
QTimer.singleShot(0, ask)
self.tasks.run(preview, title="Änderungen berechnen", area="switch", on_success=show_preview,
self.tasks.run(plan, title="Zuordnung ermitteln", area="switch", on_success=review,
on_error=self._netbox_failed)
def _apply_switch_sync(self, nb, items, create_cables, log):
+144 -3
View File
@@ -3,7 +3,7 @@
import uuid
from PySide6.QtCore import QThreadPool, Qt
from PySide6.QtGui import QGuiApplication
from PySide6.QtGui import QColor, QGuiApplication
from PySide6.QtWidgets import (
QButtonGroup,
QComboBox,
@@ -16,6 +16,8 @@ from PySide6.QtWidgets import (
QPlainTextEdit,
QPushButton,
QSpinBox,
QTableWidget,
QTableWidgetItem,
QTabWidget,
QTreeWidget,
QTreeWidgetItem,
@@ -24,7 +26,7 @@ from PySide6.QtWidgets import (
)
from netbox_sync.core.config import known_hosts_path
from netbox_sync.core.switches import PLATFORMS, forget_host_key
from netbox_sync.core.switches import MATCH_METHODS, PLATFORMS, forget_host_key
from netbox_sync.core.virtualization import test_source
from netbox_sync.ui.icons import icon
from netbox_sync.ui.models import Column, FilterProxy, RowTableModel
@@ -566,10 +568,149 @@ class SyncPreviewDialog(_Dialog):
self.root.addWidget(buttons)
class SwitchMappingDialog(_Dialog):
"""Review interface mapping and neighbor devices before computing changes.
``plans`` is a list of ``(switch, plan)`` from ``plan_switch_sync``. After
acceptance ``result`` maps switch IDs to ``interface_map`` and
``neighbor_devices`` to be stored in the profile.
"""
NEW = "\x00new"
def __init__(self, parent, plans, create_cables):
super().__init__(parent, "Zuordnung prüfen",
"Schnittstellen, deren Name auf dem Switch anders lautet als in NetBox, wurden automatisch "
"zugeordnet. Bitte prüfen und bei Bedarf korrigieren – die Auswahl wird im Profil gespeichert.",
width=820)
self.resize(980, 660)
self.plans = plans
self.result = None
tabs = QTabWidget()
# Interfaces --------------------------------------------------------
interface_page = QWidget()
interface_layout = QVBoxLayout(interface_page)
interface_layout.setContentsMargins(0, 12, 0, 0)
self.only_unclear = checkbox("Nur Schnittstellen ohne Namensgleichheit anzeigen", True)
interface_layout.addWidget(self.only_unclear)
self.interface_table = QTableWidget(0, 5)
self.interface_table.setHorizontalHeaderLabels(["Switch", "Schnittstelle (Switch)", "VLANs",
"NetBox-Schnittstelle", "Zuordnung"])
self.interface_rows = []
for switch, plan in plans:
vlans = {item["name"]: item["vlan_label"] for item in plan["interfaces"]}
choices = [("+ neu anlegen", self.NEW)] + [(name, name) for name in plan["netbox_interfaces"]]
for item in plan["interfaces"]:
if not item["physical"] and item["method"] in ("name", "new"):
continue
row = self.interface_table.rowCount()
self.interface_table.insertRow(row)
combo = QComboBox()
for text, data in choices:
combo.addItem(text, data)
combo.setCurrentIndex(max(0, combo.findData(item["netbox_name"] or self.NEW)))
for column, text in enumerate((switch["name"], item["name"], vlans.get(item["name"], ""))):
self.interface_table.setItem(row, column, QTableWidgetItem(text))
self.interface_table.setCellWidget(row, 3, combo)
method = QTableWidgetItem(MATCH_METHODS.get(item["method"], item["method"]))
if item["method"] in ("number", "order"):
method.setForeground(QColor(theme.color("warning")))
self.interface_table.setItem(row, 4, method)
self.interface_rows.append((row, switch, item, combo))
self._prepare_table(self.interface_table, (140, 170, 110, 220, 150))
interface_layout.addWidget(self.interface_table, 1)
unclear = sum(1 for _row, _switch, item, _combo in self.interface_rows if item["method"] != "name")
tabs.addTab(interface_page, f"Schnittstellen ({unclear} zu prüfen)")
self.only_unclear.toggled.connect(self._filter_interfaces)
self._filter_interfaces(True)
# Neighbors -----------------------------------------------------------
self.neighbor_rows = []
if create_cables:
neighbor_page = QWidget()
neighbor_layout = QVBoxLayout(neighbor_page)
neighbor_layout.setContentsMargins(0, 12, 0, 0)
scopes = sorted({plan["device_scope"] for _switch, plan in plans})
neighbor_layout.addWidget(label(
"Für jeden Nachbarn kann ein bestehendes NetBox-Gerät gewählt werden (Auswahl aus: "
f"{', '.join(scopes)}). Die Gegenstelle wird über den Portnamen gesucht; „kein Kabel“ "
"überspringt den Nachbarn.", muted=True, wrap=True))
self.neighbor_table = QTableWidget(0, 5)
self.neighbor_table.setHorizontalHeaderLabels(["Switch", "Lokaler Port", "Nachbar (LLDP/CDP)",
"Nachbar-Port", "NetBox-Gerät"])
for switch, plan in plans:
for neighbor in plan["neighbors"]:
row = self.neighbor_table.rowCount()
self.neighbor_table.insertRow(row)
combo = SearchableComboBox("Gerät suchen …")
combo.set_items([("– kein Kabel –", 0)] + [(device["name"], device["id"]) for device in plan["devices"]],
neighbor.get("device_id") or 0)
texts = (switch["name"], neighbor["local_port"], neighbor.get("system_name") or
neighbor.get("management_ip") or "", neighbor.get("remote_port") or "")
for column, text in enumerate(texts):
self.neighbor_table.setItem(row, column, QTableWidgetItem(text))
self.neighbor_table.setCellWidget(row, 4, combo)
self.neighbor_rows.append((switch, neighbor, combo))
self._prepare_table(self.neighbor_table, (140, 110, 220, 150, 260))
neighbor_layout.addWidget(self.neighbor_table, 1)
found = sum(1 for _switch, neighbor, _combo in self.neighbor_rows if neighbor.get("device_id"))
tabs.addTab(neighbor_page, f"Nachbarn ({found} von {len(self.neighbor_rows)} zugeordnet)")
self.root.addWidget(tabs, 1)
self.root.addWidget(self.error_banner)
buttons, _ok = form_dialog_buttons(self, "Weiter zur Vorschau")
self.root.addWidget(buttons)
@staticmethod
def _prepare_table(table, widths):
table.verticalHeader().hide()
table.setEditTriggers(QTableWidget.NoEditTriggers)
table.setSelectionMode(QTableWidget.NoSelection)
table.horizontalHeader().setStretchLastSection(True)
for column, width in enumerate(widths):
table.setColumnWidth(column, width)
def _filter_interfaces(self, only_unclear):
for row, _switch, item, _combo in self.interface_rows:
self.interface_table.setRowHidden(row, only_unclear and item["method"] == "name")
def accept(self):
result = {switch["id"]: {"interface_map": dict(switch.get("interface_map") or {}),
"neighbor_devices": dict(switch.get("neighbor_devices") or {})}
for switch, _plan in self.plans}
used = {}
for _row, switch, item, combo in self.interface_rows:
chosen = combo.currentData()
target = "" if chosen == self.NEW else chosen
mapping = result[switch["id"]]["interface_map"]
if target and (switch["id"], target) in used:
self.show_error(f"{switch['name']}: „{target}“ ist mehreren Schnittstellen zugeordnet "
f"({used[(switch['id'], target)]}, {item['name']}).")
return
if target:
used[(switch["id"], target)] = item["name"]
# Plain name matches stay automatic; everything else is remembered.
if item["method"] == "name" and target == item["netbox_name"]:
mapping.pop(item["name"], None)
else:
mapping[item["name"]] = target
for switch, neighbor, combo in self.neighbor_rows:
chosen = combo.valid_data()
chosen = int(chosen or 0)
stored = result[switch["id"]]["neighbor_devices"]
if neighbor["method"] == "auto" and chosen == (neighbor.get("device_id") or 0):
stored.pop(neighbor["local_port"], None)
else:
stored[neighbor["local_port"]] = chosen
self.result = result
super().accept()
class RawDataDialog(_Dialog):
SECTIONS = {
"interfaces": "Schnittstellen", "ipv4_interfaces": "IPv4", "ipv6_interfaces": "IPv6",
"neighbors": "LLDP", "cdp_neighbors": "CDP", "lldp_status": "LLDP-Status",
"neighbors": "LLDP", "cdp_neighbors": "CDP", "vlan_config": "VLAN-Konfiguration", "lldp_status": "LLDP-Status",
}
def __init__(self, parent, items, selected_id=None):
+2 -1
View File
@@ -4,7 +4,7 @@ import uuid
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QComboBox, QHBoxLayout, QMenu, QSplitter, QStackedWidget
from netbox_sync.core.switches import PLATFORMS
from netbox_sync.core.switches import PLATFORMS, vlan_label
from netbox_sync.ui import widgets
from netbox_sync.ui.dialogs import AddSwitchesDialog, RawDataDialog, SwitchDialog
from netbox_sync.ui.icons import icon
@@ -108,6 +108,7 @@ class SwitchesPage(Page):
dot=lambda row: "success" if row["connected"] else ("text_faint" if row["enabled"] else "danger")),
Column("speed", "Geschwindigkeit", 115, value=lambda row: _speed(row.get("speed_mbps")),
sort=lambda row: row.get("speed_mbps") or 0),
Column("vlans", "VLANs", 110, value=lambda row: vlan_label(row.get("vlans"))),
Column("ips", "IP-Adressen", 160, value=lambda row: ", ".join(row.get("ip_addresses", []))),
Column("neighbor", "Nachbar", 170, value=lambda row: (row.get("neighbor") or {}).get("system_name", "")),
Column("remote_port", "Nachbar-Port", 110, value=lambda row: (row.get("neighbor") or {}).get("remote_port", "")),