Code Monkey home page Code Monkey logo

go-anxcloud's Introduction

Documentation

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.

package main

import (
	"context"
	"fmt"
	"time"

	anexia "github.com/anexia-it/go-anxcloud/pkg"
	"github.com/anexia-it/go-anxcloud/pkg/client"
	"github.com/anexia-it/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 from environment variables, do not unset env afterwards.
	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))
	}
}

go-anxcloud's People

Contributors

erdii avatar havefun83 avatar kstiehl avatar littlefox94 avatar mfranczy avatar paepke avatar stroebitzer avatar toothstone avatar x4mp avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.