From d49c4c6ee7c54bf4d0ee1a71942bbec3f3d98d48 Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 4 Nov 2026 10:05:59 +0100 Subject: [PATCH] feat: ignore LAG note in port labels for VLAN colours and checks Co-Authored-By: Claude Opus 5.5 --- README.md | 3 +++ netbox_utilities/__init__.py | 2 +- netbox_utilities/switch_ports.py | 23 ++++++++++++++------- netbox_utilities/tests/test_switch_ports.py | 7 +++++++ pyproject.toml | 2 +- 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 3d1ec57..5c33d3f 100644 --- a/README.md +++ b/README.md @@ -540,6 +540,9 @@ account, in the front panel and in the PDF: list show a warning. Labels without a number never count as a mismatch. - Tooltips and the PDF port list show both the label and the cable VLANs with their names. +- A LAG note in the label such as `10,20 (LAG 1)` (written by NetBox + Synchronizer for LAG members) is shown, but ignored for VLAN colours, the + legend and the mismatch check. NetBox offers plugins no slot above these cards, so the panel is rendered into the left column and moved to the top by a small script; without JavaScript it diff --git a/netbox_utilities/__init__.py b/netbox_utilities/__init__.py index 328295a..1ec1bb8 100644 --- a/netbox_utilities/__init__.py +++ b/netbox_utilities/__init__.py @@ -1,6 +1,6 @@ from netbox.plugins import PluginConfig, get_plugin_config -__version__ = "0.24.0" +__version__ = "0.25.0" class NetBoxUtilitiesConfig(PluginConfig): diff --git a/netbox_utilities/switch_ports.py b/netbox_utilities/switch_ports.py index 4e6e279..fc62d60 100644 --- a/netbox_utilities/switch_ports.py +++ b/netbox_utilities/switch_ports.py @@ -117,6 +117,10 @@ def split_name(name): return match.group("prefix"), int(match.group("number")) +# "(LAG 1)" in a port label names the LAG of the port (written by NetBox Synchronizer), not a VLAN. +_LAG_NOTE = re.compile(r"(?i)\s*\(\s*LAG\s*\d+\s*\)") + + def is_optical(interface_type): text = (interface_type or "").casefold() return any(marker in text for marker in OPTICAL_MARKERS) @@ -204,7 +208,12 @@ class Port: @property def vlan(self): """The VLAN shown for the port: the label, or the VLANs of its cable if it has no label.""" - return vlan_key(self.label) or self.cable_vlan_key + return vlan_key(self.vlan_text) or self.cable_vlan_key + + @property + def vlan_text(self): + """The label without a LAG note: "10,20 (LAG 1)" → "10,20".""" + return _LAG_NOTE.sub("", self.label or "").strip() @property def cable_vlan_key(self): @@ -219,9 +228,9 @@ class Port: @property def vlan_mismatch(self): """True if the numbers in the label differ from the cable VLANs; labels without numbers never mismatch.""" - if not self.label or not self.cable_vlans: + if not self.vlan_text or not self.cable_vlans: return False - label_ids = {int(number) for number in _NATURAL.findall(self.label) if number.isdigit()} + label_ids = {int(number) for number in _NATURAL.findall(self.vlan_text) if number.isdigit()} return bool(label_ids) and label_ids != {vid for vid, _ in self.cable_vlans} @property @@ -242,9 +251,9 @@ class Port: A label with exactly one number is that VLAN ID, so "Server 30", "VLAN 30", "30" and a cable with VLAN 30 belong together; other labels ("Trunk", "10,20") stay as they are. """ - if self.label: - numbers = [number for number in _NATURAL.findall(self.label) if number.isdigit()] - return str(int(numbers[0])) if len(numbers) == 1 else vlan_key(self.label) + if self.vlan_text: + numbers = [number for number in _NATURAL.findall(self.vlan_text) if number.isdigit()] + return str(int(numbers[0])) if len(numbers) == 1 else vlan_key(self.vlan_text) return self.cable_vlan_key @@ -340,7 +349,7 @@ def vlan_legend(ports): continue group = port.vlan_group colors.setdefault(group, port.color) - if port.label and group not in labelled: + if port.vlan_text and group not in labelled: names[group] = port.vlan labelled.add(group) names.setdefault(group, port.vlan) diff --git a/netbox_utilities/tests/test_switch_ports.py b/netbox_utilities/tests/test_switch_ports.py index 67debcf..1863c6e 100644 --- a/netbox_utilities/tests/test_switch_ports.py +++ b/netbox_utilities/tests/test_switch_ports.py @@ -194,3 +194,10 @@ class VlanIdentityTest(SimpleTestCase): self.assertEqual(len({p.color for p in ports[:3]}), 1) legend = [(text, total, connected) for text, _, total, connected in switch_ports.vlan_legend(ports)] self.assertEqual(legend, [("10,20", 1, 0), ("Server 30", 3, 2), ("Trunk", 1, 0)]) + + def test_lag_note_in_label_is_no_vlan(self): + lag = port("1", label="30 (LAG 1)", cable_vlans=[(30, "")]) + self.assertEqual((lag.vlan, lag.vlan_group, lag.vlan_mismatch), ("30", "30", False)) + trunk = port("2", label="10,20 (LAG 2)") + self.assertEqual(trunk.vlan_group, "10,20") + self.assertEqual(port("3", label="(LAG 1)").vlan, "") diff --git a/pyproject.toml b/pyproject.toml index c78be58..07b9c31 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "netbox-utilities" -version = "0.24.0" +version = "0.25.0" description = "Navigation, tenant utilities, connection VLANs, partial-width racks, disks, CPU & RAM, switch port export and bulk operations for NetBox 4.6/4.7" readme = "README.md" requires-python = ">=3.12"