From ada404d0ac4f0769ad748897f4ab1e57370ef3c5 Mon Sep 17 00:00:00 2001 From: jctanner Date: Mon, 25 Sep 2017 09:24:57 -0400 Subject: [PATCH] vmware_guest: refactor spec serialization (#30819) * Refactor spec serialization so that native types are evaluated last. * Remove redundant type checks Fixes #30818 --- lib/ansible/module_utils/vmware.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/ansible/module_utils/vmware.py b/lib/ansible/module_utils/vmware.py index f76ab91eca8..fd2bad99450 100644 --- a/lib/ansible/module_utils/vmware.py +++ b/lib/ansible/module_utils/vmware.py @@ -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)