Add VLAN table + per-connector VLAN tags (structure only -- no VLANs in NetBox yet)

Inspired by a reference diagram the user liked (VLAN legend/mapping table,
per-port VLAN info) but explicitly without the literal switch CLI config
dumps that diagram also had.

- export_topology.py: pulls ipam.VLAN (with prefixes) plus, for every
  edge whose endpoint is a real Interface, that port's mode/untagged/
  tagged VLANs. NetBox has zero VLANs defined right now, so this comes
  back empty/null throughout -- the plumbing is real, the data just
  isn't there yet.
- generate_drawio.py: renders a VLAN table (VLAN/Name/Description/
  Subnet/Site) at the top of the diagram. With no VLANs it shows an
  explicit dashed 'not configured yet' placeholder row instead of
  either fabricating VLAN numbers or silently omitting the table.
  Connector labels get a 'V<id>' / 'V<id>,<id>T' suffix per side
  whenever that port actually has VLAN config -- currently a no-op,
  activates automatically the moment VLANs exist in NetBox.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NM3nivbufxEWiKKmatEtEe
This commit is contained in:
dcstephenson
2026-09-15 13:24:34 -05:00
parent b6151b7de5
commit 715e37d048
6 changed files with 142 additions and 10 deletions
+36 -2
View File
@@ -22,10 +22,38 @@ Two passes:
2. Pull every real Cable among those devices directly, so each edge
carries the exact port name on both ends (what generate_drawio.py
labels connectors with) instead of just "these two are connected."
Also exports every ipam.VLAN (with its prefixes) and, for each edge whose
endpoint is a real Interface (not a passive patch-panel port), that
interface's 802.1Q mode/untagged/tagged VLANs. As of this writing NetBox
has zero VLANs defined here, so `vlans` comes back `[]` and every edge's
`a_vlan`/`b_vlan` comes back `null` -- generate_drawio.py renders that as
an explicit "not configured yet" placeholder rather than silently
omitting the VLAN legend. The moment VLANs and interface assignments get
added in NetBox, rerunning this (no code changes needed) starts filling
the diagram in for real.
"""
from dcim.models import Device, Interface, Cable
from ipam.models import VLAN
import json
def vlan_info(vlan):
if not vlan:
return None
prefixes = [str(p.prefix) for p in vlan.prefixes.all()]
return {"vid": vlan.vid, "name": vlan.name, "prefix": prefixes[0] if prefixes else None}
def port_vlans(termination):
"""VLAN mode/tagging for a real Interface termination; None for a
passive patch-panel front/rear port, which has no 802.1Q concept."""
if not isinstance(termination, Interface):
return None
return {
"mode": termination.mode,
"untagged": vlan_info(termination.untagged_vlan),
"tagged": [vlan_info(v) for v in termination.tagged_vlans.all()],
}
switch_ids = set(
Device.objects.filter(
role__slug__in=["core-switch", "dist-switch", "field-switch"]
@@ -66,8 +94,8 @@ for c in cables:
if da.id in touched_devices and db.id in touched_devices and da.id != db.id:
key = tuple(sorted((da.id, db.id)))
edges[key] = {
"a": da.id, "a_port": ta.termination.name,
"b": db.id, "b_port": tb.termination.name,
"a": da.id, "a_port": ta.termination.name, "a_vlan": port_vlans(ta.termination),
"b": db.id, "b_port": tb.termination.name, "b_vlan": port_vlans(tb.termination),
}
devs = Device.objects.filter(id__in=touched_devices).select_related(
@@ -88,5 +116,11 @@ out = {
for d in devs
],
"edges": list(edges.values()),
"vlans": [
{"vid": v.vid, "name": v.name, "description": v.description,
"prefixes": [str(p.prefix) for p in v.prefixes.all()],
"site": v.site.name if v.site else ""}
for v in VLAN.objects.all().select_related("site").order_by("vid")
],
}
print(json.dumps(out))