circonus_annotation: check requests library version

This commit is contained in:
Pierre-Louis Bonicoli 2017-08-15 02:23:33 +02:00 committed by Toshio Kuratomi
parent 2bbe99dc26
commit 572ab3987a

View file

@ -140,6 +140,7 @@ annotation:
import json import json
import time import time
import traceback import traceback
from distutils.version import LooseVersion
try: try:
import requests import requests
@ -148,9 +149,20 @@ except ImportError:
HAS_REQUESTS = False HAS_REQUESTS = False
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.six import PY3
from ansible.module_utils._text import to_native from ansible.module_utils._text import to_native
def check_requests_dep(module):
"""Check if an adequate requests version is available"""
if not HAS_REQUESTS:
module.fail_json(msg='requests is required for this module')
else:
required_version = '2.0.0' if PY3 else '1.0.0'
if LooseVersion(requests.__version__) < LooseVersion(required_version):
module.fail_json(msg="'requests' library version should be >= %s, found: %s." % (required_version, requests.__version__))
def post_annotation(annotation, api_key): def post_annotation(annotation, api_key):
''' Takes annotation dict and api_key string''' ''' Takes annotation dict and api_key string'''
base_url = 'https://api.circonus.com/v2' base_url = 'https://api.circonus.com/v2'
@ -203,8 +215,7 @@ def main():
) )
) )
if not HAS_REQUESTS: check_requests_dep(module)
module.fail_json(msg='requests is required for this module')
annotation = create_annotation(module) annotation = create_annotation(module)
try: try: