-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SDK Default Configuration Implementation
- Loading branch information
Showing
41 changed files
with
2,627 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
// ResolveAutoDefaultsMode is used to determine the effective aws.DefaultsMode when the mode | ||
// is set to aws.AutoDefaultsMode. | ||
func ResolveAutoDefaultsMode(region string, environment aws.RuntimeEnvironment) aws.DefaultsMode { | ||
goos := getGOOS() | ||
if goos == "android" || goos == "ios" { | ||
return aws.MobileDefaultsMode | ||
} | ||
|
||
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.InRegionDefaultsMode | ||
} | ||
return aws.CrossRegionDefaultsMode | ||
} | ||
|
||
return aws.StandardDefaultsMode | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.MobileDefaultsMode, | ||
}, | ||
{ | ||
Region: "us-east-1", | ||
GOOS: "android", | ||
Environment: aws.RuntimeEnvironment{ | ||
EnvironmentIdentifier: aws.ExecutionEnvironmentID("AWS_Lambda_java8"), | ||
Region: "us-east-1", | ||
}, | ||
Expected: aws.MobileDefaultsMode, | ||
}, | ||
{ | ||
Region: "us-east-1", | ||
Environment: aws.RuntimeEnvironment{ | ||
EnvironmentIdentifier: aws.ExecutionEnvironmentID("AWS_Lambda_java8"), | ||
Region: "us-east-1", | ||
}, | ||
Expected: aws.InRegionDefaultsMode, | ||
}, | ||
{ | ||
Region: "us-east-1", | ||
Environment: aws.RuntimeEnvironment{ | ||
EnvironmentIdentifier: aws.ExecutionEnvironmentID("AWS_Lambda_java8"), | ||
Region: "us-west-2", | ||
}, | ||
Expected: aws.CrossRegionDefaultsMode, | ||
}, | ||
{ | ||
Region: "us-east-1", | ||
Environment: aws.RuntimeEnvironment{ | ||
Region: "us-east-1", | ||
EC2InstanceMetadataRegion: "us-east-1", | ||
}, | ||
Expected: aws.InRegionDefaultsMode, | ||
}, | ||
{ | ||
Region: "us-east-1", | ||
Environment: aws.RuntimeEnvironment{ | ||
EC2InstanceMetadataRegion: "us-west-2", | ||
}, | ||
Expected: aws.CrossRegionDefaultsMode, | ||
}, | ||
{ | ||
Region: "us-west-2", | ||
Environment: aws.RuntimeEnvironment{}, | ||
Expected: aws.StandardDefaultsMode, | ||
}, | ||
} | ||
|
||
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 := ResolveAutoDefaultsMode(tt.Region, tt.Environment) | ||
if got != tt.Expected { | ||
t.Errorf("expect %v, got %v", tt.Expected, got) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
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 | ||
} | ||
|
||
func duration(duration time.Duration) *time.Duration { | ||
return &duration | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.StandardDefaultsMode, | ||
Expected: Configuration{ | ||
ConnectTimeout: duration(2000 * time.Millisecond), | ||
TLSNegotiationTimeout: duration(2000 * time.Millisecond), | ||
}, | ||
}, | ||
{ | ||
Mode: aws.InRegionDefaultsMode, | ||
Expected: Configuration{ | ||
ConnectTimeout: duration(1000 * time.Millisecond), | ||
TLSNegotiationTimeout: duration(1000 * time.Millisecond), | ||
}, | ||
}, | ||
{ | ||
Mode: aws.CrossRegionDefaultsMode, | ||
Expected: Configuration{ | ||
ConnectTimeout: duration(2800 * time.Millisecond), | ||
TLSNegotiationTimeout: duration(2800 * time.Millisecond), | ||
}, | ||
}, | ||
{ | ||
Mode: aws.MobileDefaultsMode, | ||
Expected: Configuration{ | ||
ConnectTimeout: duration(10000 * time.Millisecond), | ||
TLSNegotiationTimeout: 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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.