2016-01-29 20:53:56 +01:00
|
|
|
// Package endpoints validates regional endpoints for services.
|
|
|
|
package endpoints
|
|
|
|
|
2016-10-17 23:21:08 +02:00
|
|
|
//go:generate go run -tags codegen ../model/cli/gen-endpoints/main.go endpoints.json endpoints_map.go
|
2016-01-29 20:53:56 +01:00
|
|
|
//go:generate gofmt -s -w endpoints_map.go
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NormalizeEndpoint takes and endpoint and service API information to return a
|
|
|
|
// normalized endpoint and signing region. If the endpoint is not an empty string
|
|
|
|
// the service name and region will be used to look up the service's API endpoint.
|
|
|
|
// If the endpoint is provided the scheme will be added if it is not present.
|
2016-08-11 19:01:51 +02:00
|
|
|
func NormalizeEndpoint(endpoint, serviceName, region string, disableSSL, useDualStack bool) (normEndpoint, signingRegion string) {
|
2016-01-29 20:53:56 +01:00
|
|
|
if endpoint == "" {
|
2016-08-11 19:01:51 +02:00
|
|
|
return EndpointForRegion(serviceName, region, disableSSL, useDualStack)
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return AddScheme(endpoint, disableSSL), ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// EndpointForRegion returns an endpoint and its signing region for a service and region.
|
|
|
|
// if the service and region pair are not found endpoint and signingRegion will be empty.
|
2016-08-11 19:01:51 +02:00
|
|
|
func EndpointForRegion(svcName, region string, disableSSL, useDualStack bool) (endpoint, signingRegion string) {
|
|
|
|
dualStackField := ""
|
|
|
|
if useDualStack {
|
|
|
|
dualStackField = "/dualstack"
|
|
|
|
}
|
|
|
|
|
2016-01-29 20:53:56 +01:00
|
|
|
derivedKeys := []string{
|
2016-08-11 19:01:51 +02:00
|
|
|
region + "/" + svcName + dualStackField,
|
|
|
|
region + "/*" + dualStackField,
|
|
|
|
"*/" + svcName + dualStackField,
|
|
|
|
"*/*" + dualStackField,
|
2016-01-29 20:53:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range derivedKeys {
|
|
|
|
if val, ok := endpointsMap.Endpoints[key]; ok {
|
|
|
|
ep := val.Endpoint
|
|
|
|
ep = strings.Replace(ep, "{region}", region, -1)
|
|
|
|
ep = strings.Replace(ep, "{service}", svcName, -1)
|
|
|
|
|
|
|
|
endpoint = ep
|
|
|
|
signingRegion = val.SigningRegion
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return AddScheme(endpoint, disableSSL), signingRegion
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regular expression to determine if the endpoint string is prefixed with a scheme.
|
|
|
|
var schemeRE = regexp.MustCompile("^([^:]+)://")
|
|
|
|
|
|
|
|
// AddScheme adds the HTTP or HTTPS schemes to a endpoint URL if there is no
|
|
|
|
// scheme. If disableSSL is true HTTP will be added instead of the default HTTPS.
|
|
|
|
func AddScheme(endpoint string, disableSSL bool) string {
|
|
|
|
if endpoint != "" && !schemeRE.MatchString(endpoint) {
|
|
|
|
scheme := "https"
|
|
|
|
if disableSSL {
|
|
|
|
scheme = "http"
|
|
|
|
}
|
|
|
|
endpoint = fmt.Sprintf("%s://%s", scheme, endpoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
return endpoint
|
|
|
|
}
|