vmware_guest: refactor spec serialization (#30819)

* Refactor spec serialization so that native types are evaluated last.
* Remove redundant type checks

Fixes #30818
This commit is contained in:
jctanner 2017-09-25 09:24:57 -04:00 committed by GitHub
parent e4c9ffa7e6
commit ada404d0ac

View file

@ -31,6 +31,7 @@ try:
except ImportError:
HAS_PYVMOMI = False
from ansible.module_utils._text import to_text
from ansible.module_utils.urls import fetch_url
from ansible.module_utils.six import integer_types, iteritems, string_types
@ -593,22 +594,14 @@ def serialize_spec(clonespec):
xt = type(xo)
if xo is None:
data[x] = None
elif issubclass(xt, list):
data[x] = []
for xe in xo:
data[x].append(serialize_spec(xe))
elif issubclass(xt, string_types + integer_types + (float, bool)):
data[x] = xo
elif issubclass(xt, dict):
data[x] = {}
for k, v in xo.items():
data[x][k] = serialize_spec(v)
elif isinstance(xo, vim.vm.ConfigSpec):
data[x] = serialize_spec(xo)
elif isinstance(xo, vim.vm.RelocateSpec):
data[x] = serialize_spec(xo)
elif isinstance(xo, vim.vm.device.VirtualDisk):
data[x] = serialize_spec(xo)
elif isinstance(xo, vim.vm.device.VirtualDeviceSpec.FileOperation):
data[x] = serialize_spec(xo)
elif isinstance(xo, vim.Description):
data[x] = {
'dynamicProperty': serialize_spec(xo.dynamicProperty),
@ -617,9 +610,22 @@ def serialize_spec(clonespec):
'summary': serialize_spec(xo.summary),
}
elif hasattr(xo, 'name'):
data[x] = str(xo) + ':' + xo.name
data[x] = to_text(xo) + ':' + to_text(xo.name)
elif isinstance(xo, vim.vm.ProfileSpec):
pass
elif issubclass(xt, list):
data[x] = []
for xe in xo:
data[x].append(serialize_spec(xe))
elif issubclass(xt, string_types + integer_types + (float, bool)):
data[x] = to_text(xt)
elif issubclass(xt, bool):
data[x] = xo
elif issubclass(xt, dict):
data[to_text(x)] = {}
for k, v in xo.items():
k = to_text(k)
data[x][k] = serialize_spec(v)
else:
data[x] = str(xt)