Commit 784cd737 authored by René Nussbaumer's avatar René Nussbaumer
Browse files

cmdlib: Adding _UpdateAndVerifySubDict helper



This helps with 2 dimensional dicts.
For example the hv_state and the disk_state dicts.
Signed-off-by: default avatarRené Nussbaumer <rn@google.com>
Reviewed-by: default avatarMichael Hanselmann <hansmi@google.com>
parent 3603605a
......@@ -721,6 +721,26 @@ def _GetUpdatedParams(old_params, update_dict,
return params_copy
def _UpdateAndVerifySubDict(base, updates, type_check):
"""Updates and verifies a dict with sub dicts of the same type.
@param base: The dict with the old data
@param updates: The dict with the new data
@param type_check: Dict suitable to ForceDictType to verify correct types
@returns: A new dict with updated and verified values
"""
def fn(old, value):
new = _GetUpdatedParams(old, value)
utils.ForceDictType(new, type_check)
return new
ret = copy.deepcopy(base)
ret.update(dict((key, fn(base.get(key, {}), value))
for key, value in updates.items()))
return ret
def _ReleaseLocks(lu, level, names=None, keep=None):
"""Releases locks owned by an LU.
......
......@@ -447,5 +447,79 @@ class TestLoadNodeEvacResult(unittest.TestCase):
self.assertFalse(lu.warning_log)
class TestUpdateAndVerifySubDict(unittest.TestCase):
def setUp(self):
self.type_check = {
"a": constants.VTYPE_INT,
"b": constants.VTYPE_STRING,
"c": constants.VTYPE_BOOL,
"d": constants.VTYPE_STRING,
}
def test(self):
old_test = {
"foo": {
"d": "blubb",
"a": 321,
},
"baz": {
"a": 678,
"b": "678",
"c": True,
},
}
test = {
"foo": {
"a": 123,
"b": "123",
"c": True,
},
"bar": {
"a": 321,
"b": "321",
"c": False,
},
}
mv = {
"foo": {
"a": 123,
"b": "123",
"c": True,
"d": "blubb"
},
"bar": {
"a": 321,
"b": "321",
"c": False,
},
"baz": {
"a": 678,
"b": "678",
"c": True,
},
}
verified = cmdlib._UpdateAndVerifySubDict(old_test, test, self.type_check)
self.assertEqual(verified, mv)
def testWrong(self):
test = {
"foo": {
"a": "blubb",
"b": "123",
"c": True,
},
"bar": {
"a": 321,
"b": "321",
"c": False,
},
}
self.assertRaises(errors.TypeEnforcementError,
cmdlib._UpdateAndVerifySubDict, {}, test, self.type_check)
if __name__ == "__main__":
testutils.GanetiTestProgram()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment