diff --git a/builtin/providers/digitalocean/resource_digitalocean_record.go b/builtin/providers/digitalocean/resource_digitalocean_record.go index d365e4706..1dcf7a3a9 100644 --- a/builtin/providers/digitalocean/resource_digitalocean_record.go +++ b/builtin/providers/digitalocean/resource_digitalocean_record.go @@ -68,10 +68,18 @@ func resourceDigitalOceanRecord() *schema.Resource { func resourceDigitalOceanRecordCreate(d *schema.ResourceData, meta interface{}) error { client := meta.(*digitalocean.Client) + rrValue := d.Get("value").(string) + // Ensure all records with domain value are absolute (ending with dot) + if t := d.Get("type").(string); t == "CNAME" || t == "MX" || t == "NS" || t == "SRV" { + if rrValue[len(rrValue)-1] != '.' { + rrValue += "." + } + } + newRecord := digitalocean.CreateRecord{ Type: d.Get("type").(string), Name: d.Get("name").(string), - Data: d.Get("value").(string), + Data: rrValue, Priority: d.Get("priority").(string), Port: d.Get("port").(string), Weight: d.Get("weight").(string), diff --git a/builtin/providers/digitalocean/resource_digitalocean_record_test.go b/builtin/providers/digitalocean/resource_digitalocean_record_test.go index 66ac2bb5f..dd4b1010f 100644 --- a/builtin/providers/digitalocean/resource_digitalocean_record_test.go +++ b/builtin/providers/digitalocean/resource_digitalocean_record_test.go @@ -76,6 +76,33 @@ func TestAccDigitalOceanRecord_Updated(t *testing.T) { }) } +func TestAccDigitalOceanRecord_HostnameValue(t *testing.T) { + var record digitalocean.Record + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckDigitalOceanRecordDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCheckDigitalOceanRecordConfig_cname, + Check: resource.ComposeTestCheckFunc( + testAccCheckDigitalOceanRecordExists("digitalocean_record.foobar", &record), + testAccCheckDigitalOceanRecordAttributesHostname(&record), + resource.TestCheckResourceAttr( + "digitalocean_record.foobar", "name", "terraform"), + resource.TestCheckResourceAttr( + "digitalocean_record.foobar", "domain", "foobar-test-terraform.com"), + resource.TestCheckResourceAttr( + "digitalocean_record.foobar", "value", "a.foobar-test-terraform.com"), + resource.TestCheckResourceAttr( + "digitalocean_record.foobar", "type", "CNAME"), + ), + }, + }, + }) +} + func testAccCheckDigitalOceanRecordDestroy(s *terraform.State) error { client := testAccProvider.Meta().(*digitalocean.Client) @@ -146,6 +173,17 @@ func testAccCheckDigitalOceanRecordExists(n string, record *digitalocean.Record) } } +func testAccCheckDigitalOceanRecordAttributesHostname(record *digitalocean.Record) resource.TestCheckFunc { + return func(s *terraform.State) error { + + if record.Data != "a.foobar-test-terraform.com" { + return fmt.Errorf("Bad value: %s", record.Data) + } + + return nil + } +} + const testAccCheckDigitalOceanRecordConfig_basic = ` resource "digitalocean_domain" "foobar" { name = "foobar-test-terraform.com" @@ -173,3 +211,17 @@ resource "digitalocean_record" "foobar" { value = "192.168.0.11" type = "A" }` + +const testAccCheckDigitalOceanRecordConfig_cname = ` +resource "digitalocean_domain" "foobar" { + name = "foobar-test-terraform.com" + ip_address = "192.168.0.10" +} + +resource "digitalocean_record" "foobar" { + domain = "${digitalocean_domain.foobar.name}" + + name = "terraform" + value = "a.foobar-test-terraform.com" + type = "CNAME" +}`