From ae1d6c69f2a1f585f66764de9030a56f0d62f3f3 Mon Sep 17 00:00:00 2001 From: chenying Date: Wed, 20 Dec 2017 17:08:14 +0800 Subject: [PATCH] Add jsonschema validation for karbor copies API Change-Id: I0260fe51fe60c272fbacc57314fb451c6aa040da Implements: bp karbor-json-schema-validation --- karbor/api/schemas/copies.py | 37 ++++++++++++++++++++++++++++++++++++ karbor/api/v1/copies.py | 14 +++----------- 2 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 karbor/api/schemas/copies.py diff --git a/karbor/api/schemas/copies.py b/karbor/api/schemas/copies.py new file mode 100644 index 00000000..6ce8dbc4 --- /dev/null +++ b/karbor/api/schemas/copies.py @@ -0,0 +1,37 @@ +# 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 Copies API. + +""" + +from karbor.api.validation import parameter_types + + +create = { + 'type': 'object', + 'properties': { + 'type': 'object', + 'copy': { + 'type': 'object', + 'properties': { + 'plan_id': parameter_types.uuid, + 'parameters': parameter_types.parameters, + }, + 'required': ['plan_id'], + 'additionalProperties': False, + }, + }, + 'required': ['copy'], + 'additionalProperties': False, +} diff --git a/karbor/api/v1/copies.py b/karbor/api/v1/copies.py index 2f11b3a1..8b779d29 100644 --- a/karbor/api/v1/copies.py +++ b/karbor/api/v1/copies.py @@ -20,6 +20,8 @@ from webob import exc from karbor.api import common from karbor.api.openstack import wsgi +from karbor.api.schemas import copies as copy_schema +from karbor.api import validation from karbor import exception from karbor.i18n import _ @@ -58,10 +60,9 @@ class CopiesController(wsgi.Controller): self.protection_api = protection_api.API() super(CopiesController, self).__init__() + @validation.schema(copy_schema.create) def create(self, req, provider_id, body): """Creates a new copy.""" - if not self.is_valid_body(body, 'copy'): - raise exc.HTTPUnprocessableEntity() LOG.debug('Create copy request body: %s', body) context = req.environ['karbor.context'] @@ -69,20 +70,11 @@ class CopiesController(wsgi.Controller): copy = body['copy'] plan_id = copy.get("plan_id", None) - if not uuidutils.is_uuid_like(plan_id): - msg = _("Invalid plan id provided.") - raise exception.InvalidInput(reason=msg) - if not uuidutils.is_uuid_like(provider_id): msg = _("Invalid provider id provided.") raise exception.InvalidInput(reason=msg) parameters = copy.get("parameters", None) - if parameters: - if not isinstance(parameters, dict): - msg = _("The parameters must be a dict when creating" - " a copy.") - raise exception.InvalidInput(reason=msg) try: plan = objects.Plan.get_by_id(context, plan_id)