Merge "Fix audit creation with no name and no goal or audit_template"

This commit is contained in:
Zuul
2025-06-05 13:39:38 +00:00
committed by Gerrit Code Review
3 changed files with 20 additions and 9 deletions

View File

@@ -0,0 +1,10 @@
---
fixes:
- |
Previously, when users attempted to create a new audit without providing
a name and a goal or an audit template, the API returned error 500 and an
incorrect error message was displayed.
Now, Watcher displays a helpful message and returns HTTP error 400.
For more info see: https://bugs.launchpad.net/watcher/+bug/2110947

View File

@@ -115,6 +115,11 @@ class AuditPostType(wtypes.Base):
if self.audit_type not in audit_type_values:
raise exception.AuditTypeNotFound(audit_type=self.audit_type)
if not self.audit_template_uuid and not self.goal:
message = _(
'A valid goal or audit_template_id must be provided')
raise exception.Invalid(message)
if (self.audit_type == objects.audit.AuditType.ONESHOT.value and
self.interval not in (wtypes.Unset, None)):
raise exception.AuditIntervalNotAllowed(audit_type=self.audit_type)
@@ -613,11 +618,6 @@ class AuditsController(rest.RestController):
if self.from_audits:
raise exception.OperationNotPermitted
if not audit._goal_uuid:
raise exception.Invalid(
message=_('A valid goal_id or audit_template_id '
'must be provided'))
strategy_uuid = audit.strategy_uuid
no_schema = True
if strategy_uuid is not None:

View File

@@ -1026,10 +1026,11 @@ class TestPost(api_base.FunctionalTest):
expect_errors=True,
headers={'OpenStack-API-Version': 'infra-optim 1.2'})
self.assertEqual('application/json', response.content_type)
# (amoralej) this should return HTTP error 400 with a proper message.
# I am adding this test to show the bug here, I will switch it to the
# expected error in the fixing patch.
self.assertEqual(HTTPStatus.INTERNAL_SERVER_ERROR, response.status_int)
self.assertEqual(HTTPStatus.BAD_REQUEST, response.status_int)
expected_msg = 'A valid goal or audit_template_id must be provided'
self.assertTrue(response.json['error_message'])
self.assertIn(expected_msg, response.json['error_message'])
assert not mock_trigger_audit.called
class TestDelete(api_base.FunctionalTest):