blob: fb6b84a979d5a4ac2f7214ee62d3c160e7c1c798 [file] [log] [blame]
Ash Wilson5c0161c2014-10-07 10:42:34 -04001package openstack
2
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 tokens2 "github.com/gophercloud/gophercloud/openstack/identity/v2/tokens"
6 tokens3 "github.com/gophercloud/gophercloud/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{}
54 err.Function = "openstack.V2EndpointURL"
55 return "", err
Ash Wilson5c0161c2014-10-07 10:42:34 -040056}
Ash Wilson130a6e22014-10-07 10:48:17 -040057
Guillaume Giamarchib2663b22015-04-01 01:23:29 +020058// V3EndpointURL discovers the endpoint URL for a specific service from a Catalog acquired
59// during the v3 identity service. The specified EndpointOpts are used to identify a unique,
Ash Wilson130a6e22014-10-07 10:48:17 -040060// unambiguous endpoint to return. It's an error both when multiple endpoints match the provided
61// criteria and when none do. The minimum that can be specified is a Type, but you will also often
62// need to specify a Name and/or a Region depending on what's available on your OpenStack
63// deployment.
Guillaume Giamarchib2663b22015-04-01 01:23:29 +020064func V3EndpointURL(catalog *tokens3.ServiceCatalog, opts gophercloud.EndpointOpts) (string, error) {
65 // Extract Endpoints from the catalog entries that match the requested Type, Interface,
66 // Name if provided, and Region if provided.
67 var endpoints = make([]tokens3.Endpoint, 0, 1)
68 for _, entry := range catalog.Entries {
69 if (entry.Type == opts.Type) && (opts.Name == "" || entry.Name == opts.Name) {
70 for _, endpoint := range entry.Endpoints {
71 if opts.Availability != gophercloud.AvailabilityAdmin &&
72 opts.Availability != gophercloud.AvailabilityPublic &&
73 opts.Availability != gophercloud.AvailabilityInternal {
Jon Perritt376dfce2016-02-28 23:39:09 -060074 err := &ErrInvalidAvailabilityProvided{}
Jon Perritt376dfce2016-02-28 23:39:09 -060075 err.Argument = "Availability"
76 err.Value = opts.Availability
Jon Perritta33da232016-03-02 04:43:08 -060077 return "", err
Guillaume Giamarchib2663b22015-04-01 01:23:29 +020078 }
79 if (opts.Availability == gophercloud.Availability(endpoint.Interface)) &&
80 (opts.Region == "" || endpoint.Region == opts.Region) {
81 endpoints = append(endpoints, endpoint)
82 }
Ash Wilson130a6e22014-10-07 10:48:17 -040083 }
84 }
Ash Wilson130a6e22014-10-07 10:48:17 -040085 }
86
Guillaume Giamarchib2663b22015-04-01 01:23:29 +020087 // Report an error if the options were ambiguous.
Ash Wilson130a6e22014-10-07 10:48:17 -040088 if len(endpoints) > 1 {
Jon Perrittdb0ae142016-03-13 00:33:41 -060089 return "", ErrMultipleMatchingEndpointsV3{Endpoints: endpoints}
Ash Wilson130a6e22014-10-07 10:48:17 -040090 }
Ash Wilson130a6e22014-10-07 10:48:17 -040091
Guillaume Giamarchib2663b22015-04-01 01:23:29 +020092 // Extract the URL from the matching Endpoint.
93 for _, endpoint := range endpoints {
94 return gophercloud.NormalizeURL(endpoint.URL), nil
95 }
96
97 // Report an error if there were no matching endpoints.
Jon Perritt376dfce2016-02-28 23:39:09 -060098 err := &gophercloud.ErrEndpointNotFound{}
Jon Perritt376dfce2016-02-28 23:39:09 -060099 return "", err
Ash Wilson130a6e22014-10-07 10:48:17 -0400100}