Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to AWS SDK v2 for vpc and dependant services #797

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions aws/resources/ec2_egress_only_igw.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import (
"fmt"
"time"

awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
awsgo "github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/logging"
"github.com/gruntwork-io/cloud-nuke/report"
"github.com/gruntwork-io/cloud-nuke/util"
"github.com/gruntwork-io/go-commons/errors"
)

func shouldIncludeEgressOnlyInternetGateway(gateway *ec2.EgressOnlyInternetGateway, firstSeenTime *time.Time, configObj config.Config) bool {
func shouldIncludeEgressOnlyInternetGateway(gateway types.EgressOnlyInternetGateway, firstSeenTime *time.Time, configObj config.Config) bool {
var gatewayName string
// get the tags as map
tagMap := util.ConvertEC2TagsToMap(gateway.Tags)
tagMap := util.ConvertTypesTagsToMap(gateway.Tags)
if name, ok := tagMap["Name"]; ok {
gatewayName = name
}
Expand All @@ -34,13 +34,13 @@ func (egigw *EgressOnlyInternetGateway) getAll(c context.Context, configObj conf
var firstSeenTime *time.Time
var err error

output, err := egigw.Client.DescribeEgressOnlyInternetGatewaysWithContext(egigw.Context, &ec2.DescribeEgressOnlyInternetGatewaysInput{})
output, err := egigw.Client.DescribeEgressOnlyInternetGateways(egigw.Context, &ec2.DescribeEgressOnlyInternetGatewaysInput{})
if err != nil {
return nil, errors.WithStackTrace(err)
}

for _, igw := range output.EgressOnlyInternetGateways {
firstSeenTime, err = util.GetOrCreateFirstSeen(c, egigw.Client, igw.EgressOnlyInternetGatewayId, util.ConvertEC2TagsToMap(igw.Tags))
firstSeenTime, err = util.GetOrCreateFirstSeen(c, egigw.Client, igw.EgressOnlyInternetGatewayId, util.ConvertTypesTagsToMap(igw.Tags))
if err != nil {
logging.Error("Unable to retrieve tags")
return nil, errors.WithStackTrace(err)
Expand All @@ -51,7 +51,7 @@ func (egigw *EgressOnlyInternetGateway) getAll(c context.Context, configObj conf
}
// checking the nukable permissions
egigw.VerifyNukablePermissions(result, func(id *string) error {
_, err := egigw.Client.DeleteEgressOnlyInternetGatewayWithContext(egigw.Context, &ec2.DeleteEgressOnlyInternetGatewayInput{
_, err := egigw.Client.DeleteEgressOnlyInternetGateway(egigw.Context, &ec2.DeleteEgressOnlyInternetGatewayInput{
EgressOnlyInternetGatewayId: id,
DryRun: awsgo.Bool(true),
})
Expand Down Expand Up @@ -82,7 +82,7 @@ func (egigw *EgressOnlyInternetGateway) nukeAll(ids []*string) error {

// Record status of this resource
e := report.Entry{
Identifier: awsgo.StringValue(id),
Identifier: awsgo.ToString(id),
ResourceType: "Egress Only Internet Gateway",
Error: err,
}
Expand All @@ -102,10 +102,10 @@ func (egigw *EgressOnlyInternetGateway) nukeAll(ids []*string) error {

}

func nukeEgressOnlyGateway(client ec2iface.EC2API, gateway *string) error {
logging.Debugf("[Nuke] Egress only gateway %s", awsgo.StringValue(gateway))
func nukeEgressOnlyGateway(client EgressOnlyIGAPI, gateway *string) error {
logging.Debugf("[Nuke] Egress only gateway %s", awsgo.ToString(gateway))

_, err := client.DeleteEgressOnlyInternetGateway(&ec2.DeleteEgressOnlyInternetGatewayInput{
_, err := client.DeleteEgressOnlyInternetGateway(context.Background(), &ec2.DeleteEgressOnlyInternetGatewayInput{
EgressOnlyInternetGatewayId: gateway,
})
if err != nil {
Expand Down
35 changes: 15 additions & 20 deletions aws/resources/ec2_egress_only_igw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,26 @@ import (
"testing"
"time"

awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
awsgo "github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/util"
"github.com/stretchr/testify/require"
)

type mockedEgressOnlyIgw struct {
BaseAwsResource
ec2iface.EC2API
EgressOnlyIGAPI
DescribeEgressOnlyInternetGatewaysOutput ec2.DescribeEgressOnlyInternetGatewaysOutput
DeleteEgressOnlyInternetGatewayOutput ec2.DeleteEgressOnlyInternetGatewayOutput
}

func (m mockedEgressOnlyIgw) DescribeEgressOnlyInternetGatewaysWithContext(_ awsgo.Context, _ *ec2.DescribeEgressOnlyInternetGatewaysInput, _ ...request.Option) (*ec2.DescribeEgressOnlyInternetGatewaysOutput, error) {
func (m mockedEgressOnlyIgw) DescribeEgressOnlyInternetGateways(ctx context.Context, params *ec2.DescribeEgressOnlyInternetGatewaysInput, optFns ...func(*ec2.Options)) (*ec2.DescribeEgressOnlyInternetGatewaysOutput, error) {
return &m.DescribeEgressOnlyInternetGatewaysOutput, nil
}

func (m mockedEgressOnlyIgw) DeleteEgressOnlyInternetGatewayWithContext(_ awsgo.Context, _ *ec2.DeleteEgressOnlyInternetGatewayInput, _ ...request.Option) (*ec2.DeleteEgressOnlyInternetGatewayOutput, error) {
return &m.DeleteEgressOnlyInternetGatewayOutput, nil
}

func (m mockedEgressOnlyIgw) DeleteEgressOnlyInternetGateway(_ *ec2.DeleteEgressOnlyInternetGatewayInput) (*ec2.DeleteEgressOnlyInternetGatewayOutput, error) {
func (m mockedEgressOnlyIgw) DeleteEgressOnlyInternetGateway(ctx context.Context, params *ec2.DeleteEgressOnlyInternetGatewayInput, optFns ...func(*ec2.Options)) (*ec2.DeleteEgressOnlyInternetGatewayOutput, error) {
return &m.DeleteEgressOnlyInternetGatewayOutput, nil
}

Expand All @@ -52,10 +47,10 @@ func TestEgressOnlyInternetGateway_GetAll(t *testing.T) {
object := EgressOnlyInternetGateway{
Client: mockedEgressOnlyIgw{
DescribeEgressOnlyInternetGatewaysOutput: ec2.DescribeEgressOnlyInternetGatewaysOutput{
EgressOnlyInternetGateways: []*ec2.EgressOnlyInternetGateway{
EgressOnlyInternetGateways: []types.EgressOnlyInternetGateway{
{
EgressOnlyInternetGatewayId: awsgo.String(gateway1),
Tags: []*ec2.Tag{
Tags: []types.Tag{
{
Key: awsgo.String("Name"),
Value: awsgo.String(testName1),
Expand All @@ -67,7 +62,7 @@ func TestEgressOnlyInternetGateway_GetAll(t *testing.T) {
},
{
EgressOnlyInternetGatewayId: awsgo.String(gateway2),
Tags: []*ec2.Tag{
Tags: []types.Tag{
{
Key: awsgo.String("Name"),
Value: awsgo.String(testName2),
Expand Down Expand Up @@ -126,7 +121,7 @@ func TestEgressOnlyInternetGateway_GetAll(t *testing.T) {
EgressOnlyInternetGateway: tc.configObj,
})
require.NoError(t, err)
require.Equal(t, tc.expected, awsgo.StringValueSlice(names))
require.Equal(t, tc.expected, awsgo.ToStringSlice(names))
})
}

Expand All @@ -149,21 +144,21 @@ func TestEc2EgressOnlyInternetGateway_NukeAll(t *testing.T) {
},
Client: mockedEgressOnlyIgw{
DescribeEgressOnlyInternetGatewaysOutput: ec2.DescribeEgressOnlyInternetGatewaysOutput{
EgressOnlyInternetGateways: []*ec2.EgressOnlyInternetGateway{
EgressOnlyInternetGateways: []types.EgressOnlyInternetGateway{
{
EgressOnlyInternetGatewayId: awsgo.String(gateway1),
Attachments: []*ec2.InternetGatewayAttachment{
Attachments: []types.InternetGatewayAttachment{
{
State: awsgo.String("testing-state"),
State: "testing-state",
VpcId: awsgo.String("test-gateway-vpc"),
},
},
},
{
EgressOnlyInternetGatewayId: awsgo.String(gateway2),
Attachments: []*ec2.InternetGatewayAttachment{
Attachments: []types.InternetGatewayAttachment{
{
State: awsgo.String("testing-state"),
State: "testing-state",
VpcId: awsgo.String("test-gateway-vpc"),
},
},
Expand Down
23 changes: 14 additions & 9 deletions aws/resources/ec2_egress_only_igw_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,32 @@ package resources
import (
"context"

awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/aws/aws-sdk-go-v2/aws"
awsgo "github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/go-commons/errors"
)

type EgressOnlyIGAPI interface {
DescribeEgressOnlyInternetGateways(ctx context.Context, params *ec2.DescribeEgressOnlyInternetGatewaysInput, optFns ...func(*ec2.Options)) (*ec2.DescribeEgressOnlyInternetGatewaysOutput, error)
DeleteEgressOnlyInternetGateway(ctx context.Context, params *ec2.DeleteEgressOnlyInternetGatewayInput, optFns ...func(*ec2.Options)) (*ec2.DeleteEgressOnlyInternetGatewayOutput, error)
}

// EgressOnlyInternetGateway represents all Egress only internet gateway
type EgressOnlyInternetGateway struct {
BaseAwsResource
Client ec2iface.EC2API
Client EgressOnlyIGAPI
Region string
Pools []string
}

func (egigw *EgressOnlyInternetGateway) Init(session *session.Session) {
egigw.BaseAwsResource.Init(session)
egigw.Client = ec2.New(session)
func (egigw *EgressOnlyInternetGateway) InitV2(cfg aws.Config) {
egigw.Client = ec2.NewFromConfig(cfg)
}

func (egigw *EgressOnlyInternetGateway) IsUsingV2() bool { return true }

// ResourceName - the simple name of the aws resource
func (egigw *EgressOnlyInternetGateway) ResourceName() string {
return "egress-only-internet-gateway"
Expand All @@ -49,7 +54,7 @@ func (egigw *EgressOnlyInternetGateway) GetAndSetIdentifiers(c context.Context,
return nil, err
}

egigw.Pools = awsgo.StringValueSlice(identifiers)
egigw.Pools = awsgo.ToStringSlice(identifiers)
return egigw.Pools, nil
}

Expand Down
6 changes: 3 additions & 3 deletions aws/resources/ec2_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (e *EC2Endpoints) nukeAll(identifiers []*string) error {
continue
}

err := e.nukeVpcEndpoint([]*string{id})
err := nukeVpcEndpoint(e.Client, []*string{id})

// Record status of this resource
e := report.Entry{
Expand All @@ -102,10 +102,10 @@ func (e *EC2Endpoints) nukeAll(identifiers []*string) error {
return nil
}

func (e *EC2Endpoints) nukeVpcEndpoint(endpointIds []*string) error {
func nukeVpcEndpoint(client EC2EndpointsAPI, endpointIds []*string) error {
logging.Debugf("Deleting VPC endpoints %s", aws.ToStringSlice(endpointIds))

_, err := e.Client.DeleteVpcEndpoints(e.Context, &ec2.DeleteVpcEndpointsInput{
_, err := client.DeleteVpcEndpoints(context.Background(), &ec2.DeleteVpcEndpointsInput{
VpcEndpointIds: aws.ToStringSlice(endpointIds),
})
if err != nil {
Expand Down
44 changes: 22 additions & 22 deletions aws/resources/ec2_internet_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import (
"fmt"
"time"

awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
awsgo "github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/logging"
r "github.com/gruntwork-io/cloud-nuke/report" // Alias the package as 'r'
"github.com/gruntwork-io/cloud-nuke/util"
"github.com/gruntwork-io/go-commons/errors"
)

func shouldIncludeGateway(ig *ec2.InternetGateway, firstSeenTime *time.Time, configObj config.Config) bool {
func shouldIncludeGateway(ig types.InternetGateway, firstSeenTime *time.Time, configObj config.Config) bool {
var internetGateway string
// get the tags as map
tagMap := util.ConvertEC2TagsToMap(ig.Tags)
tagMap := util.ConvertTypesTagsToMap(ig.Tags)
if name, ok := tagMap["Name"]; ok {
internetGateway = name
}
Expand All @@ -36,13 +36,13 @@ func (igw *InternetGateway) getAll(c context.Context, configObj config.Config) (
var err error

input := &ec2.DescribeInternetGatewaysInput{}
resp, err := igw.Client.DescribeInternetGatewaysWithContext(igw.Context, input)
resp, err := igw.Client.DescribeInternetGateways(igw.Context, input)
if err != nil {
logging.Debugf("[Internet Gateway] Failed to list internet gateways: %s", err)
return nil, err
}
for _, ig := range resp.InternetGateways {
firstSeenTime, err = util.GetOrCreateFirstSeen(c, igw.Client, ig.InternetGatewayId, util.ConvertEC2TagsToMap(ig.Tags))
firstSeenTime, err = util.GetOrCreateFirstSeen(c, igw.Client, ig.InternetGatewayId, util.ConvertTypesTagsToMap(ig.Tags))
if err != nil {
logging.Error("Unable to retrieve tags")
return nil, errors.WithStackTrace(err)
Expand All @@ -53,7 +53,7 @@ func (igw *InternetGateway) getAll(c context.Context, configObj config.Config) (

// get vpc id for this igw and update the map
if len(ig.Attachments) > 0 {
igw.GatewayVPCMap[awsgo.StringValue(ig.InternetGatewayId)] = awsgo.StringValue(ig.Attachments[0].VpcId)
igw.GatewayVPCMap[awsgo.ToString(ig.InternetGatewayId)] = awsgo.ToString(ig.Attachments[0].VpcId)
}
}
}
Expand All @@ -64,7 +64,7 @@ func (igw *InternetGateway) getAll(c context.Context, configObj config.Config) (
InternetGatewayId: id,
DryRun: awsgo.Bool(true),
}
_, err := igw.Client.DeleteInternetGatewayWithContext(igw.Context, params)
_, err := igw.Client.DeleteInternetGateway(igw.Context, params)
return err
})

Expand All @@ -89,7 +89,7 @@ func (igw *InternetGateway) nukeAll(identifiers []*string) error {
err := igw.nuke(id)
// Record status of this resource
e := r.Entry{ // Use the 'r' alias to refer to the package
Identifier: awsgo.StringValue(id),
Identifier: awsgo.ToString(id),
ResourceType: "Internet Gateway",
Error: err,
}
Expand All @@ -107,12 +107,12 @@ func (igw *InternetGateway) nukeAll(identifiers []*string) error {

func (igw *InternetGateway) nuke(id *string) error {
// get the vpc id for current igw
vpcID, ok := igw.GatewayVPCMap[awsgo.StringValue(id)]
vpcID, ok := igw.GatewayVPCMap[awsgo.ToString(id)]
if !ok {
logging.Debug(fmt.Sprintf("Failed to read the vpc Id for %s",
awsgo.StringValue(id)))
awsgo.ToString(id)))
return fmt.Errorf("Failed to retrieve the VPC ID for %s, which is mandatory for the internet gateway nuke operation.",
awsgo.StringValue(id))
awsgo.ToString(id))
}

err := nukeInternetGateway(igw.Client, id, vpcID)
Expand All @@ -123,39 +123,39 @@ func (igw *InternetGateway) nuke(id *string) error {
return nil
}

func nukeInternetGateway(client ec2iface.EC2API, gatewayId *string, vpcID string) error {
func nukeInternetGateway(client InternetGatewayAPI, gatewayId *string, vpcID string) error {
var err error
logging.Debug(fmt.Sprintf("Detaching Internet Gateway %s",
awsgo.StringValue(gatewayId)))
_, err = client.DetachInternetGateway(
awsgo.ToString(gatewayId)))
_, err = client.DetachInternetGateway(context.Background(),
&ec2.DetachInternetGatewayInput{
InternetGatewayId: gatewayId,
VpcId: awsgo.String(vpcID),
},
)
if err != nil {
logging.Debug(fmt.Sprintf("Failed to detach internet gateway %s",
awsgo.StringValue(gatewayId)))
awsgo.ToString(gatewayId)))
return errors.WithStackTrace(err)
}
logging.Debug(fmt.Sprintf("Successfully detached internet gateway %s",
awsgo.StringValue(gatewayId)))
awsgo.ToString(gatewayId)))

// nuking the internet gateway
logging.Debug(fmt.Sprintf("Deleting internet gateway %s",
awsgo.StringValue(gatewayId)))
_, err = client.DeleteInternetGateway(
awsgo.ToString(gatewayId)))
_, err = client.DeleteInternetGateway(context.Background(),
&ec2.DeleteInternetGatewayInput{
InternetGatewayId: gatewayId,
},
)
if err != nil {
logging.Debug(fmt.Sprintf("Failed to delete internet gateway %s",
awsgo.StringValue(gatewayId)))
awsgo.ToString(gatewayId)))
return errors.WithStackTrace(err)
}
logging.Debug(fmt.Sprintf("Successfully deleted internet gateway %s",
awsgo.StringValue(gatewayId)))
awsgo.ToString(gatewayId)))

return nil

Expand Down
Loading