Skip to content

Commit 9bd4f1f

Browse files
committed
refactor(serializers): Update serializers to use ACLAssignment model
Replaces `ACLInterfaceAssignmentSerializer` with `ACLAssignmentSerializer` to align with the unified ACLAssignment model. Removes legacy fields and validation logic that depended on outdated models. Enhances maintainability by consolidating serializers under the updated model structure. BREAKING CHANGE: Legacy ACLInterfaceAssignmentSerializer is replaced with ACLAssignmentSerializer.
1 parent 373430f commit 9bd4f1f

File tree

1 file changed

+9
-52
lines changed

1 file changed

+9
-52
lines changed

netbox_acls/api/serializers.py

Lines changed: 9 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111
from rest_framework import serializers
1212
from utilities.api import get_serializer_for_model
1313

14-
from ..constants import ACL_HOST_ASSIGNMENT_MODELS, ACL_INTERFACE_ASSIGNMENT_MODELS
14+
from ..constants import ACL_ASSIGNMENT_MODELS
1515
from ..models import (
1616
AccessList,
1717
ACLExtendedRule,
18-
ACLInterfaceAssignment,
18+
ACLAssignment,
1919
ACLStandardRule,
2020
)
2121

2222
__all__ = [
2323
"AccessListSerializer",
24-
"ACLInterfaceAssignmentSerializer",
24+
"ACLAssignmentSerializer",
2525
"ACLStandardRuleSerializer",
2626
"ACLExtendedRuleSerializer",
2727
]
@@ -45,10 +45,6 @@ class AccessListSerializer(NetBoxModelSerializer):
4545
view_name="plugins-api:netbox_acls-api:accesslist-detail",
4646
)
4747
rule_count = serializers.IntegerField(read_only=True)
48-
assigned_object_type = ContentTypeField(
49-
queryset=ContentType.objects.filter(ACL_HOST_ASSIGNMENT_MODELS),
50-
)
51-
assigned_object = serializers.SerializerMethodField(read_only=True)
5248

5349
class Meta:
5450
"""
@@ -61,9 +57,6 @@ class Meta:
6157
"url",
6258
"display",
6359
"name",
64-
"assigned_object_type",
65-
"assigned_object_id",
66-
"assigned_object",
6760
"type",
6861
"default_action",
6962
"comments",
@@ -75,14 +68,6 @@ class Meta:
7568
)
7669
brief_fields = ("id", "url", "display", "name")
7770

78-
@extend_schema_field(serializers.JSONField(allow_null=True))
79-
def get_assigned_object(self, obj):
80-
if obj.assigned_object is None:
81-
return None
82-
serializer = get_serializer_for_model(obj.assigned_object)
83-
context = {"request": self.context["request"]}
84-
return serializer(obj.assigned_object, nested=True, context=context).data
85-
8671
def validate(self, data):
8772
"""
8873
Validates api inputs before processing:
@@ -103,26 +88,26 @@ def validate(self, data):
10388
return super().validate(data)
10489

10590

106-
class ACLInterfaceAssignmentSerializer(NetBoxModelSerializer):
91+
class ACLAssignmentSerializer(NetBoxModelSerializer):
10792
"""
108-
Defines the serializer for the django ACLInterfaceAssignment model and associates it with a view.
93+
Defines the serializer for the django ACLAssignment model and associates it with a view.
10994
"""
11095

11196
url = serializers.HyperlinkedIdentityField(
112-
view_name="plugins-api:netbox_acls-api:aclinterfaceassignment-detail",
97+
view_name="plugins-api:netbox_acls-api:aclassignment-detail",
11398
)
11499
access_list = AccessListSerializer(nested=True, required=True)
115100
assigned_object_type = ContentTypeField(
116-
queryset=ContentType.objects.filter(ACL_INTERFACE_ASSIGNMENT_MODELS),
101+
queryset=ContentType.objects.filter(ACL_ASSIGNMENT_MODELS),
117102
)
118103
assigned_object = serializers.SerializerMethodField(read_only=True)
119104

120105
class Meta:
121106
"""
122-
Associates the django model ACLInterfaceAssignment & fields to the serializer.
107+
Associates the django model ACLAssignment & fields to the serializer.
123108
"""
124109

125-
model = ACLInterfaceAssignment
110+
model = ACLAssignment
126111
fields = (
127112
"id",
128113
"url",
@@ -148,34 +133,6 @@ def get_assigned_object(self, obj):
148133
context = {"request": self.context["request"]}
149134
return serializer(obj.assigned_object, nested=True, context=context).data
150135

151-
def validate(self, data):
152-
"""
153-
Validate the AccessList django model's inputs before allowing it to update the instance.
154-
- Check that the GFK object is valid.
155-
- Check that the associated interface's parent host has the selected ACL defined.
156-
"""
157-
error_message = {}
158-
acl_host = data["access_list"].assigned_object
159-
160-
if data["assigned_object_type"].model == "interface":
161-
interface_host = data["assigned_object_type"].get_object_for_this_type(id=data["assigned_object_id"]).device
162-
elif data["assigned_object_type"].model == "vminterface":
163-
interface_host = (
164-
data["assigned_object_type"].get_object_for_this_type(id=data["assigned_object_id"]).virtual_machine
165-
)
166-
else:
167-
interface_host = None
168-
# Check that the associated interface's parent host has the selected ACL defined.
169-
if acl_host != interface_host:
170-
error_acl_not_assigned_to_host = "Access List not present on the selected interface's host."
171-
error_message["access_list"] = [error_acl_not_assigned_to_host]
172-
error_message["assigned_object_id"] = [error_acl_not_assigned_to_host]
173-
174-
if error_message:
175-
raise serializers.ValidationError(error_message)
176-
177-
return super().validate(data)
178-
179136

180137
class ACLStandardRuleSerializer(NetBoxModelSerializer):
181138
"""

0 commit comments

Comments
 (0)