Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
| 3 | import ( |
Samuel A. Falvo II | b967076 | 2014-01-10 14:26:39 -0800 | [diff] [blame] | 4 | "os" |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 5 | "strings" |
| 6 | ) |
| 7 | |
| 8 | // ApiCriteria provides one or more criteria for the SDK to look for appropriate endpoints. |
| 9 | // Fields left unspecified or otherwise set to their zero-values are assumed to not be |
| 10 | // relevant, and do not participate in the endpoint search. |
Samuel A. Falvo II | b967076 | 2014-01-10 14:26:39 -0800 | [diff] [blame] | 11 | // |
| 12 | // Name specifies the desired service catalog entry name. |
| 13 | // Type specifies the desired service catalog entry type. |
| 14 | // Region specifies the desired endpoint region. |
| 15 | // If unset, Gophercloud will try to use the region set in the |
| 16 | // OS_REGION_NAME environment variable. If that's not set, |
| 17 | // region comparison will not occur. If OS_REGION_NAME is set |
| 18 | // and IgnoreEnvVars is also set, OS_REGION_NAME will be ignored. |
| 19 | // VersionId specifies the desired version of the endpoint. |
| 20 | // Note that this field is matched exactly, and is (at present) |
| 21 | // opaque to Gophercloud. Thus, requesting a version 2 |
| 22 | // endpoint will _not_ match a version 3 endpoint. |
| 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 | // IgnoreEnvVars instructs Gophercloud to ignore helpful environment variables. |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 27 | type ApiCriteria struct { |
Samuel A. Falvo II | b967076 | 2014-01-10 14:26:39 -0800 | [diff] [blame] | 28 | Name string |
| 29 | Type string |
| 30 | Region string |
| 31 | VersionId string |
| 32 | UrlChoice int |
| 33 | IgnoreEnvVars bool |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | // The choices available for UrlChoice. See the ApiCriteria structure for details. |
| 37 | const ( |
| 38 | PublicURL = iota |
| 39 | InternalURL |
| 40 | ) |
| 41 | |
| 42 | // Given a set of criteria to match on, locate the first candidate endpoint |
| 43 | // in the provided service catalog. |
| 44 | // |
| 45 | // If nothing found, the result will be a zero-valued EntryEndpoint (all URLs |
| 46 | // set to ""). |
| 47 | func FindFirstEndpointByCriteria(entries []CatalogEntry, ac ApiCriteria) EntryEndpoint { |
| 48 | rgn := strings.ToUpper(ac.Region) |
Samuel A. Falvo II | b967076 | 2014-01-10 14:26:39 -0800 | [diff] [blame] | 49 | if (rgn == "") && !ac.IgnoreEnvVars { |
| 50 | rgn = os.Getenv("OS_REGION_NAME") |
| 51 | } |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 52 | |
| 53 | for _, entry := range entries { |
| 54 | if (ac.Name != "") && (ac.Name != entry.Name) { |
| 55 | continue |
| 56 | } |
| 57 | |
Justin Santa Barbara | 017b2de | 2013-08-31 18:33:19 -0700 | [diff] [blame] | 58 | if (ac.Type != "") && (ac.Type != entry.Type) { |
| 59 | continue |
| 60 | } |
| 61 | |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 62 | for _, endpoint := range entry.Endpoints { |
Samuel A. Falvo II | b967076 | 2014-01-10 14:26:39 -0800 | [diff] [blame] | 63 | if (rgn != "") && (rgn != strings.ToUpper(endpoint.Region)) { |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 64 | continue |
| 65 | } |
| 66 | |
| 67 | if (ac.VersionId != "") && (ac.VersionId != endpoint.VersionId) { |
| 68 | continue |
| 69 | } |
| 70 | |
| 71 | return endpoint |
| 72 | } |
| 73 | } |
| 74 | return EntryEndpoint{} |
| 75 | } |