Skip to content

Commit

Permalink
Add endpoint overrides.
Browse files Browse the repository at this point in the history
  • Loading branch information
stripecodahale committed Jan 20, 2015
1 parent 902d3ec commit e8ad61e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
35 changes: 35 additions & 0 deletions gen/endpoints/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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
36 changes: 36 additions & 0 deletions model/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" }}
Expand All @@ -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
`

0 comments on commit e8ad61e

Please sign in to comment.