Skip to content

Commit e65614e

Browse files
authored
fix: require resource recreation when the type attribute changes (#247)
* fix: require resource recreation when the type attribute changes * test: test resource recreation for changes of the type attribute
1 parent f569f93 commit e65614e

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

internal/provider/record_resource.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ func (r *recordResource) Schema(ctx context.Context, _ resource.SchemaRequest, r
7373
MarkdownDescription: "Type of this DNS record " +
7474
"([See supported types](https://docs.hetzner.com/dns-console/dns/general/supported-dns-record-types/))",
7575
Required: true,
76+
PlanModifiers: []planmodifier.String{
77+
stringplanmodifier.RequiresReplaceIfConfigured(),
78+
},
7679
Validators: []validator.String{
7780
stringvalidator.LengthAtLeast(1),
7881
},

internal/provider/record_resource_test.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,3 +512,111 @@ resource "hetznerdns_record" "%s" {
512512
}
513513
}`, resourceName, name, recordType, value)
514514
}
515+
516+
func TestAccRecord_UpdateResourceType(t *testing.T) {
517+
zoneName := acctest.RandString(10) + ".online"
518+
aZoneTTL := 60
519+
520+
aName := acctest.RandString(10)
521+
oldType := "AAAA"
522+
oldValue := "2001:4860:4860::8888"
523+
newType := "A"
524+
newValue := "1.1.1.1"
525+
ttl := aZoneTTL * 2
526+
527+
resource.Test(t, resource.TestCase{
528+
PreCheck: func() { testAccPreCheck(t) },
529+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
530+
Steps: []resource.TestStep{
531+
// Create and Read testing
532+
{
533+
Config: strings.Join(
534+
[]string{
535+
testAccZoneResourceConfig("test", zoneName, aZoneTTL),
536+
testAccRecordResourceConfigWithTTL("record1", aName, oldType, oldValue, ttl),
537+
}, "\n",
538+
),
539+
Check: resource.ComposeTestCheckFunc(
540+
resource.TestCheckResourceAttrSet(
541+
"hetznerdns_record.record1", "id"),
542+
resource.TestCheckResourceAttr(
543+
"hetznerdns_record.record1", "type", oldType),
544+
resource.TestCheckResourceAttr(
545+
"hetznerdns_record.record1", "name", aName),
546+
resource.TestCheckResourceAttr(
547+
"hetznerdns_record.record1", "value", oldValue),
548+
resource.TestCheckResourceAttr(
549+
"hetznerdns_record.record1", "ttl", strconv.Itoa(ttl)),
550+
),
551+
},
552+
// ImportState testing
553+
{
554+
ResourceName: "hetznerdns_record.record1",
555+
ImportState: true,
556+
ImportStateVerify: true,
557+
},
558+
// Update and Read testing
559+
{
560+
Config: strings.Join(
561+
[]string{
562+
testAccZoneResourceConfig("test", zoneName, aZoneTTL),
563+
testAccRecordResourceConfigWithTTL("record1", aName, oldType, oldValue, ttl*2),
564+
}, "\n",
565+
),
566+
Check: resource.ComposeAggregateTestCheckFunc(
567+
resource.TestCheckResourceAttr(
568+
"hetznerdns_record.record1", "ttl", strconv.Itoa(ttl*2)),
569+
),
570+
},
571+
// Update record type and value
572+
{
573+
Config: strings.Join(
574+
[]string{
575+
testAccZoneResourceConfig("test", zoneName, aZoneTTL),
576+
testAccRecordResourceConfigWithTTL("record1", aName, newType, newValue, ttl*2),
577+
}, "\n",
578+
),
579+
Check: resource.ComposeTestCheckFunc(
580+
resource.TestCheckResourceAttrSet(
581+
"hetznerdns_record.record1", "id"),
582+
resource.TestCheckResourceAttr(
583+
"hetznerdns_record.record1", "type", newType),
584+
resource.TestCheckResourceAttr(
585+
"hetznerdns_record.record1", "name", aName),
586+
resource.TestCheckResourceAttr(
587+
"hetznerdns_record.record1", "value", newValue),
588+
resource.TestCheckResourceAttr(
589+
"hetznerdns_record.record1", "ttl", strconv.Itoa(ttl*2)),
590+
),
591+
},
592+
// ImportState testing
593+
{
594+
ResourceName: "hetznerdns_record.record1",
595+
ImportState: true,
596+
ImportStateVerify: true,
597+
},
598+
// Update and Read testing
599+
{
600+
Config: strings.Join(
601+
[]string{
602+
testAccZoneResourceConfig("test", zoneName, aZoneTTL),
603+
testAccRecordResourceConfigWithTTL("record1", aName, newType, newValue, ttl*2),
604+
}, "\n",
605+
),
606+
Check: resource.ComposeTestCheckFunc(
607+
resource.TestCheckResourceAttrSet(
608+
"hetznerdns_record.record1", "id"),
609+
resource.TestCheckResourceAttr(
610+
"hetznerdns_record.record1", "type", newType),
611+
resource.TestCheckResourceAttr(
612+
"hetznerdns_record.record1", "name", aName),
613+
resource.TestCheckResourceAttr(
614+
"hetznerdns_record.record1", "value", newValue),
615+
resource.TestCheckResourceAttr(
616+
"hetznerdns_record.record1", "ttl", strconv.Itoa(ttl*2)),
617+
),
618+
},
619+
// Delete testing automatically occurs in TestCase
620+
},
621+
})
622+
}

0 commit comments

Comments
 (0)