From e2bf82607e2751cbdba2412714e36ba4745098bc Mon Sep 17 00:00:00 2001 From: Yumeng_Bao Date: Fri, 9 Jun 2017 11:09:36 +0800 Subject: [PATCH] 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 --- watcher/applier/actions/resize.py | 43 ++++++++++++-------- watcher/tests/applier/actions/test_resize.py | 23 ++++------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/watcher/applier/actions/resize.py b/watcher/applier/actions/resize.py index 920c62fb4..862dcec60 100644 --- a/watcher/applier/actions/resize.py +++ b/watcher/applier/actions/resize.py @@ -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): diff --git a/watcher/tests/applier/actions/test_resize.py b/watcher/tests/applier/actions/test_resize.py index 52e4b9af2..0cdfc0ee9 100644 --- a/watcher/tests/applier/actions/test_resize.py +++ b/watcher/tests/applier/actions/test_resize.py @@ -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