| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
from __future__ import (absolute_import, division, print_function) |
| 20 |
|
| 21 |
__metaclass__ = type |
| 22 |
|
| 23 |
DOCUMENTATION = """ |
| 24 |
--- |
| 25 |
name: sonic |
| 26 |
short_description: HttpApi Plugin for devices supporting Restconf SONIC API |
| 27 |
description: |
| 28 |
- This HttpApi plugin provides methods to connect to Restconf SONIC API endpoints. |
| 29 |
version_added: 1.0.0 |
| 30 |
options: |
| 31 |
root_path: |
| 32 |
type: str |
| 33 |
description: |
| 34 |
- Specifies the location of the Restconf root. |
| 35 |
default: '/restconf' |
| 36 |
vars: |
| 37 |
- name: ansible_httpapi_restconf_root |
| 38 |
""" |
| 39 |
|
| 40 |
import json |
| 41 |
import time |
| 42 |
import re |
| 43 |
|
| 44 |
from ansible.module_utils._text import to_text |
| 45 |
from ansible.module_utils.connection import ConnectionError |
| 46 |
from ansible.module_utils.six.moves.urllib.error import HTTPError |
| 47 |
from ansible.plugins.httpapi import HttpApiBase |
| 48 |
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import to_list |
| 49 |
|
| 50 |
CONTENT_TYPE = 'application/yang-data+json' |
| 51 |
|
| 52 |
|
| 53 |
class HttpApi(HttpApiBase): |
| 54 |
def send_request(self, data, **message_kwargs): |
| 55 |
if data: |
| 56 |
data = json.dumps(data) |
| 57 |
|
| 58 |
path = '/'.join([self.get_option('root_path').rstrip('/'), message_kwargs.get('path', '').lstrip('/')]) |
| 59 |
|
| 60 |
headers = { |
| 61 |
'Content-Type': message_kwargs.get('content_type') or CONTENT_TYPE, |
| 62 |
'Accept': message_kwargs.get('accept') or CONTENT_TYPE, |
| 63 |
} |
| 64 |
response, response_data = self.connection.send(path, data, headers=headers, method=message_kwargs.get('method')) |
| 65 |
|
| 66 |
return handle_response(response, response_data, message_kwargs) |
| 67 |
|
| 68 |
def get(self, command): |
| 69 |
return self.send_request(path=command, data=None, method='get') |
| 70 |
|
| 71 |
def edit_config(self, requests, suppr_ntf_excp=True): |
| 72 |
"""Send a list of http requests to remote device and return results |
| 73 |
""" |
| 74 |
if requests is None: |
| 75 |
raise ValueError("'requests' value is required") |
| 76 |
|
| 77 |
responses = list() |
| 78 |
for req in to_list(requests): |
| 79 |
try: |
| 80 |
response = self.send_request(**req) |
| 81 |
except ConnectionError as exc: |
| 82 |
if suppr_ntf_excp and req.get('method') == 'get' and re.search("[nN]ot [fF]ound.*code': 404", str(exc)): |
| 83 |
|
| 84 |
response = [{}, {}] |
| 85 |
else: |
| 86 |
raise ConnectionError(to_text(exc, errors='surrogate_then_replace')) |
| 87 |
responses.append(response) |
| 88 |
return responses |
| 89 |
|
| 90 |
def edit_config_reboot(self, requests): |
| 91 |
"""Send a list of http requests to remote device and allow time for reboot |
| 92 |
""" |
| 93 |
if requests is None: |
| 94 |
raise ValueError("'requests' value is required") |
| 95 |
|
| 96 |
for req in to_list(requests): |
| 97 |
try: |
| 98 |
response = self.send_request(**req) |
| 99 |
except Exception as exc: |
| 100 |
if 'command timeout triggered' not in str(exc): |
| 101 |
raise Exception(to_text(exc, errors='surrogate_then_replace')) |
| 102 |
else: |
| 103 |
time.sleep(300) |
| 104 |
|
| 105 |
def get_capabilities(self): |
| 106 |
result = {} |
| 107 |
result['rpc'] = [] |
| 108 |
result['network_api'] = 'sonic_rest' |
| 109 |
|
| 110 |
return json.dumps(result) |
| 111 |
|
| 112 |
|
| 113 |
def handle_response(response, response_data, request_data): |
| 114 |
response_data = response_data.read() |
| 115 |
try: |
| 116 |
if not response_data: |
| 117 |
response_data = "" |
| 118 |
else: |
| 119 |
response_data = json.loads(response_data.decode('utf-8')) |
| 120 |
except ValueError: |
| 121 |
pass |
| 122 |
|
| 123 |
if isinstance(response, HTTPError): |
| 124 |
if response_data: |
| 125 |
if 'errors' in response_data: |
| 126 |
errors = response_data['errors']['error'] |
| 127 |
error_text = '\n'.join((error['error-message'] for error in errors)) |
| 128 |
else: |
| 129 |
error_text = response_data |
| 130 |
error_text.update({u'code': response.code}) |
| 131 |
error_text.update({u'request_data': request_data}) |
| 132 |
raise ConnectionError(error_text, code=response.code) |
| 133 |
raise ConnectionError(to_text(response), code=response.code) |
| 134 |
return response.getcode(), response_data |