diff --git a/cloudflare/import_resource_cloudflare_record_test.go b/cloudflare/import_resource_cloudflare_record_test.go new file mode 100644 index 0000000000..f199da1b8c --- /dev/null +++ b/cloudflare/import_resource_cloudflare_record_test.go @@ -0,0 +1,30 @@ +package cloudflare + +import ( + "fmt" + "os" + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccCloudFlareRecord_Import(t *testing.T) { + zone := os.Getenv("CLOUDFLARE_DOMAIN") + name := "cloudflare_record.foobar" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccCheckCloudFlareRecordConfigBasic(zone, name), + }, + { + ResourceName: name, + ImportStateIdPrefix: fmt.Sprintf("%s/", zone), + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} diff --git a/cloudflare/resource_cloudflare_record.go b/cloudflare/resource_cloudflare_record.go index 0c3827ad76..67fdc46d57 100644 --- a/cloudflare/resource_cloudflare_record.go +++ b/cloudflare/resource_cloudflare_record.go @@ -3,10 +3,8 @@ package cloudflare import ( "fmt" "log" - - "time" - "strings" + "time" "github.com/cloudflare/cloudflare-go" "github.com/hashicorp/terraform/helper/schema" @@ -18,6 +16,9 @@ func resourceCloudFlareRecord() *schema.Resource { Read: resourceCloudFlareRecordRead, Update: resourceCloudFlareRecordUpdate, Delete: resourceCloudFlareRecordDelete, + Importer: &schema.ResourceImporter{ + State: resourceCloudFlareRecordImport, + }, SchemaVersion: 1, MigrateState: resourceCloudFlareRecordMigrateState, @@ -270,3 +271,38 @@ func expandStringMap(inVal interface{}) map[string]string { } return outVal } + +func resourceCloudFlareRecordImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + client := meta.(*cloudflare.API) + + // split the id so we can lookup + idAttr := strings.SplitN(d.Id(), "/", 2) + var zoneName string + var recordId string + if len(idAttr) == 2 { + zoneName = idAttr[0] + recordId = idAttr[1] + } else { + return nil, fmt.Errorf("invalid id %q specified, should be in format \"zoneName/recordId\" for import", d.Id()) + } + + zoneId, err := client.ZoneIDByName(zoneName) + if err != nil { + return nil, fmt.Errorf("error finding zoneName %q: %s", zoneName, err) + } + + record, err := client.DNSRecord(zoneId, recordId) + if err != nil { + return nil, fmt.Errorf("Unable to find record with ID %q: %q", d.Id(), err) + } + + log.Printf("[INFO] Found record: %s", record.Name) + name := strings.TrimSuffix(record.Name, "."+zoneName) + + d.Set("name", name) + d.Set("domain", zoneName) + d.Set("zone_id", zoneId) + d.SetId(recordId) + + return []*schema.ResourceData{d}, nil +} diff --git a/website/docs/r/record.html.markdown b/website/docs/r/record.html.markdown index 45057009bc..8b0ec48852 100644 --- a/website/docs/r/record.html.markdown +++ b/website/docs/r/record.html.markdown @@ -47,3 +47,11 @@ The following attributes are exported: * `modified_on` - The RFC3339 timestamp of when the record was last modified * `metadata` - A key-value map of string metadata cloudflare associates with the record * `zone_id` - The zone id of the record + +## Import + +Records can be imported using a composite ID formed of zone name and record ID, e.g. + +``` +$ terraform import cloudflare_record.default example.com/ch8374ftwdghsif43 +```