Categorygithub.com/netascode/go-nxos
modulepackage
0.3.0
Repository: https://github.com/netascode/go-nxos.git
Documentation: pkg.go.dev

# README

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.

# Functions

BackoffDelayFactor modifies the backoff delay factor from the default of 3.
BackoffMaxDelay modifies the maximum delay between two retries from the default of 60.
BackoffMinDelay modifies the minimum delay between two retries from the default of 4.
MaxRetries modifies the maximum number of retries from the default of 3.
NewClient creates a new NXOS HTTP client.
NoLogPayload prevents logging of payloads.
NoRefresh prevents token refresh check.
Query sets an HTTP query parameter.
RequestTimeout modifies the HTTP request timeout from the default of 60 seconds.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

Body wraps SJSON for building JSON body strings.
Client is an HTTP NXOS NX-API client.
Req wraps http.Request for API requests.

# Type aliases

Res is an API response returned by client requests.