Skip to content

Commit 1ddadf3

Browse files
committed
feat(serializer): ACLStandardRule: Change validation to support multiple sources; Add validation to restrict to a single Source
1 parent 516841e commit 1ddadf3

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

netbox_acls/api/serializers.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@
3030

3131
# Sets a standard error message for ACL rules with an action of remark, but no remark set.
3232
error_message_no_remark = "Action is set to remark, you MUST add a remark."
33-
# Sets a standard error message for ACL rules with an action of remark, but no source_prefix is set.
34-
error_message_action_remark_source_prefix_set = "Action is set to remark, Source Prefix CANNOT be set."
33+
# Sets a standard error message for ACL rules with an action of remark, but no source/destination is set.
34+
error_message_action_remark_source_set = "Action is set to remark, Source CANNOT be set."
3535
# Sets a standard error message for ACL rules with an action not set to remark, but no remark is set.
3636
error_message_remark_without_action_remark = "CANNOT set remark unless action is set to remark."
3737
# Sets a standard error message for ACL rules no associated to an ACL of the same type.
3838
error_message_acl_type = "Provided parent Access List is not of right type."
39+
# Sets a standard error message for ACL rules when more than one IP/Host sources are set.
40+
error_message_sources_more_than_one = "Only one IP/Host related Source can be specified."
3941

4042

4143
class AccessListSerializer(NetBoxModelSerializer):
@@ -247,21 +249,28 @@ def validate(self, data):
247249
"""
248250
Validate the ACLStandardRule django model's inputs before allowing it to update the instance:
249251
- Check if action set to remark, but no remark set.
250-
- Check if action set to remark, but source_prefix set.
252+
- Check if action set to remark, but source set.
253+
- Check not more than one source is set.
251254
"""
252255
error_message = {}
253256

257+
sources = ["source_prefix", "source_iprange", "source_ipaddress", "source_aggregate", "source_service"]
258+
254259
if data.get("action") == "remark":
255260
# Check if action set to remark, but no remark set.
256261
if data.get("remark") is None:
257262
error_message["remark"] = [
258263
error_message_no_remark,
259264
]
260-
# Check if action set to remark, but source_prefix set.
261-
if data.get("source_prefix"):
262-
error_message["source_prefix"] = [
263-
error_message_action_remark_source_prefix_set,
264-
]
265+
# Check if action set to remark, but source set.
266+
if any(data.get(source) for source in sources):
267+
for source in sources:
268+
error_message[source] = [error_message_action_remark_source_set]
269+
270+
# Check not more than one source is set.
271+
if sum(bool(data.get(source)) for source in sources) > 1:
272+
for source in sources:
273+
error_message[source] = [error_message_sources_more_than_one]
265274

266275
if error_message:
267276
raise serializers.ValidationError(error_message)

0 commit comments

Comments
 (0)