blob: dc8265da761e94006a4e746c5340b35e5ad24062 [file] [log] [blame]
Ash Wilson5c0161c2014-10-07 10:42:34 -04001package openstack
2
3import (
4 "fmt"
5
6 "github.com/rackspace/gophercloud"
7 tokens2 "github.com/rackspace/gophercloud/openstack/identity/v2/tokens"
Ash Wilson130a6e22014-10-07 10:48:17 -04008 endpoints3 "github.com/rackspace/gophercloud/openstack/identity/v3/endpoints"
9 services3 "github.com/rackspace/gophercloud/openstack/identity/v3/services"
10 "github.com/rackspace/gophercloud/pagination"
Ash Wilson5c0161c2014-10-07 10:42:34 -040011)
12
13// V2EndpointURL discovers the endpoint URL for a specific service from a ServiceCatalog acquired
14// during the v2 identity service. The specified EndpointOpts are used to identify a unique,
15// unambiguous endpoint to return. It's an error both when multiple endpoints match the provided
16// criteria and when none do. The minimum that can be specified is a Type, but you will also often
17// need to specify a Name and/or a Region depending on what's available on your OpenStack
18// deployment.
19func V2EndpointURL(catalog *tokens2.ServiceCatalog, opts gophercloud.EndpointOpts) (string, error) {
20 // Extract Endpoints from the catalog entries that match the requested Type, Name if provided, and Region if provided.
21 var endpoints = make([]tokens2.Endpoint, 0, 1)
22 for _, entry := range catalog.Entries {
23 if (entry.Type == opts.Type) && (opts.Name == "" || entry.Name == opts.Name) {
24 for _, endpoint := range entry.Endpoints {
25 if opts.Region == "" || endpoint.Region == opts.Region {
26 endpoints = append(endpoints, endpoint)
27 }
28 }
29 }
30 }
31
32 // Report an error if the options were ambiguous.
33 if len(endpoints) == 0 {
34 return "", gophercloud.ErrEndpointNotFound
35 }
36 if len(endpoints) > 1 {
37 return "", fmt.Errorf("Discovered %d matching endpoints: %#v", len(endpoints), endpoints)
38 }
39
40 // Extract the appropriate URL from the matching Endpoint.
41 for _, endpoint := range endpoints {
42 switch opts.Availability {
43 case gophercloud.AvailabilityPublic:
44 return gophercloud.NormalizeURL(endpoint.PublicURL), nil
45 case gophercloud.AvailabilityInternal:
46 return gophercloud.NormalizeURL(endpoint.InternalURL), nil
47 case gophercloud.AvailabilityAdmin:
48 return gophercloud.NormalizeURL(endpoint.AdminURL), nil
49 default:
50 return "", fmt.Errorf("Unexpected availability in endpoint query: %s", opts.Availability)
51 }
52 }
53
54 return "", gophercloud.ErrEndpointNotFound
55}
Ash Wilson130a6e22014-10-07 10:48:17 -040056
57// V3EndpointLocator discovers the endpoint URL for a specific service using multiple calls against
58// an identity v3 service endpoint. The specified EndpointOpts are used to identify a unique,
59// unambiguous endpoint to return. It's an error both when multiple endpoints match the provided
60// criteria and when none do. The minimum that can be specified is a Type, but you will also often
61// need to specify a Name and/or a Region depending on what's available on your OpenStack
62// deployment.
63func V3EndpointLocator(v3Client *gophercloud.ServiceClient, opts gophercloud.EndpointOpts) (string, error) {
64 // Discover the service we're interested in.
65 var services = make([]services3.Service, 0, 1)
66 servicePager := services3.List(v3Client, services3.ListOpts{ServiceType: opts.Type})
67 err := servicePager.EachPage(func(page pagination.Page) (bool, error) {
68 part, err := services3.ExtractServices(page)
69 if err != nil {
70 return false, err
71 }
72
73 for _, service := range part {
74 if service.Name == opts.Name {
75 services = append(services, service)
76 }
77 }
78
79 return true, nil
80 })
81 if err != nil {
82 return "", err
83 }
84
85 if len(services) == 0 {
86 return "", gophercloud.ErrServiceNotFound
87 }
88 if len(services) > 1 {
89 return "", fmt.Errorf("Discovered %d matching services: %#v", len(services), services)
90 }
91 service := services[0]
92
93 // Enumerate the endpoints available for this service.
94 var endpoints []endpoints3.Endpoint
95 endpointPager := endpoints3.List(v3Client, endpoints3.ListOpts{
96 Availability: opts.Availability,
97 ServiceID: service.ID,
98 })
99 err = endpointPager.EachPage(func(page pagination.Page) (bool, error) {
100 part, err := endpoints3.ExtractEndpoints(page)
101 if err != nil {
102 return false, err
103 }
104
105 for _, endpoint := range part {
106 if opts.Region == "" || endpoint.Region == opts.Region {
107 endpoints = append(endpoints, endpoint)
108 }
109 }
110
111 return true, nil
112 })
113 if err != nil {
114 return "", err
115 }
116
117 if len(endpoints) == 0 {
118 return "", gophercloud.ErrEndpointNotFound
119 }
120 if len(endpoints) > 1 {
121 return "", fmt.Errorf("Discovered %d matching endpoints: %#v", len(endpoints), endpoints)
122 }
123 endpoint := endpoints[0]
124
125 return gophercloud.NormalizeURL(endpoint.URL), nil
126}