Ash Wilson | 5c0161c | 2014-10-07 10:42:34 -0400 | [diff] [blame] | 1 | package openstack |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 6 | "github.com/rackspace/gophercloud" |
| 7 | tokens2 "github.com/rackspace/gophercloud/openstack/identity/v2/tokens" |
Guillaume Giamarchi | b2663b2 | 2015-04-01 01:23:29 +0200 | [diff] [blame] | 8 | tokens3 "github.com/rackspace/gophercloud/openstack/identity/v3/tokens" |
Ash Wilson | 5c0161c | 2014-10-07 10:42:34 -0400 | [diff] [blame] | 9 | ) |
| 10 | |
| 11 | // V2EndpointURL discovers the endpoint URL for a specific service from a ServiceCatalog acquired |
| 12 | // during the v2 identity service. The specified EndpointOpts are used to identify a unique, |
| 13 | // unambiguous endpoint to return. It's an error both when multiple endpoints match the provided |
| 14 | // criteria and when none do. The minimum that can be specified is a Type, but you will also often |
| 15 | // need to specify a Name and/or a Region depending on what's available on your OpenStack |
| 16 | // deployment. |
| 17 | func V2EndpointURL(catalog *tokens2.ServiceCatalog, opts gophercloud.EndpointOpts) (string, error) { |
| 18 | // Extract Endpoints from the catalog entries that match the requested Type, Name if provided, and Region if provided. |
| 19 | var endpoints = make([]tokens2.Endpoint, 0, 1) |
| 20 | for _, entry := range catalog.Entries { |
| 21 | if (entry.Type == opts.Type) && (opts.Name == "" || entry.Name == opts.Name) { |
| 22 | for _, endpoint := range entry.Endpoints { |
| 23 | if opts.Region == "" || endpoint.Region == opts.Region { |
| 24 | endpoints = append(endpoints, endpoint) |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // Report an error if the options were ambiguous. |
Ash Wilson | 5c0161c | 2014-10-07 10:42:34 -0400 | [diff] [blame] | 31 | if len(endpoints) > 1 { |
| 32 | return "", fmt.Errorf("Discovered %d matching endpoints: %#v", len(endpoints), endpoints) |
| 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: |
| 45 | return "", fmt.Errorf("Unexpected availability in endpoint query: %s", opts.Availability) |
| 46 | } |
| 47 | } |
| 48 | |
Ash Wilson | fc1af5a | 2014-10-08 09:10:41 -0400 | [diff] [blame] | 49 | // Report an error if there were no matching endpoints. |
Ash Wilson | 5c0161c | 2014-10-07 10:42:34 -0400 | [diff] [blame] | 50 | return "", gophercloud.ErrEndpointNotFound |
| 51 | } |
Ash Wilson | 130a6e2 | 2014-10-07 10:48:17 -0400 | [diff] [blame] | 52 | |
Guillaume Giamarchi | b2663b2 | 2015-04-01 01:23:29 +0200 | [diff] [blame] | 53 | // V3EndpointURL discovers the endpoint URL for a specific service from a Catalog acquired |
| 54 | // 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] | 55 | // unambiguous endpoint to return. It's an error both when multiple endpoints match the provided |
| 56 | // criteria and when none do. The minimum that can be specified is a Type, but you will also often |
| 57 | // need to specify a Name and/or a Region depending on what's available on your OpenStack |
| 58 | // deployment. |
Guillaume Giamarchi | b2663b2 | 2015-04-01 01:23:29 +0200 | [diff] [blame] | 59 | func V3EndpointURL(catalog *tokens3.ServiceCatalog, opts gophercloud.EndpointOpts) (string, error) { |
| 60 | // Extract Endpoints from the catalog entries that match the requested Type, Interface, |
| 61 | // Name if provided, and Region if provided. |
| 62 | var endpoints = make([]tokens3.Endpoint, 0, 1) |
| 63 | for _, entry := range catalog.Entries { |
| 64 | if (entry.Type == opts.Type) && (opts.Name == "" || entry.Name == opts.Name) { |
| 65 | for _, endpoint := range entry.Endpoints { |
| 66 | if opts.Availability != gophercloud.AvailabilityAdmin && |
| 67 | opts.Availability != gophercloud.AvailabilityPublic && |
| 68 | opts.Availability != gophercloud.AvailabilityInternal { |
| 69 | return "", fmt.Errorf("Unexpected availability in endpoint query: %s", opts.Availability) |
| 70 | } |
| 71 | if (opts.Availability == gophercloud.Availability(endpoint.Interface)) && |
| 72 | (opts.Region == "" || endpoint.Region == opts.Region) { |
| 73 | endpoints = append(endpoints, endpoint) |
| 74 | } |
Ash Wilson | 130a6e2 | 2014-10-07 10:48:17 -0400 | [diff] [blame] | 75 | } |
| 76 | } |
Ash Wilson | 130a6e2 | 2014-10-07 10:48:17 -0400 | [diff] [blame] | 77 | } |
| 78 | |
Guillaume Giamarchi | b2663b2 | 2015-04-01 01:23:29 +0200 | [diff] [blame] | 79 | // Report an error if the options were ambiguous. |
Ash Wilson | 130a6e2 | 2014-10-07 10:48:17 -0400 | [diff] [blame] | 80 | if len(endpoints) > 1 { |
| 81 | return "", fmt.Errorf("Discovered %d matching endpoints: %#v", len(endpoints), endpoints) |
| 82 | } |
Ash Wilson | 130a6e2 | 2014-10-07 10:48:17 -0400 | [diff] [blame] | 83 | |
Guillaume Giamarchi | b2663b2 | 2015-04-01 01:23:29 +0200 | [diff] [blame] | 84 | // Extract the URL from the matching Endpoint. |
| 85 | for _, endpoint := range endpoints { |
| 86 | return gophercloud.NormalizeURL(endpoint.URL), nil |
| 87 | } |
| 88 | |
| 89 | // Report an error if there were no matching endpoints. |
| 90 | return "", gophercloud.ErrEndpointNotFound |
Ash Wilson | 130a6e2 | 2014-10-07 10:48:17 -0400 | [diff] [blame] | 91 | } |