From 501b7b6e0fec5b50e23cea981e7ca5b60b8d39ca Mon Sep 17 00:00:00 2001 From: Matthew Buckett Date: Wed, 2 Nov 2016 14:47:17 +0000 Subject: [PATCH] Remove existing records in gcloud (#308) When record already exists in gcloud we can't add a new record without removing the other one first. This is a simple fix that doesn't attempt to create multiple entries for the record but just removes the previous data. fixes #218 --- providers/dns/googlecloud/googlecloud.go | 10 ++++++++++ providers/dns/googlecloud/googlecloud_test.go | 14 ++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/providers/dns/googlecloud/googlecloud.go b/providers/dns/googlecloud/googlecloud.go index b8d9951c..ea6c0875 100644 --- a/providers/dns/googlecloud/googlecloud.go +++ b/providers/dns/googlecloud/googlecloud.go @@ -68,6 +68,16 @@ func (c *DNSProvider) Present(domain, token, keyAuth string) error { Additions: []*dns.ResourceRecordSet{rec}, } + // Look for existing records. + list, err := c.client.ResourceRecordSets.List(c.project, zone).Name(fqdn).Type("TXT").Do() + if err != nil { + return err + } + if len(list.Rrsets) > 0 { + // Attempt to delete the existing records when adding our new one. + change.Deletions = list.Rrsets + } + chg, err := c.client.Changes.Create(c.project, zone, change).Do() if err != nil { return err diff --git a/providers/dns/googlecloud/googlecloud_test.go b/providers/dns/googlecloud/googlecloud_test.go index d7378816..75a10d9a 100644 --- a/providers/dns/googlecloud/googlecloud_test.go +++ b/providers/dns/googlecloud/googlecloud_test.go @@ -70,6 +70,20 @@ func TestLiveGoogleCloudPresent(t *testing.T) { assert.NoError(t, err) } +func TestLiveGoogleCloudPresentMultiple(t *testing.T) { + if !gcloudLiveTest { + t.Skip("skipping live test") + } + + provider, err := NewDNSProviderCredentials(gcloudProject) + assert.NoError(t, err) + + // Check that we're able to create multiple entries + err = provider.Present(gcloudDomain, "1", "123d==") + err = provider.Present(gcloudDomain, "2", "123d==") + assert.NoError(t, err) +} + func TestLiveGoogleCloudCleanUp(t *testing.T) { if !gcloudLiveTest { t.Skip("skipping live test")