| Ash Wilson | 5c0161c | 2014-10-07 10:42:34 -0400 | [diff] [blame] | 1 | package openstack | 
|  | 2 |  | 
|  | 3 | import ( | 
| Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "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 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 | 
|  | 32 | err.Function = "openstack.V2EndpointURL" | 
|  | 33 | return "", err | 
| Ash Wilson | 5c0161c | 2014-10-07 10:42:34 -0400 | [diff] [blame] | 34 | } | 
|  | 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 Perritt | 376dfce | 2016-02-28 23:39:09 -0600 | [diff] [blame^] | 46 | err := &ErrInvalidAvailabilityProvided{} | 
|  | 47 | err.Function = "openstack.V2EndpointURL" | 
|  | 48 | err.Argument = "Availability" | 
|  | 49 | err.Value = opts.Availability | 
|  | 50 | return "", err | 
| Ash Wilson | 5c0161c | 2014-10-07 10:42:34 -0400 | [diff] [blame] | 51 | } | 
|  | 52 | } | 
|  | 53 |  | 
| Ash Wilson | fc1af5a | 2014-10-08 09:10:41 -0400 | [diff] [blame] | 54 | // Report an error if there were no matching endpoints. | 
| Jon Perritt | 376dfce | 2016-02-28 23:39:09 -0600 | [diff] [blame^] | 55 | err := &gophercloud.ErrEndpointNotFound{} | 
|  | 56 | err.Function = "openstack.V2EndpointURL" | 
|  | 57 | return "", err | 
| Ash Wilson | 5c0161c | 2014-10-07 10:42:34 -0400 | [diff] [blame] | 58 | } | 
| Ash Wilson | 130a6e2 | 2014-10-07 10:48:17 -0400 | [diff] [blame] | 59 |  | 
| Guillaume Giamarchi | b2663b2 | 2015-04-01 01:23:29 +0200 | [diff] [blame] | 60 | // 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 Wilson | 130a6e2 | 2014-10-07 10:48:17 -0400 | [diff] [blame] | 62 | // 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 Giamarchi | b2663b2 | 2015-04-01 01:23:29 +0200 | [diff] [blame] | 66 | func 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 Perritt | 376dfce | 2016-02-28 23:39:09 -0600 | [diff] [blame^] | 76 | err := &ErrInvalidAvailabilityProvided{} | 
|  | 77 | err.Function = "openstack.V3EndpointURL" | 
|  | 78 | err.Argument = "Availability" | 
|  | 79 | err.Value = opts.Availability | 
| Guillaume Giamarchi | b2663b2 | 2015-04-01 01:23:29 +0200 | [diff] [blame] | 80 | } | 
|  | 81 | if (opts.Availability == gophercloud.Availability(endpoint.Interface)) && | 
|  | 82 | (opts.Region == "" || endpoint.Region == opts.Region) { | 
|  | 83 | endpoints = append(endpoints, endpoint) | 
|  | 84 | } | 
| Ash Wilson | 130a6e2 | 2014-10-07 10:48:17 -0400 | [diff] [blame] | 85 | } | 
|  | 86 | } | 
| Ash Wilson | 130a6e2 | 2014-10-07 10:48:17 -0400 | [diff] [blame] | 87 | } | 
|  | 88 |  | 
| Guillaume Giamarchi | b2663b2 | 2015-04-01 01:23:29 +0200 | [diff] [blame] | 89 | // Report an error if the options were ambiguous. | 
| Ash Wilson | 130a6e2 | 2014-10-07 10:48:17 -0400 | [diff] [blame] | 90 | if len(endpoints) > 1 { | 
| Jon Perritt | 376dfce | 2016-02-28 23:39:09 -0600 | [diff] [blame^] | 91 | err := &ErrMultipleMatchingEndpointsV3{} | 
|  | 92 | err.Endpoints = endpoints | 
|  | 93 | err.Function = "openstack.V3EndpointURL" | 
|  | 94 | return "", err | 
| Ash Wilson | 130a6e2 | 2014-10-07 10:48:17 -0400 | [diff] [blame] | 95 | } | 
| Ash Wilson | 130a6e2 | 2014-10-07 10:48:17 -0400 | [diff] [blame] | 96 |  | 
| Guillaume Giamarchi | b2663b2 | 2015-04-01 01:23:29 +0200 | [diff] [blame] | 97 | // 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 Perritt | 376dfce | 2016-02-28 23:39:09 -0600 | [diff] [blame^] | 103 | err := &gophercloud.ErrEndpointNotFound{} | 
|  | 104 | err.Function = "openstack.V3EndpointURL" | 
|  | 105 | return "", err | 
| Ash Wilson | 130a6e2 | 2014-10-07 10:48:17 -0400 | [diff] [blame] | 106 | } |