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