Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenStack
oslo
python-oslo.vmware
Commits
b75a126e
Commit
b75a126e
authored
Apr 21, 2021
by
Zuul
Committed by
Gerrit Code Review
Apr 21, 2021
Browse files
Merge "Add serialize_object() helper function"
parents
fda85f1f
4f160884
Changes
2
Hide whitespace changes
Inline
Side-by-side
oslo_vmware/tests/test_vim_util.py
View file @
b75a126e
...
...
@@ -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
))
oslo_vmware/vim_util.py
View file @
b75a126e
...
...
@@ -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
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment