Replace voluptuous with JSONSchema to validate change_node_power_state
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 change_node_power_state action in watcher applier. Partially Implements: blueprint jsonschema-validation Change-Id: If9ffe5e0b107e0da5673247e4d5ec9917790827f
This commit is contained in:
committed by
Akihito INOH
parent
78de029a57
commit
5b349b4e89
@@ -18,8 +18,7 @@
|
|||||||
#
|
#
|
||||||
|
|
||||||
import enum
|
import enum
|
||||||
import six
|
import jsonschema
|
||||||
import voluptuous
|
|
||||||
|
|
||||||
from watcher._i18n import _
|
from watcher._i18n import _
|
||||||
from watcher.applier.actions import base
|
from watcher.applier.actions import base
|
||||||
@@ -53,15 +52,29 @@ class ChangeNodePowerState(base.BaseAction):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def schema(self):
|
def schema(self):
|
||||||
return voluptuous.Schema({
|
return {
|
||||||
voluptuous.Required(self.RESOURCE_ID):
|
'type': 'object',
|
||||||
voluptuous.All(
|
'properties': {
|
||||||
voluptuous.Any(*six.string_types),
|
'resource_id': {
|
||||||
voluptuous.Length(min=1)),
|
'type': 'string',
|
||||||
voluptuous.Required(self.STATE):
|
"minlength": 1
|
||||||
voluptuous.Any(*[NodeState.POWERON.value,
|
},
|
||||||
NodeState.POWEROFF.value]),
|
'state': {
|
||||||
})
|
'type': 'string',
|
||||||
|
'enum': [NodeState.POWERON.value,
|
||||||
|
NodeState.POWEROFF.value]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'required': ['resource_id', 'state'],
|
||||||
|
'additionalProperties': False,
|
||||||
|
}
|
||||||
|
|
||||||
|
def validate_parameters(self):
|
||||||
|
try:
|
||||||
|
jsonschema.validate(self.input_parameters, self.schema)
|
||||||
|
return True
|
||||||
|
except jsonschema.ValidationError as e:
|
||||||
|
raise e
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def node_uuid(self):
|
def node_uuid(self):
|
||||||
|
|||||||
@@ -15,8 +15,8 @@
|
|||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import jsonschema
|
||||||
import mock
|
import mock
|
||||||
import voluptuous
|
|
||||||
|
|
||||||
from watcher.applier.actions import base as baction
|
from watcher.applier.actions import base as baction
|
||||||
from watcher.applier.actions import change_node_power_state
|
from watcher.applier.actions import change_node_power_state
|
||||||
@@ -59,34 +59,21 @@ class TestChangeNodePowerState(base.TestCase):
|
|||||||
self.action.input_parameters = {
|
self.action.input_parameters = {
|
||||||
baction.BaseAction.RESOURCE_ID: COMPUTE_NODE,
|
baction.BaseAction.RESOURCE_ID: COMPUTE_NODE,
|
||||||
self.action.STATE: 'error'}
|
self.action.STATE: 'error'}
|
||||||
exc = self.assertRaises(
|
self.assertRaises(jsonschema.ValidationError,
|
||||||
voluptuous.Invalid, self.action.validate_parameters)
|
self.action.validate_parameters)
|
||||||
self.assertEqual(
|
|
||||||
[(['state'], voluptuous.ScalarInvalid)],
|
|
||||||
[([str(p) for p in e.path], type(e)) for e in exc.errors])
|
|
||||||
|
|
||||||
def test_parameters_resource_id_empty(self, mock_ironic, mock_nova):
|
def test_parameters_resource_id_empty(self, mock_ironic, mock_nova):
|
||||||
self.action.input_parameters = {
|
self.action.input_parameters = {
|
||||||
self.action.STATE:
|
self.action.STATE:
|
||||||
change_node_power_state.NodeState.POWERON.value,
|
change_node_power_state.NodeState.POWERON.value,
|
||||||
}
|
}
|
||||||
exc = self.assertRaises(
|
self.assertRaises(jsonschema.ValidationError,
|
||||||
voluptuous.Invalid, self.action.validate_parameters)
|
self.action.validate_parameters)
|
||||||
self.assertEqual(
|
|
||||||
[(['resource_id'], voluptuous.RequiredFieldInvalid)],
|
|
||||||
[([str(p) for p in e.path], type(e)) for e in exc.errors])
|
|
||||||
|
|
||||||
def test_parameters_applies_add_extra(self, mock_ironic, mock_nova):
|
def test_parameters_applies_add_extra(self, mock_ironic, mock_nova):
|
||||||
self.action.input_parameters = {"extra": "failed"}
|
self.action.input_parameters = {"extra": "failed"}
|
||||||
exc = self.assertRaises(
|
self.assertRaises(jsonschema.ValidationError,
|
||||||
voluptuous.Invalid, self.action.validate_parameters)
|
self.action.validate_parameters)
|
||||||
self.assertEqual(
|
|
||||||
sorted([(['resource_id'], voluptuous.RequiredFieldInvalid),
|
|
||||||
(['state'], voluptuous.RequiredFieldInvalid),
|
|
||||||
(['extra'], voluptuous.Invalid)],
|
|
||||||
key=lambda x: str(x[0])),
|
|
||||||
sorted([([str(p) for p in e.path], type(e)) for e in exc.errors],
|
|
||||||
key=lambda x: str(x[0])))
|
|
||||||
|
|
||||||
def test_change_service_state_pre_condition(self, mock_ironic, mock_nova):
|
def test_change_service_state_pre_condition(self, mock_ironic, mock_nova):
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user