Skip to content

Commit

Permalink
Improve import error handling in azure_rm_common. (ansible#29153)
Browse files Browse the repository at this point in the history
* Improve import error handling in azure_rm_common.

* Update skip.txt
  • Loading branch information
mattclay authored and nitzmahone committed Sep 11, 2017
1 parent d043ba2 commit cafd064
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
22 changes: 20 additions & 2 deletions lib/ansible/module_utils/azure_rm_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
import re
import sys
import copy
import importlib
import inspect
import traceback

from packaging.version import Version
from os.path import expanduser

from ansible.module_utils.basic import AnsibleModule
Expand Down Expand Up @@ -81,6 +79,22 @@
HAS_MSRESTAZURE = True
HAS_MSRESTAZURE_EXC = None

try:
import importlib
except ImportError:
# This passes the sanity import test, but does not provide a user friendly error message.
# Doing so would require catching Exception for all imports of Azure dependencies in modules and module_utils.
importlib = None

try:
from packaging.version import Version
HAS_PACKAGING_VERSION = True
HAS_PACKAGING_VERSION_EXC = None
except ImportError as exc:
Version = None
HAS_PACKAGING_VERSION = False
HAS_PACKAGING_VERSION_EXC = exc

# NB: packaging issue sometimes cause msrestazure not to be installed, check it separately
try:
from msrest.serialization import Serializer
Expand Down Expand Up @@ -174,6 +188,10 @@ def __init__(self, derived_arg_spec, bypass_checks=False, no_log=False,
supports_check_mode=supports_check_mode,
required_if=merged_required_if)

if not HAS_PACKAGING_VERSION:
self.fail("Do you have packaging installed? Try `pip install packaging`"
"- {0}".format(HAS_PACKAGING_VERSION_EXC))

if not HAS_MSRESTAZURE:
self.fail("Do you have msrestazure installed? Try `pip install msrestazure`"
"- {0}".format(HAS_MSRESTAZURE_EXC))
Expand Down
2 changes: 1 addition & 1 deletion test/sanity/import/skip.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
lib/ansible/module_utils/ansible_tower.py
lib/ansible/module_utils/avi.py
lib/ansible/module_utils/azure_rm_common.py
lib/ansible/module_utils/ovirt.py
lib/ansible/modules/cloud/amazon/cloudtrail.py
lib/ansible/modules/cloud/amazon/ec2_vpc_igw.py
lib/ansible/modules/cloud/amazon/ec2_vpc_route_table.py
Expand Down

0 comments on commit cafd064

Please sign in to comment.