add bridge and vlan support

This commit is contained in:
2026-03-25 15:25:02 +03:00
parent 45198209ab
commit 4c69be99e0
8 changed files with 244 additions and 53 deletions
+1 -2
View File
@@ -2,7 +2,6 @@ import shutil
import subprocess
from ..domain.models import Topology
from ..domain.models import VirtualInterface
from pathlib import Path
from py_d2 import D2Diagram, D2Shape, D2Connection
from py_d2.shape import Shape
@@ -53,7 +52,7 @@ def generate_d2_diagram(topology: Topology, output_path: Path) -> None:
for device in topology.devices.values():
for interface in device.interfaces.values():
if isinstance(interface, VirtualInterface):
if interface.itype != "physical":
continue # Skip virtual interfaces for now
shape = D2Shape(
+48 -11
View File
@@ -21,12 +21,6 @@ def make_yaml(topology: Topology, output_path: Path) -> None:
if len(interfaces_with_device) >= 2:
data["networks"].append(
# {
# network.name: [
# f"{iface.device.name}.{iface.name}"
# for iface in interfaces_with_device
# ]
# }
{
"name": network.name,
}
@@ -36,24 +30,67 @@ def make_yaml(topology: Topology, output_path: Path) -> None:
for _, device in topology.devices.items():
interfaces = dict()
interfaces["bridges"] = []
interfaces["vlans"] = []
for interface_name, interface in device.interfaces.items():
ip = interface.ip_address if interface.ip_address else None
mask = (
topology.networks[interface.network].subnet_mask
if interface.network
else None
)
mask = interface.subnet_mask if interface.subnet_mask else None
if mask is not None:
mask = mask.split("/")[1] if "/" in mask else mask
ip = f"{interface.ip_address}/{mask}" if interface.ip_address else None
index = None
# check regex Adapter[0-9]+
if interface.adapter:
if (
not interface.adapter.startswith("Adapter")
or not interface.adapter[7:].isdigit()
):
raise ValueError(
f"Invalid adapter name '{interface.adapter}' for interface '{interface_name}' on device '{device.name}'. Adapter name must be in the format 'AdapterX' where X is a number."
)
index = int(interface.adapter[7:])
if interface.itype == "bridge":
interfaces["bridges"].append(
{
"name": interface.name,
"members": interface.slave_interfaces,
"ip": ip,
"network": interface.network if interface.network else None,
"gateway": (
interface.default_gateway
if interface.default_gateway
else None
),
}
)
continue
if interface.itype == "vlan":
interfaces["vlans"].append(
{
"name": interface.name,
"parent": interface.parent_interface,
"ip": ip,
"network": interface.network if interface.network else None,
"gateway": (
interface.default_gateway
if interface.default_gateway
else None
),
}
)
continue
interfaces[interface_name] = {
"ip": ip,
"network": interface.network if interface.network else None,
"gateway": (
interface.default_gateway if interface.default_gateway else None
),
"index": index,
}
data["nodes"].append(