Skip to content

Commit 6a2b1c0

Browse files
modular-magiciandanawillow
authored andcommitted
allow importing dns record sets in any project (#3862)
Signed-off-by: Modular Magician <[email protected]>
1 parent bbd4055 commit 6a2b1c0

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

google/resource_dns_record_set.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import (
66

77
"strings"
88

9+
"net"
10+
911
"github.com/hashicorp/terraform/helper/schema"
1012
"google.golang.org/api/dns/v1"
11-
"net"
1213
)
1314

1415
func resourceDnsRecordSet() *schema.Resource {
@@ -300,14 +301,20 @@ func resourceDnsRecordSetUpdate(d *schema.ResourceData, meta interface{}) error
300301

301302
func resourceDnsRecordSetImportState(d *schema.ResourceData, _ interface{}) ([]*schema.ResourceData, error) {
302303
parts := strings.Split(d.Id(), "/")
303-
if len(parts) != 3 {
304-
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.")
304+
if len(parts) == 3 {
305+
d.Set("managed_zone", parts[0])
306+
d.Set("name", parts[1])
307+
d.Set("type", parts[2])
308+
} else if len(parts) == 4 {
309+
d.Set("project", parts[0])
310+
d.Set("managed_zone", parts[1])
311+
d.Set("name", parts[2])
312+
d.Set("type", parts[3])
313+
d.SetId(parts[1] + "/" + parts[2] + "/" + parts[3])
314+
} else {
315+
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.")
305316
}
306317

307-
d.Set("managed_zone", parts[0])
308-
d.Set("name", parts[1])
309-
d.Set("type", parts[2])
310-
311318
return []*schema.ResourceData{d}, nil
312319
}
313320

google/resource_dns_record_set_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ func TestAccDnsRecordSet_basic(t *testing.T) {
5656
ImportState: true,
5757
ImportStateVerify: true,
5858
},
59+
// Check both import formats
60+
{
61+
ResourceName: "google_dns_record_set.foobar",
62+
ImportStateId: fmt.Sprintf("%s/%s/test-record.%s.hashicorptest.com./A", getTestProjectFromEnv(), zoneName, zoneName),
63+
ImportState: true,
64+
ImportStateVerify: true,
65+
},
5966
},
6067
})
6168
}

website/docs/r/dns_record_set.markdown

+3-2
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,11 @@ Only the arguments listed above are exposed as attributes.
159159

160160
## Import
161161

162-
DNS record set can be imported using the `zone name`, `record name` and record `type`, e.g.
162+
DNS record sets can be imported using either of these accepted formats:
163163

164164
```
165-
$ terraform import google_dns_record_set.frontend prod-zone/frontend.prod.mydomain.com./A
165+
$ terraform import google_dns_record_set.frontend {{project}}/{{zone}}/{{name}}/{{type}}
166+
$ terraform import google_dns_record_set.frontend {{zone}}/{{name}}/{{type}}
166167
```
167168

168169
Note: The record name must include the trailing dot at the end.

0 commit comments

Comments
 (0)