diff --git a/gen/endpoints/endpoints.go b/gen/endpoints/endpoints.go index 3fd5eb6b1f3..01ab7b2d8b7 100644 --- a/gen/endpoints/endpoints.go +++ b/gen/endpoints/endpoints.go @@ -10,6 +10,10 @@ import ( // Lookup returns the endpoint for the given service in the given region plus // any overrides for the service name and region. func Lookup(service, region string) (uri, newService, newRegion string) { + if override := findOverride(service, region); override != nil { + return override.uri, override.service, override.region + } + switch service { case "cloudfront": @@ -135,9 +139,40 @@ func Lookup(service, region string) (uri, newService, newRegion string) { panic("unknown endpoint for " + service + " in " + region) } +// AddOverride overrides the endpoint for a specific service, using either an +// existing region name or a fake one (e.g. "test-1"). +// +// This allows developers to use local mock AWS services when they're +// writing tests for their Go code that uses aws-go: +// +// endpoints.AddOverride("EC2", "test-1", "http://localhost:3000") +// // This EC2 client uses the override as service endpoint. +// cli := ec2.New(credentials, "test-1", nil) +func AddOverride(service, region, uri string) { + overrides = append(overrides, override{service, region, uri}) +} + func format(uri, service, region string) string { uri = strings.Replace(uri, "{scheme}", "https", -1) uri = strings.Replace(uri, "{service}", service, -1) uri = strings.Replace(uri, "{region}", region, -1) return uri } + +func findOverride(service, region string) *override { + for _, override := range overrides { + if strings.ToUpper(override.service) == strings.ToUpper(service) && + override.region == region { + return &override + } + } + return nil +} + +type override struct { + service string + region string + uri string +} + +var overrides []override diff --git a/model/endpoints.go b/model/endpoints.go index 5715633176e..ed6f1bee46b 100644 --- a/model/endpoints.go +++ b/model/endpoints.go @@ -134,6 +134,10 @@ import ( // Lookup returns the endpoint for the given service in the given region plus // any overrides for the service name and region. func Lookup(service, region string) (uri, newService, newRegion string) { + if override := findOverride(service, region); override != nil { + return override.uri, override.service, override.region + } + switch service { {{ range $name, $endpoints := . }} {{ if ne $name "_default" }} @@ -158,10 +162,42 @@ func Lookup(service, region string) (uri, newService, newRegion string) { panic("unknown endpoint for " + service + " in " + region) } + +// AddOverride overrides the endpoint for a specific service, using either an +// existing region name or a fake one (e.g. "test-1"). +// +// This allows developers to use local mock AWS services when they're +// writing tests for their Go code that uses aws-go: +// +// endpoints.AddOverride("EC2", "test-1", "http://localhost:3000") +// // This EC2 client uses the override as service endpoint. +// cli := ec2.New(credentials, "test-1", nil) +func AddOverride(service, region, uri string) { + overrides = append(overrides, override{service, region, uri}) +} + func format(uri, service, region string) string { uri = strings.Replace(uri, "{scheme}", "https", -1) uri = strings.Replace(uri, "{service}", service, -1) uri = strings.Replace(uri, "{region}", region, -1) return uri } + +func findOverride(service, region string) *override { + for _, override := range overrides { + if strings.ToUpper(override.service) == strings.ToUpper(service) && + override.region == region { + return &override + } + } + return nil +} + +type override struct { + service string + region string + uri string +} + +var overrides []override `