Ruby Configd Client Library

Introduction

Please refer to Configd Client Library for the general concepts behind the API. This document will cover only Ruby 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.

require "vyatta/configd"; if __FILE__ == $PROGRAM_NAME client = Vyatta::Configd::Client.new end

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.

require "vyatta/configd"; if __FILE__ == $PROGRAM_NAME client = Vyatta::Configd::Client.new # do something with the config client.session_teardown() end

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 Ruby 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.

require "vyatta/configd"; def setup_interface_address(client, intf_name) print(intf_name, ":") path = ["interfaces", "dataplane", intf_name, "address"] addrs = client.get(path) puts addrs.join(", ") if addrs.length == 0 client.set(path << "dhcp") end end if __FILE__ == $PROGRAM_NAME client = Vyatta::Configd::Client.new client.session_setup($$.to_s()) begin client.get("interfaces dataplane").each { |name| setup_interface_address(client, name) } client.commit("setup interface addresses") rescue Vyatta::Configd::Exception => e puts e.message end client.session_teardown() end

Below is a sample run of the above application.

RPC

One can also call an RPC via this interface. The Ruby API has a special helper to enable native hash tables to be used for RPC input and output. The Ruby 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 Ruby API has a special helper to enable the return of native hash tables representing the state tree. The Ruby API also exposes the string based  raw tree_get_full method if required. Note that unlike the other scripting language APIs Ruby does not yet support multiple encoding types. The "json" encoding is used for all queries.

Below is an example execution of this program.

Method documentation

The Ruby 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 Ruby native data types are done. See the C++ documentation for the list of available methods and documentation.

Paths

Paths provided via the Ruby 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 Ruby API provides several special methods to work better natively. Below are the provided methods.