feat: allow interfaces to have no zone (shorewall '-' zone)

This commit is contained in:
2026-03-01 11:11:52 +01:00
parent 21d404229a
commit 58ef0dec63
5 changed files with 29 additions and 7 deletions

View File

@@ -0,0 +1,21 @@
"""make interface zone_id nullable
Revision ID: 0005
Revises: 0004
Create Date: 2026-03-01
"""
from alembic import op
import sqlalchemy as sa
revision = "0005"
down_revision = "0004"
branch_labels = None
depends_on = None
def upgrade() -> None:
op.alter_column("interfaces", "zone_id", existing_type=sa.Integer(), nullable=True)
def downgrade() -> None:
op.alter_column("interfaces", "zone_id", existing_type=sa.Integer(), nullable=False)

View File

@@ -59,11 +59,11 @@ class Interface(Base):
id: Mapped[int] = mapped_column(Integer, primary_key=True)
config_id: Mapped[int] = mapped_column(Integer, ForeignKey("configs.id"), nullable=False)
name: Mapped[str] = mapped_column(String(32), nullable=False)
zone_id: Mapped[int] = mapped_column(Integer, ForeignKey("zones.id"), nullable=False)
zone_id: Mapped[int | None] = mapped_column(Integer, ForeignKey("zones.id"), nullable=True)
options: Mapped[str] = mapped_column(Text, default="")
config: Mapped["Config"] = relationship("Config", back_populates="interfaces")
zone: Mapped["Zone"] = relationship("Zone", back_populates="interfaces")
zone: Mapped["Zone | None"] = relationship("Zone", back_populates="interfaces")
class Policy(Base):

View File

@@ -64,7 +64,7 @@ class ZoneOut(BaseModel):
# --- Interface ---
class InterfaceCreate(BaseModel):
name: str
zone_id: int
zone_id: Optional[int] = None
options: str = ""
@@ -78,7 +78,7 @@ class InterfaceOut(BaseModel):
id: int
config_id: int
name: str
zone_id: int
zone_id: Optional[int]
options: str
model_config = {"from_attributes": True}

View File

@@ -28,7 +28,8 @@ class ShorewallGenerator:
def interfaces(self) -> str:
lines = [self._header("interfaces"), "#ZONE".ljust(16) + "INTERFACE".ljust(16) + "OPTIONS\n"]
for iface in self._config.interfaces:
lines.append(self._col(iface.zone.name, iface.name, iface.options or "-"))
zone = iface.zone.name if iface.zone else "-"
lines.append(self._col(zone, iface.name, iface.options or "-"))
return "".join(lines)
def policy(self) -> str: