Skip to content

Commit

Permalink
Fix constraints and column types
Browse files Browse the repository at this point in the history
Refs #330
  • Loading branch information
lukasjuhrich committed Sep 6, 2023
1 parent a8d4f6e commit 7598ceb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions pycroft/model/nat.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from sqlalchemy import CheckConstraint, Column, ForeignKey, \
ForeignKeyConstraint, Integer, SmallInteger, String, Table, Text, \
UniqueConstraint, text, func, and_
Integer, SmallInteger, String, Text, \
UniqueConstraint, func, and_
from sqlalchemy.orm import relationship, backref

from pycroft.model.base import IntegerIdModel, ModelBase
from pycroft.model.net import Subnet
from pycroft.model.types import IPAddress, MACAddress
from pycroft.model.types import IPAddress, MACAddress, IPNetwork
from pycroft.model.user import User


def single_ipv4_constraint(col: Column):
return CheckConstraint(and_(func.family(col) == 4, func.masklen(col) == 32))


class NATDomain(IntegerIdModel):
name = Column(String, nullable=False)

Expand All @@ -22,7 +25,7 @@ class DHCPHostReservation(ModelBase):
mac = Column(MACAddress, nullable=False)

__table_args__ = (
CheckConstraint(and_(func.family(ip) == 4, func.masklen(ip) == 32)),
single_ipv4_constraint(col=ip),
)


Expand All @@ -31,7 +34,7 @@ class InsideNetwork(ModelBase):
nullable=False)
nat_domain = relationship(NATDomain)

ip_network = Column(Subnet, primary_key=True, nullable=False)
ip_network = Column(IPNetwork, primary_key=True, nullable=False)
gateway = Column(IPAddress, nullable=False)

__table_args__ = (
Expand All @@ -49,8 +52,7 @@ class OutsideIPAddress(ModelBase):
owner = Column(Integer)

__table_args__ = (
CheckConstraint(
'(family(ip_address) = 4) AND (masklen(ip_address) = 32)'),
single_ipv4_constraint(col=ip_address),
)


Expand All @@ -60,15 +62,15 @@ class Translation(ModelBase):
nat_domain = relationship(NATDomain)

outside_address = Column(IPAddress, primary_key=True, nullable=False)
inside_network = Column(Subnet, nullable=False)
inside_network = Column(IPNetwork, nullable=False)

owner_id = Column(Integer, ForeignKey(User.id, ondelete="CASCADE"),
nullable=False)
owner = relationship(User, backref=backref("translations",
cascade="all, delete-orphan"))

__table_args__ = (
CheckConstraint(and_(func.family(ip) == 4, func.masklen(ip) == 32)),
single_ipv4_constraint(col=outside_address),
)


Expand Down
Empty file added tests/factories/nat.py
Empty file.
Empty file added tests/model/test_nat.py
Empty file.

0 comments on commit 7598ceb

Please sign in to comment.