Fix location inheritance on record type transitions

This commit is contained in:
beatz174-bit
2026-04-29 15:51:40 +10:00
parent ef99983118
commit 37575ad093
2 changed files with 21 additions and 1 deletions
+9 -1
View File
@@ -256,7 +256,7 @@ func (r *dnsRecordResource) Update(ctx context.Context, req resource.UpdateReque
State: boolPointerFromOptional(preferKnownBool(plan.Enabled, state.Enabled)), State: boolPointerFromOptional(preferKnownBool(plan.Enabled, state.Enabled)),
Group: stringFromOptional(preferKnownString(plan.Group, state.Group)), Group: stringFromOptional(preferKnownString(plan.Group, state.Group)),
Host: stringFromOptional(preferKnownString(plan.Host, state.Host)), Host: stringFromOptional(preferKnownString(plan.Host, state.Host)),
Location: stringFromOptional(preferKnownString(plan.Location, state.Location)), Location: locationForUpdate(recordType, plan.Location, state.Location),
} }
updateReq = normalizeDNSRecordUpdateRequestForType(updateReq) updateReq = normalizeDNSRecordUpdateRequestForType(updateReq)
if !validateDNSRecordContentForType(updateReq.RecordType, updateReq.Content, dynamicIntent, &resp.Diagnostics) { if !validateDNSRecordContentForType(updateReq.RecordType, updateReq.Content, dynamicIntent, &resp.Diagnostics) {
@@ -602,6 +602,14 @@ func validateLocationForType(recordType string, location string, diagnostics *di
return true 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) { func resolveDynamicIntent(recordType string, content types.String, dynamic types.Bool, diagnostics *diag.Diagnostics) (bool, bool) {
normalizedType := strings.ToUpper(strings.TrimSpace(recordType)) normalizedType := strings.ToUpper(strings.TrimSpace(recordType))
contentPtr := stringPointerFromOptionalContent(content) contentPtr := stringPointerFromOptionalContent(content)
@@ -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) { func TestInferDynamicIntentFromState(t *testing.T) {
tests := []struct { tests := []struct {
name string name string