From d1b1a529aa60cbb5218877f675afe0f1f3a483b9 Mon Sep 17 00:00:00 2001 From: DXTimer Date: Mon, 18 Sep 2023 21:10:22 +0700 Subject: [PATCH] correct state management --- .../framework/resources/registered_domain/import.go | 1 + .../framework/resources/registered_domain/update.go | 13 ++++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/internal/framework/resources/registered_domain/import.go b/internal/framework/resources/registered_domain/import.go index c5e975ef..320c14e7 100644 --- a/internal/framework/resources/registered_domain/import.go +++ b/internal/framework/resources/registered_domain/import.go @@ -56,5 +56,6 @@ func (r *RegisteredDomainResource) ImportState(ctx context.Context, req resource resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), domainResponse.Data.ID)...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("name"), domainResponse.Data.Name)...) + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("contact_id"), domainResponse.Data.RegistrantID)...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("registrant_change"), types.ObjectNull(common.RegistrantChangeAttrType))...) } diff --git a/internal/framework/resources/registered_domain/update.go b/internal/framework/resources/registered_domain/update.go index bccf612f..bf1b3d81 100644 --- a/internal/framework/resources/registered_domain/update.go +++ b/internal/framework/resources/registered_domain/update.go @@ -110,7 +110,7 @@ func (r *RegisteredDomainResource) Update(ctx context.Context, req resource.Upda } } - registrantChange, diags := getRegistrantChange(ctx, planData) + registrantChange, diags := getRegistrantChange(ctx, stateData) if diags.HasError() { resp.Diagnostics.Append(diags...) return @@ -152,7 +152,7 @@ func (r *RegisteredDomainResource) Update(ctx context.Context, req resource.Upda // Create a new registrant change and handle any errors createRegistrantChange(ctx, planData, r, resp) } - } else if !registrantChange.Id.IsNull() { + } else if !registrantChange.Id.IsNull() && registrantChange.State.ValueString() != consts.RegistrantChangeStateCompleted { registrantChangeResponse, err = r.config.Client.Registrar.GetRegistrantChange(ctx, r.config.AccountID, int(registrantChange.Id.ValueInt64())) if err != nil { @@ -182,17 +182,20 @@ func (r *RegisteredDomainResource) Update(ctx context.Context, req resource.Upda } planData.RegistrantChange = registrantChangeObject - // Save data into Terraform state - resp.Diagnostics.Append(resp.State.Set(ctx, &planData)...) - // Exit with warning to prevent the state from being tainted resp.Diagnostics.AddError( "failed to converge on registrant change", err.Error(), ) + + // Save data into Terraform state + resp.Diagnostics.Append(resp.State.Set(ctx, &planData)...) return } } + } else { + // Use state data for registrant change if no changes are detected + planData.RegistrantChange = stateData.RegistrantChange } if planData.AutoRenewEnabled.ValueBool() != stateData.AutoRenewEnabled.ValueBool() {