goaci

package module
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 14, 2020 License: Apache-2.0 Imports: 11 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

goACI
ACI client library for Go


GoACI is a Go client library for Cisco ACI. It features a simple, extensible API, advanced JSON manipulation, and a backup client for running queries against the backup file.

Getting Started

Installing

To start using GoACI, install Go and go get:

$ go get -u github.com/brightpuddle/goaci

Basic Usage

package main

import "github.com/brightpuddle/goaci"

func main() {
    client, _ := goaci.NewClient("1.1.1.1", "user", "pwd")
    if err := client.Login(); err != nil {
        panic(err)
    }

    res, _ = client.Get("/api/mo/uni/tn-infra")
    println(res.Get("imdata.0.*.attributes.name"))
}

This will print:

infra
Result manipulation

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

res, _ := 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
}
Helpers for common patterns
res, _ := GetDn("uni/tn-infra")
res, _ := GetClass("fvTenant")
Query parameters

Pass the goaci.Query object to the Get request to add query paramters:

queryInfra := goaci.Query("query-target-filter", `eq(fvTenant.name,"infra")`)
res, _ := client.GetClass("fvTenant", queryInfra)

Pass as many paramters as needed:

res, _ := client.GetClass("isisRoute",
    goaci.Query("rsp-subtree-include", "relations"),
    goaci.Query("query-target-filter", `eq(isisRoute.pfx,"10.66.0.1/32")`,
)
POST data creation

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

exampleTenant := goaci.Body{}.Set("fvTenant.attributes.name", "goaci-example").Str
client.Post("/api/mo/uni/tn-goaci-example", exampleTenant)

These can be chained:

tenantA := goaci.Body{}.
    Set("fvTenant.attributes.name", "goaci-example-a").
    Set("fvTenant.attributes.descr", "Example tenant A")

...or nested:

attrs := goaci.Body{}.
    Set("name", "goaci-example-b").
    Set("descr", "Example tenant B").
    Str
tenantB := goaaci.Body{}.SetRaw("fvTenant.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/...", goaci.NoRefresh)
client.Refresh()

Backup client

goACI also features a backup file client for querying ACI .tar.gz backup files. This client partially mirrors the API of the HTTP client. Note that this must be imported separately.

package main

import "github.com/brightpuddle/goaci/backup"

func main() {
    client, _ := backup.NewClient("config.tar.gz")

    res, _ := client.GetDn("uni/tn-infra")
    println(res.Get("@pretty"))

    res, _ = client.GetClass("fvBD")
    for _, bd := range res.Get("#.fvBD.attributes").Array() {
        fmt.Priintln(bd)
    }

}

Documenatation and examples

See here for various examples.

And, the documentation for more details.

Documentation

Overview

Package goaci is a a Cisco ACI client library for Go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NoRefresh

func NoRefresh(req *Req)

NoRefresh prevents token refresh check. Primarily used by the Login and Refresh methods where this would be redundant.

func Query

func Query(k, v string) func(req *Req)

Query sets an HTTP query parameter.

client.GetClass("fvBD", goaci.Query("query-target-filter", `eq(fvBD.name,"bd-name")`))

Or set multiple parameters:

client.GetClass("fvBD",
  goaci.Query("rsp-subtree-include", "faults"),
  goaci.Query("query-target-filter", `eq(fvBD.name,"bd-name")`))

func RequestTimeout

func RequestTimeout(x time.Duration) func(*Client)

RequestTimeout modifies the HTTP request timeout from the default of 60 seconds.

Types

type Body

type Body struct {
	Str string
}

Body wraps SJSON for building JSON body strings. Usage example:

Body{}.Set("fvTenant.attributes.name", "mytenant").Str

func (Body) Res added in v0.3.3

func (body Body) Res() Res

Res creates a Res object, i.e. a GJSON result object.

func (Body) Set

func (body Body) Set(path, value string) Body

Set sets a JSON path to a value.

func (Body) SetRaw

func (body Body) SetRaw(path, rawValue string) Body

SetRaw sets a JSON path to a raw string value. This is primarily used for building up nested structures, e.g.:

Body{}.SetRaw("fvTenant.attributes", Body{}.Set("name", "mytenant").Str).Str

type Client added in v0.2.0

type Client struct {
	// HttpClient is the *http.Client used for API requests.
	HttpClient *http.Client
	// Url is the APIC IP or hostname, e.g. 10.0.0.1:80 (port is optional).
	Url string
	// Usr is the APIC username.
	Usr string
	// Pwd is the APIC password.
	Pwd string
	// LastRefresh is the timestamp of the last token refresh interval.
	LastRefresh time.Time
}

Client is an HTTP ACI API client. Use goaci.NewClient to initiate a client. This will ensure proper cookie handling and processing of modifiers.

func NewClient added in v0.2.0

func NewClient(url, usr, pwd string, mods ...func(*Client)) (Client, error)

NewClient creates a new ACI HTTP client. Pass modifiers in to modify the behavior of the client, e.g.

client, _ := NewClient("apic", "user", "password", RequestTimeout(120))

func (*Client) Do added in v0.4.0

func (client *Client) Do(req Req) (Res, error)

Do makes a request. Requests for Do are built ouside of the client, e.g.

req := goaci.NewReq("GET", "/api/class/fvBD", nil)
res := client.Do(req)

func (*Client) Get added in v0.2.0

func (client *Client) Get(path string, mods ...func(*Req)) (Res, error)

Get makes a GET request and returns a GJSON result. Results will be the raw data structure as returned by the APIC, wrapped in imdata, e.g.

{
  "imdata": [
    {
      "fvTenant": {
        "attributes": {
          "dn": "uni/tn-mytenant",
          "name": "mytenant",
        }
      }
    }
  ],
  "totalCount": "1"
}

func (*Client) GetClass added in v0.2.0

func (client *Client) GetClass(class string, mods ...func(*Req)) (Res, error)

GetClass makes a GET request by class and unwraps the results. Result is removed from imdata, but still wrapped in Class.attributes, e.g.

[
  {
    "fvTenant": {
      "attributes": {
        "dn": "uni/tn-mytenant",
        "name": "mytenant",
      }
    }
  }
]

func (*Client) GetDn added in v0.2.0

func (client *Client) GetDn(dn string, mods ...func(*Req)) (Res, error)

GetDn makes a GET request by DN. Result is removed from imdata and first result is removed from the list, e.g.

{
  "fvTenant": {
    "attributes": {
      "dn": "uni/tn-mytenant",
      "name": "mytenant",
    }
  }
}

func (*Client) Login added in v0.2.0

func (client *Client) Login() error

Login authenticates to the APIC.

func (*Client) Post added in v0.2.0

func (client *Client) Post(path, data string, mods ...func(*Req)) (Res, error)

Post makes a POST request and returns a GJSON result. Hint: Use the Body struct to easily create POST body data.

func (*Client) Refresh added in v0.2.0

func (client *Client) Refresh() error

Refresh refreshes the authentication token. Note that this will be handled automatically be default. Refresh will be checked every request and the token will be refreshed after 8 minutes. Pass goaci.NoRefresh to prevent automatic refresh handling and handle it directly instead.

type Req

type Req struct {
	// HttpReq is the *http.Request obejct.
	HttpReq *http.Request
	// Refresh indicates whether token refresh should be checked for this request.
	// Pass NoRefresh to disable Refresh check.
	Refresh bool
}

Req wraps http.Request for API requests.

func NewReq

func NewReq(method, uri string, body io.Reader, mods ...func(*Req)) Req

NewReq creates a new Req request.

type Res

type Res = gjson.Result

Res is an API response returned by client requests. This is a GJSON result, which offers advanced and safe parsing capabilities. https://github.com/tidwall/gjson

Directories

Path Synopsis
examples
backup command
http command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL