Cloudstack API library written in Go. Tested towards Cloudstack 3.x and 4.x. Main use so far has been to serve as a library for a Packer.io builder.
Showing the IP and state of a virtual machine:
package main
import (
"fmt""github.com/mindjiver/gopherstack""os"
)
funcmain() {
apiurl:=os.Getenv("CLOUDSTACK_API_URL")
iflen(apiurl) ==0 {
fmt.Println("Needed environment variable CLOUDSTACK_API_URL not found, exiting")
os.Exit(1)
}
apikey:=os.Getenv("CLOUDSTACK_API_KEY")
iflen(apikey) ==0 {
fmt.Println("Needed environment variable CLOUDSTACK_API_KEY not found, exiting")
os.Exit(1)
}
secretkey:=os.Getenv("CLOUDSTACK_SECRET_KEY")
iflen(secret) ==0 {
fmt.Println("Needed environment variable CLOUDSTACK_SECRET_KEY not found, exiting")
os.Exit(1)
}
cs:= gopherstack.CloudstackClient{}.New(apiurl, apikey, secretkey)
vmid:="19d2acfb-e281-4a13-8d62-e04ab501271d"response, err:=cs.ListVirtualMachines(vmid)
iferr!=nil {
fmt.Errorf("Error listing virtual machine: %s", err)
os.Exit(1)
}
iflen(response.Listvirtualmachinesresponse.Virtualmachine) >0 {
ip:=response.Listvirtualmachinesresponse.Virtualmachine[0].Nic[0].Ipaddressstate:=response.Listvirtualmachinesresponse.Virtualmachine[0].Statefmt.Printf("%s has IP : %s and state : %s\n", vmid, ip, state)
} else {
fmt.Printf("No VM with UUID: %s found\n", vmid)
}
}
