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 | 387d1bd | 2014-09-16 11:43:24 -0400 | [diff] [blame] | 11 | "github.com/rackspace/gophercloud/pagination" |
Ash Wilson | dd7188d | 2014-09-05 14:02:42 -0400 | [diff] [blame] | 12 | ) |
| 13 | |
| 14 | func TestListEndpoints(t *testing.T) { |
| 15 | // Create a service client. |
| 16 | serviceClient := createAuthenticatedClient(t) |
Ash Wilson | 7083d02 | 2014-09-09 14:10:43 -0400 | [diff] [blame] | 17 | if serviceClient == nil { |
| 18 | return |
| 19 | } |
Ash Wilson | dd7188d | 2014-09-05 14:02:42 -0400 | [diff] [blame] | 20 | |
| 21 | // Use the service to list all available endpoints. |
Ash Wilson | 3be15e1 | 2014-09-12 15:25:21 -0400 | [diff] [blame] | 22 | pager := endpoints3.List(serviceClient, endpoints3.ListOpts{}) |
Ash Wilson | 387d1bd | 2014-09-16 11:43:24 -0400 | [diff] [blame] | 23 | err := pager.EachPage(func(page pagination.Page) (bool, error) { |
Ash Wilson | 0555c64 | 2014-09-05 16:57:17 -0400 | [diff] [blame] | 24 | t.Logf("--- Page ---") |
| 25 | |
Ash Wilson | 3be15e1 | 2014-09-12 15:25:21 -0400 | [diff] [blame] | 26 | endpoints, err := endpoints3.ExtractEndpoints(page) |
| 27 | if err != nil { |
| 28 | t.Fatalf("Error extracting endpoings: %v", err) |
| 29 | } |
| 30 | |
| 31 | for _, endpoint := range endpoints { |
Ash Wilson | 0555c64 | 2014-09-05 16:57:17 -0400 | [diff] [blame] | 32 | t.Logf("Endpoint: %8s %10s %9s %s", |
| 33 | endpoint.ID, |
Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 34 | endpoint.Availability, |
Ash Wilson | 0555c64 | 2014-09-05 16:57:17 -0400 | [diff] [blame] | 35 | endpoint.Name, |
| 36 | endpoint.URL) |
| 37 | } |
| 38 | |
Ash Wilson | 3be15e1 | 2014-09-12 15:25:21 -0400 | [diff] [blame] | 39 | return true, nil |
Ash Wilson | 0555c64 | 2014-09-05 16:57:17 -0400 | [diff] [blame] | 40 | }) |
| 41 | if err != nil { |
| 42 | t.Errorf("Unexpected error while iterating endpoint pages: %v", err) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func TestNavigateCatalog(t *testing.T) { |
| 47 | // Create a service client. |
| 48 | client := createAuthenticatedClient(t) |
| 49 | |
Ash Wilson | 3be15e1 | 2014-09-12 15:25:21 -0400 | [diff] [blame] | 50 | var compute *services3.Service |
| 51 | var endpoint *endpoints3.Endpoint |
| 52 | |
Ash Wilson | 0555c64 | 2014-09-05 16:57:17 -0400 | [diff] [blame] | 53 | // Discover the service we're interested in. |
Ash Wilson | 3be15e1 | 2014-09-12 15:25:21 -0400 | [diff] [blame] | 54 | servicePager := services3.List(client, services3.ListOpts{ServiceType: "compute"}) |
Ash Wilson | 387d1bd | 2014-09-16 11:43:24 -0400 | [diff] [blame] | 55 | err := servicePager.EachPage(func(page pagination.Page) (bool, error) { |
Ash Wilson | 3be15e1 | 2014-09-12 15:25:21 -0400 | [diff] [blame] | 56 | part, err := services3.ExtractServices(page) |
| 57 | if err != nil { |
| 58 | return false, err |
| 59 | } |
| 60 | if compute != nil { |
| 61 | t.Fatalf("Expected one service, got more than one page") |
| 62 | return false, nil |
| 63 | } |
| 64 | if len(part) != 1 { |
| 65 | t.Fatalf("Expected one service, got %d", len(part)) |
| 66 | return false, nil |
| 67 | } |
| 68 | |
| 69 | compute = &part[0] |
| 70 | return true, nil |
| 71 | }) |
Ash Wilson | 0555c64 | 2014-09-05 16:57:17 -0400 | [diff] [blame] | 72 | if err != nil { |
Ash Wilson | 3be15e1 | 2014-09-12 15:25:21 -0400 | [diff] [blame] | 73 | t.Fatalf("Unexpected error iterating pages: %v", err) |
Ash Wilson | 0555c64 | 2014-09-05 16:57:17 -0400 | [diff] [blame] | 74 | } |
| 75 | |
Ash Wilson | 3be15e1 | 2014-09-12 15:25:21 -0400 | [diff] [blame] | 76 | if compute == nil { |
| 77 | t.Fatalf("No compute service found.") |
Ash Wilson | 0555c64 | 2014-09-05 16:57:17 -0400 | [diff] [blame] | 78 | } |
| 79 | |
Ash Wilson | 0555c64 | 2014-09-05 16:57:17 -0400 | [diff] [blame] | 80 | // Enumerate the endpoints available for this service. |
Ash Wilson | 3be15e1 | 2014-09-12 15:25:21 -0400 | [diff] [blame] | 81 | computePager := endpoints3.List(client, endpoints3.ListOpts{ |
Ash Wilson | efac18b | 2014-09-10 14:44:42 -0400 | [diff] [blame] | 82 | Availability: gophercloud.AvailabilityPublic, |
Ash Wilson | 3be15e1 | 2014-09-12 15:25:21 -0400 | [diff] [blame] | 83 | ServiceID: compute.ID, |
| 84 | }) |
Ash Wilson | 387d1bd | 2014-09-16 11:43:24 -0400 | [diff] [blame] | 85 | err = computePager.EachPage(func(page pagination.Page) (bool, error) { |
Ash Wilson | 3be15e1 | 2014-09-12 15:25:21 -0400 | [diff] [blame] | 86 | part, err := endpoints3.ExtractEndpoints(page) |
| 87 | if err != nil { |
| 88 | return false, err |
| 89 | } |
| 90 | if endpoint != nil { |
| 91 | t.Fatalf("Expected one endpoint, got more than one page") |
| 92 | return false, nil |
| 93 | } |
| 94 | if len(part) != 1 { |
| 95 | t.Fatalf("Expected one endpoint, got %d", len(part)) |
| 96 | return false, nil |
| 97 | } |
| 98 | |
| 99 | endpoint = &part[0] |
| 100 | return true, nil |
Ash Wilson | 0555c64 | 2014-09-05 16:57:17 -0400 | [diff] [blame] | 101 | }) |
| 102 | |
Ash Wilson | 3be15e1 | 2014-09-12 15:25:21 -0400 | [diff] [blame] | 103 | if endpoint == nil { |
| 104 | t.Fatalf("No endpoint found.") |
Ash Wilson | 0555c64 | 2014-09-05 16:57:17 -0400 | [diff] [blame] | 105 | } |
| 106 | |
Ash Wilson | 0555c64 | 2014-09-05 16:57:17 -0400 | [diff] [blame] | 107 | t.Logf("Success. The compute endpoint is at %s.", endpoint.URL) |
Ash Wilson | dd7188d | 2014-09-05 14:02:42 -0400 | [diff] [blame] | 108 | } |