Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | ) |
| 6 | |
| 7 | // ApiCriteria provides one or more criteria for the SDK to look for appropriate endpoints. |
| 8 | // Fields left unspecified or otherwise set to their zero-values are assumed to not be |
| 9 | // relevant, and do not participate in the endpoint search. |
| 10 | type ApiCriteria struct { |
| 11 | // Name specifies the desired service catalog entry name. |
| 12 | Name string |
| 13 | |
| 14 | // Region specifies the desired endpoint region. |
| 15 | Region string |
| 16 | |
| 17 | // VersionId specifies the desired version of the endpoint. |
| 18 | // Note that this field is matched exactly, and is (at present) |
| 19 | // opaque to Gophercloud. Thus, requesting a version 2 |
| 20 | // endpoint will _not_ match a version 3 endpoint. |
| 21 | VersionId string |
| 22 | |
| 23 | // The UrlChoice field inidicates whether or not gophercloud |
| 24 | // should use the public or internal endpoint URL if a |
| 25 | // candidate endpoint is found. |
| 26 | UrlChoice int |
| 27 | } |
| 28 | |
| 29 | // The choices available for UrlChoice. See the ApiCriteria structure for details. |
| 30 | const ( |
| 31 | PublicURL = iota |
| 32 | InternalURL |
| 33 | ) |
| 34 | |
| 35 | // Given a set of criteria to match on, locate the first candidate endpoint |
| 36 | // in the provided service catalog. |
| 37 | // |
| 38 | // If nothing found, the result will be a zero-valued EntryEndpoint (all URLs |
| 39 | // set to ""). |
| 40 | func FindFirstEndpointByCriteria(entries []CatalogEntry, ac ApiCriteria) EntryEndpoint { |
| 41 | rgn := strings.ToUpper(ac.Region) |
| 42 | |
| 43 | for _, entry := range entries { |
| 44 | if (ac.Name != "") && (ac.Name != entry.Name) { |
| 45 | continue |
| 46 | } |
| 47 | |
| 48 | for _, endpoint := range entry.Endpoints { |
| 49 | if (ac.Region != "") && (rgn != strings.ToUpper(endpoint.Region)) { |
| 50 | continue |
| 51 | } |
| 52 | |
| 53 | if (ac.VersionId != "") && (ac.VersionId != endpoint.VersionId) { |
| 54 | continue |
| 55 | } |
| 56 | |
| 57 | return endpoint |
| 58 | } |
| 59 | } |
| 60 | return EntryEndpoint{} |
| 61 | } |