diff options
author | Jordan Borean <jborean93@gmail.com> | 2019-02-08 07:51:16 +1000 |
---|---|---|
committer | Sam Doran <sdoran@redhat.com> | 2019-02-07 16:51:16 -0500 |
commit | c1e51ef486cec17b69727b47452a34a7796d5677 (patch) | |
tree | 5b53e3aeab23a22598580f4997b60e70e930e350 /lib/ansible/modules/notification | |
parent | 28dcfa985fd3a2fa3bea71d078e73926a2fcd59d (diff) | |
download | ansible-c1e51ef486cec17b69727b47452a34a7796d5677.tar.gz |
Use common functions for handling import errors (#51851)
* Use common functions for handling import errors
* use refactored version of gitlab modules
Diffstat (limited to 'lib/ansible/modules/notification')
-rw-r--r-- | lib/ansible/modules/notification/jabber.py | 6 | ||||
-rw-r--r-- | lib/ansible/modules/notification/matrix.py | 8 | ||||
-rw-r--r-- | lib/ansible/modules/notification/mqtt.py | 6 | ||||
-rw-r--r-- | lib/ansible/modules/notification/pushbullet.py | 8 | ||||
-rw-r--r-- | lib/ansible/modules/notification/sendgrid.py | 11 | ||||
-rw-r--r-- | lib/ansible/modules/notification/snow_record.py | 8 |
6 files changed, 33 insertions, 14 deletions
diff --git a/lib/ansible/modules/notification/jabber.py b/lib/ansible/modules/notification/jabber.py index d756fc44b2..1007cab198 100644 --- a/lib/ansible/modules/notification/jabber.py +++ b/lib/ansible/modules/notification/jabber.py @@ -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() diff --git a/lib/ansible/modules/notification/matrix.py b/lib/ansible/modules/notification/matrix.py index 622dbba290..658b9a6e6c 100644 --- a/lib/ansible/modules/notification/matrix.py +++ b/lib/ansible/modules/notification/matrix.py @@ -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 diff --git a/lib/ansible/modules/notification/mqtt.py b/lib/ansible/modules/notification/mqtt.py index 6a7444e85a..a962a81186 100644 --- a/lib/ansible/modules/notification/mqtt.py +++ b/lib/ansible/modules/notification/mqtt.py @@ -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) diff --git a/lib/ansible/modules/notification/pushbullet.py b/lib/ansible/modules/notification/pushbullet.py index f912b489f5..92c68fd60a 100644 --- a/lib/ansible/modules/notification/pushbullet.py +++ b/lib/ansible/modules/notification/pushbullet.py @@ -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: diff --git a/lib/ansible/modules/notification/sendgrid.py b/lib/ansible/modules/notification/sendgrid.py index 478d502d4b..3e93e5d897 100644 --- a/lib/ansible/modules/notification/sendgrid.py +++ b/lib/ansible/modules/notification/sendgrid.py @@ -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, diff --git a/lib/ansible/modules/notification/snow_record.py b/lib/ansible/modules/notification/snow_record.py index e9208dc7d2..d0c9ef9d05 100644 --- a/lib/ansible/modules/notification/snow_record.py +++ b/lib/ansible/modules/notification/snow_record.py @@ -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'] |