blob: 9032ec35500fbbfd6e81c2830d29de4e2a4c0ac9 [file] [log] [blame]
Ash Wilsondd7188d2014-09-05 14:02:42 -04001// +build acceptance
2
3package v3
4
5import (
6 "testing"
7
Ash Wilson0555c642014-09-05 16:57:17 -04008 "github.com/rackspace/gophercloud"
Ash Wilsondd7188d2014-09-05 14:02:42 -04009 endpoints3 "github.com/rackspace/gophercloud/openstack/identity/v3/endpoints"
Ash Wilson0555c642014-09-05 16:57:17 -040010 services3 "github.com/rackspace/gophercloud/openstack/identity/v3/services"
Ash Wilson387d1bd2014-09-16 11:43:24 -040011 "github.com/rackspace/gophercloud/pagination"
Ash Wilsondd7188d2014-09-05 14:02:42 -040012)
13
14func TestListEndpoints(t *testing.T) {
15 // Create a service client.
16 serviceClient := createAuthenticatedClient(t)
Ash Wilson7083d022014-09-09 14:10:43 -040017 if serviceClient == nil {
18 return
19 }
Ash Wilsondd7188d2014-09-05 14:02:42 -040020
21 // Use the service to list all available endpoints.
Ash Wilson3be15e12014-09-12 15:25:21 -040022 pager := endpoints3.List(serviceClient, endpoints3.ListOpts{})
Ash Wilson387d1bd2014-09-16 11:43:24 -040023 err := pager.EachPage(func(page pagination.Page) (bool, error) {
Ash Wilson0555c642014-09-05 16:57:17 -040024 t.Logf("--- Page ---")
25
Ash Wilson3be15e12014-09-12 15:25:21 -040026 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 Wilson0555c642014-09-05 16:57:17 -040032 t.Logf("Endpoint: %8s %10s %9s %s",
33 endpoint.ID,
Ash Wilsonefac18b2014-09-10 14:44:42 -040034 endpoint.Availability,
Ash Wilson0555c642014-09-05 16:57:17 -040035 endpoint.Name,
36 endpoint.URL)
37 }
38
Ash Wilson3be15e12014-09-12 15:25:21 -040039 return true, nil
Ash Wilson0555c642014-09-05 16:57:17 -040040 })
41 if err != nil {
42 t.Errorf("Unexpected error while iterating endpoint pages: %v", err)
43 }
44}
45
46func TestNavigateCatalog(t *testing.T) {
47 // Create a service client.
48 client := createAuthenticatedClient(t)
49
Ash Wilson3be15e12014-09-12 15:25:21 -040050 var compute *services3.Service
51 var endpoint *endpoints3.Endpoint
52
Ash Wilson0555c642014-09-05 16:57:17 -040053 // Discover the service we're interested in.
Ash Wilson3be15e12014-09-12 15:25:21 -040054 servicePager := services3.List(client, services3.ListOpts{ServiceType: "compute"})
Ash Wilson387d1bd2014-09-16 11:43:24 -040055 err := servicePager.EachPage(func(page pagination.Page) (bool, error) {
Ash Wilson3be15e12014-09-12 15:25:21 -040056 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 Wilson0555c642014-09-05 16:57:17 -040072 if err != nil {
Ash Wilson3be15e12014-09-12 15:25:21 -040073 t.Fatalf("Unexpected error iterating pages: %v", err)
Ash Wilson0555c642014-09-05 16:57:17 -040074 }
75
Ash Wilson3be15e12014-09-12 15:25:21 -040076 if compute == nil {
77 t.Fatalf("No compute service found.")
Ash Wilson0555c642014-09-05 16:57:17 -040078 }
79
Ash Wilson0555c642014-09-05 16:57:17 -040080 // Enumerate the endpoints available for this service.
Ash Wilson3be15e12014-09-12 15:25:21 -040081 computePager := endpoints3.List(client, endpoints3.ListOpts{
Ash Wilsonefac18b2014-09-10 14:44:42 -040082 Availability: gophercloud.AvailabilityPublic,
Ash Wilson3be15e12014-09-12 15:25:21 -040083 ServiceID: compute.ID,
84 })
Ash Wilson387d1bd2014-09-16 11:43:24 -040085 err = computePager.EachPage(func(page pagination.Page) (bool, error) {
Ash Wilson3be15e12014-09-12 15:25:21 -040086 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 Wilson0555c642014-09-05 16:57:17 -0400101 })
102
Ash Wilson3be15e12014-09-12 15:25:21 -0400103 if endpoint == nil {
104 t.Fatalf("No endpoint found.")
Ash Wilson0555c642014-09-05 16:57:17 -0400105 }
106
Ash Wilson0555c642014-09-05 16:57:17 -0400107 t.Logf("Success. The compute endpoint is at %s.", endpoint.URL)
Ash Wilsondd7188d2014-09-05 14:02:42 -0400108}