-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaws.go
78 lines (66 loc) · 1.77 KB
/
aws.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package model
import (
"context"
"os"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
)
// AWSProfile is the name of the AWS profile.
type AWSProfile string
// NewAWSProfile returns a new AWSProfile.
// If p is empty, read $AWS_PROFILE and return it.
func NewAWSProfile(p string) AWSProfile {
if p == "" {
profile := os.Getenv("AWS_PROFILE")
if profile == "" {
return AWSProfile("default")
}
return AWSProfile(profile)
}
return AWSProfile(p)
}
// String returns the string representation of the AWSProfile.
func (p AWSProfile) String() string {
return string(p)
}
// AWSConfig is the AWS config.
type AWSConfig struct {
*aws.Config
}
// NewAWSConfig creates a new AWS config.
func NewAWSConfig(ctx context.Context, profile AWSProfile, region Region) (*AWSConfig, error) {
opts := []func(*config.LoadOptions) error{}
if profile.String() != "" {
opts = append(opts, config.WithSharedConfigProfile(profile.String()))
}
if region.String() != "" {
opts = append(opts, config.WithRegion(region.String()))
}
cfg, err := config.LoadDefaultConfig(ctx, opts...)
if err != nil {
return nil, err
}
if cfg.BaseEndpoint != nil {
opts = append(opts, config.WithEndpointResolverWithOptions(aws.EndpointResolverWithOptionsFunc(func(service, region string, opts ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{
PartitionID: "aws",
URL: *cfg.BaseEndpoint,
HostnameImmutable: true,
}, nil
})))
cfg, err = config.LoadDefaultConfig(ctx, opts...)
if err != nil {
return nil, err
}
}
return &AWSConfig{
Config: &cfg,
}, nil
}
// Region returns the AWS region.
func (c *AWSConfig) Region() Region {
if Region(c.Config.Region) == "" {
return RegionUSEast1
}
return Region(c.Config.Region)
}