feat: ignore LAG note in port labels for VLAN colours and checks

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
MrBlakeandClaude Opus 5.5 committed 2026-11-04 10:05:59 +01:00
1 parent 5fcd7799e3
commit d49c4c6ee7
5 files changed
+28 -9

No files matched your search

+3
View File
@@ -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
+1 -1
View File
@@ -1,6 +1,6 @@
from netbox.plugins import PluginConfig, get_plugin_config
__version__ = "0.24.0"
__version__ = "0.25.0"
class NetBoxUtilitiesConfig(PluginConfig):
+16 -7
View File
@@ -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)
@@ -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, "")
+1 -1
View File
@@ -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"