Skip to content

Commit 18f87c7

Browse files
committed
feat(serializer): ACLExtendedRule: Change validation to support multiple destinatiions; Add validation to restrict to a single Destination
1 parent ea1dd2c commit 18f87c7

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

netbox_acls/api/serializers.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,15 @@
3232
error_message_no_remark = "Action is set to remark, you MUST add a remark."
3333
# Sets a standard error message for ACL rules with an action of remark, but no source/destination is set.
3434
error_message_action_remark_source_set = "Action is set to remark, Source CANNOT be set."
35+
error_message_action_remark_destination_set = "Action is set to remark, Destination CANNOT be set."
3536
# Sets a standard error message for ACL rules with an action not set to remark, but no remark is set.
3637
error_message_remark_without_action_remark = "CANNOT set remark unless action is set to remark."
3738
# Sets a standard error message for ACL rules no associated to an ACL of the same type.
3839
error_message_acl_type = "Provided parent Access List is not of right type."
3940
# Sets a standard error message for ACL rules when more than one IP/Host sources are set.
4041
error_message_sources_more_than_one = "Only one IP/Host related Source can be specified."
42+
# Sets a standard error message for ACL rules when more than one IP/Host destinations are set.
43+
error_message_destinations_more_than_one = "Only one IP/Host related Destination can be specified."
4144

4245

4346
class AccessListSerializer(NetBoxModelSerializer):
@@ -392,14 +395,17 @@ def validate(self, data):
392395
Validate the ACLExtendedRule django model's inputs before allowing it to update the instance:
393396
- Check if action set to remark, but no remark set.
394397
- Check if action set to remark, but source set.
398+
- Check if action set to remark, but destination set.
395399
- Check if action set to remark, but source_ports set.
396400
- Check if action set to remark, but destination_ports set.
397401
- Check if action set to remark, but protocol set.
398402
- Check not more than one source is set.
403+
- Check not more than one destination is set.
399404
"""
400405
error_message = {}
401406

402407
sources = ["source_prefix", "source_iprange", "source_ipaddress", "source_aggregate", "source_service"]
408+
destinations = ["destination_prefix", "destination_iprange", "destination_ipaddress", "destination_aggregate", "destination_service"]
403409

404410
if data.get("action") == "remark":
405411
# Check if action set to remark, but no remark set.
@@ -411,16 +417,15 @@ def validate(self, data):
411417
if any(data.get(source) for source in sources):
412418
for source in sources:
413419
error_message[source] = [error_message_action_remark_source_set]
420+
# Check if action set to remark, but destination set.
421+
if any(data.get(destination) for destination in destinations):
422+
for destination in destinations:
423+
error_message[destination] = [error_message_action_remark_destination_set]
414424
# Check if action set to remark, but source_ports set.
415425
if data.get("source_ports"):
416426
error_message["source_ports"] = [
417427
"Action is set to remark, Source Ports CANNOT be set.",
418428
]
419-
# Check if action set to remark, but destination_prefix set.
420-
if data.get("destination_prefix"):
421-
error_message["destination_prefix"] = [
422-
"Action is set to remark, Destination Prefix CANNOT be set.",
423-
]
424429
# Check if action set to remark, but destination_ports set.
425430
if data.get("destination_ports"):
426431
error_message["destination_ports"] = [
@@ -437,6 +442,10 @@ def validate(self, data):
437442
for source in sources:
438443
error_message[source] = [error_message_sources_more_than_one]
439444

445+
# Check not more than one destination is set.
446+
if sum(bool(data.get(destination)) for destination in destinations) > 1:
447+
for destination in destinations:
448+
error_message[destination] = [error_message_destinations_more_than_one]
440449

441450
if error_message:
442451
raise serializers.ValidationError(error_message)

0 commit comments

Comments
 (0)