VMware: handle KeyError in get_vm API (#60204)

Fixed if conditions for all VM params
This commit is contained in:
Pavan Bidkar 2019-08-12 14:03:13 +05:30 committed by Abhijeet Kasurde
parent 8cbfa75038
commit 0a90ec90c0
2 changed files with 16 additions and 13 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- Fix Key Error in get_vm() api in vmware.py module util (https://github.com/ansible/ansible/issues/60129).

View file

@ -912,13 +912,14 @@ class PyVmomi(object):
vm_obj = None vm_obj = None
user_desired_path = None user_desired_path = None
use_instance_uuid = self.params.get('use_instance_uuid') or False use_instance_uuid = self.params.get('use_instance_uuid') or False
if self.params['uuid'] and not use_instance_uuid: if 'uuid' in self.params and self.params['uuid']:
if not use_instance_uuid:
vm_obj = find_vm_by_id(self.content, vm_id=self.params['uuid'], vm_id_type="uuid") vm_obj = find_vm_by_id(self.content, vm_id=self.params['uuid'], vm_id_type="uuid")
elif self.params['uuid'] and use_instance_uuid: elif use_instance_uuid:
vm_obj = find_vm_by_id(self.content, vm_obj = find_vm_by_id(self.content,
vm_id=self.params['uuid'], vm_id=self.params['uuid'],
vm_id_type="instance_uuid") vm_id_type="instance_uuid")
elif self.params['name']: elif 'name' in self.params and self.params['name']:
objects = self.get_managed_objects_properties(vim_type=vim.VirtualMachine, properties=['name']) objects = self.get_managed_objects_properties(vim_type=vim.VirtualMachine, properties=['name'])
vms = [] vms = []
@ -984,7 +985,7 @@ class PyVmomi(object):
elif vms: elif vms:
# Unique virtual machine found. # Unique virtual machine found.
vm_obj = vms[0] vm_obj = vms[0]
elif self.params['moid']: elif 'moid' in self.params and self.params['moid']:
vm_obj = VmomiSupport.templateOf('VirtualMachine')(self.params['moid'], self.si._stub) vm_obj = VmomiSupport.templateOf('VirtualMachine')(self.params['moid'], self.si._stub)
if vm_obj: if vm_obj: