From 37575ad09388e385e72ddc2acacc859adb31548e Mon Sep 17 00:00:00 2001 From: beatz174-bit Date: Wed, 29 Apr 2026 15:51:40 +1000 Subject: [PATCH] Fix location inheritance on record type transitions --- internal/provider/resource_dns_record.go | 10 +++++++++- internal/provider/resource_dns_record_test.go | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/internal/provider/resource_dns_record.go b/internal/provider/resource_dns_record.go index 5242a67..f872a23 100644 --- a/internal/provider/resource_dns_record.go +++ b/internal/provider/resource_dns_record.go @@ -256,7 +256,7 @@ func (r *dnsRecordResource) Update(ctx context.Context, req resource.UpdateReque State: boolPointerFromOptional(preferKnownBool(plan.Enabled, state.Enabled)), Group: stringFromOptional(preferKnownString(plan.Group, state.Group)), Host: stringFromOptional(preferKnownString(plan.Host, state.Host)), - Location: stringFromOptional(preferKnownString(plan.Location, state.Location)), + Location: locationForUpdate(recordType, plan.Location, state.Location), } updateReq = normalizeDNSRecordUpdateRequestForType(updateReq) if !validateDNSRecordContentForType(updateReq.RecordType, updateReq.Content, dynamicIntent, &resp.Diagnostics) { @@ -602,6 +602,14 @@ func validateLocationForType(recordType string, location string, diagnostics *di return true } +func locationForUpdate(recordType string, planLocation types.String, stateLocation types.String) string { + normalizedType := strings.ToUpper(strings.TrimSpace(recordType)) + if normalizedType != "A" && normalizedType != "AAAA" { + return stringFromOptional(planLocation) + } + return stringFromOptional(preferKnownString(planLocation, stateLocation)) +} + func resolveDynamicIntent(recordType string, content types.String, dynamic types.Bool, diagnostics *diag.Diagnostics) (bool, bool) { normalizedType := strings.ToUpper(strings.TrimSpace(recordType)) contentPtr := stringPointerFromOptionalContent(content) diff --git a/internal/provider/resource_dns_record_test.go b/internal/provider/resource_dns_record_test.go index 0df3dfa..c6c35f4 100644 --- a/internal/provider/resource_dns_record_test.go +++ b/internal/provider/resource_dns_record_test.go @@ -203,6 +203,18 @@ func TestValidateLocationForType(t *testing.T) { } } +func TestLocationForUpdate(t *testing.T) { + if got := locationForUpdate("A", types.StringNull(), types.StringValue("us")); got != "us" { + t.Fatalf("expected A record update to preserve state location, got %q", got) + } + if got := locationForUpdate("CNAME", types.StringNull(), types.StringValue("us")); got != "" { + t.Fatalf("expected non-A/AAAA type to ignore prior state location when omitted, got %q", got) + } + if got := locationForUpdate("TXT", types.StringValue("eu"), types.StringValue("us")); got != "eu" { + t.Fatalf("expected explicit plan location to be returned as-is, got %q", got) + } +} + func TestInferDynamicIntentFromState(t *testing.T) { tests := []struct { name string