Use common functions for handling import errors (#51851)

* Use common functions for handling import errors

* use refactored version of gitlab modules
This commit is contained in:
Jordan Borean
2019-02-08 07:51:16 +10:00
committed by Sam Doran
parent 28dcfa985f
commit c1e51ef486
39 changed files with 233 additions and 93 deletions

View File

@@ -28,11 +28,16 @@
import os
import traceback
from ansible.module_utils.basic import missing_required_lib
CLIENT_IMP_ERR = None
try:
from manageiq_client.api import ManageIQClient
HAS_CLIENT = True
except ImportError:
CLIENT_IMP_ERR = traceback.format_exc()
HAS_CLIENT = False
@@ -55,7 +60,7 @@ def manageiq_argument_spec():
def check_client(module):
if not HAS_CLIENT:
module.fail_json(msg='manageiq_client.api is required for this module')
module.fail_json(msg=missing_required_lib('manageiq-client'), exception=CLIENT_IMP_ERR)
def validate_connection_params(module):

View File

@@ -34,14 +34,16 @@ import json
import os
import traceback
HPE_ONEVIEW_IMP_ERR = None
try:
from hpOneView.oneview_client import OneViewClient
HAS_HPE_ONEVIEW = True
except ImportError:
HPE_ONEVIEW_IMP_ERR = traceback.format_exc()
HAS_HPE_ONEVIEW = False
from ansible.module_utils import six
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils._text import to_native
from ansible.module_utils.common._collections_compat import Mapping
@@ -202,7 +204,6 @@ class OneViewModuleBase(object):
MSG_ALREADY_PRESENT = 'Resource is already present.'
MSG_ALREADY_ABSENT = 'Resource is already absent.'
MSG_DIFF_AT_KEY = 'Difference found at key \'{0}\'. '
HPE_ONEVIEW_SDK_REQUIRED = 'HPE OneView Python SDK is required for this module.'
ONEVIEW_COMMON_ARGS = dict(
config=dict(type='path'),
@@ -257,7 +258,7 @@ class OneViewModuleBase(object):
def _check_hpe_oneview_sdk(self):
if not HAS_HPE_ONEVIEW:
self.module.fail_json(msg=self.HPE_ONEVIEW_SDK_REQUIRED)
self.module.fail_json(msg=missing_required_lib('hpOneView'), exception=HPE_ONEVIEW_IMP_ERR)
def _create_oneview_client(self):
if self.module.params.get('hostname'):

View File

@@ -27,11 +27,16 @@
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
import traceback
from ansible.module_utils.basic import missing_required_lib
UCSMSDK_IMP_ERR = None
try:
import ucsmsdk
HAS_UCSMSDK = True
except Exception:
UCSMSDK_IMP_ERR = traceback.format_exc()
HAS_UCSMSDK = False
ucs_argument_spec = dict(
@@ -51,7 +56,7 @@ class UCSModule():
self.module = module
self.result = {}
if not HAS_UCSMSDK:
self.module.fail_json(msg='ucsmsdk is required for this module')
self.module.fail_json(msg=missing_required_lib('ucsmsdk'), exception=UCSMSDK_IMP_ERR)
self.login()
def __del__(self):