goaci package - github.com/brightpuddle/goaci - Go Packages

goaci

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2019 License: Apache-2.0 Imports: 14 Imported by: 1

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() {
    apic, _ := goaci.NewAPIC("1.1.1.1", "user", "pwd")
    if err := apic.Login(); err != nil {
        panic(err)
    }

    res, _ = apic.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, _ := apic.GetClass("fvTenant", queryInfra)

Pass as many paramters as needed:

res, _ := apic.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
apic.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 APIC client keeps a timer and checks elapsed time on each request, refreshing the token every 8 minutes. This can be handled manually if desired:

apic, _ := goaci.NewAPIC("1.1.1.1", "user", "pwd")
apic.Login()

res, _ := apic.Get("/api/...", goaci.NoRefresh)
apic.Refresh()
Backup client

goACI also features a "backup" client for querying ACI .tar.gz backup files. This client partially mirrors the API of the HTTP client.

backup, _ := goaci.NewBackup("config.tar.gz")

res, _ := backup.GetDn("uni/tn-infra")
res, _ = backup.GetClass("fvBD")

Examples

See the examples for more usage.

Documentation

Overview

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

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

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

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

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.

func Query

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

Query sets an HTTP query parameter.

func RequestTimeout

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

RequestTimeout modifies the HTTP request timeout from the default.

Types

type APIC

type APIC struct {
	// contains filtered or unexported fields
}

APIC is an HTTP API client.

func NewAPIC

func NewAPIC(url, usr, pwd string, mods ...func(*APIC)) (APIC, error)

NewAPIC creates a new ACI HTTP client.

func (APIC) Get

func (apic APIC) Get(path string, mods ...func(*Req)) (Res, error)

Get makes a GET request and returns a GJSON result.

func (APIC) GetClass

func (apic APIC) GetClass(class string, mods ...func(*Req)) (Res, error)

GetClass makes a GET request by class and unwraps the results.

func (APIC) GetDn

func (apic APIC) GetDn(dn string, mods ...func(*Req)) (Res, error)

GetDn makes a GET request by DN.

func (APIC) Login

func (apic APIC) Login() error

Login authenticates to the APIC.

func (APIC) Post

func (apic APIC) Post(path, data string, mods ...func(*Req)) (Res, error)

Post makes a POST request and returns a GJSON result.

func (APIC) Refresh

func (apic APIC) Refresh() error

Refresh refreshes the authentication token.

type Backup

type Backup struct {
	// contains filtered or unexported fields
}

Backup is a client that queries an ACI backup file.

func NewBackup

func NewBackup(src string) (Backup, error)

NewBackup creates a new backup file client.

func (Backup) GetClass

func (bkup Backup) GetClass(class string, mods ...func(*Req)) (Res, error)

GetClass queries the backup for an MO class.

func (Backup) GetDn

func (bkup Backup) GetDn(dn string, mods ...func(*Req)) (Res, error)

GetDn queries the backup for a specific DN.

type Body

type Body struct {
	Str string
}

Body wraps SJSON for building JSON body strings.

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.

type Req

type Req struct {
	// contains filtered or unexported fields
}

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.

type Res

type Res = gjson.Result

Res is an API response returned by APIC and Backup requests.

Directories

Path Synopsis
examples
apic command
backup command

Jump to

Keyboard shortcuts

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