Skip to content

Commit d15398d

Browse files
committed
feat(form): ACLExtendedRule: Change validation to support multiple destinations; Add validation to restrict to a single Destination
1 parent ccfb3fa commit d15398d

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

netbox_acls/forms/models.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,13 @@
5656

5757
# Sets a standard error message for ACL rules with an action of remark, but no source is set.
5858
error_message_action_remark_source_set = "Action is set to remark, Source CANNOT be set."
59+
# Sets a standard error message for ACL rules with an action of remark, but no destination is set.
60+
error_message_action_remark_destination_set = "Action is set to remark, Destination CANNOT be set."
5961

6062
# Sets a standard error message for ACL rules when more than one IP/Host sources are set.
6163
error_message_sources_more_than_one = "Only one IP/Host related Source can be specified."
64+
# Sets a standard error message for ACL rules when more than one IP/Host destinations are set.
65+
error_message_destinations_more_than_one = "Only one IP/Host related Destination can be specified."
6266

6367
class AccessListForm(NetBoxModelForm):
6468
"""
@@ -736,9 +740,11 @@ def clean(self):
736740
Validates form inputs before submitting:
737741
- Check if action set to remark, but no remark set.
738742
- Check if action set to remark, but source set.
743+
- Check if action set to remark, but destination set.
739744
- Check if action set to remark, but protocol set
740745
- Check remark set, but action not set to remark.
741746
- Check not more than one source is set.
747+
- Check not more than one destination is set.
742748
"""
743749
super().clean()
744750
cleaned_data = self.cleaned_data
@@ -748,8 +754,9 @@ def clean(self):
748754
remark = cleaned_data.get("remark")
749755

750756
sources = ["source_prefix", "source_iprange", "source_ipaddress", "source_aggregate", "source_service"]
757+
destinations = ["destination_prefix", "destination_iprange", "destination_ipaddress", "destination_aggregate", "destination_service"]
758+
751759
source_ports = cleaned_data.get("source_ports")
752-
destination_prefix = cleaned_data.get("destination_prefix")
753760
destination_ports = cleaned_data.get("destination_ports")
754761
protocol = cleaned_data.get("protocol")
755762

@@ -760,11 +767,13 @@ def clean(self):
760767
# Check if action set to remark, but source set.
761768
for source in sources:
762769
error_message[source] = [error_message_action_remark_source_set]
770+
771+
# Check if action set to remark, but destination set.
772+
for destination in destinations:
773+
error_message[destination] = [error_message_action_remark_destination_set]
763774

764775
if source_ports:
765776
error_message["source_ports"] = ["Action is set to remark, Source Ports CANNOT be set."]
766-
if destination_prefix:
767-
error_message["destination_prefix"] = ["Action is set to remark, Destination Prefix CANNOT be set."]
768777
if destination_ports:
769778
error_message["destination_ports"] = ["Action is set to remark, Destination Ports CANNOT be set."]
770779
if protocol:
@@ -777,5 +786,10 @@ def clean(self):
777786
for source in sources:
778787
error_message[source] = [error_message_sources_more_than_one]
779788

789+
# Check not more than one destination is set.
790+
elif sum(bool(cleaned_data.get(destination)) for destination in destinations) > 1:
791+
for destination in destinations:
792+
error_message[destination] = [error_message_destinations_more_than_one]
793+
780794
if error_message:
781795
raise ValidationError(error_message)

0 commit comments

Comments
 (0)