Skip to content

Commit

Permalink
fix: update records with empty records when multihost is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Zebradil committed Mar 24, 2024
1 parent 1b5e297 commit 4ea5e38
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,16 +411,24 @@ func processDomain(api *cloudflare.API, domain string, addr string, cfg runConfi
return recHost == cfgHost
}

// Update the current record if it is either:
// 1. has the same address as the desired record (ttl or comment may have changed),
// 2. has the same comment as the desired record and multihost is enabled (address and ttl may have changed),
// 3. has empty comment and multihost is disabled (address and ttl may have changed).
// NOTE: despite API returning empty Comment as `null`, cloudflare-go represents it as an empty string.
// This can break in the future.
shouldUpdateFn := func(record cloudflare.DNSRecord, cfg runConfig) bool {
return record.Content == addr ||
cfg.multihost && sameHostFn(record, cfg) ||
!cfg.multihost && record.Comment == ""
}

// Look through all existing records.
// Update the matching record if found, delete the rest.
// If no matching record is found, create a new one.
updated := false
for _, record := range existingDNSRecords {
// If a record has the same address as the desired record, update it (ttl
// or comment may have changed).
// If a record has the same comment as the desired record and multihost is
// enabled, update it (address or ttl may have changed).
if !updated && (record.Content == addr || cfg.multihost && sameHostFn(record, cfg)) {
if !updated && shouldUpdateFn(record, cfg) {
updateDNSRecord(api, zoneID, record, desiredDNSRecord)
updated = true
continue
Expand Down

0 comments on commit 4ea5e38

Please sign in to comment.