84 lines
2.4 KiB
Python
84 lines
2.4 KiB
Python
"""Configuration renderer - orchestrates CLI generation"""
|
|
from typing import Dict, List, Any
|
|
from app.cli.generators import (
|
|
generate_hostname_cli,
|
|
generate_vlan_cli,
|
|
generate_interface_cli,
|
|
generate_routing_cli,
|
|
generate_nat_cli,
|
|
generate_acl_cli,
|
|
)
|
|
|
|
|
|
class ConfigRenderer:
|
|
"""
|
|
Renders complete Cisco device configuration from config data.
|
|
|
|
Design:
|
|
- Deterministic output (always same order)
|
|
- Idempotent when possible
|
|
- Proper section formatting with "!" separators
|
|
"""
|
|
|
|
# Command order (important for Cisco IOS)
|
|
COMMAND_ORDER = [
|
|
"hostname",
|
|
"vlans",
|
|
"interfaces",
|
|
"routing",
|
|
"nat",
|
|
"acls",
|
|
]
|
|
|
|
def __init__(self):
|
|
self.cli_commands: List[str] = []
|
|
|
|
def render(self, config_data: Dict[str, Any]) -> str:
|
|
"""
|
|
Render full CLI configuration from config data dict.
|
|
|
|
Args:
|
|
config_data: Configuration dict with keys like:
|
|
- hostname (str)
|
|
- vlans (list)
|
|
- interfaces (list)
|
|
- routes (list)
|
|
- nat (dict)
|
|
- acls (list)
|
|
|
|
Returns:
|
|
Complete CLI as string
|
|
"""
|
|
self.cli_commands = []
|
|
|
|
# Process in order
|
|
if "hostname" in config_data:
|
|
self.cli_commands.extend(generate_hostname_cli(config_data["hostname"]))
|
|
self.cli_commands.append("!")
|
|
|
|
if "vlans" in config_data:
|
|
self.cli_commands.extend(generate_vlan_cli(config_data["vlans"]))
|
|
|
|
if "interfaces" in config_data:
|
|
self.cli_commands.extend(generate_interface_cli(config_data["interfaces"]))
|
|
|
|
if "routes" in config_data:
|
|
self.cli_commands.extend(generate_routing_cli(config_data["routes"]))
|
|
self.cli_commands.append("!")
|
|
|
|
if "nat" in config_data:
|
|
self.cli_commands.extend(generate_nat_cli(config_data["nat"]))
|
|
|
|
if "acls" in config_data:
|
|
self.cli_commands.extend(generate_acl_cli(config_data["acls"]))
|
|
|
|
# Add final save prompt
|
|
self.cli_commands.append("end")
|
|
self.cli_commands.append("write memory")
|
|
|
|
return "\n".join(self.cli_commands)
|
|
|
|
def get_commands_list(self) -> List[str]:
|
|
"""Return CLI commands as list (for line-by-line execution)"""
|
|
return self.cli_commands
|