Go Client for the Anexia API
Go SDK for interacting with the Anexia multi purpose API.
Installing
To use the SDK, just add github.com/anexia-it/go-anxcloud <version>
to your Go module.
Getting started
Before using the SDK you should familiarize yourself with the API. See here for more info. I you crave for an example, take a look at the terraform provider for this project
Example
The following code shows how to create a VM. To be able to do that you need to set the environment variable ANEXIA_TOKEN
to your access token.
Afterwards you can run the following.
1package main
2
3import (
4 "context"
5 "fmt"
6 "time"
7
8 anexia "github.com/anexia-it/go-anxcloud/pkg"
9 "github.com/anexia-it/go-anxcloud/pkg/client"
10 "github.com/anexia-it/go-anxcloud/pkg/vsphere/provisioning/vm"
11)
12
13func main() {
14 vlan := "<ID of the VLAN the VM should have access to>"
15 location := "<ID of the location the VM should be in>"
16
17 // Create client from environment variables, do not unset env afterwards.
18 c, err := client.New(client.AuthFromEnv(false))
19 if err != nil {
20 panic(fmt.Sprintf("could not create client: %v", err))
21 }
22
23 // Get some API.
24 provisioning := anexia.NewAPI(c).VSphere().Provisioning()
25
26 // Time out after 30 minutes. Yes it really takes that long sometimes.
27 ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
28 // Look for a free ip in the given VLAN. This IP is not reserved for you so better be quick.
29 ips, err := provisioning.IPs().GetFree(ctx, location, vlan)
30 defer cancel()
31 if err != nil {
32 panic(fmt.Sprintf("provisioning vm failed: %v", err))
33 }
34 if len(ips) < 1 {
35 panic(fmt.Sprintf("no IPs left for testing in vlan"))
36 }
37
38 // Create a NIC for the VM and connect it to the VLAN.
39 networkInterfaces := []vm.Network{{NICType: "vmxnet3", IPs: []string{ips[0].Identifier}, VLAN: vlan}}
40 // Create the definition of the new VM. The ID you see here is Flatcar.
41 definition := vm.NewAPI(c).NewDefinition(location, "template", "44b38284-6adb-430e-b4a4-1553e29f352f", "developersfirstvm", 2, 2048, 10, networkInterfaces)
42 definition.SSH = "<your SSH pub key>"
43
44 // Provision the VM.
45 provisionResponse, err := provisioning.VM().Provision(ctx, definition)
46 if err != nil {
47 panic(fmt.Sprintf("provisioning vm failed: %v", err))
48 }
49
50 // Wait for the VM to be ready.
51 _, err = provisioning.Progress().AwaitCompletion(ctx, provisionResponse.Identifier)
52 if err != nil {
53 panic(fmt.Sprintf("waiting for VM provisioning failed: %v", err))
54 }
55}