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" |
| 8 | ) |
| 9 | |
| 10 | // V2EndpointURL discovers the endpoint URL for a specific service from a ServiceCatalog acquired |
| 11 | // during the v2 identity service. The specified EndpointOpts are used to identify a unique, |
| 12 | // unambiguous endpoint to return. It's an error both when multiple endpoints match the provided |
| 13 | // criteria and when none do. The minimum that can be specified is a Type, but you will also often |
| 14 | // need to specify a Name and/or a Region depending on what's available on your OpenStack |
| 15 | // deployment. |
| 16 | func V2EndpointURL(catalog *tokens2.ServiceCatalog, opts gophercloud.EndpointOpts) (string, error) { |
| 17 | // Extract Endpoints from the catalog entries that match the requested Type, Name if provided, and Region if provided. |
| 18 | var endpoints = make([]tokens2.Endpoint, 0, 1) |
| 19 | for _, entry := range catalog.Entries { |
| 20 | if (entry.Type == opts.Type) && (opts.Name == "" || entry.Name == opts.Name) { |
| 21 | for _, endpoint := range entry.Endpoints { |
| 22 | if opts.Region == "" || endpoint.Region == opts.Region { |
| 23 | endpoints = append(endpoints, endpoint) |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | // Report an error if the options were ambiguous. |
| 30 | if len(endpoints) == 0 { |
| 31 | return "", gophercloud.ErrEndpointNotFound |
| 32 | } |
| 33 | if len(endpoints) > 1 { |
| 34 | return "", fmt.Errorf("Discovered %d matching endpoints: %#v", len(endpoints), endpoints) |
| 35 | } |
| 36 | |
| 37 | // Extract the appropriate URL from the matching Endpoint. |
| 38 | for _, endpoint := range endpoints { |
| 39 | switch opts.Availability { |
| 40 | case gophercloud.AvailabilityPublic: |
| 41 | return gophercloud.NormalizeURL(endpoint.PublicURL), nil |
| 42 | case gophercloud.AvailabilityInternal: |
| 43 | return gophercloud.NormalizeURL(endpoint.InternalURL), nil |
| 44 | case gophercloud.AvailabilityAdmin: |
| 45 | return gophercloud.NormalizeURL(endpoint.AdminURL), nil |
| 46 | default: |
| 47 | return "", fmt.Errorf("Unexpected availability in endpoint query: %s", opts.Availability) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | return "", gophercloud.ErrEndpointNotFound |
| 52 | } |