Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Records import (updated) #36

Merged
merged 2 commits into from
Mar 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions cloudflare/import_resource_cloudflare_record_test.go
Original file line number Diff line number Diff line change
@@ -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,
},
},
})
}
42 changes: 39 additions & 3 deletions cloudflare/resource_cloudflare_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package cloudflare
import (
"fmt"
"log"

"time"

"strings"
"time"

"github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -18,6 +16,9 @@ func resourceCloudFlareRecord() *schema.Resource {
Read: resourceCloudFlareRecordRead,
Update: resourceCloudFlareRecordUpdate,
Delete: resourceCloudFlareRecordDelete,
Importer: &schema.ResourceImporter{
State: resourceCloudFlareRecordImport,
},

SchemaVersion: 1,
MigrateState: resourceCloudFlareRecordMigrateState,
Expand Down Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions website/docs/r/record.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
```