Replace voluptuous with JSONSchema to validate resize action
Now in watcher,both JSONSchema and voluptuous are used to validate JSON payloads. We want to remove voluptuous and Use JSONSchema as our only JSON validation tool to keep consistence and also to make it easier to expose the validation schema through our API in future work. In this patch, we replace voluptuous with JSONSchema to validate the resize action in watcher applier. Partially Implements: blueprint jsonschema-validation Change-Id: I0ee4ba010a9f437658af81d5c971449aefc7f9c4
This commit is contained in:
@@ -17,14 +17,11 @@
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
import jsonschema
|
||||
from oslo_log import log
|
||||
import six
|
||||
import voluptuous
|
||||
|
||||
from watcher._i18n import _
|
||||
from watcher.applier.actions import base
|
||||
from watcher.common import nova_helper
|
||||
from watcher.common import utils
|
||||
|
||||
LOG = log.getLogger(__name__)
|
||||
|
||||
@@ -49,21 +46,33 @@ class Resize(base.BaseAction):
|
||||
# input parameters constants
|
||||
FLAVOR = 'flavor'
|
||||
|
||||
def check_resource_id(self, value):
|
||||
if (value is not None and
|
||||
len(value) > 0 and not
|
||||
utils.is_uuid_like(value)):
|
||||
raise voluptuous.Invalid(_("The parameter "
|
||||
"resource_id is invalid."))
|
||||
|
||||
@property
|
||||
def schema(self):
|
||||
return voluptuous.Schema({
|
||||
voluptuous.Required(self.RESOURCE_ID): self.check_resource_id,
|
||||
voluptuous.Required(self.FLAVOR):
|
||||
voluptuous.All(voluptuous.Any(*six.string_types),
|
||||
voluptuous.Length(min=1)),
|
||||
})
|
||||
return {
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'resource_id': {
|
||||
'type': 'string',
|
||||
'minlength': 1,
|
||||
'pattern': ('^([a-fA-F0-9]){8}-([a-fA-F0-9]){4}-'
|
||||
'([a-fA-F0-9]){4}-([a-fA-F0-9]){4}-'
|
||||
'([a-fA-F0-9]){12}$')
|
||||
},
|
||||
'flavor': {
|
||||
'type': 'string',
|
||||
'minlength': 1,
|
||||
},
|
||||
},
|
||||
'required': ['resource_id', 'flavor'],
|
||||
'additionalProperties': False,
|
||||
}
|
||||
|
||||
def validate_parameters(self):
|
||||
try:
|
||||
jsonschema.validate(self.input_parameters, self.schema)
|
||||
return True
|
||||
except jsonschema.ValidationError as e:
|
||||
raise e
|
||||
|
||||
@property
|
||||
def instance_uuid(self):
|
||||
|
||||
@@ -14,9 +14,8 @@
|
||||
# limitations under the License.
|
||||
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import jsonschema
|
||||
import mock
|
||||
import voluptuous
|
||||
|
||||
from watcher.applier.actions import base as baction
|
||||
from watcher.applier.actions import resize
|
||||
@@ -69,31 +68,23 @@ class TestResize(base.TestCase):
|
||||
self.INSTANCE_UUID,
|
||||
self.action.FLAVOR: None}
|
||||
self.action.input_parameters = parameters
|
||||
exc = self.assertRaises(
|
||||
voluptuous.MultipleInvalid, self.action.validate_parameters)
|
||||
self.assertEqual([(['flavor'], voluptuous.TypeInvalid)],
|
||||
[(e.path, type(e)) for e in exc.errors])
|
||||
self.assertRaises(jsonschema.ValidationError,
|
||||
self.action.validate_parameters)
|
||||
|
||||
def test_parameters_exception_flavor(self):
|
||||
parameters = {baction.BaseAction.RESOURCE_ID:
|
||||
self.INSTANCE_UUID,
|
||||
self.action.FLAVOR: None}
|
||||
self.action.input_parameters = parameters
|
||||
exc = self.assertRaises(
|
||||
voluptuous.MultipleInvalid, self.action.validate_parameters)
|
||||
self.assertEqual(
|
||||
[(['flavor'], voluptuous.TypeInvalid)],
|
||||
[(e.path, type(e)) for e in exc.errors])
|
||||
self.assertRaises(jsonschema.ValidationError,
|
||||
self.action.validate_parameters)
|
||||
|
||||
def test_parameters_exception_resource_id(self):
|
||||
parameters = {baction.BaseAction.RESOURCE_ID: "EFEF",
|
||||
self.action.FLAVOR: 'x1'}
|
||||
self.action.input_parameters = parameters
|
||||
exc = self.assertRaises(
|
||||
voluptuous.MultipleInvalid, self.action.validate_parameters)
|
||||
self.assertEqual(
|
||||
[(['resource_id'], voluptuous.Invalid)],
|
||||
[(e.path, type(e)) for e in exc.errors])
|
||||
self.assertRaises(jsonschema.ValidationError,
|
||||
self.action.validate_parameters)
|
||||
|
||||
def test_execute_resize(self):
|
||||
self.r_helper.find_instance.return_value = self.INSTANCE_UUID
|
||||
|
||||
Reference in New Issue
Block a user