Skip to content

Commit e63bee2

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

File tree

2 files changed

+76
-19
lines changed

2 files changed

+76
-19
lines changed

powerdns/resource_powerdns_record.go

+3-2
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

+73-17
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestAccPDNSRecord_Empty(t *testing.T) {
2929
}
3030

3131
func TestAccPDNSRecord_A(t *testing.T) {
32-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigA())
32+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigA)
3333
}
3434

3535
func TestAccPDNSRecord_WithPtr(t *testing.T) {
@@ -92,59 +92,59 @@ func TestAccPDNSRecord_WithCount(t *testing.T) {
9292
}
9393

9494
func TestAccPDNSRecord_AAAA(t *testing.T) {
95-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigAAAA())
95+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigAAAA)
9696
}
9797

9898
func TestAccPDNSRecord_CNAME(t *testing.T) {
99-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigCNAME())
99+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigCNAME)
100100
}
101101

102102
func TestAccPDNSRecord_HINFO(t *testing.T) {
103-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigHINFO())
103+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigHINFO)
104104
}
105105

106106
func TestAccPDNSRecord_LOC(t *testing.T) {
107-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigLOC())
107+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigLOC)
108108
}
109109

110110
func TestAccPDNSRecord_MX(t *testing.T) {
111-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigMX())
111+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigMX)
112112
}
113113

114114
func TestAccPDNSRecord_MXMulti(t *testing.T) {
115-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigMXMulti())
115+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigMXMulti)
116116
}
117117

118118
func TestAccPDNSRecord_NAPTR(t *testing.T) {
119-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigNAPTR())
119+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigNAPTR)
120120
}
121121

122122
func TestAccPDNSRecord_NS(t *testing.T) {
123-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigNS())
123+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigNS)
124124
}
125125

126126
func TestAccPDNSRecord_SPF(t *testing.T) {
127-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigSPF())
127+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigSPF)
128128
}
129129

130130
func TestAccPDNSRecord_SSHFP(t *testing.T) {
131-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigSSHFP())
131+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigSSHFP)
132132
}
133133

134134
func TestAccPDNSRecord_SRV(t *testing.T) {
135-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigSRV())
135+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigSRV)
136136
}
137137

138138
func TestAccPDNSRecord_TXT(t *testing.T) {
139-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigTXT())
139+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigTXT)
140140
}
141141

142142
func TestAccPDNSRecord_ALIAS(t *testing.T) {
143-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigALIAS())
143+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigALIAS)
144144
}
145145

146146
func TestAccPDNSRecord_SOA(t *testing.T) {
147-
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigSOA())
147+
testPDNSRecordCommonTestCore(t, testPDNSRecordConfigSOA)
148148
}
149149

150150
//
@@ -161,7 +161,9 @@ type PowerDNSRecordResourceArguments struct {
161161
Type string
162162
TTL int
163163
Records []string
164-
SetPtr bool
164+
// UpdateRecords are recordsets used for testing update behavior.
165+
UpdateRecords []string
166+
SetPtr bool
165167
}
166168

167169
type PowerDNSRecordResource struct {
@@ -207,16 +209,45 @@ func (resourceConfig *PowerDNSRecordResource) ResourceID() string {
207209
//
208210
// Test Helper Functions
209211
//
210-
func testPDNSRecordCommonTestCore(t *testing.T, recordConfig *PowerDNSRecordResource) {
212+
213+
// Common Test Core: This function builds a create / update test for the majority of test cases
214+
// Takes a function variable to avoid deep copy issues on updates.
215+
func testPDNSRecordCommonTestCore(t *testing.T, recordConfigGenerator func() *PowerDNSRecordResource) {
216+
// Update test resources.
217+
recordConfig := recordConfigGenerator()
218+
219+
ttlUpdateRecordConfig := recordConfigGenerator()
220+
ttlUpdateRecordConfig.Arguments.TTL += 100
221+
222+
recordUpdateRecordConfig := recordConfigGenerator()
223+
recordUpdateRecordConfig.Arguments.Records = recordUpdateRecordConfig.Arguments.UpdateRecords
224+
211225
resource.ParallelTest(t, resource.TestCase{
212226
PreCheck: func() { testAccPreCheck(t) },
213227
Providers: testAccProviders,
214228
CheckDestroy: testAccCheckPDNSRecordDestroy,
215229
Steps: []resource.TestStep{
230+
// Initial record creation
216231
{
217232
Config: recordConfig.ResourceDeclaration(),
218233
Check: resource.ComposeTestCheckFunc(
219234
testAccCheckPDNSRecordContents(recordConfig),
235+
// TestCheckResourceAttr() checks are skipped because records is not directly accessible
236+
// https://github.com/hashicorp/terraform/issues/21618
237+
),
238+
},
239+
// TTL update
240+
{
241+
Config: ttlUpdateRecordConfig.ResourceDeclaration(),
242+
Check: resource.ComposeTestCheckFunc(
243+
testAccCheckPDNSRecordContents(ttlUpdateRecordConfig),
244+
),
245+
},
246+
// Records update
247+
{
248+
Config: recordUpdateRecordConfig.ResourceDeclaration(),
249+
Check: resource.ComposeTestCheckFunc(
250+
testAccCheckPDNSRecordContents(recordUpdateRecordConfig),
220251
),
221252
},
222253
{
@@ -370,6 +401,9 @@ func testPDNSRecordConfigA() *PowerDNSRecordResource {
370401
record.Arguments.Type = "A"
371402
record.Arguments.Records = append(record.Arguments.Records, "1.1.1.1")
372403
record.Arguments.Records = append(record.Arguments.Records, "2.2.2.2")
404+
405+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "2.2.2.2")
406+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "3.3.3.3")
373407
return record
374408
}
375409

@@ -379,6 +413,7 @@ func testPDNSRecordConfigAWithPtr() *PowerDNSRecordResource {
379413
record.Arguments.Name = "testpdnsrecordconfigrecordawithptr.sysa.xyz."
380414
record.Arguments.Type = "A"
381415
record.Arguments.Records = append(record.Arguments.Records, "1.1.1.1")
416+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "2.2.2.2")
382417
record.Arguments.SetPtr = true
383418
return record
384419
}
@@ -400,6 +435,8 @@ func testPDNSRecordConfigAAAA() *PowerDNSRecordResource {
400435
record.Arguments.Type = "AAAA"
401436
record.Arguments.Records = append(record.Arguments.Records, "2001:db8:2000:bf0::1")
402437
record.Arguments.Records = append(record.Arguments.Records, "2001:db8:2000:bf1::1")
438+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "2001:db8:2000:bf3::1")
439+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "2001:db8:2000:bf4::1")
403440
return record
404441
}
405442

@@ -409,6 +446,7 @@ func testPDNSRecordConfigCNAME() *PowerDNSRecordResource {
409446
record.Arguments.Name = "testpdnsrecordconfigcname.sysa.xyz."
410447
record.Arguments.Type = "CNAME"
411448
record.Arguments.Records = append(record.Arguments.Records, "redis.example.com.")
449+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "redis.example.net.")
412450
return record
413451
}
414452

@@ -418,6 +456,7 @@ func testPDNSRecordConfigHINFO() *PowerDNSRecordResource {
418456
record.Arguments.Name = "testpdnsrecordconfighinfo.sysa.xyz."
419457
record.Arguments.Type = "HINFO"
420458
record.Arguments.Records = append(record.Arguments.Records, `"PC-Intel-2.4ghz" "Linux"`)
459+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, `"PC-Intel-3.2ghz" "Linux"`)
421460
return record
422461
}
423462

@@ -427,6 +466,7 @@ func testPDNSRecordConfigLOC() *PowerDNSRecordResource {
427466
record.Arguments.Name = "testpdnsrecordconfigloc.sysa.xyz."
428467
record.Arguments.Type = "LOC"
429468
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")
469+
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")
430470
return record
431471
}
432472

@@ -436,6 +476,7 @@ func testPDNSRecordConfigMX() *PowerDNSRecordResource {
436476
record.Arguments.Name = "sysa.xyz."
437477
record.Arguments.Type = "MX"
438478
record.Arguments.Records = append(record.Arguments.Records, "10 mail.example.com.")
479+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "10 mail2.example.net.")
439480
return record
440481
}
441482

@@ -446,6 +487,8 @@ func testPDNSRecordConfigMXMulti() *PowerDNSRecordResource {
446487
record.Arguments.Type = "MX"
447488
record.Arguments.Records = append(record.Arguments.Records, "10 mail.example.com.")
448489
record.Arguments.Records = append(record.Arguments.Records, "20 mail2.example.com.")
490+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "10 mail3.example.com.")
491+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "10 mail4.example.com.")
449492
return record
450493
}
451494

@@ -455,6 +498,7 @@ func testPDNSRecordConfigNAPTR() *PowerDNSRecordResource {
455498
record.Arguments.Name = "sysa.xyz."
456499
record.Arguments.Type = "NAPTR"
457500
record.Arguments.Records = append(record.Arguments.Records, `100 50 "s" "z3950+I2L+I2C" "" _z3950._tcp.gatech.edu'.`)
501+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, `100 70 "s" "z3950+I2L+I2C" "" _z3950._tcp.gatech.edu'.`)
458502
return record
459503
}
460504

@@ -465,6 +509,8 @@ func testPDNSRecordConfigNS() *PowerDNSRecordResource {
465509
record.Arguments.Type = "NS"
466510
record.Arguments.Records = append(record.Arguments.Records, "ns1.sysa.xyz.")
467511
record.Arguments.Records = append(record.Arguments.Records, "ns2.sysa.xyz.")
512+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "ns3.sysa.xyz.")
513+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "ns4.sysa.xyz.")
468514
return record
469515
}
470516

@@ -474,6 +520,7 @@ func testPDNSRecordConfigSPF() *PowerDNSRecordResource {
474520
record.Arguments.Name = "sysa.xyz."
475521
record.Arguments.Type = "SPF"
476522
record.Arguments.Records = append(record.Arguments.Records, `"v=spf1 +all"`)
523+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, `"v=spf1 -all"`)
477524
return record
478525
}
479526

@@ -483,6 +530,7 @@ func testPDNSRecordConfigSSHFP() *PowerDNSRecordResource {
483530
record.Arguments.Name = "ssh.sysa.xyz."
484531
record.Arguments.Type = "SSHFP"
485532
record.Arguments.Records = append(record.Arguments.Records, "1 1 123456789abcdef67890123456789abcdef67890")
533+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "1 1 fedcba9876543210fedcba9876543210fedcba98")
486534
return record
487535
}
488536

@@ -494,6 +542,11 @@ func testPDNSRecordConfigSRV() *PowerDNSRecordResource {
494542
record.Arguments.Records = append(record.Arguments.Records, "0 10 6379 redis1.sysa.xyz.")
495543
record.Arguments.Records = append(record.Arguments.Records, "0 10 6379 redis2.sysa.xyz.")
496544
record.Arguments.Records = append(record.Arguments.Records, "10 10 6379 redis-replica.sysa.xyz.")
545+
546+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "0 10 6379 redis1.sysa.xyz.")
547+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "0 10 6379 redis2.sysa.xyz.")
548+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "0 10 6379 redis3.sysa.xyz.")
549+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "10 10 6379 redis-replica.sysa.xyz.")
497550
return record
498551
}
499552

@@ -503,6 +556,7 @@ func testPDNSRecordConfigTXT() *PowerDNSRecordResource {
503556
record.Arguments.Name = "text.sysa.xyz."
504557
record.Arguments.Type = "TXT"
505558
record.Arguments.Records = append(record.Arguments.Records, `"text record payload"`)
559+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, `"updated text record payload"`)
506560
return record
507561
}
508562

@@ -513,6 +567,7 @@ func testPDNSRecordConfigALIAS() *PowerDNSRecordResource {
513567
record.Arguments.Type = "ALIAS"
514568
record.Arguments.TTL = 3600
515569
record.Arguments.Records = append(record.Arguments.Records, "www.some-alias.com.")
570+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "www.some-other-alias.com.")
516571
return record
517572
}
518573

@@ -524,5 +579,6 @@ func testPDNSRecordConfigSOA() *PowerDNSRecordResource {
524579
record.Arguments.Type = "SOA"
525580
record.Arguments.TTL = 3600
526581
record.Arguments.Records = append(record.Arguments.Records, "something.something. hostmaster.sysa.xyz. 2019090301 10800 3600 604800 3600")
582+
record.Arguments.UpdateRecords = append(record.Arguments.UpdateRecords, "something.something. hostmaster.sysa.xyz. 2021021801 10800 3600 604800 3600")
527583
return record
528584
}

0 commit comments

Comments
 (0)