blob: 326f65308fd631296918bf4d4b6be9e04f762d6e [file] [log] [blame]
package gophercloud
import (
"strings"
)
// ApiCriteria provides one or more criteria for the SDK to look for appropriate endpoints.
// Fields left unspecified or otherwise set to their zero-values are assumed to not be
// relevant, and do not participate in the endpoint search.
type ApiCriteria struct {
// Name specifies the desired service catalog entry name.
Name string
// Region specifies the desired endpoint region.
Region string
// VersionId specifies the desired version of the endpoint.
// Note that this field is matched exactly, and is (at present)
// opaque to Gophercloud. Thus, requesting a version 2
// endpoint will _not_ match a version 3 endpoint.
VersionId string
// The UrlChoice field inidicates whether or not gophercloud
// should use the public or internal endpoint URL if a
// candidate endpoint is found.
UrlChoice int
}
// The choices available for UrlChoice. See the ApiCriteria structure for details.
const (
PublicURL = iota
InternalURL
)
// Given a set of criteria to match on, locate the first candidate endpoint
// in the provided service catalog.
//
// If nothing found, the result will be a zero-valued EntryEndpoint (all URLs
// set to "").
func FindFirstEndpointByCriteria(entries []CatalogEntry, ac ApiCriteria) EntryEndpoint {
rgn := strings.ToUpper(ac.Region)
for _, entry := range entries {
if (ac.Name != "") && (ac.Name != entry.Name) {
continue
}
for _, endpoint := range entry.Endpoints {
if (ac.Region != "") && (rgn != strings.ToUpper(endpoint.Region)) {
continue
}
if (ac.VersionId != "") && (ac.VersionId != endpoint.VersionId) {
continue
}
return endpoint
}
}
return EntryEndpoint{}
}