cloud-sdk-go

module
v1.17.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: Apache-2.0

README

Elastic Cloud Go SDK

Go Reference

Go SDK for Elastic Cloud. Its goal is to provide common ground for all Elastic Cloud programmatic code in Go.

Installation

Run the following go get command to install the SDK in your module dependencies directory:

go get -u github.com/elastic/cloud-sdk-go

Usage

See the pkg/api package for more in depth documentation.

package main

import (
	"encoding/json"
	"log"
	"net/http"
	"os"

	"github.com/elastic/cloud-sdk-go/pkg/api"
	"github.com/elastic/cloud-sdk-go/pkg/api/deploymentapi"
	"github.com/elastic/cloud-sdk-go/pkg/auth"
)

var (
	logFormat = log.Lmsgprefix | log.Llongfile

	errLog  = log.New(os.Stderr, "ERROR ", logFormat)
	warnLog = log.New(os.Stdout, "WARN ", logFormat)
	infoLog = log.New(os.Stdout, "INFO ", logFormat)
)

func main() {
	// Export your apikey as an environment variable as EC_API_KEY. To generate
	// a new API key go to ESS or ECE Web UI > API Keys > Generate API Key.
	apiKey := os.Getenv("EC_API_KEY")
	if apiKey == "" {
		warnLog.Print("unable to obtain value from EC_API_KEY environment variable")
	}

	// Create a API instance with an API key as means of authentication.
	ess, err := api.NewAPI(api.Config{
		Client:     new(http.Client),
		AuthWriter: auth.APIKey(apiKey),
	})
	if err != nil {
		errLog.Fatal(err)
	}

	// List the user's deployments via the `deploymentapi` package (Recommended).
	res, err := deploymentapi.List(deploymentapi.ListParams{API: ess})
	if err != nil {
		errLog.Fatal(err)
	}
	infoLog.Printf("found %d deployents", len(res.Deployments))

	encoder := json.NewEncoder(infoLog.Writer())
	encoder.SetIndent("", "  ")

	if err := encoder.Encode(res); err != nil {
		errLog.Fatal(err)
	}
}

High level package overview

The project's structure is based off the standard Go project layout. Therefore, all of our library code that we expect other projects to import is placed in the pkg/ directory.

The main packages for interacting directly with our public API endpoints can be found within the pkg/api directory. The source code for these APIs are the client and models, which are generated off the public API swagger specification.

The rest are a series of packages that can be leveraged in many ways. For a detailed package description visit the SDK's packages documentation at pkg.go.dev. Alternatively you can use the godoc command on the root level of this project.

Getting started

Ecctl (the Elastic Cloud CLI tool) depends heavily on cloud-sdk-go. You can use ecctl command packages as a reference on how to leverage the SDK. One good example is the command used to list deployments.

Alternatively, if you wish to write your own APIs, take a look at our sample code, which retrieves information about the active platform, to get an idea on how the client is used.

We always welcome contributions! Take a look at our contributing guide if this is something that interests you.

Directories

Path Synopsis
cmd
generator
contains the logic for the generator command the aim of this is to process the swagger definition that's obtained from Elastic Cloud and make a few changes in order for the SDK to be fully usable by Golang
contains the logic for the generator command the aim of this is to process the swagger definition that's obtained from Elastic Cloud and make a few changes in order for the SDK to be fully usable by Golang
examples
internal
pkg
api
Package api provides an way to interact with Elastic Cloud APIs, including ESS, ECE and ESSP.
Package api provides an way to interact with Elastic Cloud APIs, including ESS, ECE and ESSP.
api/deploymentapi
Package deploymentapi contains curated functions which iteract with the deployments API, exposing an API which its usage is preferred over the direct client calls.
Package deploymentapi contains curated functions which iteract with the deployments API, exposing an API which its usage is preferred over the direct client calls.
api/deploymentapi/depresourceapi
Package depresourceapi contains curated functions which iteract with the deployment resources API, exposing an API which its usage is preferred over the direct client calls.
Package depresourceapi contains curated functions which iteract with the deployment resources API, exposing an API which its usage is preferred over the direct client calls.
api/mock
Package mock provides functions and types to help test and stub external calls that the API structures would otherwise perform causing external calls through the network.
Package mock provides functions and types to help test and stub external calls that the API structures would otherwise perform causing external calls through the network.
api/platformapi/snaprepoapi
Package snaprepoapi contains the a set of functions to interact with the platform repositories
Package snaprepoapi contains the a set of functions to interact with the platform repositories
api/userapi
Package userapi contains the methods required to manage platform users.
Package userapi contains the methods required to manage platform users.
api/userapi/authapi
Package userauthapi contains the self service logic required to manage a user's authentication settings, such as API keys.
Package userauthapi contains the self service logic required to manage a user's authentication settings, such as API keys.
api/userapi/authapi/adminapi
Package userauthadminapi contains the logic required to manage a user's authentication settings, such as API keys.
Package userauthadminapi contains the logic required to manage a user's authentication settings, such as API keys.
auth
Package auth contains all the possible ways to authenticate against the Elastic Cloud API.
Package auth contains all the possible ways to authenticate against the Elastic Cloud API.
input
Package input provides a series of functions and structures to read and process i/o from readers.
Package input provides a series of functions and structures to read and process i/o from readers.
multierror
Package multierror can be leveraged as an opinionated to handle multiple errors providing appropriate wrapping for them.
Package multierror can be leveraged as an opinionated to handle multiple errors providing appropriate wrapping for them.
output
Package output provides a series of functions and structures to use to print text to outut devices.
Package output provides a series of functions and structures to use to print text to outut devices.
plan
Package plan provides an API to interact with a deployment's pending plan.
Package plan provides an API to interact with a deployment's pending plan.
plan/planutil
Package planutil takes the functionality from the plan package and presents a more high-level API.
Package planutil takes the functionality from the plan package and presents a more high-level API.
sync
Package sync contains a series of functions, types and subpackages which can be used for concurrent purposes.
Package sync contains a series of functions, types and subpackages which can be used for concurrent purposes.
sync/pool
Package pool provies an API to queue and concurrently execute a series of tasks or work.
Package pool provies an API to queue and concurrently execute a series of tasks or work.
util
Package util encloses a series of packages within its structure, util used in the sense that these packages aren't specific or directly tied to the cloud sdk.
Package util encloses a series of packages within its structure, util used in the sense that these packages aren't specific or directly tied to the cloud sdk.
util/maps
Package maps provides a set of functions around maps.
Package maps provides a set of functions around maps.
util/slice
Package slice provides a set of functions around slices.
Package slice provides a set of functions around slices.

Jump to

Keyboard shortcuts

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