Skip to content

Commit 54d3f54

Browse files
authored
Migrate aws-sdk-go to v2 (#667)
1 parent 12cd53d commit 54d3f54

18 files changed

+570
-68437
lines changed

aws/api.go

Lines changed: 73 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,40 @@
11
package aws
22

33
import (
4-
"github.com/aws/aws-sdk-go/service/ec2"
5-
"github.com/aws/aws-sdk-go/service/elasticache"
6-
"github.com/aws/aws-sdk-go/service/iam"
7-
"github.com/aws/aws-sdk-go/service/rds"
4+
"context"
5+
6+
"github.com/aws/aws-sdk-go-v2/service/ec2"
7+
"github.com/aws/aws-sdk-go-v2/service/elasticache"
8+
"github.com/aws/aws-sdk-go-v2/service/iam"
9+
"github.com/aws/aws-sdk-go-v2/service/rds"
810
)
911

12+
// Client is an interface for API client.
13+
// This is primarily used for mock clients.
14+
type Client interface {
15+
DescribeSecurityGroups() (map[string]bool, error)
16+
DescribeSubnets() (map[string]bool, error)
17+
DescribeDBSubnetGroups() (map[string]bool, error)
18+
DescribeOptionGroups() (map[string]bool, error)
19+
DescribeDBParameterGroups() (map[string]bool, error)
20+
DescribeCacheParameterGroups() (map[string]bool, error)
21+
DescribeCacheSubnetGroups() (map[string]bool, error)
22+
DescribeInstances() (map[string]bool, error)
23+
DescribeImages(*ec2.DescribeImagesInput) (map[string]bool, error)
24+
ListInstanceProfiles() (map[string]bool, error)
25+
DescribeKeyPairs() (map[string]bool, error)
26+
DescribeEgressOnlyInternetGateways() (map[string]bool, error)
27+
DescribeInternetGateways() (map[string]bool, error)
28+
DescribeNatGateways() (map[string]bool, error)
29+
DescribeNetworkInterfaces() (map[string]bool, error)
30+
DescribeRouteTables() (map[string]bool, error)
31+
DescribeVpcPeeringConnections() (map[string]bool, error)
32+
}
33+
1034
// DescribeSecurityGroups is a wrapper of DescribeSecurityGroups
11-
func (c *Client) DescribeSecurityGroups() (map[string]bool, error) {
35+
func (c *AwsClient) DescribeSecurityGroups() (map[string]bool, error) {
1236
ret := map[string]bool{}
13-
resp, err := c.EC2.DescribeSecurityGroups(&ec2.DescribeSecurityGroupsInput{})
37+
resp, err := c.EC2.DescribeSecurityGroups(context.Background(), &ec2.DescribeSecurityGroupsInput{})
1438
if err != nil {
1539
return ret, err
1640
}
@@ -21,9 +45,9 @@ func (c *Client) DescribeSecurityGroups() (map[string]bool, error) {
2145
}
2246

2347
// DescribeSubnets is a wrapper of DescribeSubnets
24-
func (c *Client) DescribeSubnets() (map[string]bool, error) {
48+
func (c *AwsClient) DescribeSubnets() (map[string]bool, error) {
2549
ret := map[string]bool{}
26-
resp, err := c.EC2.DescribeSubnets(&ec2.DescribeSubnetsInput{})
50+
resp, err := c.EC2.DescribeSubnets(context.Background(), &ec2.DescribeSubnetsInput{})
2751
if err != nil {
2852
return ret, err
2953
}
@@ -34,9 +58,9 @@ func (c *Client) DescribeSubnets() (map[string]bool, error) {
3458
}
3559

3660
// DescribeDBSubnetGroups is a wrapper of DescribeDBSubnetGroups
37-
func (c *Client) DescribeDBSubnetGroups() (map[string]bool, error) {
61+
func (c *AwsClient) DescribeDBSubnetGroups() (map[string]bool, error) {
3862
ret := map[string]bool{}
39-
resp, err := c.RDS.DescribeDBSubnetGroups(&rds.DescribeDBSubnetGroupsInput{})
63+
resp, err := c.RDS.DescribeDBSubnetGroups(context.Background(), &rds.DescribeDBSubnetGroupsInput{})
4064
if err != nil {
4165
return ret, err
4266
}
@@ -47,9 +71,9 @@ func (c *Client) DescribeDBSubnetGroups() (map[string]bool, error) {
4771
}
4872

4973
// DescribeOptionGroups is a wrapper of DescribeOptionGroups
50-
func (c *Client) DescribeOptionGroups() (map[string]bool, error) {
74+
func (c *AwsClient) DescribeOptionGroups() (map[string]bool, error) {
5175
ret := map[string]bool{}
52-
resp, err := c.RDS.DescribeOptionGroups(&rds.DescribeOptionGroupsInput{})
76+
resp, err := c.RDS.DescribeOptionGroups(context.Background(), &rds.DescribeOptionGroupsInput{})
5377
if err != nil {
5478
return ret, err
5579
}
@@ -60,9 +84,9 @@ func (c *Client) DescribeOptionGroups() (map[string]bool, error) {
6084
}
6185

6286
// DescribeDBParameterGroups is a wrapper of DescribeDBParameterGroups
63-
func (c *Client) DescribeDBParameterGroups() (map[string]bool, error) {
87+
func (c *AwsClient) DescribeDBParameterGroups() (map[string]bool, error) {
6488
ret := map[string]bool{}
65-
resp, err := c.RDS.DescribeDBParameterGroups(&rds.DescribeDBParameterGroupsInput{})
89+
resp, err := c.RDS.DescribeDBParameterGroups(context.Background(), &rds.DescribeDBParameterGroupsInput{})
6690
if err != nil {
6791
return ret, err
6892
}
@@ -73,9 +97,9 @@ func (c *Client) DescribeDBParameterGroups() (map[string]bool, error) {
7397
}
7498

7599
// DescribeCacheParameterGroups is a wrapper of DescribeCacheParameterGroups
76-
func (c *Client) DescribeCacheParameterGroups() (map[string]bool, error) {
100+
func (c *AwsClient) DescribeCacheParameterGroups() (map[string]bool, error) {
77101
ret := map[string]bool{}
78-
resp, err := c.ElastiCache.DescribeCacheParameterGroups(&elasticache.DescribeCacheParameterGroupsInput{})
102+
resp, err := c.ElastiCache.DescribeCacheParameterGroups(context.Background(), &elasticache.DescribeCacheParameterGroupsInput{})
79103
if err != nil {
80104
return ret, err
81105
}
@@ -86,9 +110,9 @@ func (c *Client) DescribeCacheParameterGroups() (map[string]bool, error) {
86110
}
87111

88112
// DescribeCacheSubnetGroups is a wrapper of DescribeCacheSubnetGroups
89-
func (c *Client) DescribeCacheSubnetGroups() (map[string]bool, error) {
113+
func (c *AwsClient) DescribeCacheSubnetGroups() (map[string]bool, error) {
90114
ret := map[string]bool{}
91-
resp, err := c.ElastiCache.DescribeCacheSubnetGroups(&elasticache.DescribeCacheSubnetGroupsInput{})
115+
resp, err := c.ElastiCache.DescribeCacheSubnetGroups(context.Background(), &elasticache.DescribeCacheSubnetGroupsInput{})
92116
if err != nil {
93117
return ret, err
94118
}
@@ -99,9 +123,9 @@ func (c *Client) DescribeCacheSubnetGroups() (map[string]bool, error) {
99123
}
100124

101125
// DescribeInstances is a wrapper of DescribeInstances
102-
func (c *Client) DescribeInstances() (map[string]bool, error) {
126+
func (c *AwsClient) DescribeInstances() (map[string]bool, error) {
103127
ret := map[string]bool{}
104-
resp, err := c.EC2.DescribeInstances(&ec2.DescribeInstancesInput{})
128+
resp, err := c.EC2.DescribeInstances(context.Background(), &ec2.DescribeInstancesInput{})
105129
if err != nil {
106130
return ret, err
107131
}
@@ -113,10 +137,23 @@ func (c *Client) DescribeInstances() (map[string]bool, error) {
113137
return ret, err
114138
}
115139

140+
// DescribeImages is a wrapper of DescribeImages
141+
func (c *AwsClient) DescribeImages(in *ec2.DescribeImagesInput) (map[string]bool, error) {
142+
ret := map[string]bool{}
143+
resp, err := c.EC2.DescribeImages(context.Background(), in)
144+
if err != nil {
145+
return ret, err
146+
}
147+
for _, image := range resp.Images {
148+
ret[*image.ImageId] = true
149+
}
150+
return ret, err
151+
}
152+
116153
// ListInstanceProfiles is a wrapper of ListInstanceProfiles
117-
func (c *Client) ListInstanceProfiles() (map[string]bool, error) {
154+
func (c *AwsClient) ListInstanceProfiles() (map[string]bool, error) {
118155
ret := map[string]bool{}
119-
resp, err := c.IAM.ListInstanceProfiles(&iam.ListInstanceProfilesInput{})
156+
resp, err := c.IAM.ListInstanceProfiles(context.Background(), &iam.ListInstanceProfilesInput{})
120157
if err != nil {
121158
return ret, err
122159
}
@@ -127,9 +164,9 @@ func (c *Client) ListInstanceProfiles() (map[string]bool, error) {
127164
}
128165

129166
// DescribeKeyPairs is a wrapper of DescribeKeyPairs
130-
func (c *Client) DescribeKeyPairs() (map[string]bool, error) {
167+
func (c *AwsClient) DescribeKeyPairs() (map[string]bool, error) {
131168
ret := map[string]bool{}
132-
resp, err := c.EC2.DescribeKeyPairs(&ec2.DescribeKeyPairsInput{})
169+
resp, err := c.EC2.DescribeKeyPairs(context.Background(), &ec2.DescribeKeyPairsInput{})
133170
if err != nil {
134171
return ret, err
135172
}
@@ -140,9 +177,9 @@ func (c *Client) DescribeKeyPairs() (map[string]bool, error) {
140177
}
141178

142179
// DescribeEgressOnlyInternetGateways is wrapper of DescribeEgressOnlyInternetGateways
143-
func (c *Client) DescribeEgressOnlyInternetGateways() (map[string]bool, error) {
180+
func (c *AwsClient) DescribeEgressOnlyInternetGateways() (map[string]bool, error) {
144181
ret := map[string]bool{}
145-
resp, err := c.EC2.DescribeEgressOnlyInternetGateways(&ec2.DescribeEgressOnlyInternetGatewaysInput{})
182+
resp, err := c.EC2.DescribeEgressOnlyInternetGateways(context.Background(), &ec2.DescribeEgressOnlyInternetGatewaysInput{})
146183
if err != nil {
147184
return ret, err
148185
}
@@ -153,9 +190,9 @@ func (c *Client) DescribeEgressOnlyInternetGateways() (map[string]bool, error) {
153190
}
154191

155192
// DescribeInternetGateways is a wrapper of DescribeInternetGateways
156-
func (c *Client) DescribeInternetGateways() (map[string]bool, error) {
193+
func (c *AwsClient) DescribeInternetGateways() (map[string]bool, error) {
157194
ret := map[string]bool{}
158-
resp, err := c.EC2.DescribeInternetGateways(&ec2.DescribeInternetGatewaysInput{})
195+
resp, err := c.EC2.DescribeInternetGateways(context.Background(), &ec2.DescribeInternetGatewaysInput{})
159196
if err != nil {
160197
return ret, err
161198
}
@@ -166,9 +203,9 @@ func (c *Client) DescribeInternetGateways() (map[string]bool, error) {
166203
}
167204

168205
// DescribeNatGateways is a wrapper of DescribeNatGateways
169-
func (c *Client) DescribeNatGateways() (map[string]bool, error) {
206+
func (c *AwsClient) DescribeNatGateways() (map[string]bool, error) {
170207
ret := map[string]bool{}
171-
resp, err := c.EC2.DescribeNatGateways(&ec2.DescribeNatGatewaysInput{})
208+
resp, err := c.EC2.DescribeNatGateways(context.Background(), &ec2.DescribeNatGatewaysInput{})
172209
if err != nil {
173210
return ret, err
174211
}
@@ -179,9 +216,9 @@ func (c *Client) DescribeNatGateways() (map[string]bool, error) {
179216
}
180217

181218
// DescribeNetworkInterfaces is a wrapper of DescribeNetworkInterfaces
182-
func (c *Client) DescribeNetworkInterfaces() (map[string]bool, error) {
219+
func (c *AwsClient) DescribeNetworkInterfaces() (map[string]bool, error) {
183220
ret := map[string]bool{}
184-
resp, err := c.EC2.DescribeNetworkInterfaces(&ec2.DescribeNetworkInterfacesInput{})
221+
resp, err := c.EC2.DescribeNetworkInterfaces(context.Background(), &ec2.DescribeNetworkInterfacesInput{})
185222
if err != nil {
186223
return ret, err
187224
}
@@ -192,9 +229,9 @@ func (c *Client) DescribeNetworkInterfaces() (map[string]bool, error) {
192229
}
193230

194231
// DescribeRouteTables is a wrapper of DescribeRouteTables
195-
func (c *Client) DescribeRouteTables() (map[string]bool, error) {
232+
func (c *AwsClient) DescribeRouteTables() (map[string]bool, error) {
196233
ret := map[string]bool{}
197-
resp, err := c.EC2.DescribeRouteTables(&ec2.DescribeRouteTablesInput{})
234+
resp, err := c.EC2.DescribeRouteTables(context.Background(), &ec2.DescribeRouteTablesInput{})
198235
if err != nil {
199236
return ret, err
200237
}
@@ -205,9 +242,9 @@ func (c *Client) DescribeRouteTables() (map[string]bool, error) {
205242
}
206243

207244
// DescribeVpcPeeringConnections is a wrapper of DescribeVpcPeeringConnections
208-
func (c *Client) DescribeVpcPeeringConnections() (map[string]bool, error) {
245+
func (c *AwsClient) DescribeVpcPeeringConnections() (map[string]bool, error) {
209246
ret := map[string]bool{}
210-
resp, err := c.EC2.DescribeVpcPeeringConnections(&ec2.DescribeVpcPeeringConnectionsInput{})
247+
resp, err := c.EC2.DescribeVpcPeeringConnections(context.Background(), &ec2.DescribeVpcPeeringConnectionsInput{})
211248
if err != nil {
212249
return ret, err
213250
}

aws/client.go

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,36 @@
11
package aws
22

33
import (
4-
"github.com/aws/aws-sdk-go/service/ec2"
5-
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
6-
"github.com/aws/aws-sdk-go/service/ecs"
7-
"github.com/aws/aws-sdk-go/service/ecs/ecsiface"
8-
"github.com/aws/aws-sdk-go/service/elasticache"
9-
"github.com/aws/aws-sdk-go/service/elasticache/elasticacheiface"
10-
"github.com/aws/aws-sdk-go/service/elb"
11-
"github.com/aws/aws-sdk-go/service/elb/elbiface"
12-
"github.com/aws/aws-sdk-go/service/elbv2"
13-
"github.com/aws/aws-sdk-go/service/elbv2/elbv2iface"
14-
"github.com/aws/aws-sdk-go/service/iam"
15-
"github.com/aws/aws-sdk-go/service/iam/iamiface"
16-
"github.com/aws/aws-sdk-go/service/rds"
17-
"github.com/aws/aws-sdk-go/service/rds/rdsiface"
18-
awsbase "github.com/hashicorp/aws-sdk-go-base"
4+
"context"
5+
"errors"
6+
"fmt"
7+
8+
"github.com/aws/aws-sdk-go-v2/service/ec2"
9+
"github.com/aws/aws-sdk-go-v2/service/ecs"
10+
"github.com/aws/aws-sdk-go-v2/service/elasticache"
11+
"github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing"
12+
"github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
13+
"github.com/aws/aws-sdk-go-v2/service/iam"
14+
"github.com/aws/aws-sdk-go-v2/service/rds"
15+
awsbase "github.com/hashicorp/aws-sdk-go-base/v2"
1916
"github.com/mitchellh/go-homedir"
2017
"github.com/terraform-linters/tflint-plugin-sdk/logger"
2118
)
2219

23-
//go:generate go run github.com/golang/mock/mockgen -destination mock/ec2.go -package mock github.com/aws/aws-sdk-go/service/ec2/ec2iface EC2API
24-
//go:generate go run github.com/golang/mock/mockgen -destination mock/elasticache.go -package mock github.com/aws/aws-sdk-go/service/elasticache/elasticacheiface ElastiCacheAPI
25-
//go:generate go run github.com/golang/mock/mockgen -destination mock/elb.go -package mock github.com/aws/aws-sdk-go/service/elb/elbiface ELBAPI
26-
//go:generate go run github.com/golang/mock/mockgen -destination mock/elbv2.go -package mock github.com/aws/aws-sdk-go/service/elbv2/elbv2iface ELBV2API
27-
//go:generate go run github.com/golang/mock/mockgen -destination mock/iam.go -package mock github.com/aws/aws-sdk-go/service/iam/iamiface IAMAPI
28-
//go:generate go run github.com/golang/mock/mockgen -destination mock/rds.go -package mock github.com/aws/aws-sdk-go/service/rds/rdsiface RDSAPI
29-
//go:generate go run github.com/golang/mock/mockgen -destination mock/ecs.go -package mock github.com/aws/aws-sdk-go/service/ecs/ecsiface ECSAPI
30-
31-
// Client is a wrapper of the AWS SDK client
32-
// It has interfaces for each services to make testing easier
33-
type Client struct {
34-
IAM iamiface.IAMAPI
35-
EC2 ec2iface.EC2API
36-
RDS rdsiface.RDSAPI
37-
ElastiCache elasticacheiface.ElastiCacheAPI
38-
ELB elbiface.ELBAPI
39-
ELBV2 elbv2iface.ELBV2API
40-
ECS ecsiface.ECSAPI
20+
// AwsClient is a wrapper of the AWS SDK client.
21+
// This is the real implementation that satisfies the interface.
22+
type AwsClient struct {
23+
IAM *iam.Client
24+
EC2 *ec2.Client
25+
RDS *rds.Client
26+
ElastiCache *elasticache.Client
27+
ELB *elasticloadbalancing.Client
28+
ELBV2 *elasticloadbalancingv2.Client
29+
ECS *ecs.Client
4130
}
4231

32+
var _ Client = (*AwsClient)(nil)
33+
4334
// Credentials is credentials for AWS used in deep check mode
4435
type Credentials struct {
4536
AccessKey string
@@ -54,27 +45,30 @@ type Credentials struct {
5445
}
5546

5647
// NewClient returns a new Client with configured session
57-
func NewClient(creds Credentials) (*Client, error) {
48+
func NewClient(creds Credentials) (Client, error) {
5849
logger.Info("Initialize AWS Client")
5950

6051
config, err := getBaseConfig(creds)
6152
if err != nil {
6253
return nil, err
6354
}
6455

65-
s, err := awsbase.GetSession(config)
56+
_, awsConfig, diags := awsbase.GetAwsConfig(context.Background(), config)
57+
for _, diag := range diags.Errors() {
58+
err = errors.Join(err, fmt.Errorf("%s; %s", diag.Summary(), diag.Detail()))
59+
}
6660
if err != nil {
6761
return nil, err
6862
}
6963

70-
return &Client{
71-
IAM: iam.New(s),
72-
EC2: ec2.New(s),
73-
RDS: rds.New(s),
74-
ElastiCache: elasticache.New(s),
75-
ELB: elb.New(s),
76-
ELBV2: elbv2.New(s),
77-
ECS: ecs.New(s),
64+
return &AwsClient{
65+
IAM: iam.NewFromConfig(awsConfig),
66+
EC2: ec2.NewFromConfig(awsConfig),
67+
RDS: rds.NewFromConfig(awsConfig),
68+
ElastiCache: elasticache.NewFromConfig(awsConfig),
69+
ELB: elasticloadbalancing.NewFromConfig(awsConfig),
70+
ELBV2: elasticloadbalancingv2.NewFromConfig(awsConfig),
71+
ECS: ecs.NewFromConfig(awsConfig),
7872
}, nil
7973
}
8074

@@ -84,19 +78,28 @@ func getBaseConfig(creds Credentials) (*awsbase.Config, error) {
8478
return nil, err
8579
}
8680

87-
return &awsbase.Config{
81+
config := &awsbase.Config{
8882
AccessKey: creds.AccessKey,
89-
AssumeRoleARN: creds.AssumeRoleARN,
90-
AssumeRoleExternalID: creds.AssumeRoleExternalID,
91-
AssumeRolePolicy: creds.AssumeRolePolicy,
92-
AssumeRoleSessionName: creds.AssumeRoleSessionName,
9383
SecretKey: creds.SecretKey,
9484
Profile: creds.Profile,
95-
CredsFilename: expandedCredsFile,
9685
Region: creds.Region,
9786
CallerName: "tflint-ruleset-aws",
9887
CallerDocumentationURL: "https://github.com/terraform-linters/tflint-ruleset-aws/blob/master/docs/deep_checking.md",
99-
}, nil
88+
}
89+
90+
if creds.AssumeRoleARN != "" || creds.AssumeRoleExternalID != "" || creds.AssumeRolePolicy != "" || creds.AssumeRoleSessionName != "" {
91+
config.AssumeRole = &awsbase.AssumeRole{
92+
RoleARN: creds.AssumeRoleARN,
93+
ExternalID: creds.AssumeRoleExternalID,
94+
Policy: creds.AssumeRolePolicy,
95+
SessionName: creds.AssumeRoleSessionName,
96+
}
97+
}
98+
if expandedCredsFile != "" {
99+
config.SharedCredentialsFiles = []string{expandedCredsFile}
100+
}
101+
102+
return config, nil
100103
}
101104

102105
// Merge returns a merged credentials

0 commit comments

Comments
 (0)