Ash Wilson | dd7188d | 2014-09-05 14:02:42 -0400 | [diff] [blame] | 1 | // +build acceptance |
| 2 | |
| 3 | package v3 |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | |
Ash Wilson | 0555c64 | 2014-09-05 16:57:17 -0400 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud" |
Ash Wilson | dd7188d | 2014-09-05 14:02:42 -0400 | [diff] [blame] | 9 | endpoints3 "github.com/rackspace/gophercloud/openstack/identity/v3/endpoints" |
Ash Wilson | 0555c64 | 2014-09-05 16:57:17 -0400 | [diff] [blame] | 10 | services3 "github.com/rackspace/gophercloud/openstack/identity/v3/services" |
Ash Wilson | dd7188d | 2014-09-05 14:02:42 -0400 | [diff] [blame] | 11 | ) |
| 12 | |
| 13 | func TestListEndpoints(t *testing.T) { |
| 14 | // Create a service client. |
| 15 | serviceClient := createAuthenticatedClient(t) |
Ash Wilson | 7083d02 | 2014-09-09 14:10:43 -0400 | [diff] [blame] | 16 | if serviceClient == nil { |
| 17 | return |
| 18 | } |
Ash Wilson | dd7188d | 2014-09-05 14:02:42 -0400 | [diff] [blame] | 19 | |
| 20 | // Use the service to list all available endpoints. |
Ash Wilson | 0555c64 | 2014-09-05 16:57:17 -0400 | [diff] [blame] | 21 | results, err := endpoints3.List(serviceClient, endpoints3.ListOpts{}) |
Ash Wilson | dd7188d | 2014-09-05 14:02:42 -0400 | [diff] [blame] | 22 | if err != nil { |
Ash Wilson | 0555c64 | 2014-09-05 16:57:17 -0400 | [diff] [blame] | 23 | t.Fatalf("Unexpected error while listing endpoints: %v", err) |
Ash Wilson | dd7188d | 2014-09-05 14:02:42 -0400 | [diff] [blame] | 24 | } |
Ash Wilson | 0555c64 | 2014-09-05 16:57:17 -0400 | [diff] [blame] | 25 | |
| 26 | err = gophercloud.EachPage(results, func(page gophercloud.Collection) bool { |
| 27 | t.Logf("--- Page ---") |
| 28 | |
| 29 | for _, endpoint := range endpoints3.AsEndpoints(page) { |
| 30 | t.Logf("Endpoint: %8s %10s %9s %s", |
| 31 | endpoint.ID, |
Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame^] | 32 | endpoint.Availability, |
Ash Wilson | 0555c64 | 2014-09-05 16:57:17 -0400 | [diff] [blame] | 33 | endpoint.Name, |
| 34 | endpoint.URL) |
| 35 | } |
| 36 | |
| 37 | return true |
| 38 | }) |
| 39 | if err != nil { |
| 40 | t.Errorf("Unexpected error while iterating endpoint pages: %v", err) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func TestNavigateCatalog(t *testing.T) { |
| 45 | // Create a service client. |
| 46 | client := createAuthenticatedClient(t) |
| 47 | |
| 48 | // Discover the service we're interested in. |
| 49 | computeResults, err := services3.List(client, services3.ListOpts{ServiceType: "compute"}) |
| 50 | if err != nil { |
| 51 | t.Fatalf("Unexpected error while listing services: %v", err) |
| 52 | } |
| 53 | |
| 54 | allServices, err := gophercloud.AllPages(computeResults) |
| 55 | if err != nil { |
| 56 | t.Fatalf("Unexpected error while traversing service results: %v", err) |
| 57 | } |
| 58 | |
| 59 | computeServices := services3.AsServices(allServices) |
| 60 | |
| 61 | if len(computeServices) != 1 { |
| 62 | t.Logf("%d compute services are available at this endpoint.", len(computeServices)) |
| 63 | return |
| 64 | } |
| 65 | computeService := computeServices[0] |
| 66 | |
| 67 | // Enumerate the endpoints available for this service. |
| 68 | endpointResults, err := endpoints3.List(client, endpoints3.ListOpts{ |
Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame^] | 69 | Availability: gophercloud.AvailabilityPublic, |
| 70 | ServiceID: computeService.ID, |
Ash Wilson | 0555c64 | 2014-09-05 16:57:17 -0400 | [diff] [blame] | 71 | }) |
| 72 | |
| 73 | allEndpoints, err := gophercloud.AllPages(endpointResults) |
| 74 | if err != nil { |
| 75 | t.Fatalf("Unexpected error while listing endpoints: %v", err) |
| 76 | } |
| 77 | |
| 78 | endpoints := endpoints3.AsEndpoints(allEndpoints) |
| 79 | |
| 80 | if len(endpoints) != 1 { |
| 81 | t.Logf("%d endpoints are available for the service %v.", len(endpoints), computeService.Name) |
| 82 | return |
| 83 | } |
| 84 | |
| 85 | endpoint := endpoints[0] |
| 86 | t.Logf("Success. The compute endpoint is at %s.", endpoint.URL) |
Ash Wilson | dd7188d | 2014-09-05 14:02:42 -0400 | [diff] [blame] | 87 | } |