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