Skip to content

Commit

Permalink
Add pagination support to list/nuke all Route53HostedZone RRRs
Browse files Browse the repository at this point in the history
  • Loading branch information
sbocinec committed Dec 17, 2024
1 parent 315881b commit 4562e37
Showing 1 changed file with 42 additions and 31 deletions.
73 changes: 42 additions & 31 deletions aws/resources/route53_hostedzone.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,45 +66,56 @@ func (r *Route53HostedZone) nukeHostedZone(id *string) (err error) {
}

func (r *Route53HostedZone) nukeRecordSet(id *string) (err error) {
var (
changes []types.Change
marker *string
)

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

// get the resource records
output, err := r.Client.ListResourceRecordSets(r.Context, &route53.ListResourceRecordSetsInput{
HostedZoneId: id,
})
if err != nil {
logging.Errorf("[Failed] unable to list resource record set: %s", err)
return err
}
for {

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

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 (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
output, err := r.Client.ListResourceRecordSets(r.Context, &route53.ListResourceRecordSetsInput{
HostedZoneId: id,
StartRecordName: marker,
})
if err != nil {
logging.Errorf("[Failed] unable to list resource record set: %s", err)
return err
}

// Note : the request shoud contain exactly one of [AliasTarget, all of [TTL, and ResourceRecords], or TrafficPolicyInstanceId]
if record.TrafficPolicyInstanceId != nil {
// nuke the traffic policy
err := r.nukeTrafficPolicy(record.TrafficPolicyInstanceId)
if err != nil {
logging.Errorf("[Failed] unable to nuke traffic policy: %s", err)
return err
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 (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
}

record.ResourceRecords = nil
}
// Note : the request shoud contain exactly one of [AliasTarget, all of [TTL, and ResourceRecords], or TrafficPolicyInstanceId]
if record.TrafficPolicyInstanceId != nil {
// nuke the traffic policy
err := r.nukeTrafficPolicy(record.TrafficPolicyInstanceId)
if err != nil {
logging.Errorf("[Failed] unable to nuke traffic policy: %s", err)
return err
}

// set the changes slice
changes = append(changes, types.Change{
Action: types.ChangeActionDelete,
ResourceRecordSet: &record,
})
record.ResourceRecords = nil
}

// set the changes slice
changes = append(changes, types.Change{
Action: types.ChangeActionDelete,
ResourceRecordSet: &record,
})
}
if !output.IsTruncated {
break
}
marker = output.NextRecordName
}

if len(changes) > 0 {
Expand Down

0 comments on commit 4562e37

Please sign in to comment.