Merge pull request #35 from beatz174-bit/codex/fix-dynu_dns_record-update-method-x041r3

Preserve known state values during DNS record Update and add helpers/tests
This commit is contained in:
beatz174-bit
2026-04-29 12:35:54 +10:00
committed by GitHub
2 changed files with 51 additions and 12 deletions
+35 -5
View File
@@ -234,13 +234,13 @@ func (r *dnsRecordResource) Update(ctx context.Context, req resource.UpdateReque
}
updateReq := dynuclient.UpdateDNSRecordRequest{
NodeName: recordNodeName(plan.NodeName, plan.Hostname, domainName),
NodeName: recordNodeName(preferKnownString(plan.NodeName, state.NodeName), plan.Hostname, domainName),
RecordType: recordType,
Content: stringPointerFromOptionalContent(plan.Content),
TTL: int64FromOptional(plan.TTL),
State: boolPointerFromOptional(plan.State),
Group: stringFromOptional(plan.Group),
Host: stringFromOptional(plan.Host),
TTL: int64FromOptional(preferKnownInt64(plan.TTL, state.TTL)),
State: boolPointerFromOptional(preferKnownBool(plan.State, state.State)),
Group: stringFromOptional(preferKnownString(plan.Group, state.Group)),
Host: stringFromOptional(preferKnownString(plan.Host, state.Host)),
}
if !validateDNSRecordContentForType(updateReq.RecordType, updateReq.Content, dynamicIntent, &resp.Diagnostics) {
return
@@ -390,6 +390,36 @@ func stringFromOptional(value types.String) string {
return strings.TrimSpace(value.ValueString())
}
func preferKnownString(planValue types.String, stateValue types.String) types.String {
if !planValue.IsNull() && !planValue.IsUnknown() {
return planValue
}
if !stateValue.IsNull() && !stateValue.IsUnknown() {
return stateValue
}
return planValue
}
func preferKnownInt64(planValue types.Int64, stateValue types.Int64) types.Int64 {
if !planValue.IsNull() && !planValue.IsUnknown() {
return planValue
}
if !stateValue.IsNull() && !stateValue.IsUnknown() {
return stateValue
}
return planValue
}
func preferKnownBool(planValue types.Bool, stateValue types.Bool) types.Bool {
if !planValue.IsNull() && !planValue.IsUnknown() {
return planValue
}
if !stateValue.IsNull() && !stateValue.IsUnknown() {
return stateValue
}
return planValue
}
func stringPointerFromOptionalContent(value types.String) *string {
if value.IsNull() || value.IsUnknown() {
return nil
@@ -232,13 +232,13 @@ func TestIntegrationResourceDNSRecordUpdateUsesStateIDWhenPlanIDUnknown(t *testi
createPlan := dnsRecordResourceModel{
Hostname: types.StringValue("api.a.example.com"),
RecordType: types.StringValue("A"),
Content: types.StringValue("192.0.2.123"),
RecordType: types.StringValue("TXT"),
Content: types.StringValue("v=one"),
TTL: types.Int64Value(60),
State: types.BoolValue(true),
Group: types.StringValue("test"),
Host: types.StringNull(),
NodeName: types.StringNull(),
Group: types.StringValue("test-group"),
Host: types.StringValue("test-host"),
NodeName: types.StringValue("test-node"),
}
plan := tfsdk.Plan{Schema: schemaResp.Schema}
@@ -266,7 +266,7 @@ func TestIntegrationResourceDNSRecordUpdateUsesStateIDWhenPlanIDUnknown(t *testi
updatePlan.Host = types.StringUnknown()
updatePlan.NodeName = types.StringUnknown()
updatePlan.UpdatedOn = types.StringUnknown()
updatePlan.Content = types.StringValue("192.0.3.123")
updatePlan.Content = types.StringValue("v=two")
plan = tfsdk.Plan{Schema: schemaResp.Schema}
if diags := plan.Set(ctx, &updatePlan); diags.HasError() {
@@ -288,9 +288,18 @@ func TestIntegrationResourceDNSRecordUpdateUsesStateIDWhenPlanIDUnknown(t *testi
if state.ID.IsNull() || state.ID.IsUnknown() {
t.Fatalf("expected ID to remain known after update")
}
if state.Content.ValueString() != "192.0.3.123" {
if state.Content.ValueString() != "v=two" {
t.Fatalf("expected updated content, got %q", state.Content.ValueString())
}
if state.Group.ValueString() != "test-group" {
t.Fatalf("expected group preserved from state, got %q", state.Group.ValueString())
}
if state.Host.ValueString() != "test-host" {
t.Fatalf("expected host preserved from state, got %q", state.Host.ValueString())
}
if state.NodeName.ValueString() != "test-node" {
t.Fatalf("expected node_name preserved from state, got %q", state.NodeName.ValueString())
}
}
func configureResource(t *testing.T, r resource.ResourceWithConfigure, baseURL string) {