Commit 4f160884 authored by Johannes Kulik's avatar Johannes Kulik
Browse files

Add serialize_object() helper function

This function converts a SUDS object to a dictionary.

Originally implemented in Nova as "object_to_dict()", this function
moves to oslo.vmware to stop leaking the abstraction around the backing
SOAP library into depending projects. This is especially necessary since
we want to switch the backing SOAP library in [1].

[1] https://specs.openstack.org/openstack/oslo-specs/specs/victoria/oslo-vmware-soap-library-switch.html

Change-Id: Ie1d42609104e604f9386c1b1a46be7dcd286e855
parent 6d2f6dd5
......@@ -527,3 +527,33 @@ class VimUtilTest(base.TestCase):
self.assertEqual({"test_name_0": "test_val_0",
"test_name_1": "test_val_1"},
vim_util.propset_dict(mock_propset))
def test_serialize_object(self):
self.assertEqual({}, vim_util.serialize_object({}))
mobj1 = mock.MagicMock()
mobj1.__keylist__ = ['asdf']
mobj1.keys = lambda: ['asdf']
mobj1.__getitem__.side_effect = [1]
mobj2 = mock.Mock()
mobj3 = mock.MagicMock()
mobj3.__keylist__ = ['subkey1', 'subkey2']
mobj3.keys = lambda: ['subkey1', 'subkey2']
mobj3.__getitem__.side_effect = ['subvalue1', True]
mobj4 = 12
obj = {
'foo': mobj1,
'bar': [mobj2, mobj3],
'baz': mobj4
}
expected = {
'foo': {'asdf': 1},
'bar': [mobj2, {'subkey1': 'subvalue1', 'subkey2': True}],
'baz': 12
}
self.assertEqual(expected, vim_util.serialize_object(obj))
......@@ -694,3 +694,21 @@ def storage_placement_spec(client_factory,
spec.resourcePool = res_pool_ref
spec.host = host_ref
return spec
def serialize_object(obj):
"""Convert Suds object into serializable format - a dict."""
d = {}
for k, v in dict(obj).items():
if hasattr(v, '__keylist__'):
d[k] = serialize_object(v)
elif isinstance(v, list):
d[k] = []
for item in v:
if hasattr(item, '__keylist__'):
d[k].append(serialize_object(item))
else:
d[k].append(item)
else:
d[k] = v
return d
Supports Markdown
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