Share inventory plugin auth code with modules

refactor shared auth option definitions to repeat less
This commit is contained in:
AlanCoding
2020-03-29 21:39:51 -04:00
parent ec2c121762
commit e3814c6f0f
4 changed files with 85 additions and 115 deletions

View File

@@ -29,14 +29,9 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import json
import os
import traceback
from ansible.module_utils._text import to_native
from ansible.module_utils.urls import urllib_error, ConnectionError, socket, httplib
from ansible.module_utils.urls import Request # noqa
TOWER_CLI_IMP_ERR = None
try:
import tower_cli.utils.exceptions as exc
@@ -51,31 +46,6 @@ except ImportError:
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
class CollectionsParserError(Exception):
pass
def make_request(request_handler, tower_url):
'''
Makes the request to given URL, handles errors, returns JSON
'''
try:
response = request_handler.get(tower_url)
except (ConnectionError, urllib_error.URLError, socket.error, httplib.HTTPException) as e:
n_error_msg = 'Connection to remote host failed: {err}'.format(err=to_native(e))
# If Tower gives a readable error message, display that message to the user.
if callable(getattr(e, 'read', None)):
n_error_msg += ' with message: {err_msg}'.format(err_msg=to_native(e.read()))
raise CollectionsParserError(n_error_msg)
# Attempt to parse JSON.
try:
return json.loads(response.read())
except (ValueError, TypeError) as e:
# If the JSON parse fails, print the ValueError
raise CollectionsParserError('Failed to parse json from host: {err}'.format(err=to_native(e)))
def tower_auth_config(module):
'''
`tower_auth_config` attempts to load the tower-cli.cfg file