Merge "Simplify JSON Schema validation in v2 API"
This commit is contained in:
commit
25b89747a0
@ -15,7 +15,6 @@
|
||||
|
||||
import json
|
||||
|
||||
import jsonschema
|
||||
import webob.exc
|
||||
|
||||
from glance.api.v2 import base
|
||||
@ -81,14 +80,10 @@ class RequestDeserializer(wsgi.JSONRequestDeserializer):
|
||||
self.conf = conf
|
||||
self.schema_api = schema_api
|
||||
|
||||
def _validate(self, request, obj):
|
||||
schema = self.schema_api.get_schema('access')
|
||||
jsonschema.validate(obj, schema)
|
||||
|
||||
def create(self, request):
|
||||
output = super(RequestDeserializer, self).default(request)
|
||||
body = output.pop('body')
|
||||
self._validate(request, body)
|
||||
self.schema_api.validate('access', body)
|
||||
body['member'] = body.pop('tenant_id')
|
||||
output['access_record'] = body
|
||||
return output
|
||||
|
@ -15,11 +15,9 @@
|
||||
|
||||
import json
|
||||
|
||||
import jsonschema
|
||||
import webob.exc
|
||||
|
||||
from glance.api.v2 import base
|
||||
from glance.api.v2 import schemas
|
||||
from glance.common import exception
|
||||
from glance.common import wsgi
|
||||
import glance.registry.db.api
|
||||
@ -71,21 +69,17 @@ class RequestDeserializer(wsgi.JSONRequestDeserializer):
|
||||
self.conf = conf
|
||||
self.schema_api = schema_api
|
||||
|
||||
def _validate(self, request, obj):
|
||||
schema = self.schema_api.get_schema('image')
|
||||
jsonschema.validate(obj, schema)
|
||||
|
||||
def create(self, request):
|
||||
output = super(RequestDeserializer, self).default(request)
|
||||
body = output.pop('body')
|
||||
self._validate(request, body)
|
||||
self.schema_api.validate('image', body)
|
||||
output['image'] = body
|
||||
return output
|
||||
|
||||
def update(self, request):
|
||||
output = super(RequestDeserializer, self).default(request)
|
||||
body = output.pop('body')
|
||||
self._validate(request, body)
|
||||
self.schema_api.validate('image', body)
|
||||
output['image'] = body
|
||||
return output
|
||||
|
||||
|
@ -240,3 +240,8 @@ class WorkerCreationFailure(GlanceException):
|
||||
|
||||
class SchemaLoadError(GlanceException):
|
||||
message = _("Unable to load schema: %(reason)s")
|
||||
|
||||
|
||||
class InvalidObject(GlanceException):
|
||||
message = _("Provided object does not match schema "
|
||||
"'%(schema)s': %(reason)s")
|
||||
|
@ -17,6 +17,8 @@ import copy
|
||||
import json
|
||||
import logging
|
||||
|
||||
import jsonschema
|
||||
|
||||
from glance.common import exception
|
||||
|
||||
|
||||
@ -85,6 +87,13 @@ class API(object):
|
||||
schema_properties.update(copy.deepcopy(custom_properties))
|
||||
self.schema_properties[schema_name] = schema_properties
|
||||
|
||||
def validate(self, schema_name, obj):
|
||||
schema = self.get_schema(schema_name)
|
||||
try:
|
||||
jsonschema.validate(obj, schema)
|
||||
except jsonschema.ValidationError as e:
|
||||
raise exception.InvalidObject(schema=schema_name, reason=str(e))
|
||||
|
||||
|
||||
def read_schema_properties_file(conf, schema_name):
|
||||
"""Find the schema properties files and load them into a dict."""
|
||||
|
@ -16,7 +16,6 @@
|
||||
import json
|
||||
import unittest
|
||||
|
||||
import jsonschema
|
||||
import webob
|
||||
|
||||
from glance.api.v2 import image_access
|
||||
@ -169,7 +168,7 @@ class TestImageAccessDeserializerWithExtendedSchema(unittest.TestCase):
|
||||
}
|
||||
request = test_utils.FakeRequest()
|
||||
request.body = json.dumps(fixture)
|
||||
self.assertRaises(jsonschema.ValidationError,
|
||||
self.assertRaises(exception.InvalidObject,
|
||||
self.deserializer.create, request)
|
||||
|
||||
|
||||
|
@ -16,10 +16,10 @@
|
||||
import json
|
||||
import unittest
|
||||
|
||||
import jsonschema
|
||||
import webob
|
||||
|
||||
import glance.api.v2.images
|
||||
from glance.common import exception
|
||||
from glance.common import utils
|
||||
import glance.schema
|
||||
import glance.tests.unit.utils as test_utils
|
||||
@ -123,7 +123,7 @@ class TestImagesDeserializer(unittest.TestCase):
|
||||
def _test_create_fails(self, body):
|
||||
request = test_utils.FakeRequest()
|
||||
request.body = json.dumps(body)
|
||||
self.assertRaises(jsonschema.ValidationError,
|
||||
self.assertRaises(exception.InvalidObject,
|
||||
self.deserializer.create, request)
|
||||
|
||||
def test_create_no_name(self):
|
||||
@ -161,7 +161,7 @@ class TestImagesDeserializerWithExtendedSchema(unittest.TestCase):
|
||||
def test_create_bad_data(self):
|
||||
request = test_utils.FakeRequest()
|
||||
request.body = json.dumps({'name': 'image-1', 'pants': 'borked'})
|
||||
self.assertRaises(jsonschema.ValidationError,
|
||||
self.assertRaises(exception.InvalidObject,
|
||||
self.deserializer.create, request)
|
||||
|
||||
def test_update(self):
|
||||
@ -174,7 +174,7 @@ class TestImagesDeserializerWithExtendedSchema(unittest.TestCase):
|
||||
def test_update_bad_data(self):
|
||||
request = test_utils.FakeRequest()
|
||||
request.body = json.dumps({'name': 'image-1', 'pants': 'borked'})
|
||||
self.assertRaises(jsonschema.ValidationError,
|
||||
self.assertRaises(exception.InvalidObject,
|
||||
self.deserializer.update, request)
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user