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
Iury Gregory Melo Ferreira 4 months ago
parent
commit
22294abac6

+ 11
- 1
ironic_inspector/plugins/rules.py View File

@@ -115,7 +115,10 @@ class FailAction(base.RuleActionPlugin):
115 115
 
116 116
 
117 117
 class SetAttributeAction(base.RuleActionPlugin):
118
-    REQUIRED_PARAMS = {'path', 'value'}
118
+    # NOTE(iurygregory): set as optional to accept None as value, check
119
+    # that the key 'value' is present, otherwise will raise ValueError.
120
+    OPTIONAL_PARAMS = {'value'}
121
+    REQUIRED_PARAMS = {'path'}
119 122
     # TODO(dtantsur): proper validation of path
120 123
 
121 124
     FORMATTED_PARAMS = ['value']
@@ -124,6 +127,13 @@ class SetAttributeAction(base.RuleActionPlugin):
124 127
         node_info.patch([{'op': 'add', 'path': params['path'],
125 128
                           'value': params['value']}])
126 129
 
130
+    def validate(self, params, **kwargs):
131
+        if 'value' in params:
132
+            super(base.RuleActionPlugin, self).validate(params, **kwargs)
133
+        else:
134
+            msg = _('missing required parameter(s): value')
135
+            raise ValueError(msg)
136
+
127 137
 
128 138
 class SetCapabilityAction(base.RuleActionPlugin):
129 139
     REQUIRED_PARAMS = {'name'}

+ 2
- 0
ironic_inspector/test/unit/test_plugins_rules.py View File

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

+ 12
- 0
ironic_inspector/test/unit/test_rules.py View File

@@ -59,6 +59,18 @@ class TestCreateRule(BaseTest):
59 59
                           'actions': self.actions_json},
60 60
                          rule_json)
61 61
 
62
+    def test_create_action_none_value(self):
63
+        self.actions_json = [{'action': 'set-attribute',
64
+                              'path': '/properties/cpus', 'value': None}]
65
+        rule = rules.create([], self.actions_json)
66
+        rule_json = rule.as_dict()
67
+
68
+        self.assertTrue(rule_json.pop('uuid'))
69
+        self.assertEqual({'description': None,
70
+                          'conditions': [],
71
+                          'actions': self.actions_json},
72
+                         rule_json)
73
+
62 74
     def test_duplicate_uuid(self):
63 75
         rules.create([], self.actions_json, uuid=self.uuid)
64 76
         self.assertRaisesRegex(utils.Error, 'already exists',

+ 4
- 0
releasenotes/notes/unset_property_instrospectionrules-78d64b8b7643e40d.yaml View File

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

Loading…
Cancel
Save