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 <[email protected]>
This commit is contained in:
MrBlakeandClaude Opus 5.5 committed 2026-10-30 10:03:07 +01:00
1 parent 7289c21684
commit b1ecbbbb75
10 files changed
+369 -5

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
from netbox.plugins import PluginConfig, get_plugin_config
__version__ = "0.18.2"
__version__ = "0.19.0"
class NetBoxUtilitiesConfig(PluginConfig):
@@ -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; }
@@ -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();
}
})();
+24
View File
@@ -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:
+51
View File
@@ -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<prefix>.*?)(?P<number>\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:
+15
View File
@@ -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:
@@ -0,0 +1,62 @@
{% load static %}
<div class="card nbu-switch-card" data-nbu-switch-panel>
<div class="card-header d-flex flex-wrap align-items-center gap-2">
<h2 class="card-title mb-0 me-auto"><i class="mdi mdi-switch me-1" aria-hidden="true"></i>{{ texts.title }}</h2>
<span class="small text-muted">
{{ summary.total }} {{ texts.ports }} ·
<span class="text-success fw-semibold">{{ summary.connected }} {{ texts.connected }}</span> ·
{{ summary.free }} {{ texts.free }} ·
{{ summary.disabled }} {{ texts.disabled }}
</span>
<div class="btn-group btn-group-sm" role="group">
<button type="button" class="btn btn-outline-secondary active" data-nbu-switch-mode="status">{{ texts.status }}</button>
<button type="button" class="btn btn-outline-secondary" data-nbu-switch-mode="vlan">{{ texts.vlan }}</button>
</div>
<a href="{% url 'plugins:netbox_utilities:device_switch_ports_pdf' pk=object.pk %}" class="btn btn-sm btn-outline-secondary">
<i class="mdi mdi-file-pdf-box" aria-hidden="true"></i> {{ texts.export }}
</a>
</div>
<div class="card-body">
{% if not groups %}
<span class="text-muted">{{ texts.none }}</span>
{% else %}
<div class="nbu-faceplate">
{% for group in groups %}
<div class="nbu-faceplate-group">
<div class="nbu-faceplate-label">{{ group.title }}</div>
<div class="nbu-faceplate-ports">
{% for column in group.columns %}
<div class="nbu-faceplate-column">
{% for port in column %}
<a href="{{ port.url }}"
class="nbu-jack nbu-jack-{{ port.state }}{% if group.optical %} nbu-jack-sfp{% endif %}{% if port.color %} nbu-jack-labelled{% endif %}"
{% if port.color %}style="--nbu-vlan: {{ port.color }}; --nbu-ink: {{ port.ink }}"{% endif %}
data-bs-toggle="tooltip" data-bs-html="true"
title="<strong>{{ port.name|force_escape }}</strong><br>{{ port.state_text }}{% if port.label %}<br>{{ texts.vlan }}: {{ port.label|force_escape }}{% endif %}{% if port.peer %}<br>{{ texts.peer }}: {{ port.peer|force_escape }}{% endif %}{% if port.description %}<br><em>{{ port.description|force_escape }}</em>{% endif %}">
<span class="nbu-jack-number">{{ port.short }}</span>
<span class="nbu-jack-vlan">{{ port.vlan }}</span>
</a>
{% endfor %}
</div>
{% endfor %}
</div>
</div>
{% endfor %}
</div>
<div class="nbu-switch-legend small mt-2" data-nbu-switch-legend="status">
<span><span class="nbu-jack-swatch nbu-jack-connected"></span>{{ texts.connected }}</span>
<span><span class="nbu-jack-swatch nbu-jack-free"></span>{{ texts.free }}</span>
<span><span class="nbu-jack-swatch nbu-jack-disabled"></span>{{ texts.disabled }}</span>
</div>
<div class="nbu-switch-legend small mt-2 d-none" data-nbu-switch-legend="vlan">
{% for vlan, color, total, connected in legend %}
<span>
<span class="nbu-jack-swatch" style="background: {{ color|default:'#6c757d' }}"></span>{{ vlan|default:texts.no_label }}
<span class="text-muted">({{ connected }}/{{ total }})</span>
</span>
{% endfor %}
</div>
{% endif %}
</div>
</div>
<script src="{% static 'netbox_utilities/switch_panel.js' %}?v={{ asset_version }}" defer></script>
@@ -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, "")