blob: 70f68009fadcd4ab58aabd8a6aa3bd6e449922d4 [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
32 err.Function = "openstack.V2EndpointURL"
33 return "", err
Ash Wilson5c0161c2014-10-07 10:42:34 -040034 }
35
36 // Extract the appropriate URL from the matching Endpoint.
37 for _, endpoint := range endpoints {
38 switch opts.Availability {
39 case gophercloud.AvailabilityPublic:
40 return gophercloud.NormalizeURL(endpoint.PublicURL), nil
41 case gophercloud.AvailabilityInternal:
42 return gophercloud.NormalizeURL(endpoint.InternalURL), nil
43 case gophercloud.AvailabilityAdmin:
44 return gophercloud.NormalizeURL(endpoint.AdminURL), nil
45 default:
Jon Perritt376dfce2016-02-28 23:39:09 -060046 err := &ErrInvalidAvailabilityProvided{}
47 err.Function = "openstack.V2EndpointURL"
48 err.Argument = "Availability"
49 err.Value = opts.Availability
50 return "", err
Ash Wilson5c0161c2014-10-07 10:42:34 -040051 }
52 }
53
Ash Wilsonfc1af5a2014-10-08 09:10:41 -040054 // Report an error if there were no matching endpoints.
Jon Perritt376dfce2016-02-28 23:39:09 -060055 err := &gophercloud.ErrEndpointNotFound{}
56 err.Function = "openstack.V2EndpointURL"
57 return "", err
Ash Wilson5c0161c2014-10-07 10:42:34 -040058}
Ash Wilson130a6e22014-10-07 10:48:17 -040059
Guillaume Giamarchib2663b22015-04-01 01:23:29 +020060// V3EndpointURL discovers the endpoint URL for a specific service from a Catalog acquired
61// during the v3 identity service. The specified EndpointOpts are used to identify a unique,
Ash Wilson130a6e22014-10-07 10:48:17 -040062// unambiguous endpoint to return. It's an error both when multiple endpoints match the provided
63// criteria and when none do. The minimum that can be specified is a Type, but you will also often
64// need to specify a Name and/or a Region depending on what's available on your OpenStack
65// deployment.
Guillaume Giamarchib2663b22015-04-01 01:23:29 +020066func V3EndpointURL(catalog *tokens3.ServiceCatalog, opts gophercloud.EndpointOpts) (string, error) {
67 // Extract Endpoints from the catalog entries that match the requested Type, Interface,
68 // Name if provided, and Region if provided.
69 var endpoints = make([]tokens3.Endpoint, 0, 1)
70 for _, entry := range catalog.Entries {
71 if (entry.Type == opts.Type) && (opts.Name == "" || entry.Name == opts.Name) {
72 for _, endpoint := range entry.Endpoints {
73 if opts.Availability != gophercloud.AvailabilityAdmin &&
74 opts.Availability != gophercloud.AvailabilityPublic &&
75 opts.Availability != gophercloud.AvailabilityInternal {
Jon Perritt376dfce2016-02-28 23:39:09 -060076 err := &ErrInvalidAvailabilityProvided{}
77 err.Function = "openstack.V3EndpointURL"
78 err.Argument = "Availability"
79 err.Value = opts.Availability
Guillaume Giamarchib2663b22015-04-01 01:23:29 +020080 }
81 if (opts.Availability == gophercloud.Availability(endpoint.Interface)) &&
82 (opts.Region == "" || endpoint.Region == opts.Region) {
83 endpoints = append(endpoints, endpoint)
84 }
Ash Wilson130a6e22014-10-07 10:48:17 -040085 }
86 }
Ash Wilson130a6e22014-10-07 10:48:17 -040087 }
88
Guillaume Giamarchib2663b22015-04-01 01:23:29 +020089 // Report an error if the options were ambiguous.
Ash Wilson130a6e22014-10-07 10:48:17 -040090 if len(endpoints) > 1 {
Jon Perritt376dfce2016-02-28 23:39:09 -060091 err := &ErrMultipleMatchingEndpointsV3{}
92 err.Endpoints = endpoints
93 err.Function = "openstack.V3EndpointURL"
94 return "", err
Ash Wilson130a6e22014-10-07 10:48:17 -040095 }
Ash Wilson130a6e22014-10-07 10:48:17 -040096
Guillaume Giamarchib2663b22015-04-01 01:23:29 +020097 // Extract the URL from the matching Endpoint.
98 for _, endpoint := range endpoints {
99 return gophercloud.NormalizeURL(endpoint.URL), nil
100 }
101
102 // Report an error if there were no matching endpoints.
Jon Perritt376dfce2016-02-28 23:39:09 -0600103 err := &gophercloud.ErrEndpointNotFound{}
104 err.Function = "openstack.V3EndpointURL"
105 return "", err
Ash Wilson130a6e22014-10-07 10:48:17 -0400106}