blob: 8b302cc409e08f17f07e19fa999bf3e621dd6fbc [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)
16
17 // Use the service to list all available endpoints.
Ash Wilson0555c642014-09-05 16:57:17 -040018 results, err := endpoints3.List(serviceClient, endpoints3.ListOpts{})
Ash Wilsondd7188d2014-09-05 14:02:42 -040019 if err != nil {
Ash Wilson0555c642014-09-05 16:57:17 -040020 t.Fatalf("Unexpected error while listing endpoints: %v", err)
Ash Wilsondd7188d2014-09-05 14:02:42 -040021 }
Ash Wilson0555c642014-09-05 16:57:17 -040022
23 err = gophercloud.EachPage(results, func(page gophercloud.Collection) bool {
24 t.Logf("--- Page ---")
25
26 for _, endpoint := range endpoints3.AsEndpoints(page) {
27 t.Logf("Endpoint: %8s %10s %9s %s",
28 endpoint.ID,
29 endpoint.Interface,
30 endpoint.Name,
31 endpoint.URL)
32 }
33
34 return true
35 })
36 if err != nil {
37 t.Errorf("Unexpected error while iterating endpoint pages: %v", err)
38 }
39}
40
41func TestNavigateCatalog(t *testing.T) {
42 // Create a service client.
43 client := createAuthenticatedClient(t)
44
45 // Discover the service we're interested in.
46 computeResults, err := services3.List(client, services3.ListOpts{ServiceType: "compute"})
47 if err != nil {
48 t.Fatalf("Unexpected error while listing services: %v", err)
49 }
50
51 allServices, err := gophercloud.AllPages(computeResults)
52 if err != nil {
53 t.Fatalf("Unexpected error while traversing service results: %v", err)
54 }
55
56 computeServices := services3.AsServices(allServices)
57
58 if len(computeServices) != 1 {
59 t.Logf("%d compute services are available at this endpoint.", len(computeServices))
60 return
61 }
62 computeService := computeServices[0]
63
64 // Enumerate the endpoints available for this service.
65 endpointResults, err := endpoints3.List(client, endpoints3.ListOpts{
66 Interface: endpoints3.InterfacePublic,
67 ServiceID: computeService.ID,
68 })
69
70 allEndpoints, err := gophercloud.AllPages(endpointResults)
71 if err != nil {
72 t.Fatalf("Unexpected error while listing endpoints: %v", err)
73 }
74
75 endpoints := endpoints3.AsEndpoints(allEndpoints)
76
77 if len(endpoints) != 1 {
78 t.Logf("%d endpoints are available for the service %v.", len(endpoints), computeService.Name)
79 return
80 }
81
82 endpoint := endpoints[0]
83 t.Logf("Success. The compute endpoint is at %s.", endpoint.URL)
Ash Wilsondd7188d2014-09-05 14:02:42 -040084}