Netztyp VPN: standortübergreifende Tunnelnetze, Konzept v0.6

- Netz-Typ VPN; Standort optional nur für VPN, kein DHCP-Bereich
- Eindeutigkeit von Netzen ohne Standort per bedingter UniqueConstraint
- README: Netztyp, Regel, WireGuard-Netz 172.25.25.0/24 in Kapitel 10
- AUFBAUPLAN: Schritt 2 (Gegenstelle, Tunnel, Standort-Rahmen) in Phase 7
- 66 Tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
H4nSoloandClaude Opus 5.5 committed 2026-11-24 14:47:59 +01:00
1 parent ab8e0d8ffd
commit ae15a0aba8
5 files changed
+66 -4

No files matched your search

+28
View File
@@ -0,0 +1,28 @@
# Generated by Django 5.2.19 on 2026-11-24 13:22
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('inventar', '0003_ipadresse_bridge'),
]
operations = [
migrations.AlterField(
model_name='netz',
name='standort',
field=models.ForeignKey(blank=True, help_text='Bei VPN-Netzen leer lassen – ein Tunnelnetz gehört beiden Seiten.', null=True, on_delete=django.db.models.deletion.PROTECT, related_name='netze', to='inventar.standort'),
),
migrations.AlterField(
model_name='netz',
name='typ',
field=models.CharField(choices=[('transfer', 'Transfer'), ('lan', 'LAN'), ('vlan', 'VLAN'), ('vpn', 'VPN')], max_length=10),
),
migrations.AddConstraint(
model_name='netz',
constraint=models.UniqueConstraint(condition=models.Q(('standort__isnull', True)), fields=('cidr',), name='cidr_ohne_standort_eindeutig', violation_error_message='Dieses standortübergreifende Netz gibt es bereits.'),
),
]
+17 -1
View File
@@ -225,6 +225,7 @@ class Netz(GeprueftesModel):
TRANSFER = "transfer", "Transfer"
LAN = "lan", "LAN"
VLAN = "vlan", "VLAN"
VPN = "vpn", "VPN"
name = models.CharField(max_length=100)
slug = models.SlugField("Logische ID", unique=True)
@@ -234,7 +235,10 @@ class Netz(GeprueftesModel):
gateway = models.GenericIPAddressField(null=True, blank=True)
dhcp_von = models.GenericIPAddressField("DHCP von", null=True, blank=True)
dhcp_bis = models.GenericIPAddressField("DHCP bis", null=True, blank=True)
standort = models.ForeignKey(Standort, on_delete=models.PROTECT, related_name="netze")
standort = models.ForeignKey(
Standort, on_delete=models.PROTECT, null=True, blank=True, related_name="netze",
help_text="Bei VPN-Netzen leer lassen – ein Tunnelnetz gehört beiden Seiten.",
)
class Meta:
ordering = ["cidr"]
@@ -244,6 +248,11 @@ class Netz(GeprueftesModel):
fields=["standort", "cidr"], name="cidr_je_standort_eindeutig",
violation_error_message="Dieses Netz gibt es am Standort bereits.",
),
# NULL zählt in UniqueConstraints nicht als gleich – standortlose Netze extra prüfen
models.UniqueConstraint(
fields=["cidr"], condition=models.Q(standort__isnull=True), name="cidr_ohne_standort_eindeutig",
violation_error_message="Dieses standortübergreifende Netz gibt es bereits.",
),
]
def __str__(self):
@@ -274,6 +283,13 @@ class Netz(GeprueftesModel):
fehler["dhcp_bis"] = "Ende liegt vor dem Anfang."
if self.typ == self.Typ.VLAN and not self.vlan_id:
fehler["vlan_id"] = "Ein VLAN-Netz braucht eine VLAN-ID."
if self.typ == self.Typ.VPN:
if self.standort_id:
fehler["standort"] = "VPN-Netze gehören keinem Standort (Tunnel zwischen Standorten)."
if self.dhcp_von:
fehler["dhcp_von"] = "VPN-Netze haben keinen DHCP-Bereich."
elif not self.standort_id:
fehler["standort"] = "Nur VPN-Netze sind ohne Standort."
if fehler:
raise ValidationError(fehler)
+16
View File
@@ -270,6 +270,22 @@ class NetzTests(Homelab):
with self.assertRaises(ValidationError):
self.netz(dhcp_von="192.168.178.200", dhcp_bis="192.168.178.100")
def test_vpn_netz_ohne_standort(self):
vpn = self.netz(name="WireGuard", slug="wireguard", cidr="172.25.25.0/24", typ=Netz.Typ.VPN, standort=None)
self.assertIsNone(vpn.standort)
with self.assertRaises(ValidationError):
self.netz(name="Doppelt", slug="doppelt", cidr="172.25.25.0/24", typ=Netz.Typ.VPN, standort=None)
def test_vpn_netz_nicht_an_standort(self):
with self.assertRaises(ValidationError) as f:
self.netz(cidr="172.25.25.0/24", typ=Netz.Typ.VPN)
self.assertIn("standort", f.exception.message_dict)
def test_andere_netze_brauchen_standort(self):
with self.assertRaises(ValidationError) as f:
self.netz(standort=None)
self.assertIn("standort", f.exception.message_dict)
def test_vlan_netz_braucht_vlan_id(self):
with self.assertRaises(ValidationError):
self.netz(typ=Netz.Typ.VLAN)