Skip to content

Commit

Permalink
chore(resources): migrating tgw route tables to aws sdk v2
Browse files Browse the repository at this point in the history
Signed-off-by: Scott Crooks <scott.crooks@gmail.com>
  • Loading branch information
sc250024 committed Dec 5, 2024
1 parent 3051357 commit 0d01135
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
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
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

0 comments on commit 0d01135

Please sign in to comment.