pyeapiasync.clientasync
Python Async Client for eAPI
- This module provides the async client for eAPI. It provides the primary
functions for building applications that work with Arista EOS eAPI-enabled nodes using asyncio and aiohttp.
Attributes
Classes
Represents a single device for sending and receiving eAPI messages |
Functions
|
Creates an async connection instance based on the transport |
|
Creates an async connection using the supplied settings |
Module Contents
- async pyeapiasync.clientasync.make_connection_async(transport, **kwargs)[source]
Creates an async connection instance based on the transport
This function creates the EapiAsyncConnection object based on the desired transport. It looks up the transport class in the TRANSPORTS global dictionary.
- Parameters:
transport (string) – The transport to use to create the instance.
**kwargs – Arbitrary keyword arguments.
- Returns:
An instance of an async connection object based on the transport
- Raises:
TypeError – A TypeError is raised if the transport keyword is not found in the list (keys) of available transports.
- async pyeapiasync.clientasync.connect_async(transport=None, host='localhost', username='admin', password='', port=None, key_file=None, cert_file=None, ca_file=None, timeout=60, return_node=False, context=None, **kwargs)[source]
Creates an async connection using the supplied settings
This function will create an async connection to an Arista EOS node using the arguments. All arguments are optional with default values.
- Parameters:
transport (str) – Specifies the type of connection transport to use. Valid values for the connection are socket, http_local, http, https, https_certs, http_session, and https_session. The default value is specified in DEFAULT_TRANSPORT
host (str) – The IP addres or DNS host name of the connection device. The default value is ‘localhost’
username (str) – The username to pass to the device to authenticate the eAPI connection. The default value is ‘admin’
password (str) – The password to pass to the device to authenticate the eAPI connection. The default value is ‘’
port (int) – The TCP port of the endpoint for the eAPI connection. If this keyword is not specified, the default value is automatically determined by the transport type. (http=80, https=443)
key_file (str) – Path to private key file for ssl validation
cert_file (str) – Path to PEM formatted cert file for ssl validation
ca_file (str) – Path to CA PEM formatted cert file for ssl validation
timeout (int) – timeout
context (ssl.SSLContext) – ssl object’s context. The default is None
return_node (bool) – Returns a Node object if True, otherwise returns an EapiAsyncConnection object.
- Returns:
- An instance of an EapiAsyncConnection object for the specified
transport.
- class pyeapiasync.clientasync.AsyncNode(connection, **kwargs)[source]
Bases:
object
- Represents a single device for sending and receiving eAPI messages
asynchronously
- The AsyncNode object provides an instance for communicating with Arista
EOS devices using asyncio. The AsyncNode object provides easy to use methods for sending both enable and config commands to the device using a specific transport.
- connection[source]
The connection property represents the underlying transport used by the AsyncNode object to communicate with the device using eAPI.
- Type:
- running_config
The running-c0)0onfig from the device. This property is lazily loaded and refreshed over the life cycle of the instance.
- Type:
str
- startup_config
The startup-config from the device. This property is lazily loaded and refreshed over the life cycle of the instance.
- Type:
str
- autorefresh[source]
If True, the running-config and startup-config are refreshed on config events. If False, then the config properties must be manually refreshed.
- Type:
bool
- config_defaults[source]
If True, the default config options will be shown in the running-config output
- Type:
bool
- Parameters:
connection (EapiAsyncConnection) – An instance of EapiAsyncConnection used as the transport for sending and receiving eAPI requests and responses.
**kwargs – An arbitrary list of keyword arguments
- property api[source]
Provides access to API modules loaded for this node
This property provides access to the API modules that have been loaded for this node. The API modules are lazily loaded the first time the property is referenced.
- Returns:
A dictionary of API module instances
- Return type:
dict
- api_autoload()[source]
Autoload API modules
This method will try to autoload API modules from the ‘pyeapiasync.api’ package. The modules will be loaded as instance attributes based on the name of the module. For instance, the ‘vlans’ module will be loaded as self.api.vlans.
Note
This method will attempt to load all modules that end with ‘async’ in the name.
- async get_running_config()[source]
Get the running config from the device asynchronously
- Returns:
The running configuration as a string
- async get_startup_config()[source]
Get the startup config from the device asynchronously
- Returns:
The startup configuration as a string
- async get_version()[source]
Get the version from the device asynchronously
- Returns:
The version string
- async get_version_number()[source]
Get the version number from the device asynchronously
- Returns:
The version number string
- enable_authentication(password)[source]
Configures the enable mode authentication password
EOS supports an additional password authentication mechanism for sessions that want to switch to executive (or enable) mode. This method will configure the password, if required, for entering executive mode
- Parameters:
password (str) – The password string in clear text used to authenticate to exec mode
- async config(commands, **kwargs)[source]
Configures the node with the specified commands asynchronously
This method is used to send configuration commands to the node. It will take either a string, list or CliVariants type and prepend the necessary commands to put the session into config mode. pyeapiasync.utils.CliVariants facilitates alternative executions to commands sequence until one variant succeeds or all fail
- Parameters:
commands (str, list, CliVariants) – The commands to send to the node in config mode. If the commands argument is an str or CliVariants type, it will be cast to a list. The list of commands will also be prepended with the necessary commands to put the session in config mode. CliVariants could be part of a list too, however only a single occurrence of CliVariants type in commands is supported. CliVariants type facilitates execution of alternative commands sequences, e.g.:
config( [cli1, CliVariants( cli2, cli3 ), cli4] )
the example above can be translated into following sequence:config( [cli1, cli2, cli4] )
config( [cli1, cli3, cli4] )
CliVariants accepts 2 or more arguments of str, list type, or their mix. Each argument to CliVariants will be joined with the rest of commands and all command sequences will be tried until one variant succeeds. If all variants fail the last failure exception will be re-raised.**kwargs – Additional keyword arguments for expanded eAPI functionality. Only supported eAPI params are used in building the request
- Returns:
- The config method will return a list of dictionaries with the
output from each command. The function will strip the response from any commands it prepends.
- async section(regex, config=None)[source]
Returns a section of the config asynchronously
- Parameters:
regex (str) – A valid regular expression used to select sections of configuration to return
config (str) – The configuration to return. If None, the running config will be used.
- Returns:
The configuration section as a string object.
- async enable(commands, encoding='json', strict=False, send_enable=True, **kwargs)[source]
- Sends the array of commands to the node in enable mode
asynchronously
This method will send the commands to the node and evaluate the results. If a command fails due to an encoding error, then the command set will be re-issued individual with text encoding.
- Parameters:
commands (list) – The list of commands to send to the node
encoding (str) – The requested encoding of the command output. Valid values for encoding are JSON or text
strict (bool) – If False, this method will attempt to run a command with text encoding if JSON encoding fails
send_enable (bool) – If True the enable command will be prepended to the command list automatically.
**kwargs – Additional keyword arguments for expanded eAPI functionality. Only supported eAPI params are used in building the request
- Returns:
- A dict object that includes the response for each command along
with the encoding
- Raises:
TypeError –
This method does not support sending configure commands and will raise a TypeError if configuration commands are found in the list of commands provided
This method will also raise a TypeError if the specified encoding is not one of ‘json’ or ‘text’
CommandError – This method will raise a CommandError if any one of the commands fails.
- async run_commands(commands, encoding='json', send_enable=True, **kwargs)[source]
Sends the commands over the transport to the device asynchronously
This method sends the commands to the device using the nodes transport. This is a lower layer function that shouldn’t normally need to be used, preferring instead to use config() or enable().
- Parameters:
commands (list) – The ordered list of commands to send to the device using the transport
encoding (str) – The encoding method to use for the request and excpected response.
send_enable (bool) – If True the enable command will be prepended to the command list automatically.
**kwargs – Additional keyword arguments for expanded eAPI functionality. Only supported eAPI params are used in building the request
- Returns:
- This method will return the raw response from the connection
which is a Python dictionary object.
- async get_config(config='running-config', params=None, as_string=False)[source]
Retreives the config from the node asynchronously
This method will retrieve the config from the node as either a string or a list object. The config to retrieve can be specified as either the startup-config or the running-config.
- Parameters:
config (str) – Specifies to return either the nodes startup-config or running-config. The default value is the running-config
params (str) – A string of keywords to append to the command for retrieving the config.
as_string (boo) – Flag that determines the response. If True, then the configuration is returned as a raw string. If False, then the configuration is returned as a list. The default value is False
- Returns:
This method will return either a string or a list depending on the states of the as_string keyword argument.
- Raises:
TypeError – If the specified config is not one of either ‘running-config’ or ‘startup-config’
- async refresh()[source]
Refreshes the instance config properties asynchronously
This method will refresh the public running_config and startup_config properites. Since the properties are lazily loaded, this method will clear the current internal instance variables. One the next call the instance variables will be repopulated with the current config