Cutsheet: port names in UniFi that no longer fit, document a cable from topology
Lint / JS (eslint) (push) Successful in 23s
Lint / Python (flake8) (push) Successful in 1m10s
Lint / Notify on failure (push) Skipped
Lint / Deploy (push) Skipped
Lint / Python (flake8) (pull_request) Successful in 53s
Lint / JS (eslint) (pull_request) Successful in 11s
Lint / Notify on failure (pull_request) Skipped
Lint / Deploy (pull_request) Skipped
Security / Python Security (bandit) (push) Successful in 1m26s
Test / Python Tests (pytest) (push) Successful in 1m49s
Security / Python Security (bandit) (pull_request) Successful in 2m17s
Test / Python Tests (pytest) (pull_request) Successful in 2m12s

- topology.port_name_report: switch port names that no longer fit what is
  plugged in, as a checklist with suggested names: named after the wrong
  device, named after a device that moved to another port, named with nothing
  plugged in (and who was last seen there), and still "Port N" with a known
  device on it. Ports named after a device that is only off are left alone.
  Gandalf only reports; it never renames anything in UniFi.
- /api/cutsheet returns it as port_names; the cutsheet page shows it as
  "Port names in UniFi", grouped, with a summary chip.
- The topology cable panel's "not documented" gets a "document it" link that
  opens the cutsheet's new-cable form with both ends, the NIC and DAC filled
  in; /cutsheet?... now takes b_device, b_port and cable_type.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 13:48:52 -05:00
co-authored by Claude Opus 5.5
parent d6fcddb6fc
commit 4dfabce2fb
9 changed files with 248 additions and 3 deletions
+1
View File
@@ -217,6 +217,7 @@ class TestCutsheetApi:
assert view['next_label'] == 'C01'
assert view['labels']['cables'][0]['text'] == 'C07'
assert any(d['name'] == 'PP1' for d in view['catalog'])
assert isinstance(view['port_names'], list)
def test_create_cable(self, cs_client):
client, db = cs_client
+60
View File
@@ -426,3 +426,63 @@ class TestPower:
def test_every_power_device_is_listed(self):
assert [s['node'] for s in self._graph()['power']] == ['RPDU', 'UPS 2U Pro', 'USP Strip']
class TestPortNameReport:
def _report(self, poe_port_11='Port 11', pbs_up=True, extra=(), known=None):
snapshot, link_stats, modem = network(poe_port_11, pbs_up)
link_stats['unifi_switches']['24RJ45-POE']['ports'].update(dict(extra))
return topology.port_name_report(snapshot, link_stats, modem, known)
def _row(self, rows, sw, idx):
return next((r for r in rows if r['switch'] == sw and r['port_idx'] == idx), None)
def test_named_after_the_wrong_device(self):
rows = self._report(poe_port_11='Host-A Management')
r = self._row(rows, '24RJ45-POE', 11)
assert r['kind'] == 'wrong' and r['suggestion'] == 'AP'
assert r['detail'] == ('Named after host-a, but AP is plugged in; '
'host-a is on 8SFP-AGG port 1, Dream Machine Pro port 1')
assert rows[0] is r # the most urgent kind comes first
def test_named_after_a_device_that_moved(self):
rows = self._report(extra=[port(20, 'host-c', up=False)])
r = self._row(rows, '24RJ45-POE', 20)
assert r['kind'] == 'moved' and r['suggestion'] == 'Port 20'
assert r['detail'] == 'Named after host-c, which is now on 8SFP-AGG port 8'
def test_named_but_empty_says_who_was_last_seen(self):
known = {'pbs': {'uplinks': [{'switch': '24RJ45-POE', 'port_idx': 7}], 'seen': '2026-11-01 10:00:00 UTC'}}
rows = self._report(extra=[port(7, 'Guest Bedroom', up=False)], known=known)
r = self._row(rows, '24RJ45-POE', 7)
assert r['kind'] == 'empty' and r['suggestion'] == ''
assert r['detail'] == ('Nothing plugged in; last seen here: pbs (2026-11-01 10:00:00 UTC), '
'now on 24RJ45-POE port 15')
def test_unnamed_ports_get_a_suggestion(self):
rows = self._report()
# A server with two cables gets its NIC in the name; a switch-to-switch port is a downlink
assert self._row(rows, '24RJ45-POE', 4)['suggestion'] == 'host-b enp2s0'
assert self._row(rows, '24RJ45-POE', 19)['suggestion'] == 'RPDU'
assert self._row(rows, '8SFP-AGG', 7)['suggestion'] == 'Uplink'
assert self._row(rows, '8SFP-AGG', 8)['suggestion'] == 'host-c'
assert {r['kind'] for r in rows} == {'unnamed'}
def test_good_names_and_unknown_devices_are_left_alone(self):
rows = self._report()
for sw, idx in [('24RJ45-POE', 15), # "PBS" with pbs plugged in
('24RJ45-POE', 3), # "Living Room Downlink" to the Flex
('8SFP-AGG', 5), # "Downlink to 24RJ45-POE"
('24RJ45-POE', 1), # a custom name over an unknown device
('24RJ45-POE', 8), # an IP-only target
('24RJ45-POE', 12), # "Port 12", nothing plugged in
('RPDU', 1)]: # a PDU's management port
assert self._row(rows, sw, idx) is None, (sw, idx)
def test_a_port_named_after_a_device_that_is_off_is_right(self):
rows = self._report(pbs_up=False)
for sw, idx in [('24RJ45-POE', 15)]: # "PBS" while pbs is down
assert self._row(rows, sw, idx) is None, (sw, idx)
def test_no_data(self):
assert topology.port_name_report({}, {}, None) == []