From 748fa5db359a188790cb79d1a0058a490fa5251a Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Thu, 3 Aug 2017 12:31:41 +0530 Subject: [PATCH] Add check for requests module Fix adds check for requests Python module and suggests user to install, if no requests module installation found. Fixes: #27643 Signed-off-by: Abhijeet Kasurde --- lib/ansible/modules/messaging/rabbitmq_binding.py | 10 +++++++++- lib/ansible/modules/messaging/rabbitmq_exchange.py | 9 ++++++++- lib/ansible/modules/messaging/rabbitmq_queue.py | 9 ++++++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/ansible/modules/messaging/rabbitmq_binding.py b/lib/ansible/modules/messaging/rabbitmq_binding.py index b4df153233e..d01589ea07b 100644 --- a/lib/ansible/modules/messaging/rabbitmq_binding.py +++ b/lib/ansible/modules/messaging/rabbitmq_binding.py @@ -103,9 +103,14 @@ EXAMPLES = ''' ''' import json -import requests import urllib +try: + import requests + HAS_REQUESTS = True +except ImportError as e: + HAS_REQUESTS = False + from ansible.module_utils.basic import AnsibleModule @@ -127,6 +132,9 @@ def main(): supports_check_mode = True ) + if not HAS_REQUESTS: + module.fail_json(msg="requests library is required for this module. To install, use `pip install requests`") + if module.params['destination_type'] == "queue": dest_type="q" else: diff --git a/lib/ansible/modules/messaging/rabbitmq_exchange.py b/lib/ansible/modules/messaging/rabbitmq_exchange.py index a0db064ed3d..0851dbf1a92 100644 --- a/lib/ansible/modules/messaging/rabbitmq_exchange.py +++ b/lib/ansible/modules/messaging/rabbitmq_exchange.py @@ -105,7 +105,11 @@ EXAMPLES = ''' ''' import json -import requests +try: + import requests + HAS_REQUESTS = True +except ImportError as e: + HAS_REQUESTS = False import urllib from ansible.module_utils.basic import AnsibleModule @@ -137,6 +141,9 @@ def main(): urllib.quote(module.params['name'],'') ) + if not HAS_REQUESTS: + module.fail_json(msg="requests library is required for this module. To install, use `pip install requests`") + # Check if exchange already exists r = requests.get( url, auth=(module.params['login_user'],module.params['login_password'])) diff --git a/lib/ansible/modules/messaging/rabbitmq_queue.py b/lib/ansible/modules/messaging/rabbitmq_queue.py index 9849128f717..97e962870c5 100644 --- a/lib/ansible/modules/messaging/rabbitmq_queue.py +++ b/lib/ansible/modules/messaging/rabbitmq_queue.py @@ -120,7 +120,11 @@ EXAMPLES = ''' ''' import json -import requests +try: + import requests + HAS_REQUESTS = True +except ImportError as e: + HAS_REQUESTS = False import urllib from ansible.module_utils.basic import AnsibleModule @@ -155,6 +159,9 @@ def main(): module.params['name'] ) + if not HAS_REQUESTS: + module.fail_json(msg="requests library is required for this module. To install, use `pip install requests`") + # Check if queue already exists r = requests.get( url, auth=(module.params['login_user'],module.params['login_password']))