httpapi: let httpapi plugin handle HTTPErrors other than 401 (#43436)

* Hold httpapi response in BytesIO

* Let httpapi plugin deal with HTTP codes if it wants

* Python 3.5 won't json.loads() bytes

* Don't modify headers passed to send

* Move code handling back to send()

but let httpapi plugin have a say on how it happens
This commit is contained in:
Nathaniel Case 2018-08-13 10:25:06 -04:00 committed by GitHub
parent 65772ede26
commit a3385a60b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 62 additions and 23 deletions

View file

@ -11,6 +11,8 @@ from ansible.plugins import AnsiblePlugin
class HttpApiBase(AnsiblePlugin):
def __init__(self, connection):
super(HttpApiBase, self).__init__()
self.connection = connection
self._become = False
self._become_pass = ''
@ -49,6 +51,30 @@ class HttpApiBase(AnsiblePlugin):
return None
def handle_httperror(self, exc):
"""Overridable method for dealing with HTTP codes.
This method will attempt to handle known cases of HTTP status codes.
If your API uses status codes to convey information in a regular way,
you can override this method to handle it appropriately.
:returns:
* True if the code has been handled in a way that the request
may be resent without changes.
* False if this code indicates a fatal or unknown error which
cannot be handled by the plugin. This will result in an
AnsibleConnectionFailure being raised.
* Any other response passes the HTTPError along to the caller to
deal with as appropriate.
"""
if exc.code == 401 and self.connection._auth:
# Stored auth appears to be invalid, clear and retry
self.connection._auth = None
self.login(self.connection.get_option('remote_user'), self.connection.get_option('password'))
return True
return False
@abstractmethod
def send_request(self, data, **message_kwargs):
"""Prepares and sends request(s) to device."""

View file

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

View file

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