refactor code
This commit is contained in:
@@ -1,30 +1,72 @@
|
||||
from pathlib import Path
|
||||
|
||||
import yaml
|
||||
|
||||
from ..domain.models import Topology
|
||||
|
||||
"""
|
||||
Example:
|
||||
links:
|
||||
networks:
|
||||
- A: [PC1.eth0, PC2.eth0]
|
||||
meta:
|
||||
id: host-host.csv
|
||||
name: host-host.csv
|
||||
nodes:
|
||||
- role: host
|
||||
name: PC1
|
||||
interfaces:
|
||||
- eth0:
|
||||
- ip: 10.0.12.1/24
|
||||
network: A
|
||||
- role: host
|
||||
name: PC2
|
||||
interfaces:
|
||||
- eth0:
|
||||
- ip: 10.0.12.2/24
|
||||
network: A
|
||||
"""
|
||||
|
||||
def make_yaml(topology: Topology, output_path: Path) -> None:
|
||||
data = dict()
|
||||
|
||||
def make_yaml(topology: Topology, output_path: str) -> None:
|
||||
pass
|
||||
data["meta"] = {
|
||||
"id": output_path.name,
|
||||
"name": output_path.name,
|
||||
}
|
||||
|
||||
data["networks"] = []
|
||||
for _, network in topology.networks.items():
|
||||
interfaces_with_device = [
|
||||
iface for iface in network.interfaces if iface.device is not None
|
||||
]
|
||||
|
||||
if len(interfaces_with_device) >= 2:
|
||||
data["networks"].append(
|
||||
{
|
||||
network.name: [
|
||||
f"{iface.device.name}.{iface.name}"
|
||||
for iface in interfaces_with_device
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
data["nodes"] = []
|
||||
|
||||
for _, device in topology.devices.items():
|
||||
data["nodes"].append(
|
||||
{
|
||||
"role": device.role,
|
||||
"name": device.name,
|
||||
"interfaces": [
|
||||
{
|
||||
interface_name: [
|
||||
{
|
||||
"ip": (
|
||||
interface.ip_address
|
||||
if interface.ip_address
|
||||
else None
|
||||
),
|
||||
"network": (
|
||||
interface.network if interface.network else None
|
||||
),
|
||||
"gateway": (
|
||||
interface.default_gateway
|
||||
if interface.default_gateway
|
||||
else None
|
||||
),
|
||||
}
|
||||
]
|
||||
}
|
||||
for interface_name, interface in device.interfaces.items()
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
with open(str(output_path), "w", encoding="utf-8") as f:
|
||||
yaml.safe_dump(
|
||||
data,
|
||||
f,
|
||||
allow_unicode=True,
|
||||
sort_keys=False,
|
||||
default_flow_style=False,
|
||||
indent=2,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user