Skip to content

Commit 5f1f12d

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

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
@@ -55,9 +55,13 @@
5555

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

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

6266
class AccessListForm(NetBoxModelForm):
6367
"""
@@ -719,9 +723,11 @@ def clean(self):
719723
Validates form inputs before submitting:
720724
- Check if action set to remark, but no remark set.
721725
- Check if action set to remark, but source set.
726+
- Check if action set to remark, but destination set.
722727
- Check if action set to remark, but protocol set
723728
- Check remark set, but action not set to remark.
724729
- Check not more than one source is set.
730+
- Check not more than one destination is set.
725731
"""
726732
super().clean()
727733
cleaned_data = self.cleaned_data
@@ -731,8 +737,9 @@ def clean(self):
731737
remark = cleaned_data.get("remark")
732738

733739
sources = ["source_prefix", "source_iprange", "source_ipaddress", "source_aggregate", "source_service"]
740+
destinations = ["destination_prefix", "destination_iprange", "destination_ipaddress", "destination_aggregate", "destination_service"]
741+
734742
source_ports = cleaned_data.get("source_ports")
735-
destination_prefix = cleaned_data.get("destination_prefix")
736743
destination_ports = cleaned_data.get("destination_ports")
737744
protocol = cleaned_data.get("protocol")
738745

@@ -743,11 +750,13 @@ def clean(self):
743750
# Check if action set to remark, but source set.
744751
for source in sources:
745752
error_message[source] = [error_message_action_remark_source_set]
753+
754+
# Check if action set to remark, but destination set.
755+
for destination in destinations:
756+
error_message[destination] = [error_message_action_remark_destination_set]
746757

747758
if source_ports:
748759
error_message["source_ports"] = ["Action is set to remark, Source Ports CANNOT be set."]
749-
if destination_prefix:
750-
error_message["destination_prefix"] = ["Action is set to remark, Destination Prefix CANNOT be set."]
751760
if destination_ports:
752761
error_message["destination_ports"] = ["Action is set to remark, Destination Ports CANNOT be set."]
753762
if protocol:
@@ -760,5 +769,10 @@ def clean(self):
760769
for source in sources:
761770
error_message[source] = [error_message_sources_more_than_one]
762771

772+
# Check not more than one destination is set.
773+
elif sum(bool(cleaned_data.get(destination)) for destination in destinations) > 1:
774+
for destination in destinations:
775+
error_message[destination] = [error_message_destinations_more_than_one]
776+
763777
if error_message:
764778
raise ValidationError(error_message)

0 commit comments

Comments
 (0)