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 TGW Route Table to AWS SDK v2 #801

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
22 changes: 11 additions & 11 deletions aws/resources/tgw_route_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package resources
import (
"context"

"github.com/aws/aws-sdk-go/aws"
awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"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/go-commons/errors"
Expand All @@ -15,25 +16,24 @@ import (
func (tgw *TransitGatewaysRouteTables) getAll(c context.Context, configObj config.Config) ([]*string, error) {
// Remove default route table, that will be deleted along with its TransitGateway
param := &ec2.DescribeTransitGatewayRouteTablesInput{
Filters: []*ec2.Filter{
Filters: []types.Filter{
{
Name: aws.String("default-association-route-table"),
Values: []*string{
aws.String("false"),
},
Name: aws.String("default-association-route-table"),
Values: []string{"false"},
},
},
}

result, err := tgw.Client.DescribeTransitGatewayRouteTablesWithContext(tgw.Context, param)
result, err := tgw.Client.DescribeTransitGatewayRouteTables(tgw.Context, param)
if err != nil {
return nil, errors.WithStackTrace(err)
}

var ids []*string
for _, transitGatewayRouteTable := range result.TransitGatewayRouteTables {
if configObj.TransitGatewayRouteTable.ShouldInclude(config.ResourceValue{Time: transitGatewayRouteTable.CreationTime}) &&
awsgo.StringValue(transitGatewayRouteTable.State) != "deleted" && awsgo.StringValue(transitGatewayRouteTable.State) != "deleting" {
transitGatewayRouteTable.State != types.TransitGatewayRouteTableStateDeleted &&
transitGatewayRouteTable.State != types.TransitGatewayRouteTableStateDeleting {
ids = append(ids, transitGatewayRouteTable.TransitGatewayRouteTableId)
}
}
Expand All @@ -56,7 +56,7 @@ func (tgw *TransitGatewaysRouteTables) nukeAll(ids []*string) error {
TransitGatewayRouteTableId: id,
}

_, err := tgw.Client.DeleteTransitGatewayRouteTableWithContext(tgw.Context, param)
_, err := tgw.Client.DeleteTransitGatewayRouteTable(tgw.Context, param)
if err != nil {
logging.Debugf("[Failed] %s", err)
} else {
Expand Down
23 changes: 11 additions & 12 deletions aws/resources/tgw_route_tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,25 @@ import (
"testing"
"time"

"github.com/aws/aws-sdk-go/aws"
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"
"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/stretchr/testify/require"
)

type mockedTransitGatewayRouteTable struct {
ec2iface.EC2API
TransitGatewaysRouteTablesAPI
DescribeTransitGatewayRouteTablesOutput ec2.DescribeTransitGatewayRouteTablesOutput
DeleteTransitGatewayRouteTableOutput ec2.DeleteTransitGatewayRouteTableOutput
}

func (m mockedTransitGatewayRouteTable) DescribeTransitGatewayRouteTablesWithContext(_ awsgo.Context, _ *ec2.DescribeTransitGatewayRouteTablesInput, _ ...request.Option) (*ec2.DescribeTransitGatewayRouteTablesOutput, error) {
func (m mockedTransitGatewayRouteTable) DescribeTransitGatewayRouteTables(_ context.Context, _ *ec2.DescribeTransitGatewayRouteTablesInput, _ ...func(*ec2.Options)) (*ec2.DescribeTransitGatewayRouteTablesOutput, error) {
return &m.DescribeTransitGatewayRouteTablesOutput, nil
}

func (m mockedTransitGatewayRouteTable) DeleteTransitGatewayRouteTableWithContext(_ awsgo.Context, _ *ec2.DeleteTransitGatewayRouteTableInput, _ ...request.Option) (*ec2.DeleteTransitGatewayRouteTableOutput, error) {
func (m mockedTransitGatewayRouteTable) DeleteTransitGatewayRouteTable(_ context.Context, _ *ec2.DeleteTransitGatewayRouteTableInput, _ ...func(*ec2.Options)) (*ec2.DeleteTransitGatewayRouteTableOutput, error) {
return &m.DeleteTransitGatewayRouteTableOutput, nil
}
func TestTransitGatewayRouteTables_GetAll(t *testing.T) {
Expand All @@ -37,16 +36,16 @@ func TestTransitGatewayRouteTables_GetAll(t *testing.T) {
tgw := TransitGatewaysRouteTables{
Client: mockedTransitGatewayRouteTable{
DescribeTransitGatewayRouteTablesOutput: ec2.DescribeTransitGatewayRouteTablesOutput{
TransitGatewayRouteTables: []*ec2.TransitGatewayRouteTable{
TransitGatewayRouteTables: []types.TransitGatewayRouteTable{
{
TransitGatewayRouteTableId: aws.String(tableId1),
CreationTime: aws.Time(now),
State: aws.String("available"),
State: "available",
},
{
TransitGatewayRouteTableId: aws.String(tableId2),
CreationTime: aws.Time(now.Add(1)),
State: aws.String("deleting"),
State: "deleting",
},
},
},
Expand Down Expand Up @@ -74,7 +73,7 @@ func TestTransitGatewayRouteTables_GetAll(t *testing.T) {
TransitGatewayRouteTable: tc.configObj,
})
require.NoError(t, err)
require.Equal(t, tc.expected, aws.StringValueSlice(names))
require.Equal(t, tc.expected, aws.ToStringSlice(names))
})
}
}
Expand Down
24 changes: 15 additions & 9 deletions aws/resources/tgw_route_tables_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +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"
"github.com/aws/aws-sdk-go-v2/service/ec2"

"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/go-commons/errors"
)

type TransitGatewaysRouteTablesAPI interface {
DeleteTransitGatewayRouteTable(ctx context.Context, params *ec2.DeleteTransitGatewayRouteTableInput, optFns ...func(*ec2.Options)) (*ec2.DeleteTransitGatewayRouteTableOutput, error)
DescribeTransitGatewayRouteTables(ctx context.Context, params *ec2.DescribeTransitGatewayRouteTablesInput, optFns ...func(*ec2.Options)) (*ec2.DescribeTransitGatewayRouteTablesOutput, error)
}

// TransitGatewaysRouteTables - represents all transit gateways route tables
type TransitGatewaysRouteTables struct {
BaseAwsResource
Client ec2iface.EC2API
Client TransitGatewaysRouteTablesAPI
Region string
Ids []string
}

func (tgw *TransitGatewaysRouteTables) Init(session *session.Session) {
tgw.Client = ec2.New(session)
func (tgw *TransitGatewaysRouteTables) InitV2(cfg aws.Config) {
tgw.Client = ec2.NewFromConfig(cfg)
}

func (tgw *TransitGatewaysRouteTables) IsUsingV2() bool { return true }

// ResourceName - the simple name of the aws resource
func (tgw *TransitGatewaysRouteTables) ResourceName() string {
return "transit-gateway-route-table"
Expand All @@ -44,13 +50,13 @@ func (tgw *TransitGatewaysRouteTables) GetAndSetIdentifiers(c context.Context, c
return nil, err
}

tgw.Ids = awsgo.StringValueSlice(identifiers)
tgw.Ids = aws.ToStringSlice(identifiers)
return tgw.Ids, nil
}

// Nuke - nuke 'em all!!!
func (tgw *TransitGatewaysRouteTables) Nuke(identifiers []string) error {
if err := tgw.nukeAll(awsgo.StringSlice(identifiers)); err != nil {
if err := tgw.nukeAll(aws.StringSlice(identifiers)); err != nil {
return errors.WithStackTrace(err)
}

Expand Down
2 changes: 1 addition & 1 deletion v2_migration_report/output.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ run `go generate ./...` to refresh this report.
| sqs | :white_check_mark: |
| transit-gateway | :white_check_mark: |
| transit-gateway-attachment | :white_check_mark: |
| transit-gateway-route-table | |
| transit-gateway-route-table | :white_check_mark: |
| vpc | :white_check_mark: |
| vpc-lattice-service | :white_check_mark: |
| vpc-lattice-service-network | :white_check_mark: |
Expand Down