Browse Source

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
(cherry picked from commit 22294abac6)
changes/97/624397/1
Iury Gregory Melo Ferreira 4 years ago committed by Iury Gregory Melo Ferreira
parent
commit
9b6133b1f7
  1. 12
      ironic_inspector/plugins/rules.py
  2. 2
      ironic_inspector/test/unit/test_plugins_rules.py
  3. 12
      ironic_inspector/test/unit/test_rules.py
  4. 4
      releasenotes/notes/unset_property_instrospectionrules-78d64b8b7643e40d.yaml

12
ironic_inspector/plugins/rules.py

@ -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'}

2
ironic_inspector/test/unit/test_plugins_rules.py

@ -157,6 +157,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):

12
ironic_inspector/test/unit/test_rules.py

@ -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',

4
releasenotes/notes/unset_property_instrospectionrules-78d64b8b7643e40d.yaml

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