This section explains how to install, configure, and use the Avi Load Balancer Go SDK package for the various automation requirements.
Avi Load Balancer Go SDK is a Go Package that provides other APIs to communicate with the Avi Load Balancer REST APIs. Avi Load Balancer GO SDK package also provides utilities, which can be used to simplify and automate load balancing configurations on an Avi Load Balancer Controller. The following are the essential features for the Avi Load Balancer GO SDK package:
Uses Avi Load Balancer session class and provides utilities to simplify integration with the Avi Load Balancer Controller.
Helps in session authentication and keeps a cache of sessions to avoid multiple connections and tear downs across the different API sessions invocation.
Automatically updates session cookies and CSRF Tokens from the Avi Load Balancer Controllers and provides helper APIs and templates for Avi Load Balancer Objects.
X-AVI-TENANT (tenant) header handling and provides the sample source code for common load balancing examples.
SDK Directories
Avi Load Balancer Go SDK package is the source for the Go SDK for Avi Load Balancer Controller configuration. It is not required to download packages. Go will take care of package installations.
The following are the important Avi Load Balancer Go SDK directories:
examples: Go samples are in the go/examples directory. create_vs.go provides automation samples for the following configuration on Avi Load Balancer Controller:
Create session
Generic client to Avi Load Balancer Controller
Create a pool with one server, create an SSL virtual service, and fetch an object by name
clients: Contains Avi Load Balancer clients to establish a connection between the Go SDK and Avi Load Balancer Controller using the Avi Load Balancer session class. Each resource has its client to establish the connection.
session: Contains all the generic codes of the REST API calls for the Avi Load Balancer session and helper routines from REST API calls. It creates and maintains a session for the given resource.
models: Contains all models required to capture the API response. These are the structures to grab and store data of corresponding Avi Load Balancer REST API calls.
Prerequisites
Go Lang package must be installed before installing the Avi Load Balancer Go SDK package. To install the Go Lang package, see Go Lang Installation.
For more information on the Go programming language, see Go Lang Documentation.
Installing Avi Load Balancer Go SDK Package
The following is the syntax for installing Avi Load Balancer Go SDK package:
$ mkdir -p src/github.com/avinetworks/ $ cd src/github.com/avinetworks/ $ git clone https://github.com/avinetworks/sdk.git #GOPATH will be path till src dir. $ export GOPATH=~/src
Usage Examples
To create a session, a pool, and a basic virtual service (my-test-vs), execute create_vs.go file. Before executing this script, specify Avi Load Balancer Controller IP address, username, password, and tenant in create_vs.go file.
- Importing Avi Load Balancer Sessions, Models, and Clients
-
package main import ( "github.com/avinetworks/sdk/go/clients" "github.com/avinetworks/sdk/go/models" "github.com/avinetworks/sdk/go/session" )
- Creating Avi Load Balancer API Session
-
The sample syntax mentioned below creates a session with the following attributes:
Client session IP address: 10.10.25.25
Tenant: admin
Session Type: Not secure
aviClient, err := clients.NewAviClient("10.10.25.25", "admin", session.SetPassword("something"), session.SetTenant("admin"), session.SetInsecure)
- Creating a Pool
-
The syntax mentioned below creates a pool with the following attributes:
Pool Name: my-test-pool
Server IP Address: 10.90.20.12
pobj := models.Pool{} pobj.Name = "my-test-pool" serverobj := models.Server{} serverobj.Enabled = true serverobj.IP = &models.IPAddr{Type: "V4", Addr: "10.90.20.12"} pobj.Servers = append(pobj.Servers, &serverobj) npobj, err := aviClient.Pool.Create(&pobj) if err != nil { fmt.Println("Pool creation failed: ", err) return }
- Creating a Virtual Service
-
The syntax mentioned below created a virtual service with the following attributes:
Name: my-test-vs
IP Address: 10.90.20.51
Port Number: 80
The pool associated with the virtual service: my-test-pool
vsobj := models.VirtualService{} vsobj.Name = "my-test-vs" vipip := models.IPAddr{Type: "V4", Addr: "10.90.20.51"} vsobj.Vip = append(vsobj.Vip, &models.Vip{VipID: "myvip", IPAddress: &vipip}) vsobj.PoolRef = npobj.UUID vsobj.Services = append(vsobj.Services, &models.Service{Port: 80}) nvsobj, err := aviClient.VirtualService.Create(&vsobj) if err != nil { fmt.Println("VS creation failed: ", err) return } fmt.Printf("VS obj: %+v", *nvsobj)
- Fetching an Object by the Name
-
The sample syntax mentioned below fetches the virtual service details with the name my-test-vs and provides the following output: Cloud UUID: f39f950a-e6ca-442d-b546-fc31520991bb.
var obj interface{} err = aviClient.AviSession.GetObjectByName("virtualservice", "my-test-vs", &obj) fmt.Printf("VS obj: %v\n", obj) err = aviClient.AviSession.GetObject( "virtualservice", session.SetName("my-test-vs"), session.SetResult(&obj), session.SetCloudUUID("cloud-f39f950a-e6ca-442d-b546-fc31520991bb")) fmt.Printf("VS with CLOUD_UUID obj: %v", obj)
- Deleting a Virtual Service
-
The syntax mentioned below delete the desired virtual service using the Object ID for the virtual service.
aviClient.VirtualService.Delete(nvsobj.UUID)
- Deleting a Pool
-
Use the following syntax to delete the desired pool.
aviClient.Pool.Delete(npobj.UUID)
- Creating a Basic Virtual Service (my-test-vs) using create_vs.go
-
$ go run create_vs.go
- Metric and Inventory API Example
-
package main import ( //"flag" "fmt" "github.com/avinetworks/sdk/go/clients" "github.com/avinetworks/sdk/go/session" ) type MetricRequest struct { Step int `json:"step"` Limit int `json:"limit"` EntityUUID string `json:"entity_uuid"` MetricID string `json:"metric_id"` IncludeName string `json:"include_name"` IncludeRefs string `json:"include_refs"` PadMissingData string `json:"pad_missing_data"` } type Metrics struct { MetricRequests []MetricRequest `json:"metric_requests"` } func main() { // Create a session and a generic client to Avi Controller aviClient, err := clients.NewAviClient("10.10.25.42", "admin", session.SetPassword(""), session.SetTenant("admin"), session.SetInsecure) if err != nil { fmt.Println("Couldn't create session: ", err) return } mr := MetricRequest{Step: 1, Limit: 1, EntityUUID: "*", MetricID: "l7_server.max_concurrent_sessions", IncludeName: "True", IncludeRefs: "True", PadMissingData: "False"} sr := []MetricRequest{} sr = append(sr, mr) req := Metrics{MetricRequests: sr} path := "/api/analytics/metrics/collection" var rsp interface{} aviClient.AviSession.Post(path, req, &rsp) fmt.Printf("response %v\n", rsp) }
Compilation
Use the following syntax to compile the Avi Load Balancer Go SDK package.
$ go build -o /usr/bin/create_vs create_vs.go
Including Go SDK package in a third party code
The Avi Load Balancer Go SDK package can also be integrated with any third-party Go code package. The following is an example of importing the Avi Load Balancer Go SDK package into a third-party Go code package.
import ( "github.com/avinetworks/sdk/go/clients" "github.com/avinetworks/sdk/go/session"
The following is an example entry of vendor.json file for the Terraform provider:
"package": [ { "path": "github.com/avinetworks/sdk/go/clients", "revision": "796ddcccdc37a5a9771bfbb716b159ae5c9b4b11", "revisionTime": "2018-04-06T16:51:27.185773", "version": "18.1.3", "versionExact": "18.1.3" }, { "path": "github.com/avinetworks/sdk/go/session", "revision": "796ddcccdc37a5a9771bfbb716b159ae5c9b4b11", "revisionTime": "2018-04-06T16:51:27.185773", "version": "18.1.3", "versionExact": "18.1.3" } ]
Additional Information
The location on GitHub for Avi Load Balancer Go SDK package: https://github.com/vmware/alb-sdk/tree/eng/go