Merge "Add jsonschema validation for karbor scheduled operations API"

This commit is contained in:
Zuul 2017-12-19 01:37:42 +00:00 committed by Gerrit Code Review
commit 9fd757cf0a
3 changed files with 64 additions and 10 deletions

View File

@ -0,0 +1,50 @@
# 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.
"""
Schema for Karbor V1 scheduled operations API.
"""
from karbor.api.validation import parameter_types
create = {
'type': 'object',
'properties': {
'type': 'object',
'scheduled_operation': {
'type': 'object',
'properties': {
'name': parameter_types.name,
'description': parameter_types.description,
'operation_type': {'type': 'string'},
'trigger_id': parameter_types.uuid,
'operation_definition': {
'type': 'object',
'properties': {
'provider_id': parameter_types.uuid,
'plan_id': parameter_types.uuid,
},
'required': ['provider_id', 'plan_id'],
'additionalProperties': False,
},
},
'required': ['operation_type', 'trigger_id',
'operation_definition'],
'additionalProperties': False,
},
},
'required': ['scheduled_operation'],
'additionalProperties': False,
}

View File

@ -18,6 +18,9 @@ from webob import exc
from karbor.api import common
from karbor.api.openstack import wsgi
from karbor.api.schemas import scheduled_operations as \
scheduled_operation_schema
from karbor.api import validation
from karbor import exception
from karbor.i18n import _
from karbor import objects
@ -77,6 +80,7 @@ class ScheduledOperationController(wsgi.Controller):
self.operationengine_api = operationengine_api.API()
super(ScheduledOperationController, self).__init__()
@validation.schema(scheduled_operation_schema.create)
def create(self, req, body):
"""Creates a new scheduled operation."""

View File

@ -67,23 +67,23 @@ class ScheduledOperationApiTest(base.TestCase):
}
def test_create_operation_InvalidBody(self):
self.assertRaises(exc.HTTPUnprocessableEntity,
self.assertRaises(exception.ValidationError,
self.controller.create,
self.req, {})
self.req, body={})
def test_create_operation_InvalidName(self):
body = self._get_create_operation_request_body()
self.assertRaises(exc.HTTPBadRequest,
self.assertRaises(exception.ValidationError,
self.controller.create,
self.req, body)
self.req, body=body)
def test_create_operation_invalid_trigger(self):
param = self.default_create_operation_param.copy()
param['trigger_id'] = 123
body = self._get_create_operation_request_body(param)
self.assertRaises(exc.HTTPBadRequest,
self.assertRaises(exception.ValidationError,
self.controller.create,
self.req, body)
self.req, body=body)
def test_create_operation_receive_invalid_except(self):
self.remote_operation_api._create_operation_exception =\
@ -93,7 +93,7 @@ class ScheduledOperationApiTest(base.TestCase):
body = self._get_create_operation_request_body(param)
self.assertRaises(exc.HTTPBadRequest,
self.controller.create,
self.req, body)
self.req, body=body)
self.remote_operation_api._create_operation_exception = None
@ -105,7 +105,7 @@ class ScheduledOperationApiTest(base.TestCase):
body = self._get_create_operation_request_body(param)
self.assertRaises(exc.HTTPInternalServerError,
self.controller.create,
self.req, body)
self.req, body=body)
self.remote_operation_api._create_operation_exception = None
@ -114,7 +114,7 @@ class ScheduledOperationApiTest(base.TestCase):
param = self.default_create_operation_param.copy()
param['name'] = name
body = self._get_create_operation_request_body(param)
operation = self.controller.create(self.req, body)
operation = self.controller.create(self.req, body=body)
self.assertEqual(name, operation['scheduled_operation']['name'])
def test_delete_operation_receive_NotFound_except(self):
@ -168,7 +168,7 @@ class ScheduledOperationApiTest(base.TestCase):
def _create_one_operation(self):
param = self.default_create_operation_param.copy()
body = self._get_create_operation_request_body(param)
return self.controller.create(self.req, body)
return self.controller.create(self.req, body=body)
def _get_create_operation_request_body(self, param={}):
return {"scheduled_operation": param}