httpapi: Split off and save response text contents (#43305)

* Split off and save response text contents

* login might depend on httpapi
pull/40409/merge
Nathaniel Case 6 years ago committed by GitHub
parent 13dd1acbec
commit ab40c72a3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -206,12 +206,12 @@ class Connection(NetworkConnectionBase):
httpapi = httpapi_loader.get(self._network_os, self) httpapi = httpapi_loader.get(self._network_os, self)
if httpapi: if httpapi:
display.vvvv('loaded API plugin for network_os %s' % self._network_os, host=self._play_context.remote_addr)
self._implementation_plugins.append(httpapi)
httpapi.set_become(self._play_context) httpapi.set_become(self._play_context)
httpapi.login(self.get_option('remote_user'), self.get_option('password')) httpapi.login(self.get_option('remote_user'), self.get_option('password'))
display.vvvv('loaded API plugin for network_os %s' % self._network_os, host=self._play_context.remote_addr)
else: else:
raise AnsibleConnectionFailure('unable to load API plugin for network_os %s' % self._network_os) raise AnsibleConnectionFailure('unable to load API plugin for network_os %s' % self._network_os)
self._implementation_plugins.append(httpapi)
cliconf = cliconf_loader.get(self._network_os, self) cliconf = cliconf_loader.get(self._network_os, self)
if cliconf: if cliconf:
@ -258,7 +258,9 @@ class Connection(NetworkConnectionBase):
return self.send(path, data, **kwargs) return self.send(path, data, **kwargs)
raise AnsibleConnectionFailure('Could not connect to {0}: {1}'.format(self._url, exc.reason)) raise AnsibleConnectionFailure('Could not connect to {0}: {1}'.format(self._url, exc.reason))
response_text = response.read()
# Try to assign a new auth token if one is given # Try to assign a new auth token if one is given
self._auth = self.update_auth(response) or self._auth self._auth = self.update_auth(response, response_text) or self._auth
return response return response, response_text

@ -36,7 +36,7 @@ class HttpApiBase(AnsiblePlugin):
""" """
pass pass
def update_auth(self, response): def update_auth(self, response, response_text):
"""Return per-request auth token. """Return per-request auth token.
The response should be a dictionary that can be plugged into the The response should be a dictionary that can be plugged into the

@ -30,14 +30,13 @@ class HttpApi(HttpApiBase):
request = request_builder(data, output) request = request_builder(data, output)
headers = {'Content-Type': 'application/json-rpc'} headers = {'Content-Type': 'application/json-rpc'}
response = self.connection.send('/command-api', request, headers=headers, method='POST') response, response_text = self.connection.send('/command-api', request, headers=headers, method='POST')
response_text = to_text(response.read())
try: try:
response = json.loads(response_text) response_text = json.loads(response_text)
except ValueError: except ValueError:
raise ConnectionError('Response was not valid JSON, got {0}'.format(response_text)) raise ConnectionError('Response was not valid JSON, got {0}'.format(response_text))
results = handle_response(response) results = handle_response(response_text)
if self._become: if self._become:
results = results[1:] results = results[1:]
@ -50,7 +49,6 @@ class HttpApi(HttpApiBase):
# Fake a prompt for @enable_mode # Fake a prompt for @enable_mode
if self._become: if self._become:
return '#' return '#'
else:
return '>' return '>'
# Imported from module_utils # Imported from module_utils
@ -113,7 +111,13 @@ class HttpApi(HttpApiBase):
responses = list() responses = list()
def run_queue(queue, output): def run_queue(queue, output):
try:
response = to_list(self.send_request(queue, output=output)) response = to_list(self.send_request(queue, output=output))
except Exception as exc:
if check_rc:
raise
return to_text(exc)
if output == 'json': if output == 'json':
response = [json.loads(item) for item in response] response = [json.loads(item) for item in response]
return response return response

@ -27,14 +27,13 @@ class HttpApi(HttpApiBase):
request = request_builder(queue, output) request = request_builder(queue, output)
headers = {'Content-Type': 'application/json'} headers = {'Content-Type': 'application/json'}
response = self.connection.send('/ins', request, headers=headers, method='POST') response, response_text = self.connection.send('/ins', request, headers=headers, method='POST')
response_text = to_text(response.read())
try: try:
response = json.loads(response_text) response_text = json.loads(response_text)
except ValueError: except ValueError:
raise ConnectionError('Response was not valid JSON, got {0}'.format(response_text)) raise ConnectionError('Response was not valid JSON, got {0}'.format(response_text))
results = handle_response(response) results = handle_response(response_text)
if self._become: if self._become:
results = results[1:] results = results[1:]

Loading…
Cancel
Save