From ae7df37e6f4dcc7d93bf412eaa0b9b75807af4a3 Mon Sep 17 00:00:00 2001 From: James Healy Date: Thu, 27 Apr 2017 00:07:23 +1000 Subject: [PATCH] Make dnsimple_records importable (#9130) * Make dnsimple_records importable terraform 0.7 supports importing a resource into the local state, and this adds that feature to the dnsimple_record resource. Unfortunately, the DNSimple v1 API requires a domain name and record ID to fetch a record, so the import command accepts both pieces of data as a slash-delimted string like so: terraform import dnsimple_record.test example.com/1234 * add an acceptance test for importing a dnsimple_record --- .../dnsimple/import_dnsimple_record_test.go | 41 +++++++++++++++++++ .../dnsimple/resource_dnsimple_record.go | 19 +++++++++ .../providers/dnsimple/r/record.html.markdown | 10 +++++ 3 files changed, 70 insertions(+) create mode 100644 builtin/providers/dnsimple/import_dnsimple_record_test.go diff --git a/builtin/providers/dnsimple/import_dnsimple_record_test.go b/builtin/providers/dnsimple/import_dnsimple_record_test.go new file mode 100644 index 000000000..420a6e4c2 --- /dev/null +++ b/builtin/providers/dnsimple/import_dnsimple_record_test.go @@ -0,0 +1,41 @@ +package dnsimple + +import ( + "fmt" + "os" + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccDnsimpleRecord_import(t *testing.T) { + resourceName := "dnsimple_record.foobar" + domain := os.Getenv("DNSIMPLE_DOMAIN") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckDNSimpleRecordDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: fmt.Sprintf(testAccCheckDNSimpleRecordConfig_import, domain), + }, + resource.TestStep{ + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateIdPrefix: fmt.Sprintf("%s_", domain), + }, + }, + }) +} + +const testAccCheckDNSimpleRecordConfig_import = ` +resource "dnsimple_record" "foobar" { + domain = "%s" + + name = "terraform" + value = "192.168.0.10" + type = "A" + ttl = 3600 +}` diff --git a/builtin/providers/dnsimple/resource_dnsimple_record.go b/builtin/providers/dnsimple/resource_dnsimple_record.go index 3f17977a5..af1c26de9 100644 --- a/builtin/providers/dnsimple/resource_dnsimple_record.go +++ b/builtin/providers/dnsimple/resource_dnsimple_record.go @@ -16,6 +16,9 @@ func resourceDNSimpleRecord() *schema.Resource { Read: resourceDNSimpleRecordRead, Update: resourceDNSimpleRecordUpdate, Delete: resourceDNSimpleRecordDelete, + Importer: &schema.ResourceImporter{ + State: resourceDNSimpleRecordImport, + }, Schema: map[string]*schema.Schema{ "domain": { @@ -184,3 +187,19 @@ func resourceDNSimpleRecordDelete(d *schema.ResourceData, meta interface{}) erro return nil } + +func resourceDNSimpleRecordImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + parts := strings.Split(d.Id(), "_") + + if len(parts) != 2 { + return nil, fmt.Errorf("Error Importing dnsimple_record. Please make sure the record ID is in the form DOMAIN_RECORDID (i.e. example.com_1234") + } + + d.SetId(parts[1]) + d.Set("domain", parts[0]) + + if err := resourceDNSimpleRecordRead(d, meta); err != nil { + return nil, err + } + return []*schema.ResourceData{d}, nil +} diff --git a/website/source/docs/providers/dnsimple/r/record.html.markdown b/website/source/docs/providers/dnsimple/r/record.html.markdown index b91701b21..f4bb50053 100644 --- a/website/source/docs/providers/dnsimple/r/record.html.markdown +++ b/website/source/docs/providers/dnsimple/r/record.html.markdown @@ -58,3 +58,13 @@ The following attributes are exported: * `priority` - The priority of the record * `domain_id` - The domain ID of the record * `hostname` - The FQDN of the record + +## Import + +DNSimple resources can be imported using their domain name and numeric ID, e.g. + +``` +$ terraform import dnsimple_record.resource_name example.com_1234 +``` + +The numeric ID can be found in the URL when editing a record on the dnsimple web dashboard.