Skip to content

Commit 62ceba9

Browse files
modular-magiciandanawillow
authored andcommitted
Feature: DNS import and update existing resources (#3859)
Signed-off-by: Modular Magician <[email protected]>
1 parent 91961ec commit 62ceba9

File tree

3 files changed

+18
-29
lines changed

3 files changed

+18
-29
lines changed

google/resource_dns_record_set.go

+17-23
Original file line numberDiff line numberDiff line change
@@ -95,31 +95,25 @@ func resourceDnsRecordSetCreate(d *schema.ResourceData, meta interface{}) error
9595
},
9696
}
9797

98-
// we need to replace NS record sets in the same call. That means
99-
// we need to list all the current NS record sets attached to the
100-
// zone and add them to the change as deletions. We can't just add
101-
// new NS record sets, or we'll get an error about the NS record set
102-
// already existing; see terraform-providers/terraform-provider-google#95.
103-
// We also can't just remove the NS recordsets on creation, as at
104-
// least one is required. So the solution is to "update in place" by
105-
// putting the addition and the removal in the same API call.
106-
if rType == "NS" {
107-
log.Printf("[DEBUG] DNS record list request for %q", zone)
108-
res, err := config.clientDns.ResourceRecordSets.List(project, zone).Do()
109-
if err != nil {
110-
return fmt.Errorf("Error retrieving record sets for %q: %s", zone, err)
111-
}
112-
var deletions []*dns.ResourceRecordSet
98+
// The terraform provider is authoritative, so what we do here is check if
99+
// any records that we are trying to create already exist and make sure we
100+
// delete them, before adding in the changes requested. Normally this would
101+
// result in an AlreadyExistsError.
102+
log.Printf("[DEBUG] DNS record list request for %q", zone)
103+
res, err := config.clientDns.ResourceRecordSets.List(project, zone).Do()
104+
if err != nil {
105+
return fmt.Errorf("Error retrieving record sets for %q: %s", zone, err)
106+
}
107+
var deletions []*dns.ResourceRecordSet
113108

114-
for _, record := range res.Rrsets {
115-
if record.Type != "NS" || record.Name != name {
116-
continue
117-
}
118-
deletions = append(deletions, record)
119-
}
120-
if len(deletions) > 0 {
121-
chg.Deletions = deletions
109+
for _, record := range res.Rrsets {
110+
if record.Type != rType || record.Name != name {
111+
continue
122112
}
113+
deletions = append(deletions, record)
114+
}
115+
if len(deletions) > 0 {
116+
chg.Deletions = deletions
123117
}
124118

125119
log.Printf("[DEBUG] DNS Record create request: %#v", chg)

google/resource_sql_database_instance_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/hashicorp/terraform/helper/acctest"
1010
"github.com/hashicorp/terraform/helper/resource"
1111
"github.com/hashicorp/terraform/terraform"
12-
1312
sqladmin "google.golang.org/api/sqladmin/v1beta4"
1413
)
1514

website/docs/r/dns_record_set.markdown

+1-5
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ description: |-
1111
Manages a set of DNS records within Google Cloud DNS. For more information see [the official documentation](https://cloud.google.com/dns/records/) and
1212
[API](https://cloud.google.com/dns/api/v1/resourceRecordSets).
1313

14-
~> **Note:** The Google Cloud DNS API requires NS records be present at all
15-
times. To accommodate this, when creating NS records, the default records
16-
Google automatically creates will be silently overwritten. Also, when
17-
destroying NS records, Terraform will not actually remove NS records, but will
18-
report that it did.
14+
~> **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.
1915

2016
## Example Usage
2117

0 commit comments

Comments
 (0)