mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-24 13:50:22 -07:00
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:
parent
28dcfa985f
commit
c1e51ef486
39 changed files with 233 additions and 93 deletions
|
@ -83,12 +83,14 @@ import time
|
|||
import traceback
|
||||
|
||||
HAS_XMPP = True
|
||||
XMPP_IMP_ERR = None
|
||||
try:
|
||||
import xmpp
|
||||
except ImportError:
|
||||
XMPP_IMP_ERR = traceback.format_exc()
|
||||
HAS_XMPP = False
|
||||
|
||||
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
|
||||
|
||||
|
||||
|
@ -108,7 +110,7 @@ def main():
|
|||
)
|
||||
|
||||
if not HAS_XMPP:
|
||||
module.fail_json(msg="The required python xmpp library (xmpppy) is not installed")
|
||||
module.fail_json(msg=missing_required_lib('xmpppy'), exception=XMPP_IMP_ERR)
|
||||
|
||||
jid = xmpp.JID(module.params['user'])
|
||||
user = jid.getNode()
|
||||
|
|
|
@ -72,11 +72,15 @@ EXAMPLES = '''
|
|||
|
||||
RETURN = '''
|
||||
'''
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
import traceback
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
|
||||
|
||||
MATRIX_IMP_ERR = None
|
||||
try:
|
||||
from matrix_client.client import MatrixClient
|
||||
except ImportError:
|
||||
MATRIX_IMP_ERR = traceback.format_exc()
|
||||
matrix_found = False
|
||||
else:
|
||||
matrix_found = True
|
||||
|
@ -107,7 +111,7 @@ def run_module():
|
|||
)
|
||||
|
||||
if not matrix_found:
|
||||
module.fail_json(msg="Python 'matrix-client' module is required. Install via: $ pip install matrix-client")
|
||||
module.fail_json(msg=missing_required_lib('matrix-client'), exception=MATRIX_IMP_ERR)
|
||||
|
||||
if module.check_mode:
|
||||
return result
|
||||
|
|
|
@ -112,13 +112,15 @@ import os
|
|||
import traceback
|
||||
|
||||
HAS_PAHOMQTT = True
|
||||
PAHOMQTT_IMP_ERR = None
|
||||
try:
|
||||
import socket
|
||||
import paho.mqtt.publish as mqtt
|
||||
except ImportError:
|
||||
PAHOMQTT_IMP_ERR = traceback.format_exc()
|
||||
HAS_PAHOMQTT = False
|
||||
|
||||
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
|
||||
|
||||
|
||||
|
@ -147,7 +149,7 @@ def main():
|
|||
)
|
||||
|
||||
if not HAS_PAHOMQTT:
|
||||
module.fail_json(msg="Paho MQTT is not installed")
|
||||
module.fail_json(msg=missing_required_lib('paho-mqtt'), exception=PAHOMQTT_IMP_ERR)
|
||||
|
||||
server = module.params.get("server", 'localhost')
|
||||
port = module.params.get("port", 1883)
|
||||
|
|
|
@ -84,15 +84,19 @@ EXAMPLES = '''
|
|||
body: Error rate on signup service is over 90% for more than 2 minutes
|
||||
'''
|
||||
|
||||
import traceback
|
||||
|
||||
PUSHBULLET_IMP_ERR = None
|
||||
try:
|
||||
from pushbullet import PushBullet
|
||||
from pushbullet.errors import InvalidKeyError, PushError
|
||||
except ImportError:
|
||||
PUSHBULLET_IMP_ERR = traceback.format_exc()
|
||||
pushbullet_found = False
|
||||
else:
|
||||
pushbullet_found = True
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
|
||||
|
||||
|
||||
# ===========================================
|
||||
|
@ -125,7 +129,7 @@ def main():
|
|||
url = module.params['url']
|
||||
|
||||
if not pushbullet_found:
|
||||
module.fail_json(msg="Python 'pushbullet.py' module is required. Install via: $ pip install pushbullet.py")
|
||||
module.fail_json(msg=missing_required_lib('pushbullet.py'), exception=PUSHBULLET_IMP_ERR)
|
||||
|
||||
# Init pushbullet
|
||||
try:
|
||||
|
|
|
@ -115,14 +115,17 @@ EXAMPLES = '''
|
|||
# sendgrid module support methods
|
||||
#
|
||||
import os
|
||||
import traceback
|
||||
|
||||
SENDGRID_IMP_ERR = None
|
||||
try:
|
||||
import sendgrid
|
||||
HAS_SENDGRID = True
|
||||
except ImportError:
|
||||
SENDGRID_IMP_ERR = traceback.format_exc()
|
||||
HAS_SENDGRID = False
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
|
||||
from ansible.module_utils.six.moves.urllib.parse import urlencode
|
||||
from ansible.module_utils._text import to_bytes
|
||||
from ansible.module_utils.urls import fetch_url
|
||||
|
@ -234,8 +237,10 @@ def main():
|
|||
sendgrid_lib_args = [api_key, bcc, cc, headers, from_name, html_body, attachments]
|
||||
|
||||
if any(lib_arg is not None for lib_arg in sendgrid_lib_args) and not HAS_SENDGRID:
|
||||
module.fail_json(msg='You must install the sendgrid python library if you want to use any of the following arguments: '
|
||||
'api_key, bcc, cc, headers, from_name, html_body, attachments')
|
||||
reason = 'when using any of the following arguments: ' \
|
||||
'api_key, bcc, cc, headers, from_name, html_body, attachments'
|
||||
module.fail_json(msg=missing_required_lib('sendgrid', reason=reason),
|
||||
exception=SENDGRID_IMP_ERR)
|
||||
|
||||
response, info = post_sendgrid_api(module, username, password,
|
||||
from_address, to_addresses, subject, body, attachments=attachments,
|
||||
|
|
|
@ -146,18 +146,20 @@ attached_file:
|
|||
'''
|
||||
|
||||
import os
|
||||
import traceback
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
|
||||
from ansible.module_utils._text import to_bytes, to_native
|
||||
|
||||
# Pull in pysnow
|
||||
HAS_PYSNOW = False
|
||||
PYSNOW_IMP_ERR = None
|
||||
try:
|
||||
import pysnow
|
||||
HAS_PYSNOW = True
|
||||
|
||||
except ImportError:
|
||||
pass
|
||||
PYSNOW_IMP_ERR = traceback.format_exc()
|
||||
|
||||
|
||||
def run_module():
|
||||
|
@ -187,7 +189,7 @@ def run_module():
|
|||
|
||||
# check for pysnow
|
||||
if not HAS_PYSNOW:
|
||||
module.fail_json(msg='pysnow module required')
|
||||
module.fail_json(msg=missing_required_lib('pysnow'), exception=PYSNOW_IMP_ERR)
|
||||
|
||||
params = module.params
|
||||
instance = params['instance']
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue