|
31 | 31 |
|
32 | 32 | # Sets a standard error message for ACL rules with an action of remark, but no remark set.
|
33 | 33 | 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." |
36 | 36 | # Sets a standard error message for ACL rules with an action not set to remark, but no remark is set.
|
37 | 37 | error_message_remark_without_action_remark = "CANNOT set remark unless action is set to remark."
|
38 | 38 | # Sets a standard error message for ACL rules no associated to an ACL of the same type.
|
39 | 39 | 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." |
40 | 42 |
|
41 | 43 |
|
42 | 44 | class AccessListSerializer(NetBoxModelSerializer):
|
@@ -246,21 +248,28 @@ def validate(self, data):
|
246 | 248 | """
|
247 | 249 | Validate the ACLStandardRule django model's inputs before allowing it to update the instance:
|
248 | 250 | - 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. |
250 | 253 | """
|
251 | 254 | error_message = {}
|
252 | 255 |
|
| 256 | + sources = ["source_prefix", "source_iprange", "source_ipaddress", "source_aggregate", "source_service"] |
| 257 | + |
253 | 258 | if data.get("action") == "remark":
|
254 | 259 | # Check if action set to remark, but no remark set.
|
255 | 260 | if data.get("remark") is None:
|
256 | 261 | error_message["remark"] = [
|
257 | 262 | error_message_no_remark,
|
258 | 263 | ]
|
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] |
264 | 273 |
|
265 | 274 | if error_message:
|
266 | 275 | raise serializers.ValidationError(error_message)
|
|
0 commit comments