From b1ecbbbb75209039c2efe7e9af9e5381a3f571ee Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 30 Oct 2026 10:03:07 +0100 Subject: [PATCH] feat: Aruba-style switch front panel on the device page Switches show a Ports panel above the Device and Management cards: a dark faceplate with every physical port as RJ45/SFP jack (odd on top, even below, per stack member), a status view (link, free, disabled) and a VLAN view with the label colours of the PDF, contrast-aware text, legend, tooltips with peer and description, key figures and a link to the PDF export. Texts follow the user's NetBox language. The panel is rendered into the left column and moved to the top by a small script. Co-Authored-By: Claude Opus 5.5 --- README.md | 28 ++++- netbox_utilities/__init__.py | 2 +- .../netbox_utilities/netbox_utilities.css | 116 ++++++++++++++++++ .../static/netbox_utilities/switch_panel.js | 66 ++++++++++ netbox_utilities/switch_port_views.py | 24 ++++ netbox_utilities/switch_ports.py | 51 ++++++++ netbox_utilities/template_content.py | 15 +++ .../netbox_utilities/device_switch_panel.html | 62 ++++++++++ netbox_utilities/tests/test_switch_ports.py | 8 ++ pyproject.toml | 2 +- 10 files changed, 369 insertions(+), 5 deletions(-) create mode 100644 netbox_utilities/static/netbox_utilities/switch_panel.js create mode 100644 netbox_utilities/templates/netbox_utilities/device_switch_panel.html diff --git a/README.md b/README.md index f540f46..c8ad1a3 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ Plugin settings: - A **Prefix** column (first word of the site name) for device and rack lists, including sorting and filtering. - A **Disks** tab for servers, NAS, SAN and MSA devices: slot layout, disks, RAID groups, warranty and age warnings, replacement history, CSV import, HPE MSA disk export import, REST API and GraphQL. - **CPU & RAM** per server from a built-in catalogue of 700+ Intel and AMD server CPUs since 2012, with cluster capacity and overcommit for NetBox virtualization. -- **Switch port usage as PDF** for switches: front panel coloured by VLAN (from the interface label) plus the full port list, in the user's NetBox language. +- **Switch front panel** on the device page (Aruba-style, status or VLAN colours) and **switch port usage as PDF**, in the user's NetBox language. Navigation settings are stored per user. The active tenant or tenant-group selection is stored in the browser session. @@ -223,8 +223,8 @@ sudo systemctl restart netbox netbox-rq | `hardware_enabled` | `True` | **CPU & RAM** tab, catalogue, columns and cluster capacity | | `hardware_device_roles` | `["Server"]` | Device roles (case-insensitive) that show the **CPU & RAM** tab; cluster hosts always show it | | `hardware_vcpu_ratio_warning` | `4` | Warn when a host or cluster has more vCPUs per physical core | -| `switch_ports_enabled` | `True` | **Export switch port usage** button for switches | -| `switch_device_roles` | `["Switch"]` | Device roles (case-insensitive) that get the export button | +| `switch_ports_enabled` | `True` | Front panel and **Export switch port usage** button for switches | +| `switch_device_roles` | `["Switch"]` | Device roles (case-insensitive) that get the front panel and export button | ## Usage @@ -505,6 +505,28 @@ recording and editing use `netbox_utilities.add_devicehardware` and `netbox_utilities.change_devicehardware`. The catalogue uses the `netbox_utilities.*_cpumodel` permissions. +### Switch front panel + +On the **Device** tab of a switch (role from `switch_device_roles`) a **Ports** +panel appears above the *Device* and *Management* cards, similar to the port +view in the web interface of Aruba switches: + +- A dark faceplate with every physical port as an RJ45 or SFP jack, odd ports + on top and even ports below, grouped per stack member or line card. +- **Status** view: green = link/connected, dark = free, red dot = disabled. +- **VLAN** view: every VLAN from the interface label gets its own colour (the + same colours as in the PDF), the VLAN is printed in the jack and the text + colour is chosen for contrast; free ports are dimmed, connected ports framed. + A legend lists every VLAN with connected/total ports. +- Hovering shows name, status, VLAN, peer and description; clicking opens the + interface. The header shows the key figures and links to the PDF export. +- The chosen view is remembered in the browser; texts follow the user's NetBox + language (German or English). + +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 +stays in the left column. + ### Switch port usage (PDF) Devices with a role from `switch_device_roles` get an **Export switch port diff --git a/netbox_utilities/__init__.py b/netbox_utilities/__init__.py index 1da9add..d587bca 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.18.2" +__version__ = "0.19.0" class NetBoxUtilitiesConfig(PluginConfig): diff --git a/netbox_utilities/static/netbox_utilities/netbox_utilities.css b/netbox_utilities/static/netbox_utilities/netbox_utilities.css index f42c462..64b20da 100644 --- a/netbox_utilities/static/netbox_utilities/netbox_utilities.css +++ b/netbox_utilities/static/netbox_utilities/netbox_utilities.css @@ -401,3 +401,119 @@ a.nbu-disk-slot[draggable="true"] { transform: rotate(90deg); } + +/* ---------- Switch front panel on the device page (Aruba-like) ---------- */ +.nbu-faceplate { + display: flex; + flex-wrap: wrap; + gap: .75rem 1.25rem; + padding: .75rem 1rem; + border-radius: .6rem; + background: linear-gradient(180deg, #3a3f45 0%, #2a2e33 100%); + box-shadow: inset 0 1px 0 rgba(255, 255, 255, .08), 0 1px 3px rgba(0, 0, 0, .25); + overflow-x: auto; +} +.nbu-faceplate-label { + color: #ced4da; + font-size: .7rem; + margin-bottom: .3rem; + letter-spacing: .02em; +} +.nbu-faceplate-ports { + display: flex; + gap: .25rem; +} +.nbu-faceplate-column { + display: flex; + flex-direction: column; + gap: .25rem; +} +.nbu-jack { + --nbu-vlan: #6c757d; + --nbu-ink: #fff; + position: relative; + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + width: 2.15rem; + height: 1.9rem; + border-radius: .2rem .2rem .35rem .35rem; + border: 1px solid rgba(0, 0, 0, .55); + text-decoration: none !important; + line-height: 1; + transition: transform .1s, filter .1s; +} +/* RJ45 latch notch */ +.nbu-jack:not(.nbu-jack-sfp)::before { + content: ""; + position: absolute; + bottom: -1px; + left: 50%; + width: 40%; + height: .22rem; + transform: translateX(-50%); + background: #2a2e33; + border-radius: .15rem .15rem 0 0; +} +.nbu-jack-sfp { + width: 2.5rem; + border-radius: .12rem; +} +.nbu-jack:hover { + transform: translateY(-1px); + filter: brightness(1.15); +} +.nbu-jack-number { + font-size: .68rem; + font-weight: 700; +} +.nbu-jack-vlan { + font-size: .5rem; + max-width: 100%; + overflow: hidden; + white-space: nowrap; + text-overflow: ellipsis; + padding: 0 .1rem; + display: none; +} + +/* Status view: green link, dark free port, disabled with a red mark */ +.nbu-jack-connected { background: #2fb344; color: #fff; } +.nbu-jack-free { background: #15181b; color: #8a9199; } +.nbu-jack-disabled { background: #15181b; color: #6c737a; } +.nbu-jack-disabled::after { + content: ""; + position: absolute; + top: .15rem; + right: .15rem; + width: .4rem; + height: .4rem; + border-radius: 50%; + background: #d63939; +} + +/* VLAN view: fill by VLAN label; free ports dimmed, connected ports with a bright frame */ +.nbu-switch-vlan .nbu-jack-vlan { display: block; } +.nbu-switch-vlan .nbu-jack-labelled { background: var(--nbu-vlan); color: var(--nbu-ink); } +.nbu-switch-vlan .nbu-jack:not(.nbu-jack-labelled) { background: #15181b; color: #8a9199; } +.nbu-switch-vlan .nbu-jack-connected { box-shadow: 0 0 0 2px #fff; } +.nbu-switch-vlan .nbu-jack-free.nbu-jack-labelled, +.nbu-switch-vlan .nbu-jack-disabled.nbu-jack-labelled { opacity: .45; } + +.nbu-switch-legend { + display: flex; + flex-wrap: wrap; + gap: .3rem 1rem; +} +.nbu-jack-swatch { + position: relative; + display: inline-block; + width: .85rem; + height: .85rem; + border-radius: .15rem; + margin-right: .3rem; + vertical-align: -.1rem; + border: 1px solid rgba(0, 0, 0, .35); +} +.nbu-jack-swatch.nbu-jack-disabled::after { top: .1rem; right: .1rem; width: .3rem; height: .3rem; } diff --git a/netbox_utilities/static/netbox_utilities/switch_panel.js b/netbox_utilities/static/netbox_utilities/switch_panel.js new file mode 100644 index 0000000..30ab118 --- /dev/null +++ b/netbox_utilities/static/netbox_utilities/switch_panel.js @@ -0,0 +1,66 @@ +// Switch front panel on the device page: moves it above the "Device" and "Management" cards and +// switches between the status and VLAN colours. +(function () { + "use strict"; + + const STORAGE_KEY = "netbox_utilities.switch_panel_mode"; + + function relocate(panel) { + // The plugin can only render into a column; place the panel above the row with both columns. + const row = panel.closest(".row"); + if (!row || row.dataset.nbuSwitchPanelMoved) return; + row.dataset.nbuSwitchPanelMoved = "true"; + const wrapper = document.createElement("div"); + wrapper.className = "row mb-3"; + const column = document.createElement("div"); + column.className = "col col-12"; + column.appendChild(panel); + wrapper.appendChild(column); + row.parentNode.insertBefore(wrapper, row); + } + + function init() { + const panel = document.querySelector("[data-nbu-switch-panel]"); + if (!panel) return; + relocate(panel); + + if (window.bootstrap && window.bootstrap.Tooltip) { + panel.querySelectorAll('[data-bs-toggle="tooltip"]').forEach((element) => { + window.bootstrap.Tooltip.getOrCreateInstance(element, { container: "body" }); + }); + } + + function setMode(mode) { + panel.classList.toggle("nbu-switch-vlan", mode === "vlan"); + panel.querySelectorAll("[data-nbu-switch-mode]").forEach((button) => { + button.classList.toggle("active", button.dataset.nbuSwitchMode === mode); + }); + panel.querySelectorAll("[data-nbu-switch-legend]").forEach((legend) => { + legend.classList.toggle("d-none", legend.dataset.nbuSwitchLegend !== mode); + }); + try { + localStorage.setItem(STORAGE_KEY, mode); + } catch (error) { + /* private mode: the choice is just not remembered */ + } + } + + panel.querySelectorAll("[data-nbu-switch-mode]").forEach((button) => { + button.addEventListener("click", () => setMode(button.dataset.nbuSwitchMode)); + }); + + let saved = "status"; + try { + saved = localStorage.getItem(STORAGE_KEY) || "status"; + } catch (error) { + /* ignore */ + } + setMode(saved === "vlan" ? "vlan" : "status"); + } + + if (document.readyState === "loading") { + document.addEventListener("DOMContentLoaded", init); + } else { + init(); + } +})(); diff --git a/netbox_utilities/switch_port_views.py b/netbox_utilities/switch_port_views.py index 23bf1b6..737aa67 100644 --- a/netbox_utilities/switch_port_views.py +++ b/netbox_utilities/switch_port_views.py @@ -45,12 +45,36 @@ def collect_ports(interfaces): description=interface.description, peer=peer, lag=interface.lag.name if interface.lag_id else "", + url=interface.get_absolute_url(), virtual=interface.type in switch_ports.VIRTUAL_TYPES, ) ) return ports +def switch_panel_context(request, device): + """Front panel above the "Device" and "Management" cards, in the user's NetBox language.""" + interfaces = ( + Interface.objects.restrict(request.user, "view") + .filter(device=device) + .select_related("lag", "cable", "_path") + .order_by("_name") + ) + ports = collect_ports(interfaces) + switch_ports.assign_vlan_colors(ports) + texts = switch_ports.PANEL_TEXTS[switch_port_pdf.language(translation.get_language())] + for port in ports: + if port.peer == switch_ports.MARKED_CONNECTED: + port.peer = texts["marked"] + port.state_text = texts[port.state] + return { + "groups": switch_ports.build_groups(ports), + "summary": switch_ports.summary(ports), + "legend": switch_ports.vlan_legend(ports), + "texts": texts, + } + + def device_info(device): rack = "" if device.rack_id: diff --git a/netbox_utilities/switch_ports.py b/netbox_utilities/switch_ports.py index 573275a..d8af067 100644 --- a/netbox_utilities/switch_ports.py +++ b/netbox_utilities/switch_ports.py @@ -42,6 +42,38 @@ VLAN_PALETTE = ( "#9edae5", # light cyan ) +# Texts of the front panel on the device page, by NetBox user language. +PANEL_TEXTS = { + "de": { + "title": "Ports", + "status": "Status", + "vlan": "VLAN", + "connected": "Belegt", + "free": "Frei", + "disabled": "Deaktiviert", + "no_label": "ohne Label", + "ports": "Ports", + "export": "PDF exportieren", + "peer": "Gegenstelle", + "marked": "als verbunden markiert", + "none": "Keine physischen Schnittstellen.", + }, + "en": { + "title": "Ports", + "status": "Status", + "vlan": "VLAN", + "connected": "Connected", + "free": "Free", + "disabled": "Disabled", + "no_label": "no label", + "ports": "ports", + "export": "Export PDF", + "peer": "Peer", + "marked": "marked as connected", + "none": "No physical interfaces.", + }, +} + _NAME_PATTERN = re.compile(r"^(?P.*?)(?P\d+)$") _NATURAL = re.compile(r"(\d+)") @@ -87,6 +119,18 @@ def vlan_key(label): return match.group(1) if match else text +def ink_for(hex_color): + """ "#000000" or "#ffffff", whichever has the higher WCAG contrast against the colour.""" + value = hex_color.lstrip("#") + red, green, blue = (int(value[index : index + 2], 16) / 255 for index in (0, 2, 4)) + + def channel(component): + return component / 12.92 if component <= 0.03928 else ((component + 0.055) / 1.055) ** 2.4 + + luminance = 0.2126 * channel(red) + 0.7152 * channel(green) + 0.0722 * channel(blue) + return "#000000" if (luminance + 0.05) / 0.05 >= 1.05 / (luminance + 0.05) else "#ffffff" + + def vlan_color(label): key = vlan_key(label) if not key: @@ -114,6 +158,8 @@ class Port: virtual: bool = False # Set by assign_vlan_colors(); otherwise the colour comes from a hash of the label. assigned_color: str = "" + # Translated status for tooltips, set by the view. + state_text: str = "" @property def state(self): @@ -139,6 +185,11 @@ class Port: def vlan(self): return vlan_key(self.label) + @property + def ink(self): + """Text colour (black or white) that contrasts best with the VLAN colour.""" + return ink_for(self.color) if self.color else "" + @property def color(self): if not self.vlan: diff --git a/netbox_utilities/template_content.py b/netbox_utilities/template_content.py index a0b8675..70fe6f6 100644 --- a/netbox_utilities/template_content.py +++ b/netbox_utilities/template_content.py @@ -126,6 +126,21 @@ class DeviceUtilitiesContent(PluginTemplateExtension): return "" return self.render("netbox_utilities/device_bulk_module_button.html") + def left_page(self): + # Rendered into the left column; switch_panel.js moves it above the "Device" and "Management" cards. + request, device = self.context["request"], self.context["object"] + if not switch_ports_enabled() or not request.user.has_perm("dcim.view_interface"): + return "" + from . import switch_ports + from .switch_port_views import switch_panel_context + + if not switch_ports.is_switch(device): + return "" + return self.render( + "netbox_utilities/device_switch_panel.html", + {**switch_panel_context(request, device), "asset_version": __version__}, + ) + def right_page(self): placement = DeviceRackPlacement.objects.filter(device=self.context["object"]).first() if placement is None: diff --git a/netbox_utilities/templates/netbox_utilities/device_switch_panel.html b/netbox_utilities/templates/netbox_utilities/device_switch_panel.html new file mode 100644 index 0000000..94a8004 --- /dev/null +++ b/netbox_utilities/templates/netbox_utilities/device_switch_panel.html @@ -0,0 +1,62 @@ +{% load static %} +
+
+

{{ texts.title }}

+ + {{ summary.total }} {{ texts.ports }} · + {{ summary.connected }} {{ texts.connected }} · + {{ summary.free }} {{ texts.free }} · + {{ summary.disabled }} {{ texts.disabled }} + +
+ + +
+ + {{ texts.export }} + +
+
+ {% if not groups %} + {{ texts.none }} + {% else %} +
+ {% for group in groups %} +
+
{{ group.title }}
+
+ {% for column in group.columns %} +
+ {% for port in column %} + + {{ port.short }} + {{ port.vlan }} + + {% endfor %} +
+ {% endfor %} +
+
+ {% endfor %} +
+
+ {{ texts.connected }} + {{ texts.free }} + {{ texts.disabled }} +
+
+ {% for vlan, color, total, connected in legend %} + + {{ vlan|default:texts.no_label }} + ({{ connected }}/{{ total }}) + + {% endfor %} +
+ {% endif %} +
+
+ diff --git a/netbox_utilities/tests/test_switch_ports.py b/netbox_utilities/tests/test_switch_ports.py index bdb5113..bbfa87f 100644 --- a/netbox_utilities/tests/test_switch_ports.py +++ b/netbox_utilities/tests/test_switch_ports.py @@ -137,3 +137,11 @@ class PDFTest(SimpleTestCase): ink = switch_port_pdf.text_color(fill) light, dark = sorted((switch_port_pdf.luminance(fill), switch_port_pdf.luminance(ink)), reverse=True) self.assertGreaterEqual((light + 0.05) / (dark + 0.05), 3) + + +class InkTest(SimpleTestCase): + def test_text_colour_contrasts_with_vlan_colour(self): + self.assertEqual(switch_ports.ink_for("#ff7f0e"), "#000000") + self.assertEqual(switch_ports.ink_for("#1f77b4"), "#ffffff") + self.assertEqual(port("1", label="10", assigned_color="#2ca02c").ink, "#000000") + self.assertEqual(port("2").ink, "") diff --git a/pyproject.toml b/pyproject.toml index 1255f55..b17b38f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "netbox-utilities" -version = "0.18.2" +version = "0.19.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"