|
|
|
@ -107,22 +107,48 @@ func (c *route53Client) deploy(name string, t *dnsdisc.Tree) error { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
log.Info(fmt.Sprintf("Found %d TXT records", len(existing))) |
|
|
|
|
|
|
|
|
|
records := t.ToTXT(name) |
|
|
|
|
changes := c.computeChanges(name, records, existing) |
|
|
|
|
|
|
|
|
|
// Submit to API.
|
|
|
|
|
comment := fmt.Sprintf("enrtree update of %s at seq %d", name, t.Seq()) |
|
|
|
|
return c.submitChanges(changes, comment) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// deleteDomain removes all TXT records of the given domain.
|
|
|
|
|
func (c *route53Client) deleteDomain(name string) error { |
|
|
|
|
if err := c.checkZone(name); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Compute DNS changes.
|
|
|
|
|
existing, err := c.collectRecords(name) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
log.Info(fmt.Sprintf("Found %d TXT records", len(existing))) |
|
|
|
|
changes := makeDeletionChanges(existing, nil) |
|
|
|
|
|
|
|
|
|
// Submit to API.
|
|
|
|
|
comment := "enrtree delete of " + name |
|
|
|
|
return c.submitChanges(changes, comment) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// submitChanges submits the given DNS changes to Route53.
|
|
|
|
|
func (c *route53Client) submitChanges(changes []types.Change, comment string) error { |
|
|
|
|
if len(changes) == 0 { |
|
|
|
|
log.Info("No DNS changes needed") |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Submit all change batches.
|
|
|
|
|
var err error |
|
|
|
|
batches := splitChanges(changes, route53ChangeSizeLimit, route53ChangeCountLimit) |
|
|
|
|
changesToCheck := make([]*route53.ChangeResourceRecordSetsOutput, len(batches)) |
|
|
|
|
for i, changes := range batches { |
|
|
|
|
log.Info(fmt.Sprintf("Submitting %d changes to Route53", len(changes))) |
|
|
|
|
batch := &types.ChangeBatch{ |
|
|
|
|
Changes: changes, |
|
|
|
|
Comment: aws.String(fmt.Sprintf("enrtree update %d/%d of %s at seq %d", i+1, len(batches), name, t.Seq())), |
|
|
|
|
Comment: aws.String(fmt.Sprintf("%s (%d/%d)", comment, i+1, len(batches))), |
|
|
|
|
} |
|
|
|
|
req := &route53.ChangeResourceRecordSetsInput{HostedZoneId: &c.zoneID, ChangeBatch: batch} |
|
|
|
|
changesToCheck[i], err = c.api.ChangeResourceRecordSets(context.TODO(), req) |
|
|
|
@ -151,7 +177,6 @@ func (c *route53Client) deploy(name string, t *dnsdisc.Tree) error { |
|
|
|
|
time.Sleep(30 * time.Second) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -186,7 +211,8 @@ func (c *route53Client) findZoneID(name string) (string, error) { |
|
|
|
|
return "", errors.New("can't find zone ID for " + name) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// computeChanges creates DNS changes for the given record.
|
|
|
|
|
// computeChanges creates DNS changes for the given set of DNS discovery records.
|
|
|
|
|
// The 'existing' arg is the set of records that already exist on Route53.
|
|
|
|
|
func (c *route53Client) computeChanges(name string, records map[string]string, existing map[string]recordSet) []types.Change { |
|
|
|
|
// Convert all names to lowercase.
|
|
|
|
|
lrecords := make(map[string]string, len(records)) |
|
|
|
@ -223,16 +249,23 @@ func (c *route53Client) computeChanges(name string, records map[string]string, e |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Iterate over the old records and delete anything stale.
|
|
|
|
|
for path, set := range existing { |
|
|
|
|
if _, ok := records[path]; ok { |
|
|
|
|
changes = append(changes, makeDeletionChanges(existing, records)...) |
|
|
|
|
|
|
|
|
|
// Ensure changes are in the correct order.
|
|
|
|
|
sortChanges(changes) |
|
|
|
|
return changes |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// makeDeletionChanges creates record changes which delete all records not contained in 'keep'.
|
|
|
|
|
func makeDeletionChanges(records map[string]recordSet, keep map[string]string) []types.Change { |
|
|
|
|
var changes []types.Change |
|
|
|
|
for path, set := range records { |
|
|
|
|
if _, ok := keep[path]; ok { |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
// Stale entry, nuke it.
|
|
|
|
|
log.Info(fmt.Sprintf("Deleting %s = %q", path, strings.Join(set.values, ""))) |
|
|
|
|
log.Info(fmt.Sprintf("Deleting %s = %s", path, strings.Join(set.values, ""))) |
|
|
|
|
changes = append(changes, newTXTChange("DELETE", path, set.ttl, set.values...)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
sortChanges(changes) |
|
|
|
|
return changes |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|