Skip to content

Commit

Permalink
Add import support to google_dns_record_set
Browse files Browse the repository at this point in the history
  • Loading branch information
rosbo committed Dec 22, 2017
1 parent bdb7d5d commit b55a42c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
33 changes: 33 additions & 0 deletions google/import_dns_record_set_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package google

import (
"testing"

"fmt"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccDnsRecordSet_importBasic(t *testing.T) {
t.Parallel()

zoneName := fmt.Sprintf("dnszone-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDnsManagedZoneDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDnsRecordSet_basic(zoneName, "127.0.0.10", 300),
},

resource.TestStep{
ResourceName: "google_dns_record_set.foobar",
ImportStateId: zoneName + "/qa.test.com./A",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
18 changes: 18 additions & 0 deletions google/resource_dns_record_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/dns/v1"
"strings"
)

func resourceDnsRecordSet() *schema.Resource {
Expand All @@ -14,6 +15,9 @@ func resourceDnsRecordSet() *schema.Resource {
Read: resourceDnsRecordSetRead,
Delete: resourceDnsRecordSetDelete,
Update: resourceDnsRecordSetUpdate,
Importer: &schema.ResourceImporter{
State: resourceDnsRecordSetImportState,
},

Schema: map[string]*schema.Schema{
"managed_zone": &schema.Schema{
Expand Down Expand Up @@ -156,6 +160,7 @@ func resourceDnsRecordSetRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Only expected 1 record set, got %d", len(resp.Rrsets))
}

d.Set("type", resp.Rrsets[0].Type)
d.Set("ttl", resp.Rrsets[0].Ttl)
d.Set("rrdatas", resp.Rrsets[0].Rrdatas)
d.Set("project", project)
Expand Down Expand Up @@ -286,6 +291,19 @@ func resourceDnsRecordSetUpdate(d *schema.ResourceData, meta interface{}) error
return resourceDnsRecordSetRead(d, meta)
}

func resourceDnsRecordSetImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")
if len(parts) != 3 {
return nil, fmt.Errorf("Invalid dns record specifier. Expecting {project}/{zone name}/{record name}/{record type}")
}

d.Set("managed_zone", parts[0])
d.Set("name", parts[1])
d.Set("type", parts[2])

return []*schema.ResourceData{d}, nil
}

func rrdata(
d *schema.ResourceData,
) []string {
Expand Down
6 changes: 3 additions & 3 deletions google/resource_dns_record_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func testAccDnsRecordSet_basic(zoneName string, addr2 string, ttl int) string {
}
resource "google_dns_record_set" "foobar" {
managed_zone = "${google_dns_managed_zone.parent-zone.name}"
name = "test-record.hashicorptest.com."
name = "test-record.${google_dns_managed_zone.parent-zone.dns_name}"
type = "A"
rrdatas = ["127.0.0.1", "%s"]
ttl = %d
Expand All @@ -202,7 +202,7 @@ func testAccDnsRecordSet_ns(name string, ttl int) string {
}
resource "google_dns_record_set" "foobar" {
managed_zone = "${google_dns_managed_zone.parent-zone.name}"
name = "hashicorptest.com."
name = "${google_dns_managed_zone.parent-zone.dns_name}"
type = "NS"
rrdatas = ["ns.hashicorp.services.", "ns2.hashicorp.services."]
ttl = %d
Expand Down Expand Up @@ -236,7 +236,7 @@ func testAccDnsRecordSet_bigChange(zoneName string, ttl int) string {
}
resource "google_dns_record_set" "foobar" {
managed_zone = "${google_dns_managed_zone.parent-zone.name}"
name = "test-record.hashicorptest.com."
name = "test-record.${google_dns_managed_zone.parent-zone.dns_name}"
type = "CNAME"
rrdatas = ["www.terraform.io."]
ttl = %d
Expand Down
11 changes: 10 additions & 1 deletion website/docs/r/dns_record_set.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ description: |-

# google\_dns\_record\_set

Manages a set of DNS records within Google Cloud DNS.
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
Expand Down Expand Up @@ -98,3 +99,11 @@ The following arguments are supported:
## Attributes Reference

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.

```
$ terraform import google_dns_record_set.frontend prod-zone/frontend.prod.mydomain.com./A
```

0 comments on commit b55a42c

Please sign in to comment.