Skip to content

Commit

Permalink
Feature: DNS import and update existing resources
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
DeviaVir authored and modular-magician committed Jun 17, 2019
1 parent 12507a1 commit 8a69a85
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 29 deletions.
40 changes: 17 additions & 23 deletions google/resource_dns_record_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion google/resource_sql_database_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"

sqladmin "google.golang.org/api/sqladmin/v1beta4"
)

Expand Down
6 changes: 1 addition & 5 deletions website/docs/r/dns_record_set.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 8a69a85

Please sign in to comment.