Skip to content

Commit c1b62ab

Browse files
authored
SDK DefaultsMode Configuration Implementation (#1553)
* SDK Default Configuration Implementation * Regeneration
1 parent 07e5d3e commit c1b62ab

File tree

1,005 files changed

+30195
-3684
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,005 files changed

+30195
-3684
lines changed

aws/config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ type Config struct {
8585
// See the ClientLogMode type documentation for the complete set of logging modes and available
8686
// configuration.
8787
ClientLogMode ClientLogMode
88+
89+
// The configured DefaultsMode. If not specified, service clients will default to legacy.
90+
//
91+
// Supported modes are: auto, cross-region, in-region, legacy, mobile, standard
92+
DefaultsMode DefaultsMode
93+
94+
// The RuntimeEnvironment configuration, only populated if the DefaultsMode is set to
95+
// AutoDefaultsMode and is initialized by `config.LoadDefaultConfig`. You should not
96+
// populate this structure programmatically, or rely on the values here within your applications.
97+
RuntimeEnvironment RuntimeEnvironment
8898
}
8999

90100
// NewConfig returns a new Config pointer that can be chained with builder

aws/defaults/auto.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package defaults
2+
3+
import (
4+
"github.com/aws/aws-sdk-go-v2/aws"
5+
"runtime"
6+
"strings"
7+
)
8+
9+
var getGOOS = func() string {
10+
return runtime.GOOS
11+
}
12+
13+
// ResolveDefaultsModeAuto is used to determine the effective aws.DefaultsMode when the mode
14+
// is set to aws.AutoDefaultsMode.
15+
func ResolveDefaultsModeAuto(region string, environment aws.RuntimeEnvironment) aws.DefaultsMode {
16+
goos := getGOOS()
17+
if goos == "android" || goos == "ios" {
18+
return aws.DefaultsModeMobile
19+
}
20+
21+
var currentRegion string
22+
if len(environment.EnvironmentIdentifier) > 0 {
23+
currentRegion = environment.Region
24+
}
25+
26+
if len(currentRegion) == 0 && len(environment.EC2InstanceMetadataRegion) > 0 {
27+
currentRegion = environment.EC2InstanceMetadataRegion
28+
}
29+
30+
if len(region) > 0 && len(currentRegion) > 0 {
31+
if strings.EqualFold(region, currentRegion) {
32+
return aws.DefaultsModeInRegion
33+
}
34+
return aws.DefaultsModeCrossRegion
35+
}
36+
37+
return aws.DefaultsModeStandard
38+
}

aws/defaults/auto_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package defaults
2+
3+
import (
4+
"github.com/aws/aws-sdk-go-v2/aws"
5+
"strconv"
6+
"testing"
7+
)
8+
9+
func TestDetermineDefaultsMode(t *testing.T) {
10+
cases := []struct {
11+
Region string
12+
GOOS string
13+
Environment aws.RuntimeEnvironment
14+
Expected aws.DefaultsMode
15+
}{
16+
{
17+
Region: "us-east-1",
18+
GOOS: "ios",
19+
Environment: aws.RuntimeEnvironment{
20+
EnvironmentIdentifier: aws.ExecutionEnvironmentID("AWS_Lambda_java8"),
21+
Region: "us-east-1",
22+
},
23+
Expected: aws.DefaultsModeMobile,
24+
},
25+
{
26+
Region: "us-east-1",
27+
GOOS: "android",
28+
Environment: aws.RuntimeEnvironment{
29+
EnvironmentIdentifier: aws.ExecutionEnvironmentID("AWS_Lambda_java8"),
30+
Region: "us-east-1",
31+
},
32+
Expected: aws.DefaultsModeMobile,
33+
},
34+
{
35+
Region: "us-east-1",
36+
Environment: aws.RuntimeEnvironment{
37+
EnvironmentIdentifier: aws.ExecutionEnvironmentID("AWS_Lambda_java8"),
38+
Region: "us-east-1",
39+
},
40+
Expected: aws.DefaultsModeInRegion,
41+
},
42+
{
43+
Region: "us-east-1",
44+
Environment: aws.RuntimeEnvironment{
45+
EnvironmentIdentifier: aws.ExecutionEnvironmentID("AWS_Lambda_java8"),
46+
Region: "us-west-2",
47+
},
48+
Expected: aws.DefaultsModeCrossRegion,
49+
},
50+
{
51+
Region: "us-east-1",
52+
Environment: aws.RuntimeEnvironment{
53+
Region: "us-east-1",
54+
EC2InstanceMetadataRegion: "us-east-1",
55+
},
56+
Expected: aws.DefaultsModeInRegion,
57+
},
58+
{
59+
Region: "us-east-1",
60+
Environment: aws.RuntimeEnvironment{
61+
EC2InstanceMetadataRegion: "us-west-2",
62+
},
63+
Expected: aws.DefaultsModeCrossRegion,
64+
},
65+
{
66+
Region: "us-west-2",
67+
Environment: aws.RuntimeEnvironment{},
68+
Expected: aws.DefaultsModeStandard,
69+
},
70+
}
71+
72+
for i, tt := range cases {
73+
t.Run(strconv.Itoa(i), func(t *testing.T) {
74+
if len(tt.GOOS) > 0 {
75+
orig := getGOOS
76+
getGOOS = func() string {
77+
return tt.GOOS
78+
}
79+
defer func() {
80+
getGOOS = orig
81+
}()
82+
}
83+
got := ResolveDefaultsModeAuto(tt.Region, tt.Environment)
84+
if got != tt.Expected {
85+
t.Errorf("expect %v, got %v", tt.Expected, got)
86+
}
87+
})
88+
}
89+
}

aws/defaults/configuration.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package defaults
2+
3+
import "time"
4+
5+
// Configuration is the set of SDK configuration options that are determined based
6+
// on the configured DefaultsMode.
7+
type Configuration struct {
8+
// ConnectTimeout is the maximum amount of time a dial will wait for
9+
// a connect to complete.
10+
//
11+
// See https://pkg.go.dev/net#Dialer.Timeout
12+
ConnectTimeout *time.Duration
13+
14+
// TLSNegotiationTimeout specifies the maximum amount of time waiting to
15+
// wait for a TLS handshake.
16+
//
17+
// See https://pkg.go.dev/net/http#Transport.TLSHandshakeTimeout
18+
TLSNegotiationTimeout *time.Duration
19+
}
20+
21+
// GetConnectTimeout returns the ConnectTimeout value, returns false if the value is not set.
22+
func (c *Configuration) GetConnectTimeout() (time.Duration, bool) {
23+
if c.ConnectTimeout == nil {
24+
return 0, false
25+
}
26+
return *c.ConnectTimeout, true
27+
}
28+
29+
// GetTLSNegotiationTimeout returns the TLSNegotiationTimeout value, returns false if the value is not set.
30+
func (c *Configuration) GetTLSNegotiationTimeout() (time.Duration, bool) {
31+
if c.TLSNegotiationTimeout == nil {
32+
return 0, false
33+
}
34+
return *c.TLSNegotiationTimeout, true
35+
}

aws/defaults/defaults.go

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aws/defaults/defaults_codegen_test.go

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aws/defaults/defaults_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package defaults
2+
3+
import (
4+
"github.com/aws/aws-sdk-go-v2/aws"
5+
"strconv"
6+
"testing"
7+
"time"
8+
9+
"github.com/google/go-cmp/cmp"
10+
)
11+
12+
func TestConfigV1(t *testing.T) {
13+
cases := []struct {
14+
Mode aws.DefaultsMode
15+
Expected Configuration
16+
}{
17+
{
18+
Mode: aws.DefaultsModeStandard,
19+
Expected: Configuration{
20+
ConnectTimeout: aws.Duration(2000 * time.Millisecond),
21+
TLSNegotiationTimeout: aws.Duration(2000 * time.Millisecond),
22+
},
23+
},
24+
{
25+
Mode: aws.DefaultsModeInRegion,
26+
Expected: Configuration{
27+
ConnectTimeout: aws.Duration(1000 * time.Millisecond),
28+
TLSNegotiationTimeout: aws.Duration(1000 * time.Millisecond),
29+
},
30+
},
31+
{
32+
Mode: aws.DefaultsModeCrossRegion,
33+
Expected: Configuration{
34+
ConnectTimeout: aws.Duration(2800 * time.Millisecond),
35+
TLSNegotiationTimeout: aws.Duration(2800 * time.Millisecond),
36+
},
37+
},
38+
{
39+
Mode: aws.DefaultsModeMobile,
40+
Expected: Configuration{
41+
ConnectTimeout: aws.Duration(10000 * time.Millisecond),
42+
TLSNegotiationTimeout: aws.Duration(11000 * time.Millisecond),
43+
},
44+
},
45+
}
46+
47+
for i, tt := range cases {
48+
t.Run(strconv.Itoa(i), func(t *testing.T) {
49+
got, err := v1TestResolver(tt.Mode)
50+
if err != nil {
51+
t.Fatalf("expect no error, got %v", err)
52+
}
53+
if diff := cmp.Diff(tt.Expected, got); len(diff) > 0 {
54+
t.Error(diff)
55+
}
56+
})
57+
}
58+
}

aws/defaults/doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Package defaults provides recommended configuration values for AWS SDKs and CLIs.
2+
package defaults

0 commit comments

Comments
 (0)