Issues (1008)

config/config.go (2 issues)

Severity
1
package config
2
3
import (
4
	"net/http"
5
6
	"github.com/aws/aws-sdk-go-v2/aws"
7
	"github.com/aws/aws-sdk-go-v2/aws/external"
8
9
	"github.com/evalphobia/aws-sdk-go-v2-wrapper/errors"
10
	"github.com/evalphobia/aws-sdk-go-v2-wrapper/log"
11
)
12
13
const defaultRegion = "us-east-1"
14
15
// Config has AWS settings.
16
type Config struct {
17
	Region         string
18
	CommonEndpoint string
19
	Endpoints      Endpoints
20
	Logger         log.Logger
21
22
	EnableDefaultCreds bool
23
24
	// Static Credentials
25
	AccessKey string
26
	SecretKey string
27
28
	// File Credentials
29
	Filename string
30
	Profile  string
31
32
	// DefaultPrefix is used for service resource prefix
33
	// e.g.) DynamoDB table, S3 bucket, SQS Queue
34
	DefaultPrefix string
35
36
	// Specific sevice's options
37
	S3ForcePathStyle bool
38
39
	// Custom Function to wrap errors.
40
	ErrWrap func(errors.ErrorData) error
41
42
	// Custom HTTP Client
43
	CustomHTTPClient *http.Client
44
	// Showing req/resp data when `true` (Cannot use with CustomHTTPClient)
45
	UseDebugRequest bool
46
}
47
48
// AWSConfig creates *aws.Config object from the fields.
49
func (c Config) AWSConfig() (aws.Config, error) {
50
	cfg, err := c.loadConfig()
51
	if err != nil {
52
		return cfg, err
53
	}
54
55
	region := c.getRegion()
56
	if region != "" {
57
		cfg.Region = region
58
	}
59
	if c.Logger != nil {
60
		cfg.Logger = c.Logger
61
	}
62
	if c.CommonEndpoint != "" {
63
		cfg.EndpointResolver = aws.ResolveWithEndpointURL(c.CommonEndpoint)
64
		cfg.DisableEndpointHostPrefix = true
65
	}
66
	switch {
67
	case c.CustomHTTPClient != nil:
68
		cfg.HTTPClient = c.CustomHTTPClient
69
	case c.UseDebugRequest:
70
		cfg.HTTPClient = &http.Client{
71
			Transport: DefaultDebugTransport,
72
		}
73
	}
74
75
	return cfg, nil
76
}
77
78
func (c Config) GetLogger() log.Logger {
0 ignored issues
show
exported method Config.GetLogger should have comment or be unexported
Loading history...
79
	if c.Logger != nil {
80
		return c.Logger
81
	}
82
	return log.DefaultLogger
83
}
84
85
func (c Config) GetErrWrap() func(errors.ErrorData) error {
0 ignored issues
show
exported method Config.GetErrWrap should have comment or be unexported
Loading history...
86
	if c.ErrWrap != nil {
87
		return c.ErrWrap
88
	}
89
	return errors.DefaultErrWrap
90
}
91
92
func (c Config) loadConfig() (aws.Config, error) {
93
	if c.EnableDefaultCreds {
94
		return external.LoadDefaultAWSConfig()
95
	}
96
97
	var opts []external.Config
98
	if c.AccessKey != "" && c.SecretKey != "" {
99
		opts = append(opts, external.WithCredentialsProvider{
100
			CredentialsProvider: aws.StaticCredentialsProvider{
101
				Value: aws.Credentials{
102
					AccessKeyID:     c.AccessKey,
103
					SecretAccessKey: c.SecretKey,
104
				},
105
			},
106
		})
107
	}
108
109
	if c.Profile != "" {
110
		opts = append(opts, external.WithSharedConfigProfile(c.Profile))
111
	}
112
	if c.Filename != "" {
113
		opts = append(opts, external.WithSharedConfigFiles([]string{c.Filename}))
114
	}
115
	return external.LoadDefaultAWSConfig(opts...)
116
}
117
118
func (c Config) getRegion() string {
119
	if c.Region != "" {
120
		return c.Region
121
	}
122
	reg := EnvRegion()
123
	if reg != "" {
124
		return reg
125
	}
126
	return defaultRegion
127
}
128