Giter Club home page Giter Club logo

rest's Introduction

REST API Connector

This is a pyATS connection class implementation that allows scripts to connect to a device using REST via topology/YAML format.

The rest.connector module is abstraction enabled and currently supports specific platforms. Please note that the custom abstraction keyword is required in the testbed.yaml file.

This package is open sourced to Cisco DevNet in June 2019.

Installation

Installation guide can be found on our website.

$ pip install rest.connector

Copyright (c) 2018 Cisco Systems, Inc. and/or its affiliates

rest's People

Contributors

ademz avatar bastell avatar danielgraziano avatar dendod96 avatar domachad avatar dthangap avatar dwapstra avatar eric035 avatar gerriorl avatar jeaubin avatar karmoham avatar lsheikal avatar lubnarasheed avatar lukasmcclelland avatar mdear avatar mirzawaqasahmed avatar netgab avatar oianson avatar omehrabi avatar renatoalmeidaoliveira avatar rtolos avatar simingy avatar sukanya-kalluri avatar taarini avatar tahigash avatar thomasjryan avatar timcragg avatar vanwang3 avatar xiaoxinz-cisco avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rest's Issues

REST connector: Invalid URL GET on connect to IOS-XE devices (RESTCONF)

Overview

When try to connect to a device object using the rest.connector.Rest class, an exception is raised.

device.connect(via="rest")

Exception (truncated)

2023-06-22T16:25:15: %AETEST-ERROR: Caught an exception while executing section [...]:
2023-06-22T16:25:15: %AETEST-ERROR: Traceback (most recent call last):
[...]
2023-06-22T16:25:15: %AETEST-ERROR:   File "src/pyats/connections/manager.py", line 453, in pyats.connections.manager.ConnectionManager.connect
2023-06-22T16:25:15: %AETEST-ERROR:   File "src/pyats/async_/synchronize.py", line 117, in pyats.async_.synchronize.Lockable.locked._wrapped
2023-06-22T16:25:15: %AETEST-ERROR:   File "[...]/venv/lib/python3.9/site-packages/rest/connector/libs/iosxe/implementation.py", line 145, in connect
2023-06-22T16:25:15: %AETEST-ERROR:     raise RequestException("Connection to '{ip}:{port}' has returned the "
2023-06-22T16:25:15: %AETEST-ERROR: requests.exceptions.RequestException: Connection to '192.0.2.1:443' has returned the following code '404', instead of the expected status code '200'

Reproduce

Assume the following testbed:

devices:
  my_device_1:
    os: iosxe
    platform: cat9k
    connections:
      rest:
        class: rest.connector.Rest
        ip: 192.0.2.1

And the following code:

device = tb.devices['my_device_1']
device.connect(via="rest")   # Raises the exception

This is because in file implementation.py, a GET request to https://192.0.2.1:443 is sent.
=> This is not a valid RESTCONF URL for IOS-XE devices.

Workaround

When changing the line 126 from:

self.login_url = '{f}/'.format(f=self.base_url)

to

self.login_url = '{f}/'.format(f=self.base_url)
# self.login_url = self.login_url + "restconf/data/<ANY-VALID-RESTCONF-RESOURCE>"
# Example:
self.login_url = self.login_url + "restconf/data/Cisco-IOS-XE-native:native/version"

the connect works.. This is similar to the CLI based connect and the first thing which is issued is a show version

NXOS connection using https fails.

Using the following testbed.yaml snippet:

devices:
  N9K-C9508-SPINE-01:
    type: switch
    connections:
      vty:
        alias: nxapi
        protocol: https
        ip: 172.31.0.61
        port: "443"
        username: admin
        password: cisco.123
        class: rest.connector.Rest
      defaults:
        via: vty
    platform: nxos
    os: nxos

I get the following error:

# python
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pyats.topology import loader
d('testbeds/testbed-17_1.yaml')
device = testbed.devices['N9K-C9508-SPINE-01']
device.connect(alias='nxapi',>>> testbed = loader.load('testbeds/testbed-17_1.yaml')
 via='vty')
device.rest.connected
<stdin>:1: DeprecationWarning: Device N9K-C9508-SPINE-01 'tacacs.username' is deprecated in the testbed YAML.  This key has been moved to device 'credentials'.
<stdin>:1: DeprecationWarning: Device N9K-C9508-SPINE-01 'passwords.linux' is  deprecated in the testbed YAML.  Use 'credentials' instead.
>>> device = testbed.devices['N9K-C9508-SPINE-01']
>>> device.connect(alias='nxapi', via='vty')
Traceback (most recent call last):
  File "/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 597, in urlopen
    httplib_response = self._make_request(conn, method, url,
  File "/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 384, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 380, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/lib/python3.8/http/client.py", line 1347, in getresponse
    response.begin()
  File "/usr/lib/python3.8/http/client.py", line 307, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.8/http/client.py", line 276, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/venv/lib/python3.8/site-packages/requests/adapters.py", line 439, in send
    resp = conn.urlopen(
  File "/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 637, in urlopen
    retries = retries.increment(method, url, error=e, _pool=self,
  File "/venv/lib/python3.8/site-packages/urllib3/util/retry.py", line 368, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/venv/lib/python3.8/site-packages/urllib3/packages/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 597, in urlopen
    httplib_response = self._make_request(conn, method, url,
  File "/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 384, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 380, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/lib/python3.8/http/client.py", line 1347, in getresponse
    response.begin()
  File "/usr/lib/python3.8/http/client.py", line 307, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.8/http/client.py", line 276, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "src/pyats/connections/manager.py", line 339, in pyats.connections.manager.ConnectionManager.connect
  File "src/pyats/async_/synchronize.py", line 117, in pyats.async_.synchronize.Lockable.locked._wrapped
  File "/venv/lib/python3.8/site-packages/rest/connector/libs/nxos/implementation.py", line 138, in connect
    response = self.session.post(login_url, data=_data, timeout=timeout)
  File "/venv/lib/python3.8/site-packages/requests/sessions.py", line 578, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "/venv/lib/python3.8/site-packages/requests/sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "/venv/lib/python3.8/site-packages/requests/sessions.py", line 643, in send
    r = adapter.send(request, **kwargs)
  File "/venv/lib/python3.8/site-packages/requests/adapters.py", line 498, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
>>> device.rest.connected
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "src/pyats/topology/device.py", line 518, in pyats.topology.device.Device.__getattr__
AttributeError: 'Device' object has no attribute 'rest'
>>>

I'm suspecting verify=false may be needed for the connection?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.