From ddfa01959fc1871967c6d256b4e7cef9b309eff6 Mon Sep 17 00:00:00 2001 From: Dana Hoffman Date: Mon, 17 Jun 2019 22:31:48 +0000 Subject: [PATCH] allow importing dns record sets in any project Signed-off-by: Modular Magician --- google-beta/resource_dns_record_set.go | 21 ++++++++++++++------- google-beta/resource_dns_record_set_test.go | 7 +++++++ website/docs/r/dns_record_set.markdown | 5 +++-- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/google-beta/resource_dns_record_set.go b/google-beta/resource_dns_record_set.go index ed0d1325e9..5c3c70c351 100644 --- a/google-beta/resource_dns_record_set.go +++ b/google-beta/resource_dns_record_set.go @@ -6,9 +6,10 @@ import ( "strings" + "net" + "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/dns/v1" - "net" ) func resourceDnsRecordSet() *schema.Resource { @@ -306,14 +307,20 @@ func resourceDnsRecordSetUpdate(d *schema.ResourceData, meta interface{}) error func resourceDnsRecordSetImportState(d *schema.ResourceData, _ interface{}) ([]*schema.ResourceData, error) { parts := strings.Split(d.Id(), "/") - if len(parts) != 3 { - return nil, fmt.Errorf("Invalid dns record specifier. Expecting {zone-name}/{record-name}/{record-type}. The record name must include a trailing '.' at the end.") + if len(parts) == 3 { + d.Set("managed_zone", parts[0]) + d.Set("name", parts[1]) + d.Set("type", parts[2]) + } else if len(parts) == 4 { + d.Set("project", parts[0]) + d.Set("managed_zone", parts[1]) + d.Set("name", parts[2]) + d.Set("type", parts[3]) + d.SetId(parts[1] + "/" + parts[2] + "/" + parts[3]) + } else { + return nil, fmt.Errorf("Invalid dns record specifier. Expecting {zone-name}/{record-name}/{record-type} or {project}/{zone-name}/{record-name}/{record-type}. The record name must include a trailing '.' at the end.") } - d.Set("managed_zone", parts[0]) - d.Set("name", parts[1]) - d.Set("type", parts[2]) - return []*schema.ResourceData{d}, nil } diff --git a/google-beta/resource_dns_record_set_test.go b/google-beta/resource_dns_record_set_test.go index 7f273599d8..8c1072f114 100644 --- a/google-beta/resource_dns_record_set_test.go +++ b/google-beta/resource_dns_record_set_test.go @@ -56,6 +56,13 @@ func TestAccDnsRecordSet_basic(t *testing.T) { ImportState: true, ImportStateVerify: true, }, + // Check both import formats + { + ResourceName: "google_dns_record_set.foobar", + ImportStateId: fmt.Sprintf("%s/%s/test-record.%s.hashicorptest.com./A", getTestProjectFromEnv(), zoneName, zoneName), + ImportState: true, + ImportStateVerify: true, + }, }, }) } diff --git a/website/docs/r/dns_record_set.markdown b/website/docs/r/dns_record_set.markdown index 189c5287e1..c0378fa4db 100644 --- a/website/docs/r/dns_record_set.markdown +++ b/website/docs/r/dns_record_set.markdown @@ -163,10 +163,11 @@ Only the arguments listed above are exposed as attributes. ## Import -DNS record set can be imported using the `zone name`, `record name` and record `type`, e.g. +DNS record sets can be imported using either of these accepted formats: ``` -$ terraform import google_dns_record_set.frontend prod-zone/frontend.prod.mydomain.com./A +$ terraform import google_dns_record_set.frontend {{project}}/{{zone}}/{{name}}/{{type}} +$ terraform import google_dns_record_set.frontend {{zone}}/{{name}}/{{type}} ``` Note: The record name must include the trailing dot at the end.