Accept None value for set-attribute
Allows the ``set-attribute`` introspection rule action to use ``None`` as a valid value. Co-Authored-By: Riccardo Pittau <elfosardo@gmail.com> Change-Id: I1aa11c8095c4557107223e352424e3b718e2ab35 Story: #2004546 Task: #28299
This commit is contained in:
parent
f7079e9acc
commit
22294abac6
@ -115,7 +115,10 @@ class FailAction(base.RuleActionPlugin):
|
|||||||
|
|
||||||
|
|
||||||
class SetAttributeAction(base.RuleActionPlugin):
|
class SetAttributeAction(base.RuleActionPlugin):
|
||||||
REQUIRED_PARAMS = {'path', 'value'}
|
# NOTE(iurygregory): set as optional to accept None as value, check
|
||||||
|
# that the key 'value' is present, otherwise will raise ValueError.
|
||||||
|
OPTIONAL_PARAMS = {'value'}
|
||||||
|
REQUIRED_PARAMS = {'path'}
|
||||||
# TODO(dtantsur): proper validation of path
|
# TODO(dtantsur): proper validation of path
|
||||||
|
|
||||||
FORMATTED_PARAMS = ['value']
|
FORMATTED_PARAMS = ['value']
|
||||||
@ -124,6 +127,13 @@ class SetAttributeAction(base.RuleActionPlugin):
|
|||||||
node_info.patch([{'op': 'add', 'path': params['path'],
|
node_info.patch([{'op': 'add', 'path': params['path'],
|
||||||
'value': params['value']}])
|
'value': params['value']}])
|
||||||
|
|
||||||
|
def validate(self, params, **kwargs):
|
||||||
|
if 'value' in params:
|
||||||
|
super(base.RuleActionPlugin, self).validate(params, **kwargs)
|
||||||
|
else:
|
||||||
|
msg = _('missing required parameter(s): value')
|
||||||
|
raise ValueError(msg)
|
||||||
|
|
||||||
|
|
||||||
class SetCapabilityAction(base.RuleActionPlugin):
|
class SetCapabilityAction(base.RuleActionPlugin):
|
||||||
REQUIRED_PARAMS = {'name'}
|
REQUIRED_PARAMS = {'name'}
|
||||||
|
@ -158,6 +158,8 @@ class TestSetAttributeAction(test_base.NodeTest):
|
|||||||
self.assertRaises(ValueError, self.act.validate, {'value': 42})
|
self.assertRaises(ValueError, self.act.validate, {'value': 42})
|
||||||
self.assertRaises(ValueError, self.act.validate,
|
self.assertRaises(ValueError, self.act.validate,
|
||||||
{'path': '/extra/value'})
|
{'path': '/extra/value'})
|
||||||
|
self.params['value'] = None
|
||||||
|
self.act.validate(self.params)
|
||||||
|
|
||||||
@mock.patch.object(node_cache.NodeInfo, 'patch')
|
@mock.patch.object(node_cache.NodeInfo, 'patch')
|
||||||
def test_apply(self, mock_patch):
|
def test_apply(self, mock_patch):
|
||||||
|
@ -59,6 +59,18 @@ class TestCreateRule(BaseTest):
|
|||||||
'actions': self.actions_json},
|
'actions': self.actions_json},
|
||||||
rule_json)
|
rule_json)
|
||||||
|
|
||||||
|
def test_create_action_none_value(self):
|
||||||
|
self.actions_json = [{'action': 'set-attribute',
|
||||||
|
'path': '/properties/cpus', 'value': None}]
|
||||||
|
rule = rules.create([], self.actions_json)
|
||||||
|
rule_json = rule.as_dict()
|
||||||
|
|
||||||
|
self.assertTrue(rule_json.pop('uuid'))
|
||||||
|
self.assertEqual({'description': None,
|
||||||
|
'conditions': [],
|
||||||
|
'actions': self.actions_json},
|
||||||
|
rule_json)
|
||||||
|
|
||||||
def test_duplicate_uuid(self):
|
def test_duplicate_uuid(self):
|
||||||
rules.create([], self.actions_json, uuid=self.uuid)
|
rules.create([], self.actions_json, uuid=self.uuid)
|
||||||
self.assertRaisesRegex(utils.Error, 'already exists',
|
self.assertRaisesRegex(utils.Error, 'already exists',
|
||||||
|
@ -0,0 +1,4 @@
|
|||||||
|
fixes:
|
||||||
|
- |
|
||||||
|
Allows the ``set-attribute`` introspection rule action to accept ``None``
|
||||||
|
as value for a property.
|
Loading…
Reference in New Issue
Block a user