Skip to content

Commit 3565ae0

Browse files
antonta0mitchellh
authored andcommitted
providers/digitalocean: force fqdn in dns rr value
Fixes a bug that forces DNS record to be recreated when dealing with records that have domain values (CNAME, MX, NS, etc.)
1 parent c294ce3 commit 3565ae0

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

builtin/providers/digitalocean/resource_digitalocean_record.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,18 @@ func resourceDigitalOceanRecord() *schema.Resource {
6868
func resourceDigitalOceanRecordCreate(d *schema.ResourceData, meta interface{}) error {
6969
client := meta.(*digitalocean.Client)
7070

71+
rrValue := d.Get("value").(string)
72+
// Ensure all records with domain value are absolute (ending with dot)
73+
if t := d.Get("type").(string); t == "CNAME" || t == "MX" || t == "NS" || t == "SRV" {
74+
if rrValue[len(rrValue)-1] != '.' {
75+
rrValue += "."
76+
}
77+
}
78+
7179
newRecord := digitalocean.CreateRecord{
7280
Type: d.Get("type").(string),
7381
Name: d.Get("name").(string),
74-
Data: d.Get("value").(string),
82+
Data: rrValue,
7583
Priority: d.Get("priority").(string),
7684
Port: d.Get("port").(string),
7785
Weight: d.Get("weight").(string),

builtin/providers/digitalocean/resource_digitalocean_record_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,33 @@ func TestAccDigitalOceanRecord_Updated(t *testing.T) {
7676
})
7777
}
7878

79+
func TestAccDigitalOceanRecord_HostnameValue(t *testing.T) {
80+
var record digitalocean.Record
81+
82+
resource.Test(t, resource.TestCase{
83+
PreCheck: func() { testAccPreCheck(t) },
84+
Providers: testAccProviders,
85+
CheckDestroy: testAccCheckDigitalOceanRecordDestroy,
86+
Steps: []resource.TestStep{
87+
resource.TestStep{
88+
Config: testAccCheckDigitalOceanRecordConfig_cname,
89+
Check: resource.ComposeTestCheckFunc(
90+
testAccCheckDigitalOceanRecordExists("digitalocean_record.foobar", &record),
91+
testAccCheckDigitalOceanRecordAttributesHostname(&record),
92+
resource.TestCheckResourceAttr(
93+
"digitalocean_record.foobar", "name", "terraform"),
94+
resource.TestCheckResourceAttr(
95+
"digitalocean_record.foobar", "domain", "foobar-test-terraform.com"),
96+
resource.TestCheckResourceAttr(
97+
"digitalocean_record.foobar", "value", "a.foobar-test-terraform.com"),
98+
resource.TestCheckResourceAttr(
99+
"digitalocean_record.foobar", "type", "CNAME"),
100+
),
101+
},
102+
},
103+
})
104+
}
105+
79106
func testAccCheckDigitalOceanRecordDestroy(s *terraform.State) error {
80107
client := testAccProvider.Meta().(*digitalocean.Client)
81108

@@ -146,6 +173,17 @@ func testAccCheckDigitalOceanRecordExists(n string, record *digitalocean.Record)
146173
}
147174
}
148175

176+
func testAccCheckDigitalOceanRecordAttributesHostname(record *digitalocean.Record) resource.TestCheckFunc {
177+
return func(s *terraform.State) error {
178+
179+
if record.Data != "a.foobar-test-terraform.com" {
180+
return fmt.Errorf("Bad value: %s", record.Data)
181+
}
182+
183+
return nil
184+
}
185+
}
186+
149187
const testAccCheckDigitalOceanRecordConfig_basic = `
150188
resource "digitalocean_domain" "foobar" {
151189
name = "foobar-test-terraform.com"
@@ -173,3 +211,17 @@ resource "digitalocean_record" "foobar" {
173211
value = "192.168.0.11"
174212
type = "A"
175213
}`
214+
215+
const testAccCheckDigitalOceanRecordConfig_cname = `
216+
resource "digitalocean_domain" "foobar" {
217+
name = "foobar-test-terraform.com"
218+
ip_address = "192.168.0.10"
219+
}
220+
221+
resource "digitalocean_record" "foobar" {
222+
domain = "${digitalocean_domain.foobar.name}"
223+
224+
name = "terraform"
225+
value = "a.foobar-test-terraform.com"
226+
type = "CNAME"
227+
}`

0 commit comments

Comments
 (0)