Merge "api: Add schemas for messages"

This commit is contained in:
Zuul 2024-08-23 16:12:47 +00:00 committed by Gerrit Code Review
commit 4bf505404a
6 changed files with 191 additions and 6 deletions

View File

@ -0,0 +1,172 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import copy
from oslo_config import cfg
from manila.api.validation import parameter_types
from manila.api.validation import response_types
CONF = cfg.CONF
show_request_query = {
'type': 'object',
'properties': {},
'required': [],
# TODO(jonathan): Exclude additional query string parameters in a future
# microversion
'additionalProperties': True,
}
index_request_query = {
'type': 'object',
'properties': {
'limit': parameter_types.single_param(
parameter_types.non_negative_integer
),
# NOTE(stephenfin): This is parsed by 'common.get_pagination_params'
# but we ignore it. We may wish to uncomment this when that is no
# longer the case
# 'marker': parameter_types.multi_params({
# 'type': ['string'],
# }),
'offset': parameter_types.single_param(
parameter_types.non_negative_integer
),
'sort_key': parameter_types.single_param({
'type': 'string',
'default': 'created_at',
# TODO(stephenfin): These are the allowed (a.k.a. legal) filter
# keys, but we currently ignore invalid keys. We should add this in
# a future microversion.
# 'enum': [
# 'id',
# 'project_id',
# 'request_id',
# 'resource_type',
# 'action_id',
# 'detail_id',
# 'resource_id',
# 'message_level',
# 'expires_at',
# 'created_at',
# ],
}),
'sort_dir': parameter_types.single_param({
'type': 'string',
'default': 'desc',
# TODO(stephenfin): This should be an enum, but we currently treat
# anything != 'desc' as 'asc'. We should make this stricter in a
# future microversion.
# 'enum': ['asc', 'desc'],
}),
'action_id': parameter_types.single_param({
'type': 'string',
}),
'detail_id': parameter_types.single_param({
'type': 'string',
}),
# TODO(jonathan) add enum when more message level the 'ERROR'
'message_level': parameter_types.single_param({
'type': 'string',
}),
'request_id': parameter_types.single_param({
'type': 'string',
}),
'resource_id': parameter_types.single_param({
'type': 'string',
}),
'resource_type': parameter_types.multi_params({
'type': 'string',
}),
},
'required': [],
# TODO(jonathan): Exclude additional query string parameters in a future
# microversion
'additionalProperties': True,
}
index_request_query_v252 = copy.deepcopy(index_request_query)
index_request_query_v252['properties'].update({
'created_since': parameter_types.single_param({
'type': 'string',
'format': 'date-time',
}),
'created_before': parameter_types.single_param({
'type': 'string',
'format': 'date-time',
}),
})
_messages_response = {
'type': 'object',
'properties': {
'action_id': {'type': 'string'},
'created_at': {'type': 'string', 'format': 'date-time'},
'detail_id': {'type': 'string'},
'expires_at': {'type': 'string', 'format': 'date-time'},
'id': {'type': 'string', 'format': 'uuid'},
'links': response_types.links,
'message_level': {
'type': 'string',
'enum': ['ERROR'],
},
'project_id': {'type': 'string'},
'request_id': {'type': 'string'},
'resource_id': {'type': 'string', 'format': 'uuid'},
'resource_type': {'type': 'string'},
'user_message': {'type': 'string'},
},
'required': [
'action_id',
'created_at',
'detail_id',
'expires_at',
'id',
'links',
'message_level',
'project_id',
'request_id',
'resource_id',
'resource_type',
'user_message',
],
'additionalProperties': False,
}
index_response_body = {
'type': 'object',
'properties': {
'messages': {
'type': 'array',
'items': _messages_response,
},
'messages_links': response_types.collection_links,
},
'required': ['messages'],
'additionalProperties': False,
}
show_response_body = {
'type': 'object',
'properties': {
'message': _messages_response,
},
'required': ['message'],
'additionalProperties': False,
}
delete_response_body = {
'type': 'null',
}

View File

@ -117,10 +117,13 @@ index_request_query = {
'default': 'created_at',
'description': helpers.description('sort_key_resource_locks'),
}),
# TODO(stephenfin): Make this an enum of ['asc', 'desc']
'sort_dir': parameter_types.multi_params({
'type': 'string',
'default': 'desc',
# TODO(stephenfin): This should be an enum, but we currently treat
# anything != 'desc' as 'asc'. We should make this stricter in a
# future microversion.
# 'enum': ['asc', 'desc'],
'description': helpers.description('sort_dir'),
}),
'with_count': parameter_types.multi_params(parameter_types.boolean),
@ -144,7 +147,7 @@ index_request_query = {
'user_id': parameter_types.multi_params({
'type': ['string', 'null'],
'format': 'uuid',
'description': helpers.description('resource_lock_user_id_query')
'description': helpers.description('resource_lock_user_id_query'),
}),
'resource_id': parameter_types.multi_params({
'type': ['string', 'null'],

View File

@ -26,6 +26,8 @@ from webob import exc
from manila.api import common
from manila.api.openstack import wsgi
from manila.api.schemas import messages as schema
from manila.api import validation
from manila.api.views import messages as messages_view
from manila import exception
from manila.i18n import _
@ -35,9 +37,9 @@ MESSAGES_BASE_MICRO_VERSION = '2.37'
MESSAGES_QUERY_BY_TIMESTAMP = '2.52'
@validation.validated
class MessagesController(wsgi.Controller):
"""The User Messages API controller for the OpenStack API."""
_view_builder_class = messages_view.ViewBuilder
resource_name = 'message'
@ -47,6 +49,8 @@ class MessagesController(wsgi.Controller):
@wsgi.Controller.api_version(MESSAGES_BASE_MICRO_VERSION)
@wsgi.Controller.authorize('get')
@validation.request_query_schema(schema.show_request_query)
@validation.response_body_schema(schema.show_response_body)
def show(self, req, id):
"""Return the given message."""
context = req.environ['manila.context']
@ -61,6 +65,7 @@ class MessagesController(wsgi.Controller):
@wsgi.Controller.api_version(MESSAGES_BASE_MICRO_VERSION)
@wsgi.Controller.authorize
@wsgi.action("delete")
@validation.response_body_schema(schema.delete_response_body)
def delete(self, req, id):
"""Delete a message."""
context = req.environ['manila.context']
@ -75,6 +80,8 @@ class MessagesController(wsgi.Controller):
@wsgi.Controller.api_version(MESSAGES_BASE_MICRO_VERSION, '2.51')
@wsgi.Controller.authorize('get_all')
@validation.request_query_schema(schema.index_request_query)
@validation.response_body_schema(schema.index_response_body)
def index(self, req):
"""Returns a list of messages, transformed through view builder."""
context = req.environ['manila.context']
@ -96,6 +103,8 @@ class MessagesController(wsgi.Controller):
@wsgi.Controller.api_version(MESSAGES_QUERY_BY_TIMESTAMP) # noqa: F811
@wsgi.Controller.authorize('get_all')
@validation.request_query_schema(schema.index_request_query_v252)
@validation.response_body_schema(schema.index_response_body)
def index(self, req): # pylint: disable=function-redefined # noqa F811
"""Returns a list of messages, transformed through view builder."""
context = req.environ['manila.context']

View File

@ -34,7 +34,7 @@ from manila import exception
CONTEXT = context.get_admin_context()
driver_opts = {}
FAKE_UUID = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'
FAKE_UUID = '123e4567-e89b-12d3-a456-426614174000'
FAKE_UUIDS = {}
host = 'host_name'
identifier = '7cf7c200-d3af-4e05-b87e-9167c95dfcad'

View File

@ -29,7 +29,7 @@ def stub_message(id, **kwargs):
'message_level': message_levels.ERROR,
'request_id': FAKE_UUID,
'resource_type': message_field.Resource.SHARE,
'resource_id': 'fake_uuid',
'resource_id': FAKE_UUID,
'updated_at': datetime.datetime(1900, 1, 1, 1, 1, 1,
tzinfo=iso8601.UTC),
'created_at': datetime.datetime(1900, 1, 1, 1, 1, 1,

View File

@ -213,5 +213,6 @@ class MessageApiTest(test.TestCase):
version=messages.MESSAGES_QUERY_BY_TIMESTAMP,
base_url='http://localhost/share/v2')
req.environ['manila.context'] = self.ctxt
self.assertRaises(webob.exc.HTTPBadRequest,
self.assertRaises(exception.ValidationError,
self.controller.index, req)