feat: switch stacks per member device, optional AP sites with mandatory tenant

- Detect stacks (Aruba CX VSF, ArubaOS-Switch stacking/VSF, Comware IRF) from
  the configuration; new "Stack" tab assigns a NetBox device per member
  (suggested from virtual chassis positions, stored per switch); ports go to
  their member's device, LAGs and VLAN interfaces stay on the switch device
- Access points: site is optional with a default site for new APs; tenant is
  mandatory and can be chosen per Sophos site; new tenant column
- Empty states no longer get squeezed in small cards

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
MrBlakeandClaude Opus 5.5 committed 2026-11-02 13:08:30 +01:00
1 parent f400795cb5
commit 8d2bbafc8d
9 files changed
+469 -91

No files matched your search

+15 -2
View File
@@ -74,7 +74,7 @@ def test_sync_creates_new_aps_with_site_tenant_and_updates_existing():
engine.load_context(7)
aps = parse_csv(CSV)
engine.match(aps)
sites = {ap_key(aps[2]): 21}
sites = {ap_key(aps[2]): {"site": 21}}
preview = engine.sync(aps, sites, role_id=30, dry_run=True)
assert preview["errors"] == [] and preview["devices_created"] == 1 and preview["devices_updated"] == 2
@@ -101,4 +101,17 @@ def test_new_ap_without_site_is_an_error_not_a_guess():
aps = parse_csv(CSV)
engine.match(aps)
report = engine.sync(aps, {}, role_id=30, dry_run=True)
assert report["errors"] == ["Büro ohne Standort: kein NetBox-Standort zugeordnet"]
assert report["errors"][0].startswith("Büro ohne Standort: kein Standort")
def test_site_is_optional_with_default_site_and_tenant_can_be_chosen():
nb = _netbox()
engine = APSyncEngine(nb)
engine.load_context(7)
assert [tenant["name"] for tenant in engine.group_tenants] == ["Kunde Kita", "Kunde RZ"]
aps = parse_csv(CSV)
engine.match(aps)
# No site for the new AP: it goes to the default site, with an own tenant.
engine.sync(aps, {ap_key(aps[2]): {"site": None, "tenant": 8}}, role_id=30, dry_run=False, default_site=21)
created = nb.dcim.devices.created[-1]
assert (created["site"], created["tenant"]) == (21, 8)
+96 -1
View File
@@ -2,7 +2,14 @@ from types import SimpleNamespace
from fake_netbox import FakeNetBox, Record
from netbox_sync.core.switches import build_snapshot, match_interfaces, plan_switch_sync, sync_snapshot_to_netbox
from netbox_sync.core.switches import (
build_snapshot,
match_interfaces,
parse_stack,
plan_switch_sync,
stack_member,
sync_snapshot_to_netbox,
)
from test_switches import COMWARE_BRIEF, COMWARE_LLDP
@@ -280,3 +287,91 @@ def test_legacy_network_choices_still_work_and_migrate(tmp_path):
loaded = store.load()[0]["Kunde"]
assert loaded["network_map"] == {"10.1.0.0/24": {"vrf_id": None, "vrf_name": "", "create": True}}
assert "network_vrfs" not in loaded["switches"][0]
CX_STACK_CONFIG = """
hostname Switch36
vsf member 1
type jl661a
link 1 1/1/49
link 2 1/1/50
vsf member 2
type jl661a
link 1 2/1/49
link 2 2/1/51
interface lag 1
no shutdown
no routing
vlan trunk native 1
vlan trunk allowed 10,200-204
interface 1/1/1
no routing
vlan access 10
interface 1/1/52
lag 1
interface 2/1/1
no routing
vlan access 10
interface 2/1/52
lag 1
interface vlan 201
ip address 172.16.201.2/24
"""
def _stack_snapshot():
interfaces = [
{"name": name, "enabled": True, "connected": True, "media": "1GbT", "speed_mbps": 1000, "ip_addresses": [],
"vlans": {"untagged": [10], "tagged": [], "tagged_all": False}}
for name in ("1/1/1", "1/1/2", "2/1/1", "2/1/2")
] + [{"name": "lag1", "enabled": True, "connected": True, "ip_addresses": [], "vlans": None}]
return {"platform": "aruba_cx", "interfaces": interfaces, "neighbors": [],
"stack": parse_stack(CX_STACK_CONFIG, "aruba_cx", interfaces)}
def test_aruba_cx_vsf_stack_is_detected():
stack = parse_stack(CX_STACK_CONFIG, "aruba_cx")
assert stack == [{"member": 1, "model": "jl661a", "mac": ""}, {"member": 2, "model": "jl661a", "mac": ""}]
assert stack_member("2/1/5", "aruba_cx") == 2 and stack_member("lag1", "aruba_cx") is None
assert stack_member("GigabitEthernet2/0/5", "hpe_comware") == 2 and stack_member("2/A3", "aruba_aos") == 2
assert parse_stack("vsf member 1\n type jl661a\n", "aruba_cx") == [], "one member is no stack"
aos = 'stacking\n member 1 type "JL256A" mac-address 9020c2-000001\n member 2 type "JL256A"\n exit\n'
assert [(item["member"], item["model"]) for item in parse_stack(aos, "aruba_aos")] == [(1, "JL256A"), (2, "JL256A")]
def test_stack_members_go_to_their_own_netbox_devices():
nb, switch, _snapshot = _numbered_setup()
snapshot = _stack_snapshot()
# Member 2 is its own NetBox device whose device type named the ports "1/1/x".
nb.dcim.devices.records.append(Record(nb.dcim.devices, id=4, name="access-01-2", tenant=SimpleNamespace(id=7)))
for index, name in enumerate(("1/1/1", "1/1/2"), start=60):
nb.dcim.interfaces.records.append(Record(nb.dcim.interfaces, id=index, name=name, device=SimpleNamespace(id=4),
type=SimpleNamespace(value="1000base-t"), enabled=True, cable=None,
label=""))
plan = plan_switch_sync(nb, switch, snapshot)
assert [(entry["member"], entry["device_id"], entry["ports"]) for entry in plan["stack"]] == [(1, 1, 2), (2, None, 2)]
assert plan["skipped_ports"] == 2
report = sync_snapshot_to_netbox(nb, switch, snapshot, create_cables=False, dry_run=True)
assert report["notes"] == ["Stack-Mitglied 2: kein NetBox-Gerät gewählt – 2 Ports übersprungen"]
switch["stack_devices"] = {"1": 1, "2": 4}
plan = plan_switch_sync(nb, switch, snapshot)
by_name = {item["name"]: item for item in plan["interfaces"]}
assert by_name["2/1/1"]["owner_id"] == 4 and by_name["2/1/1"]["netbox_name"] == "1/1/1"
assert by_name["1/1/1"]["owner_id"] == 1 and by_name["1/1/1"]["netbox_name"] == "1"
assert plan["owner_interfaces"]["4"] == ["1/1/1", "1/1/2"]
sync_snapshot_to_netbox(nb, switch, snapshot, create_cables=False)
assert nb.dcim.interfaces.get(60).label == "10" and nb.dcim.interfaces.get(1).label == "10"
assert [(payload["device"], payload["name"]) for payload in nb.dcim.interfaces.created] == [(1, "lag1")]
def test_virtual_chassis_positions_are_suggested():
nb, switch, _snapshot = _numbered_setup()
snapshot = _stack_snapshot()
nb.dcim.devices.get(1).virtual_chassis = SimpleNamespace(id=9)
nb.dcim.devices.get(1).vc_position = 1
nb.dcim.devices.records.append(Record(nb.dcim.devices, id=4, name="access-01-2", tenant=SimpleNamespace(id=7),
virtual_chassis=SimpleNamespace(id=9), vc_position=2))
plan = plan_switch_sync(nb, switch, snapshot)
assert [entry["device_id"] for entry in plan["stack"]] == [1, 4]