Skip to content

Commit

Permalink
change port route53 cidr collection, route53 hostedzone, route53 traf…
Browse files Browse the repository at this point in the history
…fic policy to awsSDKv2 gruntwork-io#770
  • Loading branch information
wakeful committed Nov 25, 2024
1 parent c7caecb commit ba8d1ee
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 145 deletions.
28 changes: 15 additions & 13 deletions aws/resources/route53_cidr_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package resources
import (
"context"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/route53"
"github.com/aws/aws-sdk-go-v2/service/route53/types"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/logging"
"github.com/gruntwork-io/cloud-nuke/report"
)

func (r *Route53CidrCollection) getAll(c context.Context, configObj config.Config) ([]*string, error) {
func (r *Route53CidrCollection) getAll(_ context.Context, configObj config.Config) ([]*string, error) {
var ids []*string

result, err := r.Client.ListCidrCollectionsWithContext(r.Context, &route53.ListCidrCollectionsInput{})
result, err := r.Client.ListCidrCollections(r.Context, &route53.ListCidrCollectionsInput{})
if err != nil {
logging.Errorf("[Failed] unable to list cidr collection: %s", err)
return nil, err
Expand All @@ -26,30 +27,31 @@ func (r *Route53CidrCollection) getAll(c context.Context, configObj config.Confi
ids = append(ids, r.Id)
}
}

return ids, nil
}

func (r *Route53CidrCollection) nukeCidrLocations(id *string) (err error) {
// get attached cidr blocks
loc, err := r.Client.ListCidrBlocksWithContext(r.Context, &route53.ListCidrBlocksInput{
loc, err := r.Client.ListCidrBlocks(r.Context, &route53.ListCidrBlocksInput{
CollectionId: id,
})
if err != nil {
logging.Errorf("[Failed] unable to list cidr blocks: %v", err)
return err
}

var changes []*route53.CidrCollectionChange
var changes []types.CidrCollectionChange
for _, block := range loc.CidrBlocks {
changes = append(changes, &route53.CidrCollectionChange{
CidrList: []*string{block.CidrBlock},
Action: aws.String("DELETE_IF_EXISTS"),
changes = append(changes, types.CidrCollectionChange{
CidrList: []string{aws.ToString(block.CidrBlock)},
Action: types.CidrCollectionChangeActionDeleteIfExists,
LocationName: block.LocationName,
})
}

if len(changes) > 0 {
_, err = r.Client.ChangeCidrCollectionWithContext(r.Context, &route53.ChangeCidrCollectionInput{
_, err = r.Client.ChangeCidrCollection(r.Context, &route53.ChangeCidrCollectionInput{
Id: id,
Changes: changes,
})
Expand Down Expand Up @@ -81,7 +83,7 @@ func (r *Route53CidrCollection) nukeAll(identifiers []*string) (err error) {
}

// delete the cidr collection
if _, err = r.Client.DeleteCidrCollectionWithContext(r.Context, &route53.DeleteCidrCollectionInput{
if _, err = r.Client.DeleteCidrCollection(r.Context, &route53.DeleteCidrCollectionInput{
Id: id,
}); err != nil {
logging.Errorf("[Failed] unable to nuke the cidr collection: %v ", err)
Expand All @@ -93,7 +95,7 @@ func (r *Route53CidrCollection) nukeAll(identifiers []*string) (err error) {

// Record status of this resource
e := report.Entry{
Identifier: aws.StringValue(id),
Identifier: aws.ToString(id),
ResourceType: "Route53 cidr collection",
Error: err,
}
Expand All @@ -103,7 +105,7 @@ func (r *Route53CidrCollection) nukeAll(identifiers []*string) (err error) {
logging.Errorf("[Failed] %s: %s", *id, err)
} else {
deletedIds = append(deletedIds, id)
logging.Debugf("Deleted Route53 cidr collection: %s", aws.StringValue(id))
logging.Debugf("Deleted Route53 cidr collection: %s", aws.ToString(id))
}
}

Expand Down
30 changes: 15 additions & 15 deletions aws/resources/route53_cidr_collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,34 @@ import (
"regexp"
"testing"

"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/route53"
"github.com/aws/aws-sdk-go/service/route53/route53iface"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/route53"
"github.com/aws/aws-sdk-go-v2/service/route53/types"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/stretchr/testify/require"
)

type mockedR53CidrCollection struct {
route53iface.Route53API
Route53CidrCollectionAPI
ListCidrCollectionsOutput route53.ListCidrCollectionsOutput
ListCidrBlocksOutput route53.ListCidrBlocksOutput
ChangeCidrCollectionOutput route53.ChangeCidrCollectionOutput
ListCidrCollectionsOutput route53.ListCidrCollectionsOutput
DeleteCidrCollectionOutput route53.DeleteCidrCollectionOutput
}

func (mock mockedR53CidrCollection) ListCidrBlocksWithContext(_ awsgo.Context, _ *route53.ListCidrBlocksInput, _ ...request.Option) (*route53.ListCidrBlocksOutput, error) {
func (mock mockedR53CidrCollection) ListCidrBlocks(_ context.Context, _ *route53.ListCidrBlocksInput, _ ...func(*route53.Options)) (*route53.ListCidrBlocksOutput, error) {
return &mock.ListCidrBlocksOutput, nil
}

func (mock mockedR53CidrCollection) ChangeCidrCollectionWithContext(_ awsgo.Context, _ *route53.ChangeCidrCollectionInput, _ ...request.Option) (*route53.ChangeCidrCollectionOutput, error) {
func (mock mockedR53CidrCollection) ChangeCidrCollection(_ context.Context, _ *route53.ChangeCidrCollectionInput, _ ...func(*route53.Options)) (*route53.ChangeCidrCollectionOutput, error) {
return &mock.ChangeCidrCollectionOutput, nil
}
func (mock mockedR53CidrCollection) ListCidrCollectionsWithContext(_ awsgo.Context, _ *route53.ListCidrCollectionsInput, _ ...request.Option) (*route53.ListCidrCollectionsOutput, error) {

func (mock mockedR53CidrCollection) ListCidrCollections(_ context.Context, _ *route53.ListCidrCollectionsInput, _ ...func(*route53.Options)) (*route53.ListCidrCollectionsOutput, error) {
return &mock.ListCidrCollectionsOutput, nil
}
func (mock mockedR53CidrCollection) DeleteCidrCollectionWithContext(_ awsgo.Context, _ *route53.DeleteCidrCollectionInput, _ ...request.Option) (*route53.DeleteCidrCollectionOutput, error) {

func (mock mockedR53CidrCollection) DeleteCidrCollection(_ context.Context, _ *route53.DeleteCidrCollectionInput, _ ...func(*route53.Options)) (*route53.DeleteCidrCollectionOutput, error) {
return &mock.DeleteCidrCollectionOutput, nil
}

Expand All @@ -48,7 +48,7 @@ func TestR53CidrCollection_GetAll(t *testing.T) {
rc := Route53CidrCollection{
Client: mockedR53CidrCollection{
ListCidrCollectionsOutput: route53.ListCidrCollectionsOutput{
CidrCollections: []*route53.CollectionSummary{
CidrCollections: []types.CollectionSummary{
{
Id: aws.String(testId1),
Name: aws.String(testName1),
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestR53CidrCollection_GetAll(t *testing.T) {
Route53CIDRCollection: tc.configObj,
})
require.NoError(t, err)
require.Equal(t, tc.expected, aws.StringValueSlice(names))
require.Equal(t, tc.expected, aws.ToStringSlice(names))
})
}
}
Expand All @@ -99,7 +99,7 @@ func TestR53CidrCollection_Nuke(t *testing.T) {
rc := Route53CidrCollection{
Client: mockedR53CidrCollection{
ListCidrBlocksOutput: route53.ListCidrBlocksOutput{
CidrBlocks: []*route53.CidrBlockSummary{
CidrBlocks: []types.CidrBlockSummary{
{
CidrBlock: aws.String("222::0"),
LocationName: aws.String("sample-location-01"),
Expand All @@ -108,7 +108,7 @@ func TestR53CidrCollection_Nuke(t *testing.T) {
},
ChangeCidrCollectionOutput: route53.ChangeCidrCollectionOutput{},
ListCidrCollectionsOutput: route53.ListCidrCollectionsOutput{
CidrCollections: []*route53.CollectionSummary{
CidrCollections: []types.CollectionSummary{
{
Id: aws.String("collection-id-01"),
Name: aws.String("collection-name-01"),
Expand Down
27 changes: 17 additions & 10 deletions aws/resources/route53_cidr_collection_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,33 @@ 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/route53"
"github.com/aws/aws-sdk-go/service/route53/route53iface"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/route53"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/go-commons/errors"
)

type Route53CidrCollectionAPI interface {
ListCidrCollections(ctx context.Context, params *route53.ListCidrCollectionsInput, optFns ...func(*route53.Options)) (*route53.ListCidrCollectionsOutput, error)
ListCidrBlocks(ctx context.Context, params *route53.ListCidrBlocksInput, optFns ...func(*route53.Options)) (*route53.ListCidrBlocksOutput, error)
ChangeCidrCollection(ctx context.Context, params *route53.ChangeCidrCollectionInput, optFns ...func(*route53.Options)) (*route53.ChangeCidrCollectionOutput, error)
DeleteCidrCollection(ctx context.Context, params *route53.DeleteCidrCollectionInput, optFns ...func(*route53.Options)) (*route53.DeleteCidrCollectionOutput, error)
}

// Route53CidrCollection - represents all Route53CidrCollection
type Route53CidrCollection struct {
BaseAwsResource
Client route53iface.Route53API
Client Route53CidrCollectionAPI
Region string
Ids []string
}

func (r *Route53CidrCollection) Init(session *session.Session) {
r.Client = route53.New(session)
func (r *Route53CidrCollection) InitV2(cfg aws.Config) {
r.Client = route53.NewFromConfig(cfg)
}

func (r *Route53CidrCollection) IsUsingV2() bool { return true }

// ResourceName - the simple name of the aws resource
func (r *Route53CidrCollection) ResourceName() string {
return "route53-cidr-collection"
Expand All @@ -38,7 +45,7 @@ func (r *Route53CidrCollection) ResourceIdentifiers() []string {
return r.Ids
}

func (rc *Route53CidrCollection) GetAndSetResourceConfig(configObj config.Config) config.ResourceType {
func (r *Route53CidrCollection) GetAndSetResourceConfig(configObj config.Config) config.ResourceType {
return configObj.Route53CIDRCollection
}

Expand All @@ -48,13 +55,13 @@ func (r *Route53CidrCollection) GetAndSetIdentifiers(c context.Context, configOb
return nil, err
}

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

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

Expand Down
38 changes: 19 additions & 19 deletions aws/resources/route53_hostedzone.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ 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/route53"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/route53"
"github.com/aws/aws-sdk-go-v2/service/route53/types"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/logging"
"github.com/gruntwork-io/cloud-nuke/report"
Expand All @@ -14,7 +14,7 @@ import (
func (r *Route53HostedZone) getAll(_ context.Context, configObj config.Config) ([]*string, error) {
var ids []*string

result, err := r.Client.ListHostedZonesWithContext(r.Context, &route53.ListHostedZonesInput{})
result, err := r.Client.ListHostedZones(r.Context, &route53.ListHostedZonesInput{})
if err != nil {
logging.Errorf("[Failed] unable to list hosted-zones: %s", err)
return nil, err
Expand All @@ -25,7 +25,7 @@ func (r *Route53HostedZone) getAll(_ context.Context, configObj config.Config) (
Name: zone.Name,
}) {
ids = append(ids, zone.Id)
r.HostedZonesDomains[awsgo.StringValue(zone.Id)] = zone
r.HostedZonesDomains[aws.ToString(zone.Id)] = &zone
}
}
return ids, nil
Expand All @@ -38,15 +38,15 @@ func (r *Route53HostedZone) getAll(_ context.Context, configObj config.Config) (
func (r *Route53HostedZone) nukeTrafficPolicy(id *string) (err error) {
logging.Debugf("[Traffic Policy] nuking the traffic policy attached with the hosted zone")

_, err = r.Client.DeleteTrafficPolicyInstance(&route53.DeleteTrafficPolicyInstanceInput{
_, err = r.Client.DeleteTrafficPolicyInstance(r.Context, &route53.DeleteTrafficPolicyInstanceInput{
Id: id,
})
return err
}

func (r *Route53HostedZone) nukeHostedZone(id *string) (err error) {

_, err = r.Client.DeleteHostedZoneWithContext(r.Context, &route53.DeleteHostedZoneInput{
_, err = r.Client.DeleteHostedZone(r.Context, &route53.DeleteHostedZoneInput{
Id: id,
})

Expand All @@ -56,7 +56,7 @@ func (r *Route53HostedZone) nukeHostedZone(id *string) (err error) {
func (r *Route53HostedZone) nukeRecordSet(id *string) (err error) {

// get the resource records
output, err := r.Client.ListResourceRecordSets(&route53.ListResourceRecordSetsInput{
output, err := r.Client.ListResourceRecordSets(r.Context, &route53.ListResourceRecordSetsInput{
HostedZoneId: id,
})
if err != nil {
Expand All @@ -65,14 +65,14 @@ func (r *Route53HostedZone) nukeRecordSet(id *string) (err error) {
}

// get the domain name
var domainName = awsgo.StringValue(r.HostedZonesDomains[awsgo.StringValue(id)].Name)
var domainName = aws.ToString(r.HostedZonesDomains[aws.ToString(id)].Name)

var changes []*route53.Change
var changes []types.Change
for _, record := range output.ResourceRecordSets {
// Note : We can't delete the SOA record or the NS record named ${domain-name}.
// Reference : https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/resource-record-sets-deleting.html
if (aws.StringValue(record.Type) == "NS" || aws.StringValue(record.Type) == "SOA") && awsgo.StringValue(record.Name) == domainName {
logging.Infof("[Skipping] resource record set type is : %s", aws.StringValue(record.Type))
if (record.Type == types.RRTypeNs || record.Type == types.RRTypeSoa) && aws.ToString(record.Name) == domainName {
logging.Infof("[Skipping] resource record set type is : %s", string(record.Type))
continue
}

Expand All @@ -89,16 +89,16 @@ func (r *Route53HostedZone) nukeRecordSet(id *string) (err error) {
}

// set the changes slice
changes = append(changes, &route53.Change{
Action: aws.String("DELETE"),
ResourceRecordSet: record,
changes = append(changes, types.Change{
Action: types.ChangeActionDelete,
ResourceRecordSet: &record,
})
}

if len(changes) > 0 {
_, err = r.Client.ChangeResourceRecordSets(&route53.ChangeResourceRecordSetsInput{
_, err = r.Client.ChangeResourceRecordSets(r.Context, &route53.ChangeResourceRecordSetsInput{
HostedZoneId: id,
ChangeBatch: &route53.ChangeBatch{
ChangeBatch: &types.ChangeBatch{
Changes: changes,
},
})
Expand Down Expand Up @@ -142,7 +142,7 @@ func (r *Route53HostedZone) nukeAll(identifiers []*string) (err error) {
err = r.nuke(id)
// Record status of this resource
e := report.Entry{
Identifier: aws.StringValue(id),
Identifier: aws.ToString(id),
ResourceType: "Route53 hosted zone",
Error: err,
}
Expand All @@ -152,7 +152,7 @@ func (r *Route53HostedZone) nukeAll(identifiers []*string) (err error) {
logging.Errorf("[Failed] %s: %s", *id, err)
} else {
deletedIds = append(deletedIds, id)
logging.Debugf("Deleted Route53 Hosted Zone: %s", aws.StringValue(id))
logging.Debugf("Deleted Route53 Hosted Zone: %s", aws.ToString(id))
}
}

Expand Down
Loading

0 comments on commit ba8d1ee

Please sign in to comment.