blob: ea893c2deae8796e0c3e004c60d9a6199acf27fb [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)
Ash Wilson4b33eea2014-10-22 15:45:45 -040049 if client == nil {
50 return
51 }
Ash Wilson0555c642014-09-05 16:57:17 -040052
Ash Wilson3be15e12014-09-12 15:25:21 -040053 var compute *services3.Service
54 var endpoint *endpoints3.Endpoint
55
Ash Wilson0555c642014-09-05 16:57:17 -040056 // Discover the service we're interested in.
Ash Wilson3be15e12014-09-12 15:25:21 -040057 servicePager := services3.List(client, services3.ListOpts{ServiceType: "compute"})
Ash Wilson387d1bd2014-09-16 11:43:24 -040058 err := servicePager.EachPage(func(page pagination.Page) (bool, error) {
Ash Wilson3be15e12014-09-12 15:25:21 -040059 part, err := services3.ExtractServices(page)
60 if err != nil {
61 return false, err
62 }
63 if compute != nil {
64 t.Fatalf("Expected one service, got more than one page")
65 return false, nil
66 }
67 if len(part) != 1 {
68 t.Fatalf("Expected one service, got %d", len(part))
69 return false, nil
70 }
71
72 compute = &part[0]
73 return true, nil
74 })
Ash Wilson0555c642014-09-05 16:57:17 -040075 if err != nil {
Ash Wilson3be15e12014-09-12 15:25:21 -040076 t.Fatalf("Unexpected error iterating pages: %v", err)
Ash Wilson0555c642014-09-05 16:57:17 -040077 }
78
Ash Wilson3be15e12014-09-12 15:25:21 -040079 if compute == nil {
80 t.Fatalf("No compute service found.")
Ash Wilson0555c642014-09-05 16:57:17 -040081 }
82
Ash Wilson0555c642014-09-05 16:57:17 -040083 // Enumerate the endpoints available for this service.
Ash Wilson3be15e12014-09-12 15:25:21 -040084 computePager := endpoints3.List(client, endpoints3.ListOpts{
Ash Wilsonefac18b2014-09-10 14:44:42 -040085 Availability: gophercloud.AvailabilityPublic,
Ash Wilson3be15e12014-09-12 15:25:21 -040086 ServiceID: compute.ID,
87 })
Ash Wilson387d1bd2014-09-16 11:43:24 -040088 err = computePager.EachPage(func(page pagination.Page) (bool, error) {
Ash Wilson3be15e12014-09-12 15:25:21 -040089 part, err := endpoints3.ExtractEndpoints(page)
90 if err != nil {
91 return false, err
92 }
93 if endpoint != nil {
94 t.Fatalf("Expected one endpoint, got more than one page")
95 return false, nil
96 }
97 if len(part) != 1 {
98 t.Fatalf("Expected one endpoint, got %d", len(part))
99 return false, nil
100 }
101
102 endpoint = &part[0]
103 return true, nil
Ash Wilson0555c642014-09-05 16:57:17 -0400104 })
105
Ash Wilson3be15e12014-09-12 15:25:21 -0400106 if endpoint == nil {
107 t.Fatalf("No endpoint found.")
Ash Wilson0555c642014-09-05 16:57:17 -0400108 }
109
Ash Wilson0555c642014-09-05 16:57:17 -0400110 t.Logf("Success. The compute endpoint is at %s.", endpoint.URL)
Ash Wilsondd7188d2014-09-05 14:02:42 -0400111}