Python Configd Client Library

Introduction

Please refer to Configd Client Library for the general concepts behind the API. This document will cover only Python specifics.

Mechanics

Before we can use the configd API we need to establish a connection to the daemon this is done when a Client object is instantiated. Each client object represents a new connection to configd so they should be shared to avoid connection setup overhead. When the Client object is collected, the connection is terminated.

from vyatta import configd client = configd.Client()

Once connected we can do a variety of actions on the system, one can configure the device, request the operational state of the device, or perform feature specific RPCs. When configuring the device via the Configd APIs one follows much the same procedure as with the CLI. One creates a configuration session, makes changes, commits those changes, then tears down the session. One may view the RUNNING datastore without a session being created. Sessions persist process exit and must be explicitly removed with "session_teardown". The minimal configuration application looks like the following.

from vyatta import configd from os import getpid client = configd.Client() client.session_setup(str(getpid())) # do something with config client.session_teardown()

From here, one may perform actions similar to what one does on the CLI. Since the interface here is meant for programatic interaction, one may also introspect on the data-model and the current state of the candidate tree to determine available actions, this is similar to using "tab-completion" to inspect the CLI.

Examples

Configuration

The following example uses the Python configd API to set all dataplane interfaces to listen for DHCP addresses if no other address is assigned to that interface. While this is a silly thing to do, it demonstrates the basic mechanics of the this API.

from vyatta import configd from os import getpid from sys import stdout, stderr def setup_interface_address(client, intf_name): stdout.write(intf_name + ":") path = ["interfaces", "dataplane", intf_name, "address"] addrs = client.get(path) print(", ".join(addrs)) if len(addrs) == 0 : client.set(path + ["dhcp"]) def main(): client = configd.Client() client.session_setup(str(getpid())) try: for intf_name in client.get("interfaces dataplane"): setup_interface_address(client, intf_name) client.commit("setup interface addresses") except configd.Exception as e: stderr.write(str(e)) client.session_teardown() if __name__ == "__main__": main()

Below is a sample run of the above application.

RPC

One can also call an RPC via this interface. The Python API has a special helper to enable native dictionaries to be used for RPC input and output. The Python API also exposes the string based raw call_rpc method if required.

Below is an example run of the RPC application.

Operational State Data

Finally one may query for operational data information. The Python API has a special helper to enable the return of native dictionaries representing the state tree. The Python API also exposes the string based  raw tree_get_full method if required.

Below is an example execution of this program.

Method documentation

The Python API is generated using SWIG from the C++ API. CamelCase method names are converted to under_case method names and the appropriate input and output conversions to Python native data types are done. See the C++ documentation for the list of available methods and documentation.

Paths

Paths provided via the Python API may either Arrays or space separated strings and will be converted to the appropriate C++ type automatically before calling the underlying method.

Special Methods

As mentioned in the examples section the Python API provides several special methods to work better natively. Below are the provided methods.