Skip to content

Commit

Permalink
Fix error handling in cloudflare_turnstile
Browse files Browse the repository at this point in the history
When we receive an error from the API, we would record an error (not return)
and write an invalid State. For some reason, Terraform accepts it... yet
it seems to corrupt the tf state.

Add some returns to handle error more cleanly.
  • Loading branch information
punkeel committed May 3, 2024
1 parent ba46db6 commit 3ef096f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/3284.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/cloudflare_turnstile: Fix error handling corrupting state
```
5 changes: 5 additions & 0 deletions internal/framework/service/turnstile/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func (r *TurnstileWidgetResource) Create(ctx context.Context, req resource.Creat
})
if err != nil {
resp.Diagnostics.AddError("Error creating challenge widget", err.Error())
return
}

data = buildChallengeModelFromWidget(
Expand All @@ -96,6 +97,7 @@ func (r *TurnstileWidgetResource) Read(ctx context.Context, req resource.ReadReq

if err != nil {
resp.Diagnostics.AddError("Error reading challenge widget", err.Error())
return
}

data = buildChallengeModelFromWidget(
Expand Down Expand Up @@ -129,6 +131,7 @@ func (r *TurnstileWidgetResource) Update(ctx context.Context, req resource.Updat

if err != nil {
resp.Diagnostics.AddError("Error reading challenge widget", err.Error())
return
}

data = buildChallengeModelFromWidget(
Expand All @@ -151,13 +154,15 @@ func (r *TurnstileWidgetResource) Delete(ctx context.Context, req resource.Delet
err := r.client.V1.DeleteTurnstileWidget(ctx, cfv1.AccountIdentifier(data.AccountID.ValueString()), data.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError("Error deleting challenge widget", err.Error())
return
}
}

func (r *TurnstileWidgetResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
idParts := strings.Split(req.ID, "/")
if len(idParts) != 2 {
resp.Diagnostics.AddError("Error importing challenge widget", "Invalid ID specified. Please specify the ID as \"accounts_id/sitekey\"")
return
}
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("account_id"), idParts[0])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), idParts[1])...)
Expand Down

0 comments on commit 3ef096f

Please sign in to comment.