blob: 894543497710f9ceba5cdebbd012ba4866c62f60 [file] [log] [blame]
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -08001package main
2
3import (
4 "fmt"
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -08005 "github.com/rackspace/gophercloud/openstack/compute/images"
6 "github.com/rackspace/gophercloud/openstack/identity"
7 "github.com/rackspace/gophercloud/openstack/utils"
Samuel A. Falvo II6dbf9f62014-02-13 13:51:58 -08008 "os"
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -08009 "text/tabwriter"
10)
11
12func main() {
13 ao, err := utils.AuthOptions()
14 if err != nil {
15 panic(err)
16 }
17
18 a, err := identity.Authenticate(ao)
19 if err != nil {
20 panic(err)
21 }
22
23 sc, err := identity.GetServiceCatalog(a)
24 if err != nil {
25 panic(err)
26 }
27
28 eps, err := findAllComputeEndpoints(sc)
29 if err != nil {
30 panic(err)
31 }
32
33 w := new(tabwriter.Writer)
34 w.Init(os.Stdout, 2, 8, 2, ' ', 0)
35 fmt.Fprintln(w, "ID\tRegion\tName\tStatus\tCreated\t")
36
37 region := os.Getenv("OS_REGION_NAME")
38 n := 0
39 for _, ep := range eps {
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080040 if (region != "") && (region != ep.Region) {
41 continue
42 }
43
Samuel A. Falvo II6dbf9f62014-02-13 13:51:58 -080044 client := images.NewClient(ep.PublicURL, a, ao)
45
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080046 listResults, err := images.List(client)
47 if err != nil {
48 panic(err)
49 }
50
51 imgs, err := images.GetImages(listResults)
52 if err != nil {
53 panic(err)
54 }
55
56 n = n + len(imgs)
57
58 for _, i := range imgs {
59 fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t\n", i.Id, ep.Region, i.Name, i.Status, i.Created)
60 }
61 }
62 w.Flush()
63 fmt.Printf("--------\n%d images listed.\n", n)
64}
65
Samuel A. Falvo II17ae5652014-02-12 20:47:43 -080066func findAllComputeEndpoints(sc *identity.ServiceCatalog) ([]identity.Endpoint, error) {
67 ces, err := sc.CatalogEntries()
68 if err != nil {
69 return nil, err
70 }
71
72 for _, ce := range ces {
73 if ce.Type == "compute" {
74 return ce.Endpoints, nil
75 }
76 }
77
78 return nil, fmt.Errorf("Compute endpoint not found.")
79}