Skip to content

Commit

Permalink
SDK DefaultsMode Configuration Implementation (#1553)
Browse files Browse the repository at this point in the history
* SDK Default Configuration Implementation
* Regeneration
  • Loading branch information
skmcgrail authored Jan 14, 2022
1 parent 07e5d3e commit c1b62ab
Show file tree
Hide file tree
Showing 1,005 changed files with 30,195 additions and 3,684 deletions.
10 changes: 10 additions & 0 deletions aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ type Config struct {
// See the ClientLogMode type documentation for the complete set of logging modes and available
// configuration.
ClientLogMode ClientLogMode

// The configured DefaultsMode. If not specified, service clients will default to legacy.
//
// Supported modes are: auto, cross-region, in-region, legacy, mobile, standard
DefaultsMode DefaultsMode

// The RuntimeEnvironment configuration, only populated if the DefaultsMode is set to
// AutoDefaultsMode and is initialized by `config.LoadDefaultConfig`. You should not
// populate this structure programmatically, or rely on the values here within your applications.
RuntimeEnvironment RuntimeEnvironment
}

// NewConfig returns a new Config pointer that can be chained with builder
Expand Down
38 changes: 38 additions & 0 deletions aws/defaults/auto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package defaults

import (
"github.com/aws/aws-sdk-go-v2/aws"
"runtime"
"strings"
)

var getGOOS = func() string {
return runtime.GOOS
}

// ResolveDefaultsModeAuto is used to determine the effective aws.DefaultsMode when the mode
// is set to aws.AutoDefaultsMode.
func ResolveDefaultsModeAuto(region string, environment aws.RuntimeEnvironment) aws.DefaultsMode {
goos := getGOOS()
if goos == "android" || goos == "ios" {
return aws.DefaultsModeMobile
}

var currentRegion string
if len(environment.EnvironmentIdentifier) > 0 {
currentRegion = environment.Region
}

if len(currentRegion) == 0 && len(environment.EC2InstanceMetadataRegion) > 0 {
currentRegion = environment.EC2InstanceMetadataRegion
}

if len(region) > 0 && len(currentRegion) > 0 {
if strings.EqualFold(region, currentRegion) {
return aws.DefaultsModeInRegion
}
return aws.DefaultsModeCrossRegion
}

return aws.DefaultsModeStandard
}
89 changes: 89 additions & 0 deletions aws/defaults/auto_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package defaults

import (
"github.com/aws/aws-sdk-go-v2/aws"
"strconv"
"testing"
)

func TestDetermineDefaultsMode(t *testing.T) {
cases := []struct {
Region string
GOOS string
Environment aws.RuntimeEnvironment
Expected aws.DefaultsMode
}{
{
Region: "us-east-1",
GOOS: "ios",
Environment: aws.RuntimeEnvironment{
EnvironmentIdentifier: aws.ExecutionEnvironmentID("AWS_Lambda_java8"),
Region: "us-east-1",
},
Expected: aws.DefaultsModeMobile,
},
{
Region: "us-east-1",
GOOS: "android",
Environment: aws.RuntimeEnvironment{
EnvironmentIdentifier: aws.ExecutionEnvironmentID("AWS_Lambda_java8"),
Region: "us-east-1",
},
Expected: aws.DefaultsModeMobile,
},
{
Region: "us-east-1",
Environment: aws.RuntimeEnvironment{
EnvironmentIdentifier: aws.ExecutionEnvironmentID("AWS_Lambda_java8"),
Region: "us-east-1",
},
Expected: aws.DefaultsModeInRegion,
},
{
Region: "us-east-1",
Environment: aws.RuntimeEnvironment{
EnvironmentIdentifier: aws.ExecutionEnvironmentID("AWS_Lambda_java8"),
Region: "us-west-2",
},
Expected: aws.DefaultsModeCrossRegion,
},
{
Region: "us-east-1",
Environment: aws.RuntimeEnvironment{
Region: "us-east-1",
EC2InstanceMetadataRegion: "us-east-1",
},
Expected: aws.DefaultsModeInRegion,
},
{
Region: "us-east-1",
Environment: aws.RuntimeEnvironment{
EC2InstanceMetadataRegion: "us-west-2",
},
Expected: aws.DefaultsModeCrossRegion,
},
{
Region: "us-west-2",
Environment: aws.RuntimeEnvironment{},
Expected: aws.DefaultsModeStandard,
},
}

for i, tt := range cases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
if len(tt.GOOS) > 0 {
orig := getGOOS
getGOOS = func() string {
return tt.GOOS
}
defer func() {
getGOOS = orig
}()
}
got := ResolveDefaultsModeAuto(tt.Region, tt.Environment)
if got != tt.Expected {
t.Errorf("expect %v, got %v", tt.Expected, got)
}
})
}
}
35 changes: 35 additions & 0 deletions aws/defaults/configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package defaults

import "time"

// Configuration is the set of SDK configuration options that are determined based
// on the configured DefaultsMode.
type Configuration struct {
// ConnectTimeout is the maximum amount of time a dial will wait for
// a connect to complete.
//
// See https://pkg.go.dev/net#Dialer.Timeout
ConnectTimeout *time.Duration

// TLSNegotiationTimeout specifies the maximum amount of time waiting to
// wait for a TLS handshake.
//
// See https://pkg.go.dev/net/http#Transport.TLSHandshakeTimeout
TLSNegotiationTimeout *time.Duration
}

// GetConnectTimeout returns the ConnectTimeout value, returns false if the value is not set.
func (c *Configuration) GetConnectTimeout() (time.Duration, bool) {
if c.ConnectTimeout == nil {
return 0, false
}
return *c.ConnectTimeout, true
}

// GetTLSNegotiationTimeout returns the TLSNegotiationTimeout value, returns false if the value is not set.
func (c *Configuration) GetTLSNegotiationTimeout() (time.Duration, bool) {
if c.TLSNegotiationTimeout == nil {
return 0, false
}
return *c.TLSNegotiationTimeout, true
}
46 changes: 46 additions & 0 deletions aws/defaults/defaults.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions aws/defaults/defaults_codegen_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions aws/defaults/defaults_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package defaults

import (
"github.com/aws/aws-sdk-go-v2/aws"
"strconv"
"testing"
"time"

"github.com/google/go-cmp/cmp"
)

func TestConfigV1(t *testing.T) {
cases := []struct {
Mode aws.DefaultsMode
Expected Configuration
}{
{
Mode: aws.DefaultsModeStandard,
Expected: Configuration{
ConnectTimeout: aws.Duration(2000 * time.Millisecond),
TLSNegotiationTimeout: aws.Duration(2000 * time.Millisecond),
},
},
{
Mode: aws.DefaultsModeInRegion,
Expected: Configuration{
ConnectTimeout: aws.Duration(1000 * time.Millisecond),
TLSNegotiationTimeout: aws.Duration(1000 * time.Millisecond),
},
},
{
Mode: aws.DefaultsModeCrossRegion,
Expected: Configuration{
ConnectTimeout: aws.Duration(2800 * time.Millisecond),
TLSNegotiationTimeout: aws.Duration(2800 * time.Millisecond),
},
},
{
Mode: aws.DefaultsModeMobile,
Expected: Configuration{
ConnectTimeout: aws.Duration(10000 * time.Millisecond),
TLSNegotiationTimeout: aws.Duration(11000 * time.Millisecond),
},
},
}

for i, tt := range cases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
got, err := v1TestResolver(tt.Mode)
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
if diff := cmp.Diff(tt.Expected, got); len(diff) > 0 {
t.Error(diff)
}
})
}
}
2 changes: 2 additions & 0 deletions aws/defaults/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package defaults provides recommended configuration values for AWS SDKs and CLIs.
package defaults
Loading

0 comments on commit c1b62ab

Please sign in to comment.