Skip to content

Commit 2780713

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

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
@@ -33,12 +33,15 @@
3333
error_message_no_remark = "Action is set to remark, you MUST add a remark."
3434
# Sets a standard error message for ACL rules with an action of remark, but no source/destination is set.
3535
error_message_action_remark_source_set = "Action is set to remark, Source CANNOT be set."
36+
error_message_action_remark_destination_set = "Action is set to remark, Destination CANNOT be set."
3637
# Sets a standard error message for ACL rules with an action not set to remark, but no remark is set.
3738
error_message_remark_without_action_remark = "CANNOT set remark unless action is set to remark."
3839
# Sets a standard error message for ACL rules no associated to an ACL of the same type.
3940
error_message_acl_type = "Provided parent Access List is not of right type."
4041
# Sets a standard error message for ACL rules when more than one IP/Host sources are set.
4142
error_message_sources_more_than_one = "Only one IP/Host related Source can be specified."
43+
# Sets a standard error message for ACL rules when more than one IP/Host destinations are set.
44+
error_message_destinations_more_than_one = "Only one IP/Host related Destination can be specified."
4245

4346

4447
class AccessListSerializer(NetBoxModelSerializer):
@@ -381,14 +384,17 @@ def validate(self, data):
381384
Validate the ACLExtendedRule django model's inputs before allowing it to update the instance:
382385
- Check if action set to remark, but no remark set.
383386
- Check if action set to remark, but source set.
387+
- Check if action set to remark, but destination set.
384388
- Check if action set to remark, but source_ports set.
385389
- Check if action set to remark, but destination_ports set.
386390
- Check if action set to remark, but protocol set.
387391
- Check not more than one source is set.
392+
- Check not more than one destination is set.
388393
"""
389394
error_message = {}
390395

391396
sources = ["source_prefix", "source_iprange", "source_ipaddress", "source_aggregate", "source_service"]
397+
destinations = ["destination_prefix", "destination_iprange", "destination_ipaddress", "destination_aggregate", "destination_service"]
392398

393399
if data.get("action") == "remark":
394400
# Check if action set to remark, but no remark set.
@@ -400,16 +406,15 @@ def validate(self, data):
400406
if any(data.get(source) for source in sources):
401407
for source in sources:
402408
error_message[source] = [error_message_action_remark_source_set]
409+
# Check if action set to remark, but destination set.
410+
if any(data.get(destination) for destination in destinations):
411+
for destination in destinations:
412+
error_message[destination] = [error_message_action_remark_destination_set]
403413
# Check if action set to remark, but source_ports set.
404414
if data.get("source_ports"):
405415
error_message["source_ports"] = [
406416
"Action is set to remark, Source Ports CANNOT be set.",
407417
]
408-
# Check if action set to remark, but destination_prefix set.
409-
if data.get("destination_prefix"):
410-
error_message["destination_prefix"] = [
411-
"Action is set to remark, Destination Prefix CANNOT be set.",
412-
]
413418
# Check if action set to remark, but destination_ports set.
414419
if data.get("destination_ports"):
415420
error_message["destination_ports"] = [
@@ -426,6 +431,10 @@ def validate(self, data):
426431
for source in sources:
427432
error_message[source] = [error_message_sources_more_than_one]
428433

434+
# Check not more than one destination is set.
435+
if sum(bool(data.get(destination)) for destination in destinations) > 1:
436+
for destination in destinations:
437+
error_message[destination] = [error_message_destinations_more_than_one]
429438

430439
if error_message:
431440
raise serializers.ValidationError(error_message)

0 commit comments

Comments
 (0)