blob: 29d02c43f9299b5fffc6f1c2ca086f1c5839537f [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"
Guillaume Giamarchib2663b22015-04-01 01:23:29 +02008 tokens3 "github.com/rackspace/gophercloud/openstack/identity/v3/tokens"
Ash Wilson5c0161c2014-10-07 10:42:34 -04009)
10
11// V2EndpointURL discovers the endpoint URL for a specific service from a ServiceCatalog acquired
12// during the v2 identity service. The specified EndpointOpts are used to identify a unique,
13// unambiguous endpoint to return. It's an error both when multiple endpoints match the provided
14// criteria and when none do. The minimum that can be specified is a Type, but you will also often
15// need to specify a Name and/or a Region depending on what's available on your OpenStack
16// deployment.
17func V2EndpointURL(catalog *tokens2.ServiceCatalog, opts gophercloud.EndpointOpts) (string, error) {
18 // Extract Endpoints from the catalog entries that match the requested Type, Name if provided, and Region if provided.
19 var endpoints = make([]tokens2.Endpoint, 0, 1)
20 for _, entry := range catalog.Entries {
21 if (entry.Type == opts.Type) && (opts.Name == "" || entry.Name == opts.Name) {
22 for _, endpoint := range entry.Endpoints {
23 if opts.Region == "" || endpoint.Region == opts.Region {
24 endpoints = append(endpoints, endpoint)
25 }
26 }
27 }
28 }
29
30 // Report an error if the options were ambiguous.
Ash Wilson5c0161c2014-10-07 10:42:34 -040031 if len(endpoints) > 1 {
32 return "", fmt.Errorf("Discovered %d matching endpoints: %#v", len(endpoints), endpoints)
33 }
34
35 // Extract the appropriate URL from the matching Endpoint.
36 for _, endpoint := range endpoints {
37 switch opts.Availability {
38 case gophercloud.AvailabilityPublic:
39 return gophercloud.NormalizeURL(endpoint.PublicURL), nil
40 case gophercloud.AvailabilityInternal:
41 return gophercloud.NormalizeURL(endpoint.InternalURL), nil
42 case gophercloud.AvailabilityAdmin:
43 return gophercloud.NormalizeURL(endpoint.AdminURL), nil
44 default:
45 return "", fmt.Errorf("Unexpected availability in endpoint query: %s", opts.Availability)
46 }
47 }
48
Ash Wilsonfc1af5a2014-10-08 09:10:41 -040049 // Report an error if there were no matching endpoints.
Ash Wilson5c0161c2014-10-07 10:42:34 -040050 return "", gophercloud.ErrEndpointNotFound
51}
Ash Wilson130a6e22014-10-07 10:48:17 -040052
Guillaume Giamarchib2663b22015-04-01 01:23:29 +020053// V3EndpointURL discovers the endpoint URL for a specific service from a Catalog acquired
54// during the v3 identity service. The specified EndpointOpts are used to identify a unique,
Ash Wilson130a6e22014-10-07 10:48:17 -040055// unambiguous endpoint to return. It's an error both when multiple endpoints match the provided
56// criteria and when none do. The minimum that can be specified is a Type, but you will also often
57// need to specify a Name and/or a Region depending on what's available on your OpenStack
58// deployment.
Guillaume Giamarchib2663b22015-04-01 01:23:29 +020059func V3EndpointURL(catalog *tokens3.ServiceCatalog, opts gophercloud.EndpointOpts) (string, error) {
60 // Extract Endpoints from the catalog entries that match the requested Type, Interface,
61 // Name if provided, and Region if provided.
62 var endpoints = make([]tokens3.Endpoint, 0, 1)
63 for _, entry := range catalog.Entries {
64 if (entry.Type == opts.Type) && (opts.Name == "" || entry.Name == opts.Name) {
65 for _, endpoint := range entry.Endpoints {
66 if opts.Availability != gophercloud.AvailabilityAdmin &&
67 opts.Availability != gophercloud.AvailabilityPublic &&
68 opts.Availability != gophercloud.AvailabilityInternal {
69 return "", fmt.Errorf("Unexpected availability in endpoint query: %s", opts.Availability)
70 }
71 if (opts.Availability == gophercloud.Availability(endpoint.Interface)) &&
72 (opts.Region == "" || endpoint.Region == opts.Region) {
73 endpoints = append(endpoints, endpoint)
74 }
Ash Wilson130a6e22014-10-07 10:48:17 -040075 }
76 }
Ash Wilson130a6e22014-10-07 10:48:17 -040077 }
78
Guillaume Giamarchib2663b22015-04-01 01:23:29 +020079 // Report an error if the options were ambiguous.
Ash Wilson130a6e22014-10-07 10:48:17 -040080 if len(endpoints) > 1 {
81 return "", fmt.Errorf("Discovered %d matching endpoints: %#v", len(endpoints), endpoints)
82 }
Ash Wilson130a6e22014-10-07 10:48:17 -040083
Guillaume Giamarchib2663b22015-04-01 01:23:29 +020084 // Extract the URL from the matching Endpoint.
85 for _, endpoint := range endpoints {
86 return gophercloud.NormalizeURL(endpoint.URL), nil
87 }
88
89 // Report an error if there were no matching endpoints.
90 return "", gophercloud.ErrEndpointNotFound
Ash Wilson130a6e22014-10-07 10:48:17 -040091}