From 0f4f7d813e99e35e38fe3726219c1f47f6443ba3 Mon Sep 17 00:00:00 2001 From: Craig Peterson Date: Wed, 3 May 2017 09:46:39 -0600 Subject: [PATCH] no purge (#111) --- providers/activedir/domains.go | 3 --- providers/cloudflare/cloudflareProvider.go | 3 --- providers/diff/diff.go | 10 ++++++++++ providers/diff/diff_test.go | 21 +++++++++++++++++++-- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/providers/activedir/domains.go b/providers/activedir/domains.go index 29416bf35..3bfec4514 100644 --- a/providers/activedir/domains.go +++ b/providers/activedir/domains.go @@ -55,9 +55,6 @@ func (c *adProvider) GetDomainCorrections(dc *models.DomainConfig) ([]*models.Co // Generate changes. corrections := []*models.Correction{} for _, del := range dels { - if dc.KeepUnknown { - break - } corrections = append(corrections, c.deleteRec(dc.Name, del.Existing)) } for _, cre := range creates { diff --git a/providers/cloudflare/cloudflareProvider.go b/providers/cloudflare/cloudflareProvider.go index acdaaa86b..1cc81dbf5 100644 --- a/providers/cloudflare/cloudflareProvider.go +++ b/providers/cloudflare/cloudflareProvider.go @@ -103,9 +103,6 @@ func (c *CloudflareApi) GetDomainCorrections(dc *models.DomainConfig) ([]*models corrections := []*models.Correction{} for _, d := range del { - if dc.KeepUnknown { - continue - } corrections = append(corrections, c.deleteRec(d.Existing.Original.(*cfRecord), id)) } for _, d := range create { diff --git a/providers/diff/diff.go b/providers/diff/diff.go index 091317327..eee58eab3 100644 --- a/providers/diff/diff.go +++ b/providers/diff/diff.go @@ -2,6 +2,7 @@ package diff import ( "fmt" + "log" "sort" "github.com/StackExchange/dnscontrol/models" @@ -66,6 +67,15 @@ func (d *differ) IncrementalDiff(existing []*models.RecordConfig) (unchanged, cr k := key{d.NameFQDN, d.Type} desiredByNameAndType[k] = append(desiredByNameAndType[k], d) } + //if NO_PURGE is set, just remove anything that is only in existing. + if d.dc.KeepUnknown { + for k := range existingByNameAndType { + if _, ok := desiredByNameAndType[k]; !ok { + log.Printf("Ignoring record set %s %s due to NO_PURGE", k.rType, k.name) + delete(existingByNameAndType, k) + } + } + } // Look through existing records. This will give us changes and deletions and some additions. // Each iteration is only for a single type/name record set for key, existingRecords := range existingByNameAndType { diff --git a/providers/diff/diff_test.go b/providers/diff/diff_test.go index 60c5c25de..644e6b050 100644 --- a/providers/diff/diff_test.go +++ b/providers/diff/diff_test.go @@ -128,9 +128,14 @@ func TestMetaChange(t *testing.T) { } func checkLengths(t *testing.T, existing, desired []*models.RecordConfig, unCount, createCount, delCount, modCount int, valFuncs ...func(*models.RecordConfig) map[string]string) (un, cre, del, mod Changeset) { + return checkLengthsFull(t, existing, desired, unCount, createCount, delCount, modCount, false, valFuncs...) +} + +func checkLengthsFull(t *testing.T, existing, desired []*models.RecordConfig, unCount, createCount, delCount, modCount int, keepUnknown bool, valFuncs ...func(*models.RecordConfig) map[string]string) (un, cre, del, mod Changeset) { dc := &models.DomainConfig{ - Name: "example.com", - Records: desired, + Name: "example.com", + Records: desired, + KeepUnknown: keepUnknown, } d := New(dc, valFuncs...) un, cre, del, mod = d.IncrementalDiff(existing) @@ -151,3 +156,15 @@ func checkLengths(t *testing.T, existing, desired []*models.RecordConfig, unCoun } return } + +func TestNoPurge(t *testing.T) { + existing := []*models.RecordConfig{ + myRecord("www MX 1 1.1.1.1"), + myRecord("www MX 1 2.2.2.2"), + myRecord("www2 MX 1 1.1.1.1"), + } + desired := []*models.RecordConfig{ + myRecord("www MX 1 1.1.1.1"), + } + checkLengthsFull(t, existing, desired, 1, 0, 1, 0, true) +}