Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 1 | package openstack |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "fmt" |
| 6 | "github.com/rackspace/gophercloud/openstack/compute/servers" |
| 7 | "github.com/rackspace/gophercloud/openstack/compute/images" |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame^] | 8 | "github.com/rackspace/gophercloud/openstack/compute/flavors" |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 9 | "github.com/rackspace/gophercloud/openstack/identity" |
| 10 | "github.com/rackspace/gophercloud/openstack/utils" |
| 11 | "os" |
| 12 | "text/tabwriter" |
| 13 | ) |
| 14 | |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 15 | type testState struct { |
| 16 | o identity.AuthOptions |
| 17 | a identity.AuthResults |
| 18 | sc *identity.ServiceCatalog |
| 19 | eps []identity.Endpoint |
| 20 | w *tabwriter.Writer |
| 21 | } |
| 22 | |
| 23 | func prepForTest() (*testState, error) { |
| 24 | var err error |
| 25 | |
| 26 | ts := new(testState) |
| 27 | |
| 28 | ts.o, err = utils.AuthOptions() |
| 29 | if err != nil { |
| 30 | return ts, err |
| 31 | } |
| 32 | |
| 33 | ts.a, err = identity.Authenticate(ts.o) |
| 34 | if err != nil { |
| 35 | return ts, err |
| 36 | } |
| 37 | |
| 38 | ts.sc, err = identity.GetServiceCatalog(ts.a) |
| 39 | if err != nil { |
| 40 | return ts, err |
| 41 | } |
| 42 | |
| 43 | ts.eps, err = findAllComputeEndpoints(ts.sc) |
| 44 | if err != nil { |
| 45 | return ts, err |
| 46 | } |
| 47 | |
| 48 | ts.w = new(tabwriter.Writer) |
| 49 | ts.w.Init(os.Stdout, 2, 8, 2, ' ', 0) |
| 50 | |
| 51 | return ts, nil |
| 52 | } |
| 53 | |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 54 | func TestListServers(t *testing.T) { |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 55 | ts, err := prepForTest() |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 56 | if err != nil { |
| 57 | t.Error(err) |
| 58 | return |
| 59 | } |
| 60 | |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 61 | fmt.Fprintln(ts.w, "ID\tRegion\tName\tIPv4\tIPv6\t") |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 62 | |
| 63 | region := os.Getenv("OS_REGION_NAME") |
| 64 | n := 0 |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 65 | for _, ep := range ts.eps { |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 66 | if (region != "") && (region != ep.Region) { |
| 67 | continue |
| 68 | } |
| 69 | |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 70 | client := servers.NewClient(ep.PublicURL, ts.a, ts.o) |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 71 | |
| 72 | listResults, err := servers.List(client) |
| 73 | if err != nil { |
| 74 | t.Error(err) |
| 75 | return |
| 76 | } |
| 77 | |
| 78 | svrs, err := servers.GetServers(listResults) |
| 79 | if err != nil { |
| 80 | t.Error(err) |
| 81 | return |
| 82 | } |
| 83 | |
| 84 | n = n + len(svrs) |
| 85 | |
| 86 | for _, s := range svrs { |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 87 | fmt.Fprintf(ts.w, "%s\t%s\t%s\t%s\t%s\t\n", s.Id, s.Name, ep.Region, s.AccessIPv4, s.AccessIPv6) |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 88 | } |
| 89 | } |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 90 | ts.w.Flush() |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 91 | fmt.Printf("--------\n%d servers listed.\n", n) |
| 92 | } |
| 93 | |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 94 | func TestListImages(t *testing.T) { |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 95 | ts, err := prepForTest() |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 96 | if err != nil { |
| 97 | t.Error(err) |
| 98 | return |
| 99 | } |
| 100 | |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 101 | fmt.Fprintln(ts.w, "ID\tRegion\tName\tStatus\tCreated\t") |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 102 | |
| 103 | region := os.Getenv("OS_REGION_NAME") |
| 104 | n := 0 |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 105 | for _, ep := range ts.eps { |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 106 | if (region != "") && (region != ep.Region) { |
| 107 | continue |
| 108 | } |
| 109 | |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 110 | client := images.NewClient(ep.PublicURL, ts.a, ts.o) |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 111 | |
| 112 | listResults, err := images.List(client) |
| 113 | if err != nil { |
| 114 | t.Error(err) |
| 115 | return |
| 116 | } |
| 117 | |
| 118 | imgs, err := images.GetImages(listResults) |
| 119 | if err != nil { |
| 120 | t.Error(err) |
| 121 | return |
| 122 | } |
| 123 | |
| 124 | n = n + len(imgs) |
| 125 | |
| 126 | for _, i := range imgs { |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 127 | fmt.Fprintf(ts.w, "%s\t%s\t%s\t%s\t%s\t\n", i.Id, ep.Region, i.Name, i.Status, i.Created) |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 128 | } |
| 129 | } |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 130 | ts.w.Flush() |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 131 | fmt.Printf("--------\n%d images listed.\n", n) |
| 132 | } |
| 133 | |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 134 | func TestListFlavors(t *testing.T) { |
| 135 | ts, err := prepForTest() |
| 136 | if err != nil { |
| 137 | t.Error(err) |
| 138 | return |
| 139 | } |
| 140 | |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame^] | 141 | fmt.Fprintln(ts.w, "ID\tRegion\tName\tRAM\tDisk\tVCPUs\t") |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 142 | |
| 143 | region := os.Getenv("OS_REGION_NAME") |
| 144 | n := 0 |
| 145 | for _, ep := range ts.eps { |
| 146 | if (region != "") && (region != ep.Region) { |
| 147 | continue |
| 148 | } |
| 149 | |
| 150 | client := flavors.NewClient(ep.PublicURL, ts.a, ts.o) |
| 151 | |
| 152 | listResults, err := flavors.List(client) |
| 153 | if err != nil { |
| 154 | t.Error(err) |
| 155 | return |
| 156 | } |
| 157 | |
| 158 | flavs, err := flavors.GetFlavors(listResults) |
| 159 | if err != nil { |
| 160 | t.Error(err) |
| 161 | return |
| 162 | } |
| 163 | |
| 164 | n = n + len(flavs) |
| 165 | |
| 166 | for _, f := range flavs { |
Samuel A. Falvo II | 10decf9 | 2014-02-13 17:05:35 -0800 | [diff] [blame^] | 167 | fmt.Fprintf(ts.w, "%s\t%s\t%s\t%d\t%d\t%d\t\n", f.Id, ep.Region, f.Name, f.Ram, f.Disk, f.VCpus) |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | ts.w.Flush() |
| 171 | fmt.Printf("--------\n%d images listed.\n", n) |
| 172 | } |
| 173 | |
| 174 | func findAllComputeEndpoints(sc *identity.ServiceCatalog) ([]identity.Endpoint, error) { |
| 175 | ces, err := sc.CatalogEntries() |
| 176 | if err != nil { |
| 177 | return nil, err |
| 178 | } |
| 179 | |
| 180 | for _, ce := range ces { |
| 181 | if ce.Type == "compute" { |
| 182 | return ce.Endpoints, nil |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | return nil, fmt.Errorf("Compute endpoint not found.") |
| 187 | } |
| 188 | |