Skip to content

Latest commit

 

History

History
117 lines (80 loc) · 2.98 KB

README.md

File metadata and controls

117 lines (80 loc) · 2.98 KB

Tests

go-nxos

go-nxos is a Go client library for Cisco NX-OS devices. It is based on Nathan's excellent goaci module and features a simple, extensible API and advanced JSON manipulation.

Getting Started

Installing

To start using go-nxos, install Go and go get:

$ go get -u github.com/netascode/go-nxos

Basic Usage

package main

import "github.com/netascode/go-nxos"

func main() {
    client, _ := nxos.NewClient("1.1.1.1", "user", "pwd", true)

    res, _ := client.Get("/api/mo/sys/intf/phys-[eth1/1]")
    println(res.Get("imdata.0.*.attributes.id").String())
}

This will print:

eth1/1

Result manipulation

nxos.Result uses GJSON to simplify handling JSON results. See the GJSON documentation for more detail.

res, _ := client.GetClass("l1PhysIf")
println(res.Get("0.l1PhysIf.attributes.name").String()) // name of first physical interface

for _, int := range res.Array() {
    println(int.Get("*.attributes|@pretty")) // pretty print physical interface attributes
}

for _, attr := range res.Get("#.l1PhysIf.attributes").Array() {
    println(attr.Get("@pretty")) // pretty print BD attributes
}

Helpers for common patterns

res, _ := client.GetDn("sys/intf/phys-[eth1/1]")
res, _ := client.GetClass("l1PhysIf")
res, _ := client.DeleteDn("sys/userext/user-[testuser]")

Query parameters

Pass the nxos.Query object to the Get request to add query parameters:

queryInfra := nxos.Query("query-target-filter", `eq(l1PhysIf.id,"eth1/1")`)
res, _ := client.GetClass("l1PhysIf", queryInfra)

Pass as many parameters as needed:

res, _ := client.GetClass("interfaceEntity",
    nxos.Query("rsp-subtree-include", "l1PhysIf"),
    nxos.Query("query-target-filter", `eq(l1PhysIf.id,"eth1/1")`)
)

POST data creation

nxos.Body is a wrapper for SJSON. SJSON supports a path syntax simplifying JSON creation.

exampleInt := nxos.Body{}.Set("l1PhysIf.attributes.id", "eth1/1").Str
client.Post("/api/mo/sys/intf/phys-[eth1/1]", exampleInt)

These can be chained:

int1 := nxos.Body{}.
    Set("l1PhysIf.attributes.id", "eth1/1").
    Set("l1PhysIf.attributes.mode", "trunk")

...or nested:

attrs := nxos.Body{}.
    Set("id", "eth1/1").
    Set("mode", "trunk").
    Str
int1 := nxos.Body{}.SetRaw("l1PhysIf.attributes", attrs).Str

Token refresh

Token refresh is handled automatically. The client keeps a timer and checks elapsed time on each request, refreshing the token every 8 minutes. This can be handled manually if desired:

res, _ := client.Get("/api/...", nxos.NoRefresh)
client.Refresh()

Documentation

See the documentation for more details.