Ash Wilson | 5c0161c | 2014-10-07 10:42:34 -0400 | [diff] [blame] | 1 | package openstack |
| 2 | |
| 3 | import ( |
Krzysztof Szukiełojć | 3f41d08 | 2017-05-07 14:43:06 +0200 | [diff] [blame] | 4 | "gerrit.mcp.mirantis.net/debian/gophercloud.git" |
Krzysztof Szukiełojć | 24a29ce | 2017-05-07 14:24:02 +0200 | [diff] [blame] | 5 | 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 Wilson | 5c0161c | 2014-10-07 10:42:34 -0400 | [diff] [blame] | 7 | ) |
| 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. |
| 15 | func 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 Wilson | 5c0161c | 2014-10-07 10:42:34 -0400 | [diff] [blame] | 29 | if len(endpoints) > 1 { |
Jon Perritt | 376dfce | 2016-02-28 23:39:09 -0600 | [diff] [blame] | 30 | err := &ErrMultipleMatchingEndpointsV2{} |
| 31 | err.Endpoints = endpoints |
Jon Perritt | 376dfce | 2016-02-28 23:39:09 -0600 | [diff] [blame] | 32 | return "", err |
Ash Wilson | 5c0161c | 2014-10-07 10:42:34 -0400 | [diff] [blame] | 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: |
Jon Perritt | 376dfce | 2016-02-28 23:39:09 -0600 | [diff] [blame] | 45 | err := &ErrInvalidAvailabilityProvided{} |
Jon Perritt | 376dfce | 2016-02-28 23:39:09 -0600 | [diff] [blame] | 46 | err.Argument = "Availability" |
| 47 | err.Value = opts.Availability |
| 48 | return "", err |
Ash Wilson | 5c0161c | 2014-10-07 10:42:34 -0400 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
Ash Wilson | fc1af5a | 2014-10-08 09:10:41 -0400 | [diff] [blame] | 52 | // Report an error if there were no matching endpoints. |
Jon Perritt | 376dfce | 2016-02-28 23:39:09 -0600 | [diff] [blame] | 53 | err := &gophercloud.ErrEndpointNotFound{} |
Jon Perritt | 376dfce | 2016-02-28 23:39:09 -0600 | [diff] [blame] | 54 | return "", err |
Ash Wilson | 5c0161c | 2014-10-07 10:42:34 -0400 | [diff] [blame] | 55 | } |
Ash Wilson | 130a6e2 | 2014-10-07 10:48:17 -0400 | [diff] [blame] | 56 | |
Guillaume Giamarchi | b2663b2 | 2015-04-01 01:23:29 +0200 | [diff] [blame] | 57 | // 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 Wilson | 130a6e2 | 2014-10-07 10:48:17 -0400 | [diff] [blame] | 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. |
Guillaume Giamarchi | b2663b2 | 2015-04-01 01:23:29 +0200 | [diff] [blame] | 63 | func 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 Perritt | 376dfce | 2016-02-28 23:39:09 -0600 | [diff] [blame] | 73 | err := &ErrInvalidAvailabilityProvided{} |
Jon Perritt | 376dfce | 2016-02-28 23:39:09 -0600 | [diff] [blame] | 74 | err.Argument = "Availability" |
| 75 | err.Value = opts.Availability |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 76 | return "", err |
Guillaume Giamarchi | b2663b2 | 2015-04-01 01:23:29 +0200 | [diff] [blame] | 77 | } |
| 78 | if (opts.Availability == gophercloud.Availability(endpoint.Interface)) && |
| 79 | (opts.Region == "" || endpoint.Region == opts.Region) { |
| 80 | endpoints = append(endpoints, endpoint) |
| 81 | } |
Ash Wilson | 130a6e2 | 2014-10-07 10:48:17 -0400 | [diff] [blame] | 82 | } |
| 83 | } |
Ash Wilson | 130a6e2 | 2014-10-07 10:48:17 -0400 | [diff] [blame] | 84 | } |
| 85 | |
Guillaume Giamarchi | b2663b2 | 2015-04-01 01:23:29 +0200 | [diff] [blame] | 86 | // Report an error if the options were ambiguous. |
Ash Wilson | 130a6e2 | 2014-10-07 10:48:17 -0400 | [diff] [blame] | 87 | if len(endpoints) > 1 { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 88 | return "", ErrMultipleMatchingEndpointsV3{Endpoints: endpoints} |
Ash Wilson | 130a6e2 | 2014-10-07 10:48:17 -0400 | [diff] [blame] | 89 | } |
Ash Wilson | 130a6e2 | 2014-10-07 10:48:17 -0400 | [diff] [blame] | 90 | |
Guillaume Giamarchi | b2663b2 | 2015-04-01 01:23:29 +0200 | [diff] [blame] | 91 | // 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 Perritt | 376dfce | 2016-02-28 23:39:09 -0600 | [diff] [blame] | 97 | err := &gophercloud.ErrEndpointNotFound{} |
Jon Perritt | 376dfce | 2016-02-28 23:39:09 -0600 | [diff] [blame] | 98 | return "", err |
Ash Wilson | 130a6e2 | 2014-10-07 10:48:17 -0400 | [diff] [blame] | 99 | } |