Go Client For Nutanix Storage Versioned APIs
The Go client for Nutanix Storage Versioned APIs is designed for Go client application developers offering them simple and flexible access to APIs that manages volume groups and storage containers in Nutanix cluster.
Features
- Invoke Nutanix APIs with a simple interface.
- Handle Authentication seamlessly.
- Reduce boilerplate code implementation.
- Use standard methods for installation.
Version
- API version: v4.0.a3
- Package version: v4.0.2-alpha.3
Requirements.
Go 1.11 or above are fully supported and tested.
Installation & Usage
Installation
Using go get
Install the latest version
$ go get github.com/nutanix/ntnx-api-golang-clients/storage-go-client/v4/...
Install a specific version
$ go get github.com/nutanix/ntnx-api-golang-clients/storage-go-client/v4/...@v4.0.2-alpha.3
Using go modules
Install the latest version
In go.mod file add the following
module your-module
go {GO_VERSION}
require (
github.com/nutanix/ntnx-api-golang-clients/storage-go-client/v4 latest
)
Install a specific version
In go.mod file add the following
module your-module
go {GO_VERSION}
require (
github.com/nutanix/ntnx-api-golang-clients/storage-go-client/v4 v4.0.2-alpha.3
)
Configuration
The Go client for Nutanix Storage Versioned APIs can be configured with the following parameters
Parameter |
Description |
Required |
Default Value |
Scheme |
URI scheme for connecting to the cluster (HTTP or HTTPS using SSL/TLS) |
No |
https |
Host |
IPv4/IPv6 address or FQDN of the cluster to which the client will connect to |
Yes |
N/A |
Port |
Port on the cluster to which the client will connect to |
No |
9440 |
Username |
Username to connect to a cluster |
Yes |
N/A |
Password |
Password to connect to a cluster |
Yes |
N/A |
Debug |
Runs the client in debug mode if specified |
No |
False |
VerifySSL |
Verify SSL certificate of cluster, the client will connect to |
No |
True |
Proxy |
Configure a proxy, the client will connect to |
No |
N/A |
MaxRetryAttempts |
Maximum number of retry attempts while connecting to the cluster |
No |
5 |
RetryInterval |
Interval in milliseconds at which retry attempts are made |
No |
3000 |
LoggerFile |
File location to which debug logs are written to |
No |
N/A |
ConnectTimeout |
Connection timeout in milliseconds for all operations |
No |
30000 |
ReadTimeout |
Read timeout in milliseconds for all operations |
No |
30000 |
A Proxy can be configured with the following parameters
Parameter |
Description |
Required |
Default Value |
Proxy.Scheme |
URI Scheme for connecting to the proxy ("http", "https" or "socks5") |
Yes |
N/A |
Proxy.Host |
Host of the proxy to which the client will connect to |
Yes |
N/A |
Proxy.Port |
Port of the proxy to which the client will connect to |
Yes |
N/A |
Proxy.Username |
Username to connect to the proxy |
- |
N/A |
Proxy.Password |
Password to connect to the proxy |
- |
N/A |
Sample Configuration
import (
"github.com/nutanix/ntnx-api-golang-clients/storage-go-client/v4/client"
)
var (
ApiClientInstance *client.ApiClient
)
ApiClientInstance = client.NewApiClient()
ApiClientInstance.Host = "10.19.50.27" // IPv4/IPv6 address or FQDN of the cluster
ApiClientInstance.Port = 9440 // Port to which to connect to
ApiClientInstance.Username = "admin" // UserName to connect to the cluster
ApiClientInstance.Password = "password" // Password to connect to the cluster
Proxy Configuration
import (
"github.com/nutanix/ntnx-api-golang-clients/storage-go-client/v4/client"
)
var (
ApiClientInstance *client.ApiClient
)
ApiClientInstance = client.NewApiClient()
// Configure the client as shown in the previous step
// ...
ApiClientInstance.Proxy = new(client.Proxy)
ApiClientInstance.Proxy.Scheme = "socks5"
ApiClientInstance.Proxy.Username = "proxy_admin"
ApiClientInstance.Proxy.Password = "proxy_password"
ApiClientInstance.Proxy.Host = "127.0.0.1"
ApiClientInstance.Proxy.Port = 1080
Authentication
Nutanix APIs currently support HTTP Basic Authentication only, and the Go client can be configured using the username and password parameters to send Basic headers along with every request.
Retry Mechanism
The Go client can be configured to retry requests that fail with the following status codes. The numbers of seconds before which the next retry is attempted is determined by the RetryInterval.
import (
"github.com/nutanix/ntnx-api-golang-clients/storage-go-client/v4/client"
)
var (
ApiClientInstance *client.ApiClient
)
ApiClientInstance = client.NewApiClient()
ApiClientInstance.MaxRetryAttempts = 5 // Max retry attempts while reconnecting on a loss of connection
ApiClientInstance.RetryInterval = 5000 // Interval in ms to use during retry attempts
Usage
Invoking an operation
import (
"github.com/nutanix/ntnx-api-golang-clients/storage-go-client/v4/client"
"github.com/nutanix/ntnx-api-golang-clients/storage-go-client/v4/api"
)
var (
ApiClientInstance *client.ApiClient
StorageContainerApiInstance *api.StorageContainerApi
)
ApiClientInstance = client.NewApiClient()
// Configure the client as shown in the previous step
// ...
// Initialize the API
StorageContainerApiInstance = api.NewStorageContainerApi(ApiClientInstance)
containerExtId := "fEDBAfd3-acBc-efE9-Ad67-580f6fFCa6DD"
//
getResponse, err := StorageContainerApiInstance.GetStorageContainerByExtId(&containerExtId)
if err != nil {
....
}
Request Options
The library provides the ability to specify additional options that can be applied directly on the 'ApiClient' object used to make network calls to the API. The library also provides a mechanism to specify operation specific headers.
The 'ApiClient' can be configured to send additional headers on each request.
import (
"github.com/nutanix/ntnx-api-golang-clients/storage-go-client/v4/client"
)
var (
ApiClientInstance *client.ApiClient
)
ApiClientInstance = client.NewApiClient()
ApiClientInstance.AddDefaultHeader("Accept-Encoding", "gzip, deflate, br")
You can also modify the headers sent with each individual operation:
Nutanix APIs require that concurrent updates are protected using ETag headers. This would mean that the ETag header received in the response of a fetch (GET) operation should be used as an If-Match header for the modification (PUT) operation.
import (
"github.com/nutanix/ntnx-api-golang-clients/storage-go-client/v4/client"
"github.com/nutanix/ntnx-api-golang-clients/storage-go-client/v4/api"
// import request body DTO for put api
)
var (
ApiClientInstance *client.ApiClient
StorageContainerApiInstance *api.StorageContainerApi
)
ApiClientInstance = client.NewApiClient()
// Configure the client as shown in a previous step
// ...
// Initialize the API
StorageContainerApiInstance = api.NewStorageContainerApi(ApiClientInstance)
containerExtId := "fEDBAfd3-acBc-efE9-Ad67-580f6fFCa6DD"
//
getResponse, err := StorageContainerApiInstance.GetStorageContainerByExtId(&containerExtId)
if err != nil {
....
}
// Extract E-Tag Header
etagValue := ApiClientInstance.GetEtag(getResponse)
args := make(map[string] interface {})
args["If-Match"] = etagValue
// ...
// Perform update call with received E-Tag reference
// initialize/change parameters for update
// ...
storageContainer := getResponse.GetData().(import1.StorageContainer)
// The body parameter in the following operation is received from the previous GET request's response which needs to be updated.
response, err := StorageContainerApiInstance.UpdateStorageContainer(&storageContainer, &containerExtId, args)
if err != nil {
....
}
List Operations
List Operations for Nutanix APIs support pagination, filtering, sorting and projections. The table below details the parameters that can be used to set the options for pagination etc.
Parameter |
Description |
_page |
specifies the page number of the result set. Must be a positive integer between 0 and the maximum number of pages that are available for that resource. Any number out of this range will lead to no results being returned. |
_limit |
specifies the total number of records returned in the result set. Must be a positive integer between 0 and 100. Any number out of this range will lead to a validation error. If the limit is not provided a default value of 50 records will be returned in the result set |
_filter |
allows clients to filter a collection of resources. The expression specified with $filter is evaluated for each resource in the collection, and only items where the expression evaluates to true are included in the response. Expression specified with the $filter must conform to the OData V4.01 URL conventions. |
_orderby |
allows clients to specify the sort criteria for the returned list of objects. Resources can be sorted in ascending order using asc or descending order using desc. If asc or desc are not specified the resources will be sorted in ascending order by default. For example, 'orderby=templateName desc' would get all templates sorted by templateName in desc order. |
_select |
allows clients to request a specific set of properties for each entity or complex type. Expression specified with the $select must conform to the OData V4.01 URL conventions. If a $select expression consists of a single select item that is an asterisk (i.e. *), then all properties on the matching resource will be returned. |
_expand |
allows clients to request related resources when a resource that satisfies a particular request is retrieved. Each expand item is evaluated relative to the entity containing the property being expanded. Other query options can be applied to an expanded property by appending a semicolon-separated list of query options, enclosed in parentheses, to the property name. Allowed system query options are $filter,$select, $orderby. |
import (
"github.com/nutanix/ntnx-api-golang-clients/storage-go-client/v4/client"
"github.com/nutanix/ntnx-api-golang-clients/storage-go-client/v4/api"
)
var (
ApiClientInstance *client.ApiClient
StorageContainerApiInstance *api.StorageContainerApi
)
ApiClientInstance = client.NewApiClient()
// Configure the client
// ...
// Initialize the API
StorageContainerApiInstance = api.NewStorageContainerApi(ApiClientInstance)
page := 0
limit := 50
filter := "string_sample_data"
orderby := "string_sample_data"
select := "string_sample_data"
//
response, err := StorageContainerApiInstance.GetAllStorageContainers(&page, &limit, &filter, &orderby, &select)
if err != nil {
....
}
The list of filterable and sortable fields with expansion keys can be found in the documentation here.
API Reference
This library has a full set of API Reference Documentation. This documentation is auto-generated, and the location may change.
License
This library is licensed under Nutanix proprietary license. Full license text is available in LICENSE.
In case of issues please reach out to us at the mailing list