Skip to content

Commit 1b73386

Browse files
committed
Make AWS service creation a little drier
1 parent 070911e commit 1b73386

File tree

4 files changed

+29
-33
lines changed

4 files changed

+29
-33
lines changed

aws/config.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package aws
2+
3+
import (
4+
"context"
5+
6+
"github.com/aws/aws-sdk-go-v2/aws"
7+
"github.com/aws/aws-sdk-go-v2/config"
8+
"github.com/aws/aws-sdk-go-v2/credentials"
9+
)
10+
11+
// NewConfig creates a new AWS config with the given credentials and region
12+
func NewConfig(accessKey, secretKey, region string) (aws.Config, error) {
13+
opts := []func(*config.LoadOptions) error{config.WithRegion(region)}
14+
15+
if accessKey != "" && secretKey != "" {
16+
opts = append(opts, config.WithCredentialsProvider(credentials.StaticCredentialsProvider{Value: aws.Credentials{
17+
AccessKeyID: accessKey, SecretAccessKey: secretKey,
18+
}}))
19+
}
20+
21+
return config.LoadDefaultConfig(context.TODO(), opts...)
22+
}

aws/cwatch/service.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import (
77
"time"
88

99
"github.com/aws/aws-sdk-go-v2/aws"
10-
"github.com/aws/aws-sdk-go-v2/config"
11-
"github.com/aws/aws-sdk-go-v2/credentials"
1210
"github.com/aws/aws-sdk-go-v2/service/cloudwatch"
1311
"github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
12+
awsx "github.com/nyaruka/gocommon/aws"
1413
"github.com/nyaruka/gocommon/syncx"
1514
)
1615

@@ -20,16 +19,9 @@ type Service struct {
2019
batcher *syncx.Batcher[types.MetricDatum]
2120
}
2221

22+
// NewService creates a new Cloudwatch service with the given credentials and configuration
2323
func NewService(accessKey, secretKey, region, namespace string, wg *sync.WaitGroup) (*Service, error) {
24-
opts := []func(*config.LoadOptions) error{config.WithRegion(region)}
25-
26-
if accessKey != "" && secretKey != "" {
27-
opts = append(opts, config.WithCredentialsProvider(credentials.StaticCredentialsProvider{Value: aws.Credentials{
28-
AccessKeyID: accessKey, SecretAccessKey: secretKey,
29-
}}))
30-
}
31-
32-
cfg, err := config.LoadDefaultConfig(context.TODO(), opts...)
24+
cfg, err := awsx.NewConfig(accessKey, secretKey, region)
3325
if err != nil {
3426
return nil, err
3527
}

aws/dynamo/service.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ import (
55
"fmt"
66

77
"github.com/aws/aws-sdk-go-v2/aws"
8-
"github.com/aws/aws-sdk-go-v2/config"
9-
"github.com/aws/aws-sdk-go-v2/credentials"
108
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
119
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
10+
awsx "github.com/nyaruka/gocommon/aws"
1211
)
1312

1413
// Service is simple abstraction layer to work with a DynamoDB-compatible database
@@ -19,15 +18,7 @@ type Service struct {
1918

2019
// NewService creates a new dynamodb service with the given credentials and configuration
2120
func NewService(accessKey, secretKey, region, endpoint, tablePrefix string) (*Service, error) {
22-
opts := []func(*config.LoadOptions) error{config.WithRegion(region)}
23-
24-
if accessKey != "" && secretKey != "" {
25-
opts = append(opts, config.WithCredentialsProvider(credentials.StaticCredentialsProvider{Value: aws.Credentials{
26-
AccessKeyID: accessKey, SecretAccessKey: secretKey,
27-
}}))
28-
}
29-
30-
cfg, err := config.LoadDefaultConfig(context.TODO(), opts...)
21+
cfg, err := awsx.NewConfig(accessKey, secretKey, region)
3122
if err != nil {
3223
return nil, err
3324
}

aws/s3x/service.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ import (
99
"time"
1010

1111
"github.com/aws/aws-sdk-go-v2/aws"
12-
"github.com/aws/aws-sdk-go-v2/config"
13-
"github.com/aws/aws-sdk-go-v2/credentials"
1412
"github.com/aws/aws-sdk-go-v2/service/s3"
1513
"github.com/aws/aws-sdk-go-v2/service/s3/types"
14+
awsx "github.com/nyaruka/gocommon/aws"
1615
)
1716

1817
// Service is simple abstraction layer to work with a S3-compatible storage service
@@ -23,15 +22,7 @@ type Service struct {
2322

2423
// NewService creates a new S3 service with the given credentials and configuration
2524
func NewService(accessKey, secretKey, region, endpoint string, minio bool) (*Service, error) {
26-
opts := []func(*config.LoadOptions) error{config.WithRegion(region)}
27-
28-
if accessKey != "" && secretKey != "" {
29-
opts = append(opts, config.WithCredentialsProvider(credentials.StaticCredentialsProvider{Value: aws.Credentials{
30-
AccessKeyID: accessKey, SecretAccessKey: secretKey,
31-
}}))
32-
}
33-
34-
cfg, err := config.LoadDefaultConfig(context.TODO(), opts...)
25+
cfg, err := awsx.NewConfig(accessKey, secretKey, region)
3526
if err != nil {
3627
return nil, err
3728
}

0 commit comments

Comments
 (0)