blob: e6cf4a00eaeb59d1bfce5ddd0a5c4f2e7095488e [file] [log] [blame]
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -07001package gophercloud
2
3import (
Samuel A. Falvo IIb9670762014-01-10 14:26:39 -08004 "os"
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -07005 "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 IIb9670762014-01-10 14:26:39 -080011//
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 II2e2b8772013-07-04 15:40:15 -070027type ApiCriteria struct {
Samuel A. Falvo IIb9670762014-01-10 14:26:39 -080028 Name string
29 Type string
30 Region string
31 VersionId string
32 UrlChoice int
33 IgnoreEnvVars bool
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -070034}
35
36// The choices available for UrlChoice. See the ApiCriteria structure for details.
37const (
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 "").
47func FindFirstEndpointByCriteria(entries []CatalogEntry, ac ApiCriteria) EntryEndpoint {
48 rgn := strings.ToUpper(ac.Region)
Samuel A. Falvo IIb9670762014-01-10 14:26:39 -080049 if (rgn == "") && !ac.IgnoreEnvVars {
50 rgn = os.Getenv("OS_REGION_NAME")
51 }
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -070052
53 for _, entry := range entries {
54 if (ac.Name != "") && (ac.Name != entry.Name) {
55 continue
56 }
57
Justin Santa Barbara017b2de2013-08-31 18:33:19 -070058 if (ac.Type != "") && (ac.Type != entry.Type) {
59 continue
60 }
61
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -070062 for _, endpoint := range entry.Endpoints {
Samuel A. Falvo IIb9670762014-01-10 14:26:39 -080063 if (rgn != "") && (rgn != strings.ToUpper(endpoint.Region)) {
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -070064 continue
65 }
66
67 if (ac.VersionId != "") && (ac.VersionId != endpoint.VersionId) {
68 continue
69 }
70
71 return endpoint
72 }
73 }
74 return EntryEndpoint{}
75}