Skip to content

Usage examples

Pierre-Luc Dion edited this page Aug 27, 2021 · 1 revision

Usage example of cloudstack-go,

List hosts

package main

import (
	"encoding/json"
	"fmt"
	"log"

	"github.com/apache/cloudstack-go/cloudstack"
)

func main() {
	acs_url := "https://<IP>/client/api"
	api_key := ""
	api_secret := ""
	validate_ssl := false

	// Create a new API client
	cs := cloudstack.NewAsyncClient(acs_url, api_key, api_secret, validate_ssl)
	p := cs.Host.NewListHostsParams()

	hosts, err := cs.Host.ListHosts(p)
	if err != nil {
		log.Fatalf("Error listing volumes: %s", err)
	}

	b, err := json.MarshalIndent(hosts.Hosts, "", "  ")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf(string(b))
}

list volumes for a project

package main

import (
	"encoding/json"
	"fmt"
	"log"

	"github.com/apache/cloudstack-go/cloudstack"
)

func main() {
	acs_url := "https://<IP>/client/api"
	api_key := ""
	api_secret := ""

	// Create a new API client
	cs := cloudstack.NewAsyncClient(acs_url, api_key, api_secret, false)
	p := cs.Volume.NewListVolumesParams()
	p.SetListall(true)
	p.SetProjectid("<ProjectID>")

	vols, err := cs.Volume.ListVolumes(p)
	if err != nil {
		log.Fatalf("Error listing volumes: %s", err)
	}

	b, err := json.MarshalIndent(vols.Volumes, "", "  ")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf(string(b))
}
Clone this wiki locally