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.

1 2 3 4 5 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.

1 2 3 4 5 6 7 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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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.

1 2 3 4 $ ruby configdapi.rb dp0s3:10.156.58.201/23 dp0s10: dp0s11:

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.

1 2 3 4 5 6 7 require "vyatta/configd"; if __FILE__ == $PROGRAM_NAME client = Vyatta::Configd::Client.new output = client.call_rpc_hash("vyatta-op-v1", "ping", {"host"=>"1.1.1.1"}) print "rx-packet-count ", output["rx-packet-count"], "\n" end

Below is an example run of the RPC application.

1 2 $ ruby configdapi.rb rx-packet-count 3

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.

1 2 3 4 5 6 7 require "vyatta/configd"; if __FILE__ == $PROGRAM_NAME client = Vyatta::Configd::Client.new tree = client.tree_get_full_hash("system state platform") print "OS Release: ", tree["platform"]["os-release"], "\n" end

Below is an example execution of this program.

1 2 $ ruby configdapi.rb OS Release: Vyatta:Master

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.

1 2 3 4 Client.get(path) -> array Client.call_rpc_hash(module, name, input) -> Hash Client.tree_get_hash(path) -> Hash Client.tree_get_full_hash(path) -> Hash