79 lines
2.6 KiB
Python
79 lines
2.6 KiB
Python
"""Configuration model"""
|
|
from sqlalchemy import Column, String, Integer, ForeignKey, Text, LargeBinary, Index, Enum
|
|
from sqlalchemy.orm import relationship
|
|
from app.models.base import BaseModel
|
|
import enum
|
|
import json
|
|
|
|
|
|
class ConfigStatus(str, enum.Enum):
|
|
"""Configuration status"""
|
|
DRAFT = "draft"
|
|
VALIDATED = "validated"
|
|
PUSHED = "pushed"
|
|
FAILED = "failed"
|
|
|
|
|
|
class Configuration(BaseModel):
|
|
"""Device configuration (config builder state + generated CLI)"""
|
|
__tablename__ = "configurations"
|
|
__table_args__ = (
|
|
Index("ix_configurations_project_id", "project_id"),
|
|
Index("ix_configurations_device_id", "device_id"),
|
|
)
|
|
|
|
project_id = Column(Integer, ForeignKey("projects.id"), nullable=False)
|
|
device_id = Column(Integer, ForeignKey("devices.id"), nullable=False)
|
|
name = Column(String(255), nullable=False)
|
|
|
|
# Config builder state (JSON: vlans, interfaces, routing, nat, acls, etc.)
|
|
config_data = Column(Text, nullable=True)
|
|
|
|
# Generated CLI commands
|
|
generated_cli = Column(Text, nullable=True)
|
|
|
|
# Validation results
|
|
validation_errors = Column(Text, nullable=True) # JSON: list of errors
|
|
validation_warnings = Column(Text, nullable=True) # JSON: list of warnings
|
|
|
|
# Status
|
|
status = Column(Enum(ConfigStatus), default=ConfigStatus.DRAFT, nullable=False)
|
|
|
|
# Relationships
|
|
project = relationship("Project", back_populates="configurations")
|
|
device = relationship("Device", back_populates="configurations")
|
|
push_logs = relationship("PushLog", back_populates="configuration", cascade="all, delete-orphan")
|
|
|
|
def get_config_dict(self):
|
|
"""Parse config_data JSON"""
|
|
if self.config_data:
|
|
return json.loads(self.config_data)
|
|
return {}
|
|
|
|
def set_config_dict(self, data: dict):
|
|
"""Set config_data from dict"""
|
|
self.config_data = json.dumps(data)
|
|
|
|
def get_errors(self):
|
|
"""Parse validation_errors JSON"""
|
|
if self.validation_errors:
|
|
return json.loads(self.validation_errors)
|
|
return []
|
|
|
|
def set_errors(self, errors: list):
|
|
"""Set validation_errors from list"""
|
|
self.validation_errors = json.dumps(errors)
|
|
|
|
def get_warnings(self):
|
|
"""Parse validation_warnings JSON"""
|
|
if self.validation_warnings:
|
|
return json.loads(self.validation_warnings)
|
|
return []
|
|
|
|
def set_warnings(self, warnings: list):
|
|
"""Set validation_warnings from list"""
|
|
self.validation_warnings = json.dumps(warnings)
|
|
|
|
def __repr__(self):
|
|
return f"<Configuration {self.name}>"
|