@@ -7,10 +7,13 @@ import (
77
88 "github.com/aws/aws-sdk-go-v2/service/route53"
99 "github.com/aws/aws-sdk-go-v2/service/route53/types"
10- "github.com/gruntwork-io/terratest/modules/aws"
10+ awsTerratest "github.com/gruntwork-io/terratest/modules/aws"
11+
12+ "github.com/aws/aws-sdk-go-v2/aws"
1113)
1214
1315
16+
1417func GetDNSZoneByNameE (t * testing.T , ctx context.Context , hostName string , awsRegion string ) (* types.HostedZone , error ) {
1518 if hostName == "" {
1619 return nil , fmt .Errorf ("hostName cannot be empty" )
@@ -19,7 +22,7 @@ func GetDNSZoneByNameE(t *testing.T, ctx context.Context, hostName string, awsRe
1922 return nil , fmt .Errorf ("awsRegion cannot be empty" )
2023 }
2124
22- client , err := aws .NewRoute53ClientE (t , awsRegion )
25+ client , err := awsTerratest .NewRoute53ClientE (t , awsRegion )
2326 if err != nil {
2427 return nil , err
2528 }
@@ -38,3 +41,55 @@ func GetDNSZoneByNameE(t *testing.T, ctx context.Context, hostName string, awsRe
3841 }
3942 return nil , fmt .Errorf ("no exact match found for hosted zone %s" , hostName )
4043}
44+
45+ func CleanDNSZoneID (t * testing.T , ctx context.Context , zoneID string , awsRegion string ) error {
46+ route53Client , err := awsTerratest .NewRoute53ClientE (t , awsRegion )
47+ if err != nil {
48+ return err
49+ }
50+
51+ o , err := route53Client .ListResourceRecordSets (ctx , & route53.ListResourceRecordSetsInput {
52+ HostedZoneId : & zoneID ,
53+ MaxItems : aws .Int32 (100 ),
54+ })
55+ if err != nil {
56+ return err
57+ }
58+
59+ var changes []types.Change
60+
61+ for _ , record := range o .ResourceRecordSets {
62+
63+ if record .Type == types .RRTypeNs || record .Type == types .RRTypeSoa {
64+ continue
65+ }
66+ // Build a deletion change for each record
67+ changes = append (changes , types.Change {
68+ Action : types .ChangeActionDelete ,
69+ ResourceRecordSet : & record ,
70+ })
71+ }
72+
73+ if len (changes ) == 0 {
74+ fmt .Println ("No deletable records found." )
75+ return nil
76+ }
77+
78+ // Prepare the change batch
79+ changeBatch := & types.ChangeBatch {
80+ Changes : changes ,
81+ }
82+
83+ // Call ChangeResourceRecordSets to delete the records
84+ changeInput := & route53.ChangeResourceRecordSetsInput {
85+ HostedZoneId : aws .String (zoneID ),
86+ ChangeBatch : changeBatch ,
87+ }
88+
89+ _ , err = route53Client .ChangeResourceRecordSets (ctx , changeInput )
90+ if err != nil {
91+ return err
92+ }
93+
94+ return nil
95+ }
0 commit comments