blob: 36aabc57153b164ec77d407cbb94498f4d3b4933 [file] [log] [blame]
Ash Wilson5c0161c2014-10-07 10:42:34 -04001package openstack
2
3import (
Krzysztof Szukiełojć3f41d082017-05-07 14:43:06 +02004 "gerrit.mcp.mirantis.net/debian/gophercloud.git"
Krzysztof Szukiełojć24a29ce2017-05-07 14:24:02 +02005 tokens2 "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack/identity/v2/tokens"
6 tokens3 "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack/identity/v3/tokens"
Ash Wilson5c0161c2014-10-07 10:42:34 -04007)
8
9// V2EndpointURL discovers the endpoint URL for a specific service from a ServiceCatalog acquired
10// during the v2 identity service. The specified EndpointOpts are used to identify a unique,
11// unambiguous endpoint to return. It's an error both when multiple endpoints match the provided
12// criteria and when none do. The minimum that can be specified is a Type, but you will also often
13// need to specify a Name and/or a Region depending on what's available on your OpenStack
14// deployment.
15func V2EndpointURL(catalog *tokens2.ServiceCatalog, opts gophercloud.EndpointOpts) (string, error) {
16 // Extract Endpoints from the catalog entries that match the requested Type, Name if provided, and Region if provided.
17 var endpoints = make([]tokens2.Endpoint, 0, 1)
18 for _, entry := range catalog.Entries {
19 if (entry.Type == opts.Type) && (opts.Name == "" || entry.Name == opts.Name) {
20 for _, endpoint := range entry.Endpoints {
21 if opts.Region == "" || endpoint.Region == opts.Region {
22 endpoints = append(endpoints, endpoint)
23 }
24 }
25 }
26 }
27
28 // Report an error if the options were ambiguous.
Ash Wilson5c0161c2014-10-07 10:42:34 -040029 if len(endpoints) > 1 {
Jon Perritt376dfce2016-02-28 23:39:09 -060030 err := &ErrMultipleMatchingEndpointsV2{}
31 err.Endpoints = endpoints
Jon Perritt376dfce2016-02-28 23:39:09 -060032 return "", err
Ash Wilson5c0161c2014-10-07 10:42:34 -040033 }
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:
Jon Perritt376dfce2016-02-28 23:39:09 -060045 err := &ErrInvalidAvailabilityProvided{}
Jon Perritt376dfce2016-02-28 23:39:09 -060046 err.Argument = "Availability"
47 err.Value = opts.Availability
48 return "", err
Ash Wilson5c0161c2014-10-07 10:42:34 -040049 }
50 }
51
Ash Wilsonfc1af5a2014-10-08 09:10:41 -040052 // Report an error if there were no matching endpoints.
Jon Perritt376dfce2016-02-28 23:39:09 -060053 err := &gophercloud.ErrEndpointNotFound{}
Jon Perritt376dfce2016-02-28 23:39:09 -060054 return "", err
Ash Wilson5c0161c2014-10-07 10:42:34 -040055}
Ash Wilson130a6e22014-10-07 10:48:17 -040056
Guillaume Giamarchib2663b22015-04-01 01:23:29 +020057// V3EndpointURL discovers the endpoint URL for a specific service from a Catalog acquired
58// during the v3 identity service. The specified EndpointOpts are used to identify a unique,
Ash Wilson130a6e22014-10-07 10:48:17 -040059// 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.
Guillaume Giamarchib2663b22015-04-01 01:23:29 +020063func V3EndpointURL(catalog *tokens3.ServiceCatalog, opts gophercloud.EndpointOpts) (string, error) {
64 // Extract Endpoints from the catalog entries that match the requested Type, Interface,
65 // Name if provided, and Region if provided.
66 var endpoints = make([]tokens3.Endpoint, 0, 1)
67 for _, entry := range catalog.Entries {
68 if (entry.Type == opts.Type) && (opts.Name == "" || entry.Name == opts.Name) {
69 for _, endpoint := range entry.Endpoints {
70 if opts.Availability != gophercloud.AvailabilityAdmin &&
71 opts.Availability != gophercloud.AvailabilityPublic &&
72 opts.Availability != gophercloud.AvailabilityInternal {
Jon Perritt376dfce2016-02-28 23:39:09 -060073 err := &ErrInvalidAvailabilityProvided{}
Jon Perritt376dfce2016-02-28 23:39:09 -060074 err.Argument = "Availability"
75 err.Value = opts.Availability
Jon Perritta33da232016-03-02 04:43:08 -060076 return "", err
Guillaume Giamarchib2663b22015-04-01 01:23:29 +020077 }
78 if (opts.Availability == gophercloud.Availability(endpoint.Interface)) &&
79 (opts.Region == "" || endpoint.Region == opts.Region) {
80 endpoints = append(endpoints, endpoint)
81 }
Ash Wilson130a6e22014-10-07 10:48:17 -040082 }
83 }
Ash Wilson130a6e22014-10-07 10:48:17 -040084 }
85
Guillaume Giamarchib2663b22015-04-01 01:23:29 +020086 // Report an error if the options were ambiguous.
Ash Wilson130a6e22014-10-07 10:48:17 -040087 if len(endpoints) > 1 {
Jon Perrittdb0ae142016-03-13 00:33:41 -060088 return "", ErrMultipleMatchingEndpointsV3{Endpoints: endpoints}
Ash Wilson130a6e22014-10-07 10:48:17 -040089 }
Ash Wilson130a6e22014-10-07 10:48:17 -040090
Guillaume Giamarchib2663b22015-04-01 01:23:29 +020091 // Extract the URL from the matching Endpoint.
92 for _, endpoint := range endpoints {
93 return gophercloud.NormalizeURL(endpoint.URL), nil
94 }
95
96 // Report an error if there were no matching endpoints.
Jon Perritt376dfce2016-02-28 23:39:09 -060097 err := &gophercloud.ErrEndpointNotFound{}
Jon Perritt376dfce2016-02-28 23:39:09 -060098 return "", err
Ash Wilson130a6e22014-10-07 10:48:17 -040099}