Merge "Accept None value for set-attribute"

This commit is contained in:
Zuul 2018-12-08 22:51:52 +00:00 committed by Gerrit Code Review
commit f4cfab2a81
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.