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
This commit is contained in:
James Healy 2017-04-27 00:07:23 +10:00 committed by Paul Stack
parent fdf55c7c68
commit ae7df37e6f
3 changed files with 70 additions and 0 deletions

View File

@ -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
}`

View File

@ -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
}

View File

@ -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.