Skip to content

Commit

Permalink
fix: null pointer and carry out targeted state updates
Browse files Browse the repository at this point in the history
  • Loading branch information
DXTimer committed Sep 20, 2023
1 parent b968e67 commit 35d98c8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
5 changes: 3 additions & 2 deletions internal/framework/resources/registered_domain/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/dnsimple/dnsimple-go/dnsimple"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
Expand Down Expand Up @@ -328,7 +329,7 @@ func tryToConvergeRegistrantChange(ctx context.Context, data *RegisteredDomainRe
return nil, true
}

if registrantChangeResponse.Data.State != consts.DomainStateRegistered {
if registrantChangeResponse.Data.State != consts.RegistrantChangeStateCompleted {
tflog.Info(ctx, fmt.Sprintf("[RETRYING] Registrant change is not complete, current state: %s", registrantChangeResponse.Data.State))

return fmt.Errorf("registrant change is not complete, current state: %s. You can try to run terraform again to try and converge the registrant change", registrantChangeResponse.Data.State), false
Expand Down Expand Up @@ -410,7 +411,7 @@ func createRegistrantChange(ctx context.Context, data *RegisteredDomainResourceM
data.RegistrantChange = registrantChangeObject

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("registrant_change"), registrantChangeObject)...)

// Exit with warning to prevent the state from being tainted
resp.Diagnostics.AddWarning(
Expand Down
14 changes: 12 additions & 2 deletions internal/framework/resources/registered_domain/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"

"github.com/dnsimple/dnsimple-go/dnsimple"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/terraform-providers/terraform-provider-dnsimple/internal/consts"
"github.com/terraform-providers/terraform-provider-dnsimple/internal/framework/utils"
Expand Down Expand Up @@ -119,7 +120,7 @@ func (r *RegisteredDomainResource) Update(ctx context.Context, req resource.Upda
var registrantChangeResponse *dnsimple.RegistrantChangeResponse
if planData.ContactId.ValueInt64() != stateData.ContactId.ValueInt64() {
if !registrantChange.Id.IsNull() {
convergenceState, err := tryToConvergeRegistrantChange(ctx, planData, &resp.Diagnostics, r, int(registrantChange.Id.ValueInt64()))
convergenceState, _ := tryToConvergeRegistrantChange(ctx, planData, &resp.Diagnostics, r, int(registrantChange.Id.ValueInt64()))
if convergenceState == RegistrantChangeFailed {
// Response is already populated with the error we can safely return
return
Expand All @@ -130,6 +131,15 @@ func (r *RegisteredDomainResource) Update(ctx context.Context, req resource.Upda
// user needs to run terraform again to try and converge the registrant change

// Update the data with the current registrant change
registrantChangeResponse, err = r.config.Client.Registrar.GetRegistrantChange(ctx, r.config.AccountID, int(registrantChange.Id.ValueInt64()))
if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("failed to read DNSimple Registrant Change Id: %d", registrantChange.Id.ValueInt64()),
err.Error(),
)
return
}

registrantChangeObject, diags := r.registrantChangeAPIResponseToObject(ctx, registrantChangeResponse.Data)
if diags.HasError() {
resp.Diagnostics.Append(diags...)
Expand All @@ -138,7 +148,7 @@ 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)...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("registrant_change"), registrantChangeObject)...)

// Exit with warning to prevent the state from being tainted
resp.Diagnostics.AddWarning(
Expand Down

0 comments on commit 35d98c8

Please sign in to comment.