blob: 14783b50e192a921b0618b9f6eb5c777751b4878 [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 Wilsondd7188d2014-09-05 14:02:42 -040011)
12
13func TestListEndpoints(t *testing.T) {
14 // Create a service client.
15 serviceClient := createAuthenticatedClient(t)
Ash Wilson7083d022014-09-09 14:10:43 -040016 if serviceClient == nil {
17 return
18 }
Ash Wilsondd7188d2014-09-05 14:02:42 -040019
20 // Use the service to list all available endpoints.
Ash Wilson0555c642014-09-05 16:57:17 -040021 results, err := endpoints3.List(serviceClient, endpoints3.ListOpts{})
Ash Wilsondd7188d2014-09-05 14:02:42 -040022 if err != nil {
Ash Wilson0555c642014-09-05 16:57:17 -040023 t.Fatalf("Unexpected error while listing endpoints: %v", err)
Ash Wilsondd7188d2014-09-05 14:02:42 -040024 }
Ash Wilson0555c642014-09-05 16:57:17 -040025
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 Wilsonefac18b2014-09-10 14:44:42 -040032 endpoint.Availability,
Ash Wilson0555c642014-09-05 16:57:17 -040033 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
44func 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 Wilsonefac18b2014-09-10 14:44:42 -040069 Availability: gophercloud.AvailabilityPublic,
70 ServiceID: computeService.ID,
Ash Wilson0555c642014-09-05 16:57:17 -040071 })
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 Wilsondd7188d2014-09-05 14:02:42 -040087}