Add jsonschema validation for karbor restores API

Change-Id: Ib32a9d8fd28275d8a2a89ae436506c395671262c
Partial-Implements: bp karbor-json-schema-validation
This commit is contained in:
chenying 2017-12-12 22:11:29 +08:00
parent d3d8e78f27
commit c5ae53d58c
4 changed files with 53 additions and 16 deletions

View File

@ -0,0 +1,40 @@
# 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 Restores API.
"""
from karbor.api.validation import parameter_types
create = {
'type': 'object',
'properties': {
'type': 'object',
'restore': {
'type': 'object',
'properties': {
'provider_id': parameter_types.uuid,
'checkpoint_id': parameter_types.uuid,
'restore_target': {'type': ['string', 'null']},
'restore_auth': parameter_types.metadata,
'parameters': parameter_types.parameters,
},
'required': ['provider_id', 'checkpoint_id', 'parameters'],
'additionalProperties': False,
},
},
'required': ['restore'],
'additionalProperties': False,
}

View File

@ -20,6 +20,8 @@ from webob import exc
from karbor.api import common
from karbor.api.openstack import wsgi
from karbor.api.schemas import restores as restore_schema
from karbor.api import validation
from karbor.common import constants
from karbor import exception
from karbor.i18n import _
@ -201,6 +203,7 @@ class RestoresController(wsgi.Controller):
"""Return restores search options allowed by non-admin."""
return CONF.query_restore_filters
@validation.schema(restore_schema.create)
def create(self, req, body):
"""Creates a new restore."""
if not self.is_valid_body(body, 'restore'):

View File

@ -45,7 +45,7 @@ CONF.register_opts(cinder_client_opts, group=CONFIG_GROUP)
CONF.set_default('service_name', 'cinderv3', CONFIG_GROUP)
CONF.set_default('service_type', 'volumev3', CONFIG_GROUP)
CINDERCLIENT_VERSION = '3'
CINDERCLIENT_VERSION = '3.43'
def create(context, conf, **kwargs):

View File

@ -16,7 +16,6 @@ from oslo_config import cfg
from webob import exc
from karbor.api.v1 import restores
from karbor.common import constants
from karbor import context
from karbor import exception
from karbor.tests import base
@ -24,7 +23,6 @@ from karbor.tests.unit.api import fakes
CONF = cfg.CONF
DEFAULT_PROJECT_ID = '39bb894794b741e982bd26144d2949f6'
DEFAULT_PROVIDER_ID = 'efc6a88b-9096-4bb6-8634-cda182a6e12a'
DEFAULT_CHECKPOINT_ID = '09edcbdc-d1c2-49c1-a212-122627b20968'
DEFAULT_RESTORE_TARGET = '192.168.1.2/identity/'
@ -52,7 +50,7 @@ class RestoreApiTest(base.TestCase):
restore = self._restore_in_request_body()
body = {"restore": restore}
req = fakes.HTTPRequest.blank('/v1/restores')
self.controller.create(req, body)
self.controller.create(req, body=body)
self.assertTrue(mock_restore_create.called)
self.assertTrue(mock_rpc_restore.called)
@ -60,22 +58,22 @@ class RestoreApiTest(base.TestCase):
restore = self._restore_in_request_body()
body = {"restorexx": restore}
req = fakes.HTTPRequest.blank('/v1/restores')
self.assertRaises(exc.HTTPUnprocessableEntity, self.controller.create,
req, body)
self.assertRaises(exception.ValidationError, self.controller.create,
req, body=body)
def test_restore_create_InvalidProviderId(self):
restore = self._restore_in_request_body(provider_id="")
body = {"restore": restore}
req = fakes.HTTPRequest.blank('/v1/restores')
self.assertRaises(exception.InvalidInput, self.controller.create,
req, body)
self.assertRaises(exception.ValidationError, self.controller.create,
req, body=body)
def test_restore_create_Invalidcheckpoint_id(self):
restore = self._restore_in_request_body(checkpoint_id="")
body = {"restore": restore}
req = fakes.HTTPRequest.blank('/v1/restores')
self.assertRaises(exception.InvalidInput, self.controller.create,
req, body)
self.assertRaises(exception.ValidationError, self.controller.create,
req, body=body)
@mock.patch(
'karbor.api.v1.restores.RestoresController._get_all')
@ -100,21 +98,17 @@ class RestoreApiTest(base.TestCase):
req, "1")
def _restore_in_request_body(
self, project_id=DEFAULT_PROJECT_ID,
provider_id=DEFAULT_PROVIDER_ID,
self, provider_id=DEFAULT_PROVIDER_ID,
checkpoint_id=DEFAULT_CHECKPOINT_ID,
restore_target=DEFAULT_RESTORE_TARGET,
restore_auth=DEFAULT_RESTORE_AUTH,
parameters=DEFAULT_PARAMETERS,
status=constants.RESOURCE_STATUS_STARTED):
parameters=DEFAULT_PARAMETERS):
restore_req = {
'project_id': project_id,
'provider_id': provider_id,
'checkpoint_id': checkpoint_id,
'restore_target': restore_target,
'restore_auth': restore_auth,
'parameters': parameters,
'status': status,
}
return restore_req