Go Configd Client Library

Introduction

Please refer to Configd Client Library for the general concepts behind the API. This document will cover only Go specifics. The Go client library is fairly low level, like the C library. There are additional helper libraries that make the Go library easier to use. The "github.com/danos/utils/pathutil" library can do path encoding for the Go library allowing slices of strings to be used for paths in the programs using this API. The "github.com/danos/encoding/rfc7951/data" library is useful for handling the feature defined data returned from this API.

Mechanics

Before we can use the configd API we need to establish a connection to the daemon, instantiating the CfgClient object will do this. The destructor for the client object will close the connection.

package main import ( "fmt" "os" configd "github.com/danos/configd/client" ) func handleError(err error) { if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } } func main() { client, err := configd.Connect() handleError(err) defer client.Close() }

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 SessionTeardown. The minimal configuration application looks like the following.

package main import ( "fmt" "os" "strconv" configd "github.com/danos/configd/client" ) func handleError(err error) { if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } } func main() { client, err := configd.Connect( configd.SessionID(strconv.Itoa(os.Getpid())), ) handleError(err) defer client.Close() err = client.SessionSetup() handleError(err) defer client.SessionTeardown() //do something with configuration. }

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

package main import ( "fmt" "os" "strconv" "strings" configd "github.com/danos/configd/client" "github.com/danos/configd/rpc" "github.com/danos/utils/pathutil" ) func handleError(err error) { if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } } func setupInterfaceAddress(client *configd.Client, intf string) error { fmt.Printf("%s:", intf) path := []string{"interfaces", "dataplane", intf, "address"} addrs, err := client.Get(rpc.AUTO, pathutil.Pathstr(path)) if err != nil { return err } fmt.Println(strings.Join(addrs, ", ")) if len(addrs) == 0 { path = append(path, "dhcp") out, err := client.Set(pathutil.Pathstr(path)) if out != "" { fmt.Println(out) } return err } return nil } func main() { client, err := configd.Connect( configd.SessionID(strconv.Itoa(os.Getpid())), ) handleError(err) defer client.Close() err = client.SessionSetup() handleError(err) defer client.SessionTeardown() intfs, err := client.Get(rpc.AUTO, pathutil.Pathstr([]string{"interfaces", "dataplane"})) handleError(err) for _, intf := range intfs { err := setupInterfaceAddress(client, intf) handleError(err) } out, err := client.Commit("setup interface addresses", false) if out != "" { fmt.Println(out) } handleError(err) }

Below is a sample run of the above application.

RPC

One can also call an RPC via this interface.

Below is an example run of the RPC application.

Operational State Data

Finally one may query for operational data information.

Below is an example execution of this program.