Samuel A. Falvo II | 17ae565 | 2014-02-12 20:47:43 -0800 | [diff] [blame^] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "github.com/rackspace/gophercloud/openstack/compute/images" |
| 7 | "github.com/rackspace/gophercloud/openstack/identity" |
| 8 | "github.com/rackspace/gophercloud/openstack/utils" |
| 9 | "text/tabwriter" |
| 10 | ) |
| 11 | |
| 12 | func 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 { |
| 40 | client := images.NewClient(ep.PublicURL, a, ao) |
| 41 | |
| 42 | if (region != "") && (region != ep.Region) { |
| 43 | continue |
| 44 | } |
| 45 | |
| 46 | 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 | |
| 66 | |
| 67 | func findAllComputeEndpoints(sc *identity.ServiceCatalog) ([]identity.Endpoint, error) { |
| 68 | ces, err := sc.CatalogEntries() |
| 69 | if err != nil { |
| 70 | return nil, err |
| 71 | } |
| 72 | |
| 73 | for _, ce := range ces { |
| 74 | if ce.Type == "compute" { |
| 75 | return ce.Endpoints, nil |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return nil, fmt.Errorf("Compute endpoint not found.") |
| 80 | } |
| 81 | |