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 | |
Justin Santa Barbara | 017b2de | 2013-08-31 18:33:19 -0700 | [diff] [blame] | 14 | // Type specifies the desired service catalog entry type. |
| 15 | Type string |
| 16 | |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 17 | // Region specifies the desired endpoint region. |
| 18 | Region string |
| 19 | |
| 20 | // VersionId specifies the desired version of the endpoint. |
| 21 | // Note that this field is matched exactly, and is (at present) |
| 22 | // opaque to Gophercloud. Thus, requesting a version 2 |
| 23 | // endpoint will _not_ match a version 3 endpoint. |
| 24 | VersionId string |
| 25 | |
| 26 | // The UrlChoice field inidicates whether or not gophercloud |
| 27 | // should use the public or internal endpoint URL if a |
| 28 | // candidate endpoint is found. |
| 29 | UrlChoice int |
| 30 | } |
| 31 | |
| 32 | // The choices available for UrlChoice. See the ApiCriteria structure for details. |
| 33 | const ( |
| 34 | PublicURL = iota |
| 35 | InternalURL |
| 36 | ) |
| 37 | |
| 38 | // Given a set of criteria to match on, locate the first candidate endpoint |
| 39 | // in the provided service catalog. |
| 40 | // |
| 41 | // If nothing found, the result will be a zero-valued EntryEndpoint (all URLs |
| 42 | // set to ""). |
| 43 | func FindFirstEndpointByCriteria(entries []CatalogEntry, ac ApiCriteria) EntryEndpoint { |
| 44 | rgn := strings.ToUpper(ac.Region) |
| 45 | |
| 46 | for _, entry := range entries { |
| 47 | if (ac.Name != "") && (ac.Name != entry.Name) { |
| 48 | continue |
| 49 | } |
| 50 | |
Justin Santa Barbara | 017b2de | 2013-08-31 18:33:19 -0700 | [diff] [blame] | 51 | if (ac.Type != "") && (ac.Type != entry.Type) { |
| 52 | continue |
| 53 | } |
| 54 | |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 55 | for _, endpoint := range entry.Endpoints { |
| 56 | if (ac.Region != "") && (rgn != strings.ToUpper(endpoint.Region)) { |
| 57 | continue |
| 58 | } |
| 59 | |
| 60 | if (ac.VersionId != "") && (ac.VersionId != endpoint.VersionId) { |
| 61 | continue |
| 62 | } |
| 63 | |
| 64 | return endpoint |
| 65 | } |
| 66 | } |
| 67 | return EntryEndpoint{} |
| 68 | } |