Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: DNS import and update existing resources #3859

Merged
merged 1 commit into from
Jun 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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