diff --git a/karbor/api/schemas/scheduled_operations.py b/karbor/api/schemas/scheduled_operations.py new file mode 100644 index 00000000..8442daff --- /dev/null +++ b/karbor/api/schemas/scheduled_operations.py @@ -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, +} diff --git a/karbor/api/v1/scheduled_operations.py b/karbor/api/v1/scheduled_operations.py index 57232130..05eb1c9f 100644 --- a/karbor/api/v1/scheduled_operations.py +++ b/karbor/api/v1/scheduled_operations.py @@ -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.""" diff --git a/karbor/tests/unit/api/v1/test_scheduled_operation.py b/karbor/tests/unit/api/v1/test_scheduled_operation.py index 7ae466a5..be0f3ce7 100644 --- a/karbor/tests/unit/api/v1/test_scheduled_operation.py +++ b/karbor/tests/unit/api/v1/test_scheduled_operation.py @@ -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}