Skip to content

Instantly share code, notes, and snippets.

@nelsnelson
Created October 22, 2014 14:42
Show Gist options
  • Save nelsnelson/4473cce4c4b6b7878682 to your computer and use it in GitHub Desktop.
Save nelsnelson/4473cce4c4b6b7878682 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"os"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
"github.com/rackspace/gophercloud/openstack"
"github.com/rackspace/gophercloud/rackspace"
"github.com/rackspace/gophercloud/openstack/compute/v2/servers"
)
func main() {
authUrl := os.Getenv("OS_AUTH_URL")
username := os.Getenv("OS_USERNAME")
apiKey := os.Getenv("OS_API_KEY")
tenantID := os.Getenv("OS_TENANT_ID")
fmt.Println("OS_AUTH_URL:", authUrl)
fmt.Println("OS_USERNAME:", username)
fmt.Println("OS_PASSWORD:", password)
fmt.Println("OS_TENANT_ID:", tenantID)
auth_options := gophercloud.AuthOptions{
IdentityEndpoint: authUrl,
Username: username,
APIKey: apiKey,
TenantID: tenantID,
AllowReauth: true,
}
//auth := gophercloud.AuthOptions{
// Username: os.Getenv("RAX_USERNAME"),
// APIKey: os.Getenv("RAX_APIKEY"),
//}
// Attempt to authenticate with them.
fmt.Println("Attempting to authenticate to", auth_options.IdentityEndpoint)
provider, err := openstack.AuthenticatedClient(auth_options)
if err != nil {
fmt.Println("Error authenticating:", err)
os.Exit(1)
}
compute, err := rackspace.NewComputeV2(provider, gophercloud.EndpointOpts{
Region: "IAD",
})
if err != nil {
fmt.Println("Unable to find compute service:", err)
os.Exit(1)
}
err = servers.List(compute, nil).EachPage(func(page pagination.Page) (bool, error) {
contents, err := servers.ExtractServers(page)
if err != nil {
return false, err
}
for _, server := range contents {
fmt.Println("- Server id [%s] is named [%s]", server.ID, server.Name)
}
return true, nil
})
if err != nil {
fmt.Println("Unable to list servers:", err)
os.Exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment