Skip to content

Commit

Permalink
correct state management
Browse files Browse the repository at this point in the history
  • Loading branch information
DXTimer committed Sep 18, 2023
1 parent a17cffa commit d1b1a52
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions internal/framework/resources/registered_domain/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))...)
}
13 changes: 8 additions & 5 deletions internal/framework/resources/registered_domain/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit d1b1a52

Please sign in to comment.