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:
Iury Gregory Melo Ferreira 2018-12-05 09:59:57 +01:00
parent f7079e9acc
commit 22294abac6
4 changed files with 29 additions and 1 deletions

View File

@ -115,7 +115,10 @@ class FailAction(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
FORMATTED_PARAMS = ['value']
@ -124,6 +127,13 @@ class SetAttributeAction(base.RuleActionPlugin):
node_info.patch([{'op': 'add', 'path': params['path'],
'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):
REQUIRED_PARAMS = {'name'}

View File

@ -158,6 +158,8 @@ class TestSetAttributeAction(test_base.NodeTest):
self.assertRaises(ValueError, self.act.validate, {'value': 42})
self.assertRaises(ValueError, self.act.validate,
{'path': '/extra/value'})
self.params['value'] = None
self.act.validate(self.params)
@mock.patch.object(node_cache.NodeInfo, 'patch')
def test_apply(self, mock_patch):

View File

@ -59,6 +59,18 @@ class TestCreateRule(BaseTest):
'actions': self.actions_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):
rules.create([], self.actions_json, uuid=self.uuid)
self.assertRaisesRegex(utils.Error, 'already exists',

View File

@ -0,0 +1,4 @@
fixes:
- |
Allows the ``set-attribute`` introspection rule action to accept ``None``
as value for a property.