Skip to content

Commit ca4f19c

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

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
@@ -31,12 +31,14 @@
3131

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

4143

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

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

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

0 commit comments

Comments
 (0)