Eapilib

Provides wrapper for eAPI calls

This module provides a connection to eAPI by wrapping eAPI calls in an instance of Connection. The connection module provides an easy implementation for sending and receiving calls over eAPI using a HTTP/S transport.

exception pyeapi.eapilib.CommandError(code, message, **kwargs)[source]

Bases: pyeapi.eapilib.EapiError

Base exception raised for command errors

The CommandError instance provides a custom exception that can be used if the eAPI command(s) fail. It provides some additional information that can be used to understand what caused the exception.

Parameters
  • error_code (int) – The error code returned from the eAPI call.

  • error_text (string) – The error text message that coincides with the error_code

  • commands (array) – The list of commands that were sent to the node that generated the error

  • message (string) – The exception error message which is a concatenation of the error_code and error_text

get_trace()[source]
property trace
exception pyeapi.eapilib.ConnectionError(connection_type, message, commands=None)[source]

Bases: pyeapi.eapilib.EapiError

Base exception raised for connection errors

Connection errors are raised when a connection object is unable to connect to the node. Typically these errors can result from using the wrong transport type or not providing valid credentials.

Parameters
  • commands (array) – The list of commands there were sent to the node that when the exception was raised

  • connection_type (string) – The string identifier for the connection object that generate the error

  • message (string) – The exception error message

  • response (string) – The message generate from the response packet

class pyeapi.eapilib.EapiConnection[source]

Bases: object

Creates a connection to eAPI for sending and receiving eAPI requests

The EapiConnection object provides an implementation for sending and receiving eAPI requests and responses. This class should not need to be instantiated directly.

authentication(username, password)[source]

Configures the user authentication for eAPI

This method configures the username and password combination to use for authenticating to eAPI.

Parameters
  • username (str) – The username to use to authenticate the eAPI connection with

  • password (str) – The password in clear text to use to authenticate the eAPI connection with

execute(commands, encoding='json', **kwargs)[source]

Executes the list of commands on the destination node

This method takes a list of commands and sends them to the destination node, returning the results. The execute method handles putting the destination node in enable mode and will pass the enable password, if required.

Parameters
  • commands (list) – A list of commands to execute on the remote node

  • encoding (string) – The encoding to send along with the request message to the destination node. Valid values include ‘json’ or ‘text’. This argument will influence the response object encoding

  • **kwargs – Arbitrary keyword arguments

Returns

A decoded response message as a native Python dictionary object that has been deserialized from JSON.

Raises

CommandError – A CommandError is raised that includes the error code, error message along with the list of commands that were sent to the node. The exception instance is also stored in the error property and is availble until the next request is sent

request(commands, encoding=None, reqid=None, **kwargs)[source]

Generates an eAPI request object

This method will take a list of EOS commands and generate a valid eAPI request object form them. The eAPI request object is then JSON encoding and returned to the caller.

eAPI Request Object

{
    "jsonrpc": "2.0",
    "method": "runCmds",
    "params": {
        "version": 1,
        "cmds": [
            <commands>
        ],
        "format": [json, text],
    }
    "id": <reqid>
}
Parameters
  • commands (list) – A list of commands to include in the eAPI request object

  • encoding (string) – The encoding method passed as the format parameter in the eAPI request

  • reqid (string) – A custom value to assign to the request ID field. This value is automatically generated if not passed

  • **kwargs – Additional keyword arguments for expanded eAPI functionality. Only supported eAPI params are used in building the request

Returns

A JSON encoding request structure that can be send over eAPI

send(data)[source]

Sends the eAPI request to the destination node

This method is responsible for sending an eAPI request to the destination node and returning a response based on the eAPI response object. eAPI responds to request messages with either a success message or failure message.

eAPI Response - success

{
    "jsonrpc": "2.0",
    "result": [
        {},
        {}
        {
            "warnings": [
                <message>
            ]
        },
    ],
    "id": <reqid>
}

eAPI Response - failure

{
    "jsonrpc": "2.0",
    "error": {
        "code": <int>,
        "message": <string>
        "data": [
            {},
            {},
            {
                "errors": [
                    <message>
                ]
            }
        ]
    }
    "id": <reqid>
}
Parameters

data (string) – The data to be included in the body of the eAPI request object

Returns

A decoded response. The response object is deserialized from

JSON and returned as a standard Python dictionary object

Raises

CommandError if an eAPI failure response object is returned from – the node. The CommandError exception includes the error code and error message from the eAPI response.

exception pyeapi.eapilib.EapiError(message, commands=None)[source]

Bases: Exception

Base exception class for all exceptions generated by eapilib

This is the base exception class for all exceptions generated by eapilib. It is provided as a catch all for exceptions and should not be directly raised by an methods or functions

Parameters
  • commands (array) – The list of commands there were sent to the node that when the exception was raised

  • message (string) – The exception error message

class pyeapi.eapilib.HTTPSCertConnection(path, host, port, key_file, cert_file, ca_file, timeout=None)[source]

Bases: http.client.HTTPSConnection

Class to make a HTTPS connection, with support for full client-based SSL Authentication.

connect()[source]

Connect to a host on a given (SSL) port. If ca_file is pointing somewhere, use it to check Server Certificate.

Redefined/copied and extended from httplib.py:1105 (Python 2.6.x). This is needed to pass cert_reqs=ssl.CERT_REQUIRED as parameter to ssl.wrap_socket(), which forces SSL to check server certificate against our client certificate.

class pyeapi.eapilib.HttpConnection(path, *args, **kwargs)[source]

Bases: http.client.HTTPConnection

class pyeapi.eapilib.HttpEapiConnection(host, port=None, path=None, username=None, password=None, timeout=60, **kwargs)[source]

Bases: pyeapi.eapilib.EapiConnection

class pyeapi.eapilib.HttpLocalEapiConnection(port=None, path=None, timeout=60, **kwargs)[source]

Bases: pyeapi.eapilib.EapiConnection

class pyeapi.eapilib.HttpsConnection(path, *args, **kwargs)[source]

Bases: http.client.HTTPSConnection

class pyeapi.eapilib.HttpsEapiCertConnection(host, port=None, path=None, key_file=None, cert_file=None, ca_file=None, timeout=60, **kwargs)[source]

Bases: pyeapi.eapilib.EapiConnection

class pyeapi.eapilib.HttpsEapiConnection(host, port=None, path=None, username=None, password=None, context=None, timeout=60, **kwargs)[source]

Bases: pyeapi.eapilib.EapiConnection

disable_certificate_verification()[source]
class pyeapi.eapilib.SocketConnection(path, timeout=60)[source]

Bases: http.client.HTTPConnection

connect()[source]

Connect to the host and port specified in __init__.

class pyeapi.eapilib.SocketEapiConnection(path=None, timeout=60, **kwargs)[source]

Bases: pyeapi.eapilib.EapiConnection

pyeapi.eapilib.https_connection_factory(path, host, port, context=None, timeout=60)[source]