Skip to content

Commit 632c081

Browse files
committed
Allow existing powerdns_records to be updated instead of destroyed & recreated
1 parent be05e0d commit 632c081

File tree

2 files changed

+76
-23
lines changed

2 files changed

+76
-23
lines changed

powerdns/resource_powerdns_record.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
func resourcePDNSRecord() *schema.Resource {
1313
return &schema.Resource{
1414
Create: resourcePDNSRecordCreate,
15+
Update: resourcePDNSRecordCreate,
1516
Read: resourcePDNSRecordRead,
1617
Delete: resourcePDNSRecordDelete,
1718
Exists: resourcePDNSRecordExists,
@@ -41,13 +42,13 @@ func resourcePDNSRecord() *schema.Resource {
4142
"ttl": {
4243
Type: schema.TypeInt,
4344
Required: true,
44-
ForceNew: true,
45+
ForceNew: false,
4546
},
4647
"records": {
4748
Type: schema.TypeSet,
4849
Elem: &schema.Schema{Type: schema.TypeString},
4950
Required: true,
50-
ForceNew: true,
51+
ForceNew: false,
5152
Set: schema.HashString,
5253
},
5354

powerdns/resource_powerdns_record_test.go

Lines changed: 73 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ import (
1111
"github.com/hashicorp/terraform-plugin-sdk/terraform"
1212
)
1313

14-
//
15-
// Tests
16-
//
17-
1814
func TestAccPDNSRecord_Empty(t *testing.T) {
1915
resource.ParallelTest(t, resource.TestCase{
2016
PreCheck: func() { testAccPreCheck(t) },
@@ -29,7 +25,7 @@ func TestAccPDNSRecord_Empty(t *testing.T) {
2925
}
3026

3127
func TestAccPDNSRecord_A(t *testing.T) {
32-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigA())
28+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigA)
3329
}
3430

3531
func TestAccPDNSRecord_WithPtr(t *testing.T) {
@@ -92,59 +88,59 @@ func TestAccPDNSRecord_WithCount(t *testing.T) {
9288
}
9389

9490
func TestAccPDNSRecord_AAAA(t *testing.T) {
95-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigAAAA())
91+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigAAAA)
9692
}
9793

9894
func TestAccPDNSRecord_CNAME(t *testing.T) {
99-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigCNAME())
95+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigCNAME)
10096
}
10197

10298
func TestAccPDNSRecord_HINFO(t *testing.T) {
103-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigHINFO())
99+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigHINFO)
104100
}
105101

106102
func TestAccPDNSRecord_LOC(t *testing.T) {
107-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigLOC())
103+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigLOC)
108104
}
109105

110106
func TestAccPDNSRecord_MX(t *testing.T) {
111-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigMX())
107+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigMX)
112108
}
113109

114110
func TestAccPDNSRecord_MXMulti(t *testing.T) {
115-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigMXMulti())
111+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigMXMulti)
116112
}
117113

118114
func TestAccPDNSRecord_NAPTR(t *testing.T) {
119-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigNAPTR())
115+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigNAPTR)
120116
}
121117

122118
func TestAccPDNSRecord_NS(t *testing.T) {
123-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigNS())
119+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigNS)
124120
}
125121

126122
func TestAccPDNSRecord_SPF(t *testing.T) {
127-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigSPF())
123+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigSPF)
128124
}
129125

130126
func TestAccPDNSRecord_SSHFP(t *testing.T) {
131-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigSSHFP())
127+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigSSHFP)
132128
}
133129

134130
func TestAccPDNSRecord_SRV(t *testing.T) {
135-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigSRV())
131+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigSRV)
136132
}
137133

138134
func TestAccPDNSRecord_TXT(t *testing.T) {
139-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigTXT())
135+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigTXT)
140136
}
141137

142138
func TestAccPDNSRecord_ALIAS(t *testing.T) {
143-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigALIAS())
139+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigALIAS)
144140
}
145141

146142
func TestAccPDNSRecord_SOA(t *testing.T) {
147-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigSOA())
143+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigSOA)
148144
}
149145

150146
//
@@ -161,7 +157,9 @@ type PowerDNSRecordResourceArguments struct {
161157
Type string
162158
TTL int
163159
Records []string
164-
SetPtr bool
160+
// UpdateRecords are recordsets used for testing update behavior.
161+
UpdateRecords []string
162+
SetPtr bool
165163
}
166164

167165
type PowerDNSRecordResource struct {
@@ -207,16 +205,45 @@ func (resourceConfig *PowerDNSRecordResource) ResourceID() string {
207205
//
208206
// Test Helper Functions
209207
//
210-
func testPDNSRecordCommonTestCore(t *testing.T, recordConfig *PowerDNSRecordResource) {
208+
209+
// Common Test Core: This function builds a create / update test for the majority of test cases
210+
// Takes a function variable to avoid deep copy issues on updates.
211+
func testPDNSRecordCommonTestCore(t *testing.T, recordConfigGenerator func() *PowerDNSRecordResource) {
212+
// Update test resources.
213+
recordConfig := recordConfigGenerator()
214+
215+
ttlUpdateRecordConfig := recordConfigGenerator()
216+
ttlUpdateRecordConfig.Arguments.TTL += 100
217+
218+
recordUpdateRecordConfig := recordConfigGenerator()
219+
recordUpdateRecordConfig.Arguments.Records = recordUpdateRecordConfig.Arguments.UpdateRecords
220+
211221
resource.ParallelTest(t, resource.TestCase{
212222
PreCheck: func() { testAccPreCheck(t) },
213223
Providers: testAccProviders,
214224
CheckDestroy: testAccCheckPDNSRecordDestroy,
215225
Steps: []resource.TestStep{
226+
// Initial record creation
216227
{
217228
Config: recordConfig.ResourceDeclaration(),
218229
Check: resource.ComposeTestCheckFunc(
219230
testAccCheckPDNSRecordContents(recordConfig),
231+
// TestCheckResourceAttr() checks are skipped because records is not directly accessible
232+
// https://github.com/hashicorp/terraform/issues/21618
233+
),
234+
},
235+
// TTL update
236+
{
237+
Config: ttlUpdateRecordConfig.ResourceDeclaration(),
238+
Check: resource.ComposeTestCheckFunc(
239+
testAccCheckPDNSRecordContents(ttlUpdateRecordConfig),
240+
),
241+
},
242+
// Records update
243+
{
244+
Config: recordUpdateRecordConfig.ResourceDeclaration(),
245+
Check: resource.ComposeTestCheckFunc(
246+
testAccCheckPDNSRecordContents(recordUpdateRecordConfig),
220247
),
221248
},
222249
{
@@ -370,6 +397,9 @@ func testPDNSRecordConfigA() *PowerDNSRecordResource {
370397
record.Arguments.Type = "A"
371398
record.Arguments.Records = append(record.Arguments.Records, "1.1.1.1")
372399
record.Arguments.Records = append(record.Arguments.Records, "2.2.2.2")
400+
401+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "2.2.2.2")
402+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "3.3.3.3")
373403
return record
374404
}
375405

@@ -379,6 +409,7 @@ func testPDNSRecordConfigAWithPtr() *PowerDNSRecordResource {
379409
record.Arguments.Name = "testpdnsrecordconfigrecordawithptr.sysa.xyz."
380410
record.Arguments.Type = "A"
381411
record.Arguments.Records = append(record.Arguments.Records, "1.1.1.1")
412+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "2.2.2.2")
382413
record.Arguments.SetPtr = true
383414
return record
384415
}
@@ -400,6 +431,8 @@ func testPDNSRecordConfigAAAA() *PowerDNSRecordResource {
400431
record.Arguments.Type = "AAAA"
401432
record.Arguments.Records = append(record.Arguments.Records, "2001:db8:2000:bf0::1")
402433
record.Arguments.Records = append(record.Arguments.Records, "2001:db8:2000:bf1::1")
434+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "2001:db8:2000:bf3::1")
435+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "2001:db8:2000:bf4::1")
403436
return record
404437
}
405438

@@ -409,6 +442,7 @@ func testPDNSRecordConfigCNAME() *PowerDNSRecordResource {
409442
record.Arguments.Name = "testpdnsrecordconfigcname.sysa.xyz."
410443
record.Arguments.Type = "CNAME"
411444
record.Arguments.Records = append(record.Arguments.Records, "redis.example.com.")
445+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "redis.example.net.")
412446
return record
413447
}
414448

@@ -418,6 +452,7 @@ func testPDNSRecordConfigHINFO() *PowerDNSRecordResource {
418452
record.Arguments.Name = "testpdnsrecordconfighinfo.sysa.xyz."
419453
record.Arguments.Type = "HINFO"
420454
record.Arguments.Records = append(record.Arguments.Records, `"PC-Intel-2.4ghz" "Linux"`)
455+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, `"PC-Intel-3.2ghz" "Linux"`)
421456
return record
422457
}
423458

@@ -427,6 +462,7 @@ func testPDNSRecordConfigLOC() *PowerDNSRecordResource {
427462
record.Arguments.Name = "testpdnsrecordconfigloc.sysa.xyz."
428463
record.Arguments.Type = "LOC"
429464
record.Arguments.Records = append(record.Arguments.Records, "51 56 0.123 N 5 54 0.000 E 4.00m 1.00m 10000.00m 10.00m")
465+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "51 10 43.900 N 1 49 34.300 E 4.00m 1.00m 10000.00m 10.00m")
430466
return record
431467
}
432468

@@ -436,6 +472,7 @@ func testPDNSRecordConfigMX() *PowerDNSRecordResource {
436472
record.Arguments.Name = "sysa.xyz."
437473
record.Arguments.Type = "MX"
438474
record.Arguments.Records = append(record.Arguments.Records, "10 mail.example.com.")
475+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "10 mail2.example.net.")
439476
return record
440477
}
441478

@@ -446,6 +483,8 @@ func testPDNSRecordConfigMXMulti() *PowerDNSRecordResource {
446483
record.Arguments.Type = "MX"
447484
record.Arguments.Records = append(record.Arguments.Records, "10 mail.example.com.")
448485
record.Arguments.Records = append(record.Arguments.Records, "20 mail2.example.com.")
486+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "10 mail3.example.com.")
487+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "10 mail4.example.com.")
449488
return record
450489
}
451490

@@ -455,6 +494,7 @@ func testPDNSRecordConfigNAPTR() *PowerDNSRecordResource {
455494
record.Arguments.Name = "sysa.xyz."
456495
record.Arguments.Type = "NAPTR"
457496
record.Arguments.Records = append(record.Arguments.Records, `100 50 "s" "z3950+I2L+I2C" "" _z3950._tcp.gatech.edu'.`)
497+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, `100 70 "s" "z3950+I2L+I2C" "" _z3950._tcp.gatech.edu'.`)
458498
return record
459499
}
460500

@@ -465,6 +505,8 @@ func testPDNSRecordConfigNS() *PowerDNSRecordResource {
465505
record.Arguments.Type = "NS"
466506
record.Arguments.Records = append(record.Arguments.Records, "ns1.sysa.xyz.")
467507
record.Arguments.Records = append(record.Arguments.Records, "ns2.sysa.xyz.")
508+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "ns3.sysa.xyz.")
509+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "ns4.sysa.xyz.")
468510
return record
469511
}
470512

@@ -474,6 +516,7 @@ func testPDNSRecordConfigSPF() *PowerDNSRecordResource {
474516
record.Arguments.Name = "sysa.xyz."
475517
record.Arguments.Type = "SPF"
476518
record.Arguments.Records = append(record.Arguments.Records, `"v=spf1 +all"`)
519+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, `"v=spf1 -all"`)
477520
return record
478521
}
479522

@@ -483,6 +526,7 @@ func testPDNSRecordConfigSSHFP() *PowerDNSRecordResource {
483526
record.Arguments.Name = "ssh.sysa.xyz."
484527
record.Arguments.Type = "SSHFP"
485528
record.Arguments.Records = append(record.Arguments.Records, "1 1 123456789abcdef67890123456789abcdef67890")
529+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "1 1 fedcba9876543210fedcba9876543210fedcba98")
486530
return record
487531
}
488532

@@ -494,6 +538,11 @@ func testPDNSRecordConfigSRV() *PowerDNSRecordResource {
494538
record.Arguments.Records = append(record.Arguments.Records, "0 10 6379 redis1.sysa.xyz.")
495539
record.Arguments.Records = append(record.Arguments.Records, "0 10 6379 redis2.sysa.xyz.")
496540
record.Arguments.Records = append(record.Arguments.Records, "10 10 6379 redis-replica.sysa.xyz.")
541+
542+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "0 10 6379 redis1.sysa.xyz.")
543+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "0 10 6379 redis2.sysa.xyz.")
544+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "0 10 6379 redis3.sysa.xyz.")
545+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "10 10 6379 redis-replica.sysa.xyz.")
497546
return record
498547
}
499548

@@ -503,6 +552,7 @@ func testPDNSRecordConfigTXT() *PowerDNSRecordResource {
503552
record.Arguments.Name = "text.sysa.xyz."
504553
record.Arguments.Type = "TXT"
505554
record.Arguments.Records = append(record.Arguments.Records, `"text record payload"`)
555+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, `"updated text record payload"`)
506556
return record
507557
}
508558

@@ -513,6 +563,7 @@ func testPDNSRecordConfigALIAS() *PowerDNSRecordResource {
513563
record.Arguments.Type = "ALIAS"
514564
record.Arguments.TTL = 3600
515565
record.Arguments.Records = append(record.Arguments.Records, "www.some-alias.com.")
566+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "www.some-other-alias.com.")
516567
return record
517568
}
518569

@@ -524,5 +575,6 @@ func testPDNSRecordConfigSOA() *PowerDNSRecordResource {
524575
record.Arguments.Type = "SOA"
525576
record.Arguments.TTL = 3600
526577
record.Arguments.Records = append(record.Arguments.Records, "something.something. hostmaster.sysa.xyz. 2019090301 10800 3600 604800 3600")
578+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "something.something. hostmaster.sysa.xyz. 2021021801 10800 3600 604800 3600")
527579
return record
528580
}

0 commit comments

Comments
 (0)