Return HTTP code 400 when creating an audit with wrong parameters

Currently, when trying to create an audit which misses a mandatory
parameter watcher returns error 500 instead of 400 which is the
documented error in the API [1] and the appropiate error code for
malformed requests.

This patch catch parameters validation errors according to the json
schema for each strategy and returns error 400. It also fixes the
unit test to validate the expected behavior.

[1] https://docs.openstack.org/api-ref/resource-optimization/#audits

Closes-Bug: #2110538
Change-Id: I23232b3b54421839bb01d54386d4e7b244f4e2a0
This commit is contained in:
Alfredo Moralejo
2025-05-14 09:20:25 +02:00
parent 891119470c
commit 4629402f38
3 changed files with 19 additions and 11 deletions

View File

@@ -33,6 +33,7 @@ import datetime
from dateutil import tz
from http import HTTPStatus
import jsonschema
from oslo_log import log
from oslo_utils import timeutils
import pecan
@@ -627,8 +628,12 @@ class AuditsController(rest.RestController):
if schema:
# validate input parameter with default value feedback
no_schema = False
utils.StrictDefaultValidatingDraft4Validator(schema).validate(
audit.parameters)
try:
utils.StrictDefaultValidatingDraft4Validator(
schema).validate(audit.parameters)
except jsonschema.exceptions.ValidationError as e:
raise exception.Invalid(
_('Invalid parameters for strategy: %s') % e)
if no_schema and audit.parameters:
raise exception.Invalid(_('Specify parameters but no predefined '