Skip to content

Commit e0dda86

Browse files
authored
Refactor Config Module, Add Config Package Documentation and Examples, Improve Overall SDK Readme (#822)
1 parent d6986a7 commit e0dda86

25 files changed

+547
-418
lines changed

README.md

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# AWS SDK for Go v2
22

3-
![Build Status](https://codebuild.us-west-2.amazonaws.com/badges?uuid=eyJlbmNyeXB0ZWREYXRhIjoib1lGQ3N6RFJsalI5a3BPcXB3Rytaak9kYVh1ZW1lZExPNjgzaU9Udng3VE5OL1I3czIwcVhkMUlUeG91ajBVaWRYcVVJSEVQcmZwTWVyT1p5MGszbnA4PSIsIml2UGFyYW1ldGVyU3BlYyI6IkhrZ1VMN20zRmtYY1BrR0wiLCJtYXRlcmlhbFNldFNlcmlhbCI6MX0%3D&branch=master) [![API Reference](https://img.shields.io/badge/api-reference-blue.svg)](https://docs.aws.amazon.com/sdk-for-go/v2/api) [![Join the chat at https://gitter.im/aws/aws-sdk-go](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/aws/aws-sdk-go-v2?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![Apache V2 License](https://img.shields.io/badge/license-Apache%20V2-blue.svg)](https://github.com/aws/aws-sdk-go/blob/master/LICENSE.txt)
3+
![Build Status](https://codebuild.us-west-2.amazonaws.com/badges?uuid=eyJlbmNyeXB0ZWREYXRhIjoib1lGQ3N6RFJsalI5a3BPcXB3Rytaak9kYVh1ZW1lZExPNjgzaU9Udng3VE5OL1I3czIwcVhkMUlUeG91ajBVaWRYcVVJSEVQcmZwTWVyT1p5MGszbnA4PSIsIml2UGFyYW1ldGVyU3BlYyI6IkhrZ1VMN20zRmtYY1BrR0wiLCJtYXRlcmlhbFNldFNlcmlhbCI6MX0%3D&branch=master) [![API Reference](https://img.shields.io/badge/api-reference-blue.svg)](https://pkg.go.dev/mod/github.com/aws/aws-sdk-go-v2) [![Apache V2 License](https://img.shields.io/badge/license-Apache%20V2-blue.svg)](https://github.com/aws/aws-sdk-go/blob/master/LICENSE.txt)
44

55

66
`aws-sdk-go-v2` is the **Developer Preview** (aka **beta**) for the v2 AWS SDK for the Go programming language. This Developer Preview is provided to receive feedback from the language community on SDK changes prior to the final release. As such users should expect the SDK to release minor version releases that break backwards compatability. The release notes for the breaking change will include information about the breaking change, and how you can migrate to the latest version.
@@ -12,7 +12,6 @@ We'll be expanding out the [Issues] and [Projects] sections with additional chan
1212
Jump To:
1313
* [Project Status](_#Project-Status_)
1414
* [Getting Started](_#Getting-Started_)
15-
* [Quick Examples](_#Quick-Examples_)
1615
* [Getting Help](_#Getting-Help_)
1716
* [Contributing](_#Feedback-and-contributing_)
1817
* [More Resources](_#Resources_)
@@ -33,63 +32,73 @@ Users should expect significant changes that could affect the following (non-exh
3332
* Minimum Supported Go Release following the [Language Release Policy](https://golang.org/doc/devel/release.html#policy)
3433

3534
## Getting started
35+
To get started working with the SDK setup your project for Go modules, and retrieve the SDK dependencies with `go get`.
36+
This example shows how you can use the v2 SDK to make an API request using the SDK's [Amazon DynamoDB] client.
3637

37-
To get started working with the SDK is to use `go get` to add the SDK to your application dependencies using Go modules.
38-
38+
###### Initialize Project
3939
```sh
40-
go get github.com/aws/aws-sdk-go-v2
41-
go get github.com/aws/aws-sdk-go-v2/service/dynamodb
40+
$ mkdir ~/helloaws
41+
$ cd ~/helloaws
42+
$ go mod init helloaws
43+
```
44+
###### Add SDK Dependencies
45+
```sh
46+
$ go get github.com/aws/aws-sdk-go-v2/aws
47+
$ go get github.com/aws/aws-sdk-go-v2/aws/config
48+
$ go get github.com/aws/aws-sdk-go-v2/service/dynamodb
4249
```
4350

44-
## Quick Examples
45-
46-
### Hello AWS
47-
48-
This example shows how you can use the v2 SDK to make an API request using the SDK's [Amazon DynamoDB] client.
51+
###### Write Code
52+
In your preferred editor add the following content to `main.go`
4953

5054
```go
5155
package main
5256

5357
import (
54-
"context"
55-
"fmt"
58+
"context"
59+
"fmt"
60+
"log"
5661

57-
"github.com/aws/aws-sdk-go-v2/aws"
58-
"github.com/aws/aws-sdk-go-v2/config"
59-
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
62+
"github.com/aws/aws-sdk-go-v2/aws"
63+
"github.com/aws/aws-sdk-go-v2/config"
64+
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
6065
)
6166

6267
func main() {
63-
// Using the SDK's default configuration, loading additional config
64-
// and credentials values from the environment variables, shared
65-
// credentials, and shared configuration files
66-
cfg, err := config.LoadDefaultConfig()
67-
if err != nil {
68-
panic("unable to load SDK config, " + err.Error())
69-
}
70-
71-
// Set the AWS Region that the service clients should use
72-
cfg.Region = "us-west-2"
73-
74-
// Using the Config value, create the DynamoDB client
75-
svc := dynamodb.NewFromConfig(cfg)
76-
77-
// Build the request with its input parameters
78-
resp, err := svc.DescribeTable(context.Background(), &dynamodb.DescribeTableInput{
79-
TableName: aws.String("myTable"),
80-
})
81-
if err != nil {
82-
panic("failed to describe table, " + err.Error())
83-
}
84-
85-
if err != nil {
86-
panic("failed to describe table, " + err.Error())
87-
}
88-
89-
fmt.Println("Response", resp)
68+
// Using the SDK's default configuration, loading additional config
69+
// and credentials values from the environment variables, shared
70+
// credentials, and shared configuration files
71+
cfg, err := config.LoadDefaultConfig(config.WithRegion("us-west-2"))
72+
if err != nil {
73+
log.Fatalf("unable to load SDK config, %v", err)
74+
}
75+
76+
// Using the Config value, create the DynamoDB client
77+
svc := dynamodb.NewFromConfig(cfg)
78+
79+
// Build the request with its input parameters
80+
resp, err := svc.ListTables(context.Background(), &dynamodb.ListTablesInput{
81+
Limit: aws.Int32(5),
82+
})
83+
if err != nil {
84+
log.Fatalf("failed to list tables, %v", err)
85+
}
86+
87+
fmt.Println("Tables:")
88+
for _, tableName := range resp.TableNames {
89+
fmt.Println(aws.ToString(tableName))
90+
}
9091
}
9192
```
9293

94+
###### Compile and Execute
95+
```sh
96+
$ go run .
97+
Table:
98+
tableOne
99+
tableTwo
100+
```
101+
93102
## Getting Help
94103

95104
Please use these community resources for getting help. We use the GitHub issues

config/codegen/main.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@ const (
1616
)
1717

1818
var implAsserts = map[string][]string{
19-
"SharedConfigProfileProvider": {envConfigType, `WithSharedConfigProfile("")`},
20-
"SharedConfigFilesProvider": {envConfigType, "WithSharedConfigFiles([]string{})"},
21-
"CustomCABundleProvider": {envConfigType, "WithCustomCABundle([]byte{})"},
22-
"RegionProvider": {envConfigType, sharedConfigType, `WithRegion("")` /*"&WithEC2MetadataRegion{}"*/},
23-
"MFATokenFuncProvider": {`WithMFATokenFunc(func() (string, error) { return "", nil })`},
24-
"CredentialsProviderProvider": {`WithCredentialsProvider{credentials.NewStaticCredentialsProvider("", "", "")}`},
25-
"DefaultRegionProvider": {`WithDefaultRegion("")`},
19+
"SharedConfigProfileProvider": {envConfigType, `WithSharedConfigProfile("")`},
20+
"SharedConfigFilesProvider": {envConfigType, `WithSharedConfigFiles(nil)`},
21+
"CustomCABundleProvider": {envConfigType, `WithCustomCABundle(nil)`},
22+
"RegionProvider": {envConfigType, sharedConfigType, `WithRegion("")`, `WithEC2IMDSRegion{}`},
23+
"CredentialsProviderProvider": {`WithCredentialsProvider(nil)`},
24+
"DefaultRegionProvider": {`WithDefaultRegion("")`},
25+
"EC2RoleCredentialOptionsProvider": {`WithEC2RoleCredentialOptions(nil)`},
26+
"EndpointCredentialOptionsProvider": {`WithEndpointCredentialOptions(nil)`},
27+
"EndpointResolverProvider": {`WithEndpointResolver(nil)`},
28+
"APIOptionsProvider": {`WithAPIOptions(nil)`},
29+
"HTTPClientProvider": {`WithHTTPClient(nil)`},
30+
"AssumeRoleCredentialOptionsProvider": {`WithAssumeRoleCredentialOptions(nil)`},
31+
"WebIdentityRoleCredentialOptionsProvider": {`WithWebIdentityRoleCredentialOptions(nil)`},
2632
}
2733

2834
var tplProviderTests = template.Must(template.New("tplProviderTests").Funcs(map[string]interface{}{
@@ -41,10 +47,6 @@ var tplProviderTests = template.Must(template.New("tplProviderTests").Funcs(map[
4147
4248
package config
4349
44-
import (
45-
"github.com/aws/aws-sdk-go-v2/credentials"
46-
)
47-
4850
{{ $sortedKeys := SortKeys . }}
4951
{{- range $_, $provider := $sortedKeys }}
5052
{{- $implementors := index $ $provider -}}

config/config.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,30 @@ import (
44
"github.com/aws/aws-sdk-go-v2/aws"
55
)
66

7-
// DefaultLoaders are a slice of functions that will read external configuration
7+
// defaultLoaders are a slice of functions that will read external configuration
88
// sources for configuration values. These values are read by the AWSConfigResolvers
99
// using interfaces to extract specific information from the external configuration.
10-
var DefaultLoaders = []Loader{
11-
LoadEnvConfig,
12-
LoadSharedConfigIgnoreNotExist,
10+
var defaultLoaders = []loader{
11+
loadEnvConfig,
12+
loadSharedConfigIgnoreNotExist,
1313
}
1414

15-
// DefaultAWSConfigResolvers are a slice of functions that will resolve external
15+
// defaultAWSConfigResolvers are a slice of functions that will resolve external
1616
// configuration values into AWS configuration values.
1717
//
1818
// This will setup the AWS configuration's Region,
19-
var DefaultAWSConfigResolvers = []AWSConfigResolver{
20-
ResolveDefaultAWSConfig,
21-
ResolveCustomCABundle,
22-
ResolveHTTPClient,
23-
ResolveEndpointResolver,
24-
ResolveAPIOptions,
25-
26-
ResolveRegion,
19+
var defaultAWSConfigResolvers = []awsConfigResolver{
20+
resolveDefaultAWSConfig,
21+
resolveCustomCABundle,
22+
resolveHTTPClient,
23+
resolveEndpointResolver,
24+
resolveAPIOptions,
25+
26+
resolveRegion,
2727
// TODO: Add back EC2 Region Resolver Support
28-
ResolveDefaultRegion,
28+
resolveDefaultRegion,
2929

30-
ResolveCredentials,
30+
resolveCredentials,
3131
}
3232

3333
// A Config represents a generic configuration value or set of values. This type
@@ -37,22 +37,22 @@ var DefaultAWSConfigResolvers = []AWSConfigResolver{
3737
// to extract specific data from the Config.
3838
type Config interface{}
3939

40-
// A Loader is used to load external configuration data and returns it as
40+
// A loader is used to load external configuration data and returns it as
4141
// a generic Config type.
4242
//
4343
// The loader should return an error if it fails to load the external configuration
4444
// or the configuration data is malformed, or required components missing.
45-
type Loader func(Configs) (Config, error)
45+
type loader func(configs) (Config, error)
4646

47-
// An AWSConfigResolver will extract configuration data from the Configs slice
47+
// An awsConfigResolver will extract configuration data from the configs slice
4848
// using the provider interfaces to extract specific functionality. The extracted
4949
// configuration values will be written to the AWS Config value.
5050
//
5151
// The resolver should return an error if it it fails to extract the data, the
5252
// data is malformed, or incomplete.
53-
type AWSConfigResolver func(cfg *aws.Config, configs Configs) error
53+
type awsConfigResolver func(cfg *aws.Config, configs configs) error
5454

55-
// Configs is a slice of Config values. These values will be used by the
55+
// configs is a slice of Config values. These values will be used by the
5656
// AWSConfigResolvers to extract external configuration values to populate the
5757
// AWS Config type.
5858
//
@@ -61,15 +61,15 @@ type AWSConfigResolver func(cfg *aws.Config, configs Configs) error
6161
//
6262
// Use ResolveAWSConfig after external Config values have been added or loaded
6363
// to extract the loaded configuration values into the AWS Config.
64-
type Configs []Config
64+
type configs []Config
6565

6666
// AppendFromLoaders iterates over the slice of loaders passed in calling each
6767
// loader function in order. The external config value returned by the loader
68-
// will be added to the returned Configs slice.
68+
// will be added to the returned configs slice.
6969
//
7070
// If a loader returns an error this method will stop iterating and return
7171
// that error.
72-
func (cs Configs) AppendFromLoaders(loaders []Loader) (Configs, error) {
72+
func (cs configs) AppendFromLoaders(loaders []loader) (configs, error) {
7373
for _, fn := range loaders {
7474
cfg, err := fn(cs)
7575
if err != nil {
@@ -88,7 +88,7 @@ func (cs Configs) AppendFromLoaders(loaders []Loader) (Configs, error) {
8888
//
8989
// If an resolver returns an error this method will return that error, and stop
9090
// iterating over the resolvers.
91-
func (cs Configs) ResolveAWSConfig(resolvers []AWSConfigResolver) (aws.Config, error) {
91+
func (cs configs) ResolveAWSConfig(resolvers []awsConfigResolver) (aws.Config, error) {
9292
var cfg aws.Config
9393

9494
for _, fn := range resolvers {
@@ -109,7 +109,7 @@ func (cs Configs) ResolveAWSConfig(resolvers []AWSConfigResolver) (aws.Config, e
109109

110110
// ResolveConfig calls the provide function passing slice of configuration sources.
111111
// This implements the aws.ConfigResolver interface.
112-
func (cs Configs) ResolveConfig(f func(configs []interface{}) error) error {
112+
func (cs configs) ResolveConfig(f func(configs []interface{}) error) error {
113113
var cfgs []interface{}
114114
for i := range cs {
115115
cfgs = append(cfgs, cs[i])
@@ -121,7 +121,7 @@ func (cs Configs) ResolveConfig(f func(configs []interface{}) error) error {
121121
// populates an AWS Config with the values from the external configurations.
122122
//
123123
// An optional variadic set of additional Config values can be provided as input
124-
// that will be prepended to the Configs slice. Use this to add custom configuration.
124+
// that will be prepended to the configs slice. Use this to add custom configuration.
125125
// The custom configurations must satisfy the respective providers for their data
126126
// or the custom data will be ignored by the resolvers and config loaders.
127127
//
@@ -136,14 +136,14 @@ func (cs Configs) ResolveConfig(f func(configs []interface{}) error) error {
136136
// The default configuration sources are:
137137
// * Environment Variables
138138
// * Shared Configuration and Shared Credentials files.
139-
func LoadDefaultConfig(configs ...Config) (aws.Config, error) {
140-
var cfgs Configs
141-
cfgs = append(cfgs, configs...)
139+
func LoadDefaultConfig(cfgs ...Config) (aws.Config, error) {
140+
var cfgCpy configs
141+
cfgCpy = append(cfgCpy, cfgs...)
142142

143-
cfgs, err := cfgs.AppendFromLoaders(DefaultLoaders)
143+
cfgCpy, err := cfgCpy.AppendFromLoaders(defaultLoaders)
144144
if err != nil {
145145
return aws.Config{}, err
146146
}
147147

148-
return cfgs.ResolveAWSConfig(DefaultAWSConfigResolvers)
148+
return cfgCpy.ResolveAWSConfig(defaultAWSConfigResolvers)
149149
}

config/config_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010
)
1111

1212
func TestConfigs_SharedConfigOptions(t *testing.T) {
13-
_, err := Configs{
13+
_, err := configs{
1414
WithSharedConfigProfile("profile-name"),
1515
WithSharedConfigFiles([]string{"creds-file"}),
16-
}.AppendFromLoaders([]Loader{
17-
func(configs Configs) (Config, error) {
16+
}.AppendFromLoaders([]loader{
17+
func(configs configs) (Config, error) {
1818
var profile string
1919
var files []string
2020
var err error
@@ -54,8 +54,8 @@ func TestConfigs_SharedConfigOptions(t *testing.T) {
5454
func TestConfigs_AppendFromLoaders(t *testing.T) {
5555
expectCfg := WithRegion("mock-region")
5656

57-
cfgs, err := Configs{}.AppendFromLoaders([]Loader{
58-
func(configs Configs) (Config, error) {
57+
cfgs, err := configs{}.AppendFromLoaders([]loader{
58+
func(configs configs) (Config, error) {
5959
if e, a := 0, len(configs); e != a {
6060
t.Errorf("expect %v configs, got %v", e, a)
6161
}
@@ -77,19 +77,19 @@ func TestConfigs_AppendFromLoaders(t *testing.T) {
7777
}
7878

7979
func TestConfigs_ResolveAWSConfig(t *testing.T) {
80-
configSources := Configs{
80+
configSources := configs{
8181
WithRegion("mock-region"),
82-
WithCredentialsProvider{credentials.StaticCredentialsProvider{
82+
WithCredentialsProvider(credentials.StaticCredentialsProvider{
8383
Value: aws.Credentials{
8484
AccessKeyID: "AKID", SecretAccessKey: "SECRET",
8585
Source: "provider",
8686
},
87-
}},
87+
}),
8888
}
8989

90-
cfg, err := configSources.ResolveAWSConfig([]AWSConfigResolver{
91-
ResolveRegion,
92-
ResolveCredentials,
90+
cfg, err := configSources.ResolveAWSConfig([]awsConfigResolver{
91+
resolveRegion,
92+
resolveCredentials,
9393
})
9494
if err != nil {
9595
t.Fatalf("expect no error, got %v", err)

config/doc.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,13 @@
88
// Use the LoadDefaultConfig to load configuration from all the SDK's supported
99
// sources, and resolve credentials using the SDK's default credential chain.
1010
//
11-
// * TODO Additional documentation needed.
11+
// LoadDefaultConfig allows for a variadic list of additional Config sources that can
12+
// provide one or more configuration values which can be used to programmatically control the resolution
13+
// of a specific value, or allow for broader range of additional configuration sources not supported by the SDK.
14+
// A Config source implements one or more provider interfaces defined in this package. Config sources passed in will
15+
// take precedence over the default environment and shared config sources used by the SDK. If one or more Config sources
16+
// implement the same provider interface, priority will be handled by the order in which the sources were passed in.
17+
//
18+
// A number of helpers (prefixed by ``With``) are provided in this package that implement their respective provider
19+
// interface. These helpers should be used for overriding configuration programatically at runtime.
1220
package config

0 commit comments

Comments
 (0)