Development

This commit is contained in:
Matthew Grotke 2026-05-21 09:35:03 -04:00
parent fb76f893e9
commit efbd21cb59
2 changed files with 84 additions and 92 deletions

View file

@ -267,32 +267,22 @@ def derive_vlan_id(subnet, prefix):
return None
def resolve_vlan_derived_fields(data):
"""Return a deep copy of data with vlan_id and interface computed for every VLAN.
WireGuard VLANs are assigned wg0/wg1/... in ascending vlan_id order for
deterministic interface naming regardless of JSON list order.
Does not mutate the input dict.
"""
import copy
result = copy.deepcopy(data)
lan = result.get("general", {}).get("lan_interface", "eth0")
vlans = result.get("vlans", [])
for vlan in vlans:
vlan["vlan_id"] = derive_vlan_id(vlan.get("subnet", ""), vlan.get("subnet_mask", 24))
wg_entries = [(i, v) for i, v in enumerate(vlans) if is_wg(v)]
wg_sorted = sorted(wg_entries, key=lambda x: (x[1].get("vlan_id") is None, x[1].get("vlan_id") or 0))
for wg_idx, (_, vlan) in enumerate(wg_sorted):
vlan["interface"] = f"wg{wg_idx}"
for vlan in vlans:
if not is_wg(vlan):
vid = vlan.get("vlan_id", 1)
vlan["interface"] = lan if vid == 1 else f"{lan}.{vid}"
return result
def derive_interface(vlan, data):
"""Derive the interface name for a VLAN without mutating data."""
lan = data.get('general', {}).get('lan_interface', 'eth0')
if is_wg(vlan):
wg_vlans = [v for v in data.get('vlans', []) if is_wg(v)]
wg_sorted = sorted(
wg_vlans,
key=lambda v: (
derive_vlan_id(v.get('subnet', ''), v.get('subnet_mask', 24)) is None,
derive_vlan_id(v.get('subnet', ''), v.get('subnet_mask', 24)) or 0,
)
)
idx = next((i for i, v in enumerate(wg_sorted) if v is vlan), 0)
return f'wg{idx}'
vid = derive_vlan_id(vlan.get('subnet', ''), vlan.get('subnet_mask', 24))
return lan if vid == 1 else f'{lan}.{vid}'
# ===================================================================