@@ -107,22 +107,48 @@ func (c *route53Client) deploy(name string, t *dnsdisc.Tree) error {
107
107
return err
108
108
}
109
109
log .Info (fmt .Sprintf ("Found %d TXT records" , len (existing )))
110
-
111
110
records := t .ToTXT (name )
112
111
changes := c .computeChanges (name , records , existing )
112
+
113
+ // Submit to API.
114
+ comment := fmt .Sprintf ("enrtree update of %s at seq %d" , name , t .Seq ())
115
+ return c .submitChanges (changes , comment )
116
+ }
117
+
118
+ // deleteDomain removes all TXT records of the given domain.
119
+ func (c * route53Client ) deleteDomain (name string ) error {
120
+ if err := c .checkZone (name ); err != nil {
121
+ return err
122
+ }
123
+
124
+ // Compute DNS changes.
125
+ existing , err := c .collectRecords (name )
126
+ if err != nil {
127
+ return err
128
+ }
129
+ log .Info (fmt .Sprintf ("Found %d TXT records" , len (existing )))
130
+ changes := makeDeletionChanges (existing , nil )
131
+
132
+ // Submit to API.
133
+ comment := "enrtree delete of " + name
134
+ return c .submitChanges (changes , comment )
135
+ }
136
+
137
+ // submitChanges submits the given DNS changes to Route53.
138
+ func (c * route53Client ) submitChanges (changes []types.Change , comment string ) error {
113
139
if len (changes ) == 0 {
114
140
log .Info ("No DNS changes needed" )
115
141
return nil
116
142
}
117
143
118
- // Submit all change batches.
144
+ var err error
119
145
batches := splitChanges (changes , route53ChangeSizeLimit , route53ChangeCountLimit )
120
146
changesToCheck := make ([]* route53.ChangeResourceRecordSetsOutput , len (batches ))
121
147
for i , changes := range batches {
122
148
log .Info (fmt .Sprintf ("Submitting %d changes to Route53" , len (changes )))
123
149
batch := & types.ChangeBatch {
124
150
Changes : changes ,
125
- Comment : aws .String (fmt .Sprintf ("enrtree update %d/%d of %s at seq %d " , i + 1 , len (batches ), name , t . Seq ( ))),
151
+ Comment : aws .String (fmt .Sprintf ("%s ( %d/%d) " , comment , i + 1 , len (batches ))),
126
152
}
127
153
req := & route53.ChangeResourceRecordSetsInput {HostedZoneId : & c .zoneID , ChangeBatch : batch }
128
154
changesToCheck [i ], err = c .api .ChangeResourceRecordSets (context .TODO (), req )
@@ -151,7 +177,6 @@ func (c *route53Client) deploy(name string, t *dnsdisc.Tree) error {
151
177
time .Sleep (30 * time .Second )
152
178
}
153
179
}
154
-
155
180
return nil
156
181
}
157
182
@@ -186,7 +211,8 @@ func (c *route53Client) findZoneID(name string) (string, error) {
186
211
return "" , errors .New ("can't find zone ID for " + name )
187
212
}
188
213
189
- // computeChanges creates DNS changes for the given record.
214
+ // computeChanges creates DNS changes for the given set of DNS discovery records.
215
+ // The 'existing' arg is the set of records that already exist on Route53.
190
216
func (c * route53Client ) computeChanges (name string , records map [string ]string , existing map [string ]recordSet ) []types.Change {
191
217
// Convert all names to lowercase.
192
218
lrecords := make (map [string ]string , len (records ))
@@ -223,16 +249,23 @@ func (c *route53Client) computeChanges(name string, records map[string]string, e
223
249
}
224
250
225
251
// Iterate over the old records and delete anything stale.
226
- for path , set := range existing {
227
- if _ , ok := records [path ]; ok {
252
+ changes = append (changes , makeDeletionChanges (existing , records )... )
253
+
254
+ // Ensure changes are in the correct order.
255
+ sortChanges (changes )
256
+ return changes
257
+ }
258
+
259
+ // makeDeletionChanges creates record changes which delete all records not contained in 'keep'.
260
+ func makeDeletionChanges (records map [string ]recordSet , keep map [string ]string ) []types.Change {
261
+ var changes []types.Change
262
+ for path , set := range records {
263
+ if _ , ok := keep [path ]; ok {
228
264
continue
229
265
}
230
- // Stale entry, nuke it.
231
- log .Info (fmt .Sprintf ("Deleting %s = %q" , path , strings .Join (set .values , "" )))
266
+ log .Info (fmt .Sprintf ("Deleting %s = %s" , path , strings .Join (set .values , "" )))
232
267
changes = append (changes , newTXTChange ("DELETE" , path , set .ttl , set .values ... ))
233
268
}
234
-
235
- sortChanges (changes )
236
269
return changes
237
270
}
238
271
0 commit comments