From c676ba9460a8110ca0efd6c6971fc94093b1494f Mon Sep 17 00:00:00 2001 From: Chase Date: Mon, 17 Jun 2019 22:03:52 +0000 Subject: [PATCH] Feature: DNS import and update existing resources Signed-off-by: Modular Magician --- google-beta/resource_dns_record_set.go | 40 +++++++++++--------------- website/docs/r/dns_record_set.markdown | 6 +--- 2 files changed, 18 insertions(+), 28 deletions(-) diff --git a/google-beta/resource_dns_record_set.go b/google-beta/resource_dns_record_set.go index ed0d1325e9..d6333fdf03 100644 --- a/google-beta/resource_dns_record_set.go +++ b/google-beta/resource_dns_record_set.go @@ -95,31 +95,25 @@ func resourceDnsRecordSetCreate(d *schema.ResourceData, meta interface{}) error }, } - // we need to replace NS record sets in the same call. That means - // we need to list all the current NS record sets attached to the - // zone and add them to the change as deletions. We can't just add - // new NS record sets, or we'll get an error about the NS record set - // already existing; see terraform-providers/terraform-provider-google#95. - // We also can't just remove the NS recordsets on creation, as at - // least one is required. So the solution is to "update in place" by - // putting the addition and the removal in the same API call. - if rType == "NS" { - log.Printf("[DEBUG] DNS record list request for %q", zone) - res, err := config.clientDns.ResourceRecordSets.List(project, zone).Do() - if err != nil { - return fmt.Errorf("Error retrieving record sets for %q: %s", zone, err) - } - var deletions []*dns.ResourceRecordSet + // The terraform provider is authoritative, so what we do here is check if + // any records that we are trying to create already exist and make sure we + // delete them, before adding in the changes requested. Normally this would + // result in an AlreadyExistsError. + log.Printf("[DEBUG] DNS record list request for %q", zone) + res, err := config.clientDns.ResourceRecordSets.List(project, zone).Do() + if err != nil { + return fmt.Errorf("Error retrieving record sets for %q: %s", zone, err) + } + var deletions []*dns.ResourceRecordSet - for _, record := range res.Rrsets { - if record.Type != "NS" || record.Name != name { - continue - } - deletions = append(deletions, record) - } - if len(deletions) > 0 { - chg.Deletions = deletions + for _, record := range res.Rrsets { + if record.Type != rType || record.Name != name { + continue } + deletions = append(deletions, record) + } + if len(deletions) > 0 { + chg.Deletions = deletions } log.Printf("[DEBUG] DNS Record create request: %#v", chg) diff --git a/website/docs/r/dns_record_set.markdown b/website/docs/r/dns_record_set.markdown index 189c5287e1..b943e17b75 100644 --- a/website/docs/r/dns_record_set.markdown +++ b/website/docs/r/dns_record_set.markdown @@ -11,11 +11,7 @@ description: |- Manages a set of DNS records within Google Cloud DNS. For more information see [the official documentation](https://cloud.google.com/dns/records/) and [API](https://cloud.google.com/dns/api/v1/resourceRecordSets). -~> **Note:** The Google Cloud DNS API requires NS records be present at all -times. To accommodate this, when creating NS records, the default records -Google automatically creates will be silently overwritten. Also, when -destroying NS records, Terraform will not actually remove NS records, but will -report that it did. +~> **Note:** The provider treats this resource as an authoritative record set. This means existing records (including the default records) for the given type will be overwritten when you create this resource in Terraform. In addition, the Google Cloud DNS API requires NS records to be present at all times, so Terraform will not actually remove NS records during destroy but will report that it did. ## Example Usage