Beispiel #1
0
 def test_escape(self):
     uri = uniconfig_worker.apply_functions(
         "interfaces/interface=escape(GigabitEthernet0/0/0/0)/abcd/escape(a/b)"
     )
     assert uri == "interfaces/interface=GigabitEthernet0%2F0%2F0%2F0/abcd/a%2Fb"
     uri = uniconfig_worker.apply_functions(None)
     assert uri is None
     uri = uniconfig_worker.apply_functions("")
     assert uri is ""
def write_structured_data(task):
    device_id = task['inputData']['device_id']
    uri = task['inputData']['uri']
    uri = uniconfig_worker.apply_functions(uri)

    template = task['inputData']['template']
    params = task['inputData']['params'] if task['inputData']['params'] else {}

    data_json = template if isinstance(template, basestring) else json.dumps(template if template else {})
    data_json = Template(data_json).substitute(params)

    id_url = Template(odl_url_unified_mount).substitute({"id": device_id}) + "/yang-ext:mount" + (uri if uri else "")
    id_url = Template(id_url).substitute(params)

    r = requests.put(id_url, data=data_json, headers=odl_headers, auth=odl_credentials)
    response_code, response_json = parse_response(r)

    if response_code == requests.codes.no_content or response_code == requests.codes.created:
        return {'status': 'COMPLETED', 'output': {'url': id_url,
                                                  'request_url': id_url,
                                                  'response_code': response_code,
                                                  'response_body': response_json},
                'logs': ["Mountpoint with ID %s updated successfully" % device_id]}
    else:
        return {'status': 'FAILED', 'output': {'url': id_url,
                                               'request_url': id_url,
                                               'response_code': response_code,
                                               'response_body': response_json},
                'logs': ["Unable to update device with ID %s" % device_id]}
def read_structured_data(task):
    device_id = task['inputData']['device_id']
    uri = task['inputData']['uri']
    uri = uniconfig_worker.apply_functions(uri)

    id_url = Template(odl_url_unified_mount).substitute({"id": device_id}) + "/yang-ext:mount" + (uri if uri else "") + "?content=config"

    r = requests.get(id_url, headers=odl_headers, auth=odl_credentials)
    response_code, response_json = parse_response(r)

    if response_code == requests.codes.ok:
        return {'status': 'COMPLETED', 'output': {'url': id_url,
                                                  'response_code': response_code,
                                                  'response_body': response_json},
                'logs': ["Mountpoint with ID %s read successfully" % device_id]}
    else:
        return {'status': 'FAILED', 'output': {'url': id_url,
                                               'response_code': response_code,
                                               'response_body': response_json},
                'logs': ["Unable to read device with ID %s" % device_id]}
def delete_structured_data(task):
    device_id = task['inputData']['device_id']
    uri = task['inputData']['uri']
    uri = uniconfig_worker.apply_functions(uri)
    uniconfig_tx_id = task['inputData'][
        'uniconfig_tx_id'] if 'uniconfig_tx_id' in task["inputData"] else ""

    id_url = Template(odl_url_unified_mount).substitute(
        {"id": device_id}) + "/yang-ext:mount" + (uri if uri else "")

    r = requests.delete(id_url,
                        headers=add_uniconfig_tx_cookie(uniconfig_tx_id),
                        auth=odl_credentials)
    response_code, response_json = parse_response(r)

    if response_code == requests.codes.no_content:
        return {
            'status': 'COMPLETED',
            'output': {
                'url': id_url,
                'request_url': id_url,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["Mountpoint with ID %s updated successfully" % device_id]
        }
    else:
        return {
            'status': 'FAILED',
            'output': {
                'url': id_url,
                'request_url': id_url,
                'response_code': response_code,
                'response_body': response_json
            },
            'logs': ["Unable to update device with ID %s" % device_id]
        }