VMware: Add more robust logic to deal with VM moref (#39462)

This fix adds more robust logic to deal with virtual machine
managed object and its comparsion.

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2018-05-09 10:23:01 +05:30 committed by ansibot
parent ea489b3f33
commit 3c389aee73

View file

@ -7,9 +7,11 @@
from __future__ import absolute_import, division, print_function from __future__ import absolute_import, division, print_function
__metaclass__ = type __metaclass__ = type
ANSIBLE_METADATA = {'metadata_version': '1.1', ANSIBLE_METADATA = {
'metadata_version': '1.1',
'status': ['preview'], 'status': ['preview'],
'supported_by': 'community'} 'supported_by': 'community'
}
DOCUMENTATION = ''' DOCUMENTATION = '''
--- ---
@ -74,7 +76,7 @@ folders:
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_native from ansible.module_utils._text import to_native
from ansible.module_utils.vmware import PyVmomi, get_all_objs, vmware_argument_spec from ansible.module_utils.vmware import PyVmomi, vmware_argument_spec, find_vm_by_id
try: try:
from pyVmomi import vim from pyVmomi import vim
@ -90,17 +92,24 @@ class PyVmomiHelper(PyVmomi):
def getvm_folder_paths(self): def getvm_folder_paths(self):
results = [] results = []
vms = []
if self.uuid:
vm_obj = find_vm_by_id(self.content, vm_id=self.uuid, vm_id_type="uuid")
if vm_obj is None:
self.module.fail_json(msg="Failed to find the virtual machine with UUID : %s" % self.uuid)
vms = [vm_obj]
elif self.name:
objects = self.get_managed_objects_properties(vim_type=vim.VirtualMachine, properties=['name'])
for temp_vm_object in objects:
if temp_vm_object.obj.name == self.name:
vms.append(temp_vm_object.obj)
for vm in vms:
folder_path = self.get_vm_path(self.content, vm)
results.append(folder_path)
# compare the folder path of each VM against the search path
vmList = get_all_objs(self.content, [vim.VirtualMachine])
for item in vmList.items():
vobj = item[0]
if not isinstance(vobj.parent, vim.Folder):
continue
# Match by name or uuid
if vobj.config.name == self.name or vobj.config.uuid == self.uuid:
folderpath = self.get_vm_path(self.content, vobj)
results.append(folderpath)
return results return results