From fa5acf32e529bca06d52c4e4d09ba8cc4e746a69 Mon Sep 17 00:00:00 2001 From: beatz174-bit Date: Wed, 29 Apr 2026 12:10:19 +1000 Subject: [PATCH] Fix dns_record update identity lookup from state --- internal/provider/resource_dns_record.go | 14 +++- .../resource_dns_record_integration_test.go | 78 ++++++++++++++++++- 2 files changed, 87 insertions(+), 5 deletions(-) diff --git a/internal/provider/resource_dns_record.go b/internal/provider/resource_dns_record.go index 9189211..6945bf6 100644 --- a/internal/provider/resource_dns_record.go +++ b/internal/provider/resource_dns_record.go @@ -198,18 +198,26 @@ func (r *dnsRecordResource) Read(ctx context.Context, req resource.ReadRequest, func (r *dnsRecordResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { var plan dnsRecordResourceModel + var state dnsRecordResourceModel resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) + resp.Diagnostics.Append(req.State.Get(ctx, &state)...) if resp.Diagnostics.HasError() { return } - domainID, recordID, err := parseDNSRecordID(plan.ID.ValueString()) + domainID, recordID, err := parseDNSRecordID(state.ID.ValueString()) if err != nil { resp.Diagnostics.AddError("Invalid resource ID", err.Error()) return } - domainName := strings.TrimSpace(plan.DomainName.ValueString()) + domainName := "" + if !plan.DomainName.IsNull() && !plan.DomainName.IsUnknown() { + domainName = strings.TrimSpace(plan.DomainName.ValueString()) + } + if domainName == "" && !state.DomainName.IsNull() && !state.DomainName.IsUnknown() { + domainName = strings.TrimSpace(state.DomainName.ValueString()) + } if domainName == "" { _, resolvedDomainName, err := r.clientProvider.client.GetRootDomain(ctx, plan.Hostname.ValueString()) if err != nil { @@ -267,7 +275,7 @@ func (r *dnsRecordResource) Update(ctx context.Context, req resource.UpdateReque } nextState := mapDNSRecordToState(*record, dynamicIntent) - nextState.ID = types.StringValue(formatDNSRecordID(record.DomainID, record.ID)) + nextState.ID = state.ID resp.Diagnostics.Append(resp.State.Set(ctx, &nextState)...) } diff --git a/internal/provider/resource_dns_record_integration_test.go b/internal/provider/resource_dns_record_integration_test.go index edacb9f..a22c721 100644 --- a/internal/provider/resource_dns_record_integration_test.go +++ b/internal/provider/resource_dns_record_integration_test.go @@ -64,7 +64,7 @@ func TestIntegrationResourceDNSRecordLifecycleAndImport(t *testing.T) { } updateResp := resource.UpdateResponse{State: tfsdk.State{Schema: schemaResp.Schema}} - r.Update(ctx, resource.UpdateRequest{Plan: plan}, &updateResp) + r.Update(ctx, resource.UpdateRequest{Plan: plan, State: createResp.State}, &updateResp) if updateResp.Diagnostics.HasError() { t.Fatalf("update diagnostics: %v", updateResp.Diagnostics) } @@ -204,7 +204,7 @@ func TestIntegrationResourceDNSRecordDynamicAStateStableAndTransitionToStatic(t } updateResp := resource.UpdateResponse{State: tfsdk.State{Schema: schemaResp.Schema}} - r.Update(ctx, resource.UpdateRequest{Plan: plan}, &updateResp) + r.Update(ctx, resource.UpdateRequest{Plan: plan, State: createResp.State}, &updateResp) if updateResp.Diagnostics.HasError() { t.Fatalf("update diagnostics: %v", updateResp.Diagnostics) } @@ -219,6 +219,80 @@ func TestIntegrationResourceDNSRecordDynamicAStateStableAndTransitionToStatic(t } } +func TestIntegrationResourceDNSRecordUpdateUsesStateIDWhenPlanIDUnknown(t *testing.T) { + ctx := context.Background() + fake := fakedynu.NewServer() + defer fake.Close() + + r := NewDNSRecordResource().(*dnsRecordResource) + configureResource(t, r, fake.BaseURL()) + + var schemaResp resource.SchemaResponse + r.Schema(ctx, resource.SchemaRequest{}, &schemaResp) + + createPlan := dnsRecordResourceModel{ + Hostname: types.StringValue("api.a.example.com"), + RecordType: types.StringValue("A"), + Content: types.StringValue("192.0.2.123"), + TTL: types.Int64Value(60), + State: types.BoolValue(true), + Group: types.StringValue("test"), + Host: types.StringNull(), + NodeName: types.StringNull(), + } + + plan := tfsdk.Plan{Schema: schemaResp.Schema} + if diags := plan.Set(ctx, &createPlan); diags.HasError() { + t.Fatalf("set create plan diagnostics: %v", diags) + } + + createResp := resource.CreateResponse{State: tfsdk.State{Schema: schemaResp.Schema}} + r.Create(ctx, resource.CreateRequest{Plan: plan}, &createResp) + if createResp.Diagnostics.HasError() { + t.Fatalf("create diagnostics: %v", createResp.Diagnostics) + } + + var state dnsRecordResourceModel + if diags := createResp.State.Get(ctx, &state); diags.HasError() { + t.Fatalf("state get diagnostics: %v", diags) + } + + updatePlan := state + updatePlan.ID = types.StringUnknown() + updatePlan.DomainID = types.Int64Unknown() + updatePlan.DomainName = types.StringUnknown() + updatePlan.Dynamic = types.BoolUnknown() + updatePlan.Group = types.StringUnknown() + updatePlan.Host = types.StringUnknown() + updatePlan.NodeName = types.StringUnknown() + updatePlan.UpdatedOn = types.StringUnknown() + updatePlan.Content = types.StringValue("192.0.3.123") + + plan = tfsdk.Plan{Schema: schemaResp.Schema} + if diags := plan.Set(ctx, &updatePlan); diags.HasError() { + t.Fatalf("set update plan diagnostics: %v", diags) + } + + updateResp := resource.UpdateResponse{State: tfsdk.State{Schema: schemaResp.Schema}} + r.Update(ctx, resource.UpdateRequest{ + Plan: plan, + State: createResp.State, + }, &updateResp) + if updateResp.Diagnostics.HasError() { + t.Fatalf("update diagnostics: %v", updateResp.Diagnostics) + } + + if diags := updateResp.State.Get(ctx, &state); diags.HasError() { + t.Fatalf("updated state diagnostics: %v", diags) + } + if state.ID.IsNull() || state.ID.IsUnknown() { + t.Fatalf("expected ID to remain known after update") + } + if state.Content.ValueString() != "192.0.3.123" { + t.Fatalf("expected updated content, got %q", state.Content.ValueString()) + } +} + func configureResource(t *testing.T, r resource.ResourceWithConfigure, baseURL string) { t.Helper() resp := resource.ConfigureResponse{}