go-anxcloud

module
v0.7.6 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2024 License: Apache-2.0

README

Documentation codecov

Go Client for the Anexia API

Go SDK for interacting with the Anexia Engine API.

Installing

To use the SDK, just add go.anx.io/go-anxcloud <version> to your Go module.

Getting started

Before using the SDK you should familiarize yourself with the Anexia Engine API.

The library is used in our terraform provider, check it out if you want some examples how to use it.

Example

Below is a short example using the new generic client in this package. Not all APIs can already be used with it, but we are working on that. Find more examples in the docs (linked to docs for main branch, not the latest (or any) release).

package main

import (
	"context"
	"log"

	"go.anx.io/go-anxcloud/pkg/api"
	apiTypes "go.anx.io/go-anxcloud/pkg/api/types"
	"go.anx.io/go-anxcloud/pkg/client"

	// apis usable with the generic client have their own package in a location analog to this
	lbaasv1 "go.anx.io/go-anxcloud/pkg/apis/lbaas/v1"
)

func main() {
	apiClient, err := api.NewAPI(
		api.WithClientOptions(
			// Get auth token from ANEXIA_TOKEN environment variable.
			// The boolean parameter specifies if the environment variable should be unset.
			client.TokenFromEnv(false),
		),
	)
	if err != nil {
		log.Fatalf("Error creating ANX API client: %v", err)
	}

	// let's list LBaaS backends of a known LoadBalancer
	frontend := lbaasv1.Frontend{
		LoadBalancer: &lbaasv1.LoadBalancer{Identifier: "285b954fdf2a449c8fdae01cc6074025"},
	}

	var frontends apiTypes.ObjectChannel
	err = apiClient.List(context.TODO(), &frontend,
		// Listing can be done with either a page iterator or a channel, we use a channel here.
		api.ObjectChannel(&frontends),

		// Most APIs only give a very small subset when listing resources, add this flag to
		// get all attributes, at the cost of doing lots of API requests.
		api.FullObjects(true),
	)
	if err != nil {
		log.Fatalf("Error listing backends for LoadBalancer '%v': %v", frontend.LoadBalancer.Identifier, err)
	}

	for retriever := range frontends {
		// reinitialise frontend every loop to reset pointers and avoid potential overwriting of data in the next loop
		var frontend lbaasv1.Frontend
		if err := retriever(&frontend); err != nil {
			log.Fatalf("Error retrieving Frontend: %v", err)
		}

		log.Printf("Got Frontend named '%v' with mode '%v'", frontend.Name, frontend.Mode)
	}
}

This new generic client will one day be the only client in go-anxcloud. The legacy API-specific clients are deprecated and will be removed in the go-anxcloud release following the one with all APIs go-anxcloud supports usable with the generic client (so if the generic client in 0.5.0 supports at least everything there is another client for in go-anxcloud, 0.6.0 will drop the API-specific clients).

Example how to create a VM with the API-specific, deprecated client.
package main

import (
	"context"
	"fmt"
	"time"

	anexia "go.anx.io/go-anxcloud/pkg"
	"go.anx.io/go-anxcloud/pkg/client"
	"go.anx.io/go-anxcloud/pkg/vsphere/provisioning/vm"
)

func main() {
	vlan := "<ID of the VLAN the VM should have access to>"
	location := "<ID of the location the VM should be in>"

	// Create client using the auth token in environment variable ANEXIA_TOKEN and do not unset the environment variable.
	c, err := client.New(client.AuthFromEnv(false))
	if err != nil {
		panic(fmt.Sprintf("could not create client: %v", err))
	}

	// Get some API.
	provisioning := anexia.NewAPI(c).VSphere().Provisioning()

	// Time out after 30 minutes. Yes it really takes that long sometimes.
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
	// Look for a free ip in the given VLAN. This IP is not reserved for you so better be quick.
	ips, err := provisioning.IPs().GetFree(ctx, location, vlan)
	defer cancel()
	if err != nil {
		panic(fmt.Sprintf("provisioning vm failed: %v", err))
	}
	if len(ips) < 1 {
		panic(fmt.Sprintf("no IPs left for testing in vlan"))
	}

	// Create a NIC for the VM and connect it to the VLAN.
	networkInterfaces := []vm.Network{{NICType: "vmxnet3", IPs: []string{ips[0].Identifier}, VLAN: vlan}}
	// Create the definition of the new VM. The ID you see here is Flatcar.
	definition := vm.NewAPI(c).NewDefinition(location, "template", "44b38284-6adb-430e-b4a4-1553e29f352f", "developersfirstvm", 2, 2048, 10, networkInterfaces)
	definition.SSH = "<your SSH pub key>"

	// Provision the VM.
	provisionResponse, err := provisioning.VM().Provision(ctx, definition)
	if err != nil {
		panic(fmt.Sprintf("provisioning vm failed: %v", err))
	}

	// Wait for the VM to be ready.
	_, err = provisioning.Progress().AwaitCompletion(ctx, provisionResponse.Identifier)
	if err != nil {
		panic(fmt.Sprintf("waiting for VM provisioning failed: %v", err))
	}
}

Directories

Path Synopsis
pkg
Package pkg contains all API functionality and helpers.
Package pkg contains all API functionality and helpers.
api
Package api implements a generic API client for our engine, reducing the amount of duplicated code in this library.
Package api implements a generic API client for our engine, reducing the amount of duplicated code in this library.
api/types
Package types contains everything needed by APIs implementing the interfaces to be compatible with the generic API client.
Package types contains everything needed by APIs implementing the interfaces to be compatible with the generic API client.
apis/core/v1/helper
Package helper exists to break a circular-dependency between pkg/api and pkg/apis/core/v1, that was introduced when adding the AutoTag option to the generic client.
Package helper exists to break a circular-dependency between pkg/api and pkg/apis/core/v1, that was introduced when adding the AutoTag option to the generic client.
apis/lbaas/v1/test
Package test includes testing utilities and is not intended for production code usage.
Package test includes testing utilities and is not intended for production code usage.
client
Package client provides a client for interacting with the anxcloud API.
Package client provides a client for interacting with the anxcloud API.
clouddns
Package clouddns contains API functionality for clouddns.
Package clouddns contains API functionality for clouddns.
clouddns/zone
Package zone implements API functions residing under /zone.
Package zone implements API functions residing under /zone.
core
Package core contains API functionality for /core.
Package core contains API functionality for /core.
core/location
Package location implements API functions residing under /core/location.json.
Package location implements API functions residing under /core/location.json.
core/resource
Package resource implements API functions residing under /core/resource.
Package resource implements API functions residing under /core/resource.
core/service
Package service implements API functions residing under /cofe/service.
Package service implements API functions residing under /cofe/service.
core/tags
Package tags implements API functions residing under /core/tags.
Package tags implements API functions residing under /core/tags.
ipam
Package ipam implements API functions residing under /ipam.
Package ipam implements API functions residing under /ipam.
ipam/address
Package address implements API functions residing under /ipam/address.
Package address implements API functions residing under /ipam/address.
ipam/prefix
Package prefix implements API functions residing under /ipam/prefix.
Package prefix implements API functions residing under /ipam/prefix.
test
Package test contains API functionality for "testing" the API.
Package test contains API functionality for "testing" the API.
test/echo
Package echo contains API functionality for issuing echo requests to the API.
Package echo contains API functionality for issuing echo requests to the API.
utils/object/compare
Package compare gives some utilities to compare Objects with little code required and to reconcile a list of desired Objects with a list of existing ones to lists of Objects to create and destroy.
Package compare gives some utilities to compare Objects with little code required and to reconcile a list of desired Objects with a list of existing ones to lists of Objects to create and destroy.
utils/object/filter
Package filter implements a helper for Objects supporting filtered List operations.
Package filter implements a helper for Objects supporting filtered List operations.
utils/pointer
Package pointer provides helper functions for creating pointers of values and retrieving values of pointers
Package pointer provides helper functions for creating pointers of values and retrieving values of pointers
vlan
Package vlan implements API functions residing under /vlan.
Package vlan implements API functions residing under /vlan.
vsphere
Package vsphere contains API functionality for vsphere.
Package vsphere contains API functionality for vsphere.
vsphere/info
Package info implements API functions residing under /info.
Package info implements API functions residing under /info.
vsphere/powercontrol
Package powercontrol implements API functions residing under /powercontrol.
Package powercontrol implements API functions residing under /powercontrol.
vsphere/provisioning
Package provisioning contains APi funcationality for the provisioning of VMs.
Package provisioning contains APi funcationality for the provisioning of VMs.
vsphere/provisioning/cpuperformancetypes
Package templates implements API functions residing under /provisioning/cpuperformancetype.
Package templates implements API functions residing under /provisioning/cpuperformancetype.
vsphere/provisioning/disktype
Package disktype implements API functions residing under /provisioning/disk_type.
Package disktype implements API functions residing under /provisioning/disk_type.
vsphere/provisioning/ips
Package ips implements API functions residing under /provisioning/ips.
Package ips implements API functions residing under /provisioning/ips.
vsphere/provisioning/location
Package location implements API functions residing under /provisioning/location.
Package location implements API functions residing under /provisioning/location.
vsphere/provisioning/nictype
Package nictype implements API functions residing under /provisioning/nictype.
Package nictype implements API functions residing under /provisioning/nictype.
vsphere/provisioning/progress
Package progress implements API functions residing under /provisioning/progress.
Package progress implements API functions residing under /provisioning/progress.
vsphere/provisioning/templates
Package templates implements API functions residing under /provisioning/templates.
Package templates implements API functions residing under /provisioning/templates.
vsphere/provisioning/vm
Package vm implements API functions residing under /provisioning/vm.
Package vm implements API functions residing under /provisioning/vm.
vsphere/search
Package search implements API functions residing under /search.
Package search implements API functions residing under /search.

Jump to

Keyboard shortcuts

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