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

Ensure all Route53HostedZones are deleted by adding pagination support #815

Merged
Merged
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
88 changes: 47 additions & 41 deletions aws/resources/route53_hostedzone.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@ import (

func (r *Route53HostedZone) getAll(_ context.Context, configObj config.Config) ([]*string, error) {
var ids []*string
paginator := route53.NewListHostedZonesPaginator(r.Client, &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
}
for paginator.HasMorePages() {
result, err := paginator.NextPage(r.Context)
if err != nil {
logging.Errorf("[Failed] unable to list hosted-zones: %s", err)
return nil, err
}

for _, zone := range result.HostedZones {
if configObj.Route53HostedZone.ShouldInclude(config.ResourceValue{
Name: zone.Name,
}) {
ids = append(ids, zone.Id)
r.HostedZonesDomains[aws.ToString(zone.Id)] = &zone
for _, zone := range result.HostedZones {
if configObj.Route53HostedZone.ShouldInclude(config.ResourceValue{
Name: zone.Name,
}) {
ids = append(ids, zone.Id)
r.HostedZonesDomains[aws.ToString(zone.Id)] = &zone
}
}
}
return ids, nil
Expand Down Expand Up @@ -54,45 +57,48 @@ func (r *Route53HostedZone) nukeHostedZone(id *string) (err error) {
}

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

// get the resource records
output, err := r.Client.ListResourceRecordSets(r.Context, &route53.ListResourceRecordSetsInput{
// get the domain name
domainName := aws.ToString(r.HostedZonesDomains[aws.ToString(id)].Name)

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

// get the domain name
var domainName = aws.ToString(r.HostedZonesDomains[aws.ToString(id)].Name)
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
}

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
}
// 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
}

// 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
record.ResourceRecords = nil
}

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

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

if len(changes) > 0 {
Expand Down