Handle virtual machine config element gracefully (#32924)
This fix uses '_get_vm_prop' API to handle virtual machine related properties rather than failing with AttributeError. Handled invalid request type while connecting to ESXi server, which is caused by malformed request. Fixes: #32477 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
parent
90e9969fb5
commit
a40ce1ba5e
2 changed files with 13 additions and 8 deletions
|
@ -26,7 +26,7 @@ try:
|
||||||
# requests is required for exception handling of the ConnectionError
|
# requests is required for exception handling of the ConnectionError
|
||||||
import requests
|
import requests
|
||||||
from pyVim import connect
|
from pyVim import connect
|
||||||
from pyVmomi import vim
|
from pyVmomi import vim, vmodl
|
||||||
HAS_PYVMOMI = True
|
HAS_PYVMOMI = True
|
||||||
except ImportError:
|
except ImportError:
|
||||||
HAS_PYVMOMI = False
|
HAS_PYVMOMI = False
|
||||||
|
@ -420,11 +420,15 @@ def connect_to_api(module, disconnect_atexit=True):
|
||||||
" to log on to vCenter or ESXi API at %s: %s" % (username, hostname, e.msg))
|
" to log on to vCenter or ESXi API at %s: %s" % (username, hostname, e.msg))
|
||||||
except (requests.ConnectionError, ssl.SSLError) as e:
|
except (requests.ConnectionError, ssl.SSLError) as e:
|
||||||
module.fail_json(msg="Unable to connect to vCenter or ESXi API at %s on TCP/443: %s" % (hostname, e))
|
module.fail_json(msg="Unable to connect to vCenter or ESXi API at %s on TCP/443: %s" % (hostname, e))
|
||||||
|
except vmodl.fault.InvalidRequest as e:
|
||||||
|
# Request is malformed
|
||||||
|
module.fail_json(msg="Failed to get a response from server %s as "
|
||||||
|
"request is malformed: %s" % (hostname, e.msg))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
module.fail_json(msg="Unknown error connecting to vCenter or ESXi API at %s: %s" % (hostname, e))
|
module.fail_json(msg="Unknown error while connecting to vCenter or ESXi API at %s: %s" % (hostname, e))
|
||||||
|
|
||||||
if service_instance is None:
|
if service_instance is None:
|
||||||
module.fail_json(msg="Unknown error connecting to vCenter or ESXi API at %s" % hostname)
|
module.fail_json(msg="Unknown error while connecting to vCenter or ESXi API at %s" % hostname)
|
||||||
|
|
||||||
# Disabling atexit should be used in special cases only.
|
# Disabling atexit should be used in special cases only.
|
||||||
# Such as IP change of the ESXi host which removes the connection anyway.
|
# Such as IP change of the ESXi host which removes the connection anyway.
|
||||||
|
|
|
@ -51,12 +51,12 @@ virtual_machines:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from pyVmomi import vim, vmodl
|
from pyVmomi import vim, vmodl
|
||||||
HAS_PYVMOMI = True
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
HAS_PYVMOMI = False
|
pass
|
||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
from ansible.module_utils.vmware import HAS_PYVMOMI, connect_to_api, get_all_objs, vmware_argument_spec
|
from ansible.module_utils.vmware import (HAS_PYVMOMI, connect_to_api, get_all_objs,
|
||||||
|
vmware_argument_spec, _get_vm_prop)
|
||||||
|
|
||||||
|
|
||||||
# https://github.com/vmware/pyvmomi-community-samples/blob/master/samples/getallvms.py
|
# https://github.com/vmware/pyvmomi-community-samples/blob/master/samples/getallvms.py
|
||||||
|
@ -72,8 +72,9 @@ def get_all_virtual_machines(content):
|
||||||
if _ip_address is None:
|
if _ip_address is None:
|
||||||
_ip_address = ""
|
_ip_address = ""
|
||||||
_mac_address = []
|
_mac_address = []
|
||||||
if vm.config is not None:
|
all_devices = _get_vm_prop(vm, ('config', 'hardware', 'device'))
|
||||||
for dev in vm.config.hardware.device:
|
if all_devices:
|
||||||
|
for dev in all_devices:
|
||||||
if isinstance(dev, vim.vm.device.VirtualEthernetCard):
|
if isinstance(dev, vim.vm.device.VirtualEthernetCard):
|
||||||
_mac_address.append(dev.macAddress)
|
_mac_address.append(dev.macAddress)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue