VMware: Avoid misleading PyVmomi error if requests import fails

* Avoid misleading PyVmomi error if requests import fails

Requests is imported by the VMware module_utils as an external
dependency; however, because it is in a try/catch block containing the
imports for PyVmomi, if requests fails to import properly, Ansible will
instead complain about PyVmomi not being installed.

By moving the import outside of the try/catch block, if requests fails
to import, an error like the following will be returned:

    ImportError: No module named requests

This should result in less confusion.

* catch requests ImportError

Signed-off-by: Jim Gu <jim@jimgu.com>
Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>

(cherry picked from commit 99ee30768a)
This commit is contained in:
Jim Gu 2018-10-26 03:10:25 -04:00 committed by Toshio Kuratomi
parent 1bf3d54e01
commit 853236d650
2 changed files with 11 additions and 0 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- Avoid misleading PyVmomi error if requests import fails in vmware module utils.

View file

@ -15,6 +15,11 @@ from random import randint
try: try:
# requests is required for exception handling of the ConnectionError # requests is required for exception handling of the ConnectionError
import requests import requests
HAS_REQUESTS = True
except ImportError:
HAS_REQUESTS = False
try:
from pyVim import connect from pyVim import connect
from pyVmomi import vim, vmodl from pyVmomi import vim, vmodl
HAS_PYVMOMI = True HAS_PYVMOMI = True
@ -764,6 +769,10 @@ class PyVmomi(object):
""" """
Constructor Constructor
""" """
if not HAS_REQUESTS:
self.module.fail_json(msg="Unable to find 'requests' Python library which is required."
" Please install using 'pip install requests'")
if not HAS_PYVMOMI: if not HAS_PYVMOMI:
module.fail_json(msg='PyVmomi Python module required. Install using "pip install PyVmomi"') module.fail_json(msg='PyVmomi Python module required. Install using "pip install PyVmomi"')