restconf_config module (#51971)

* Add restconf_config module

* Try to do the right thing when given partial paths

* Add PATCH

* Delete should not require content

* Non-JSON exceptions need raising, too

* Let ConnectionError objects pass through exec_jsonrpc
This commit is contained in:
Nathaniel Case 2019-03-04 08:27:18 -05:00 committed by GitHub
parent 5cc6a70398
commit d5aabd02ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 196 additions and 12 deletions

View file

@ -41,8 +41,10 @@ options:
import json
from ansible.module_utils._text import to_text
from ansible.module_utils.network.common.utils import to_list
from ansible.module_utils.connection import ConnectionError
from ansible.module_utils.six.moves.urllib.error import HTTPError
from ansible.plugins.httpapi import HttpApiBase
@ -54,26 +56,36 @@ class HttpApi(HttpApiBase):
if data:
data = json.dumps(data)
path = self.get_option('root_path') + message_kwargs.get('path', '')
path = '/'.join([self.get_option('root_path').rstrip('/'), message_kwargs.get('path', '').lstrip('/')])
headers = {
'Content-Type': message_kwargs.get('content_type') or CONTENT_TYPE,
'Accept': message_kwargs.get('accept') or CONTENT_TYPE,
}
response, response_data = self.connection.send(path, data, headers=headers, method=message_kwargs.get('method'))
try:
response, response_data = self.connection.send(path, data, headers=headers, method=message_kwargs.get('method'))
except HTTPError as exc:
response_data = exc
return handle_response(response_data.read())
return handle_response(response_data)
def handle_httperror(self, exc):
return None
def handle_response(response):
if 'error' in response and 'jsonrpc' not in response:
error = response['error']
try:
response_json = json.loads(response.read())
except ValueError:
if isinstance(response, HTTPError):
raise ConnectionError(to_text(response), code=response.code)
return response.read()
error_text = []
for data in error['data']:
error_text.extend(data.get('errors', []))
error_text = '\n'.join(error_text) or error['message']
if 'errors' in response_json and 'jsonrpc' not in response_json:
errors = response_json['errors']['error']
raise ConnectionError(error_text, code=error['code'])
error_text = '\n'.join((error['error-message'] for error in errors))
return response
raise ConnectionError(error_text, code=response.code)
return response_json