go-aci
is a Go client library for Cisco ACI. It is based on Nathan's excellent goaci module and features a simple, extensible API and advanced JSON manipulation.
To start using go-aci
, install Go and go get
:
$ go get -u github.com/netascode/go-aci
package main
import "github.com/netascode/go-aci"
func main() {
client, _ := aci.NewClient("https://1.1.1.1", "user", "pwd")
res, _ = client.Get("/api/mo/uni/tn-infra")
println(res.Get("imdata.0.*.attributes.name"))
}
This will print:
infra
aci.Result
uses GJSON to simplify handling JSON results. See the GJSON documentation for more detail.
res, _ := client.GetClass("fvBD")
res.Get("0.fvBD.attributes.name").Str // name of first BD
res.Get("0.*.attributes.name").Str // name of first BD (if you don't know the class)
for _, bd := range res.Array() {
println(res.Get("*.attributes|@pretty")) // pretty print BD attributes
}
for _, bd := range res.Get("#.fvBD.attributes").Array() {
println(res.Get("@pretty")) // pretty print BD attributes
}
res, _ := client.GetDn("uni/tn-infra")
res, _ := client.GetClass("fvTenant")
Pass the aci.Query
object to the Get
request to add query paramters:
queryInfra := aci.Query("query-target-filter", `eq(fvTenant.name,"infra")`)
res, _ := client.GetClass("fvTenant", queryInfra)
Pass as many paramters as needed:
res, _ := client.GetClass("isisRoute",
aci.Query("rsp-subtree-include", "relations"),
aci.Query("query-target-filter", `eq(isisRoute.pfx,"10.66.0.1/32")`),
)
aci.Body
is a wrapper for SJSON. SJSON supports a path syntax simplifying JSON creation.
exampleTenant := aci.Body{}.Set("fvTenant.attributes.name", "aci-example").Str
client.Post("/api/mo/uni/tn-aci-example", exampleTenant)
These can be chained:
tenantA := aci.Body{}.
Set("fvTenant.attributes.name", "aci-example-a").
Set("fvTenant.attributes.descr", "Example tenant A")
...or nested:
attrs := aci.Body{}.
Set("name", "aci-example-b").
Set("descr", "Example tenant B").
Str
tenantB := aci.Body{}.SetRaw("fvTenant.attributes", attrs).Str
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/...", aci.NoRefresh)
client.Refresh()
See the documentation for more details.