Skip to content

Commit eb44575

Browse files
committed
draft host logic model
1 parent 6fd9fba commit eb44575

File tree

2 files changed

+66
-30
lines changed

2 files changed

+66
-30
lines changed

netbox_acls/models/access_lists.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from dcim.models import Device, Interface, VirtualChassis
66
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
77
from django.contrib.contenttypes.models import ContentType
8+
from django.core.exceptions import ValidationError
89
from django.core.validators import RegexValidator
910
from django.db import models
1011
from django.urls import reverse
@@ -146,6 +147,22 @@ def get_absolute_url(self):
146147
args=[self.pk],
147148
)
148149

150+
def clean(self):
151+
super().clean()
152+
153+
# Get the model type of the assigned interface.
154+
if self.assigned_object_type.model_class() == VMInterface:
155+
interface_host = self.assigned_object.virtual_machine
156+
elif self.assigned_object_type.model_class() == Interface:
157+
interface_host = self.assigned_object.device
158+
# Check if the assigned interface's host is the same as the host assigned to the access list.
159+
if interface_host != self.access_list.assigned_object:
160+
raise ValidationError(
161+
{
162+
"assigned_object": "The assigned object must be the same as the device assigned to it."
163+
}
164+
)
165+
149166
@classmethod
150167
def get_prerequisite_models(cls):
151168
return [AccessList]

netbox_acls/tests/test_models.py

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,28 @@ def setUpTestData(cls):
5151
device_type=devicetype,
5252
device_role=devicerole,
5353
)
54-
virtual_chassis = VirtualChassis.objects.create(name="Virtual Chassis 1")
55-
virtual_chassis_member = Device.objects.create(
56-
name="VC Device",
57-
site=site,
58-
device_type=devicetype,
59-
device_role=devicerole,
60-
virtual_chassis=virtual_chassis,
61-
vc_position=1,
62-
)
63-
cluster_member = Device.objects.create(
64-
name="Cluster Device",
65-
site=site,
66-
device_type=devicetype,
67-
device_role=devicerole,
68-
)
69-
clustertype = ClusterType.objects.create(name="Cluster Type 1")
70-
cluster = Cluster.objects.create(
71-
name="Cluster 1",
72-
type=clustertype,
73-
)
54+
# virtual_chassis = VirtualChassis.objects.create(name="Virtual Chassis 1")
55+
# virtual_chassis_member = Device.objects.create(
56+
# name="VC Device",
57+
# site=site,
58+
# device_type=devicetype,
59+
# device_role=devicerole,
60+
# virtual_chassis=virtual_chassis,
61+
# vc_position=1,
62+
# )
63+
# cluster_member = Device.objects.create(
64+
# name="Cluster Device",
65+
# site=site,
66+
# device_type=devicetype,
67+
# device_role=devicerole,
68+
# )
69+
# clustertype = ClusterType.objects.create(name="Cluster Type 1")
70+
# cluster = Cluster.objects.create(
71+
# name="Cluster 1",
72+
# type=clustertype,
73+
# )
7474
virtual_machine = VirtualMachine.objects.create(name="VirtualMachine 1")
75+
virtual_machine.save()
7576
prefix = Prefix.objects.create(prefix="10.0.0.0/8")
7677

7778

@@ -256,12 +257,12 @@ def setUpTestData(cls):
256257
VMInterface(name="Interface 2", virtual_machine=virtual_machine),
257258
)
258259
)
259-
prefixes = Prefix.objects.bulk_create(
260-
(
261-
Prefix(prefix=IPNetwork("10.0.0.0/24")),
262-
Prefix(prefix=IPNetwork("192.168.1.0/24")),
263-
)
264-
)
260+
#prefixes = Prefix.objects.bulk_create(
261+
# (
262+
# Prefix(prefix=IPNetwork("10.0.0.0/24")),
263+
# Prefix(prefix=IPNetwork("192.168.1.0/24")),
264+
# )
265+
#)
265266

266267
def test_acl_interface_assignment_success(self):
267268
"""
@@ -272,18 +273,36 @@ def test_acl_interface_assignment_success(self):
272273
comments="STANDARD_ACL",
273274
type="standard",
274275
default_action="permit",
275-
assigned_object_id=1,
276-
assigned_object_type=ContentType.objects.get_for_model(Device),
276+
assigned_object=Device.objects.first(),
277277
)
278278
device_acl.save()
279279
acl_device_interface = ACLInterfaceAssignment(
280280
access_list=device_acl,
281281
direction="ingress",
282-
assigned_object_id=1,
283-
assigned_object_type=ContentType.objects.get_for_model(Interface),
282+
assigned_object=Interface.objects.first(),
284283
)
285284
acl_device_interface.full_clean()
286285

286+
def test_aclinterface_assignment_fail(self):
287+
"""
288+
Test that ACLInterfaceAssignment passes validation if the ACL is assigned to the host and not already assigned to the vminterface and direction.
289+
"""
290+
device_acl = AccessList(
291+
name="STANDARD_ACL",
292+
comments="STANDARD_ACL",
293+
type="standard",
294+
default_action="permit",
295+
assigned_object=Device.objects.first(),
296+
)
297+
device_acl.save()
298+
acl_vm_interface = ACLInterfaceAssignment(
299+
access_list=device_acl,
300+
direction="ingress",
301+
assigned_object=VMInterface.objects.first(),
302+
)
303+
with self.assertRaises(ValidationError):
304+
acl_vm_interface.full_clean()
305+
287306
def test_acl_vminterface_assignment_success(self):
288307
"""
289308
Test that ACLInterfaceAssignment passes validation if the ACL is assigned to the host and not already assigned to the vminterface and direction.

0 commit comments

Comments
 (0)