From 3ef096fb77587344d97a432ca69ac31f86896f84 Mon Sep 17 00:00:00 2001 From: Maxime Guerreiro Date: Fri, 3 May 2024 15:17:01 +0200 Subject: [PATCH] Fix error handling in cloudflare_turnstile 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. --- .changelog/3284.txt | 3 +++ internal/framework/service/turnstile/resource.go | 5 +++++ 2 files changed, 8 insertions(+) create mode 100644 .changelog/3284.txt diff --git a/.changelog/3284.txt b/.changelog/3284.txt new file mode 100644 index 0000000000..eed2b8c9e3 --- /dev/null +++ b/.changelog/3284.txt @@ -0,0 +1,3 @@ +```release-note:bug +resource/cloudflare_turnstile: Fix error handling corrupting state +``` diff --git a/internal/framework/service/turnstile/resource.go b/internal/framework/service/turnstile/resource.go index 8d67710fd6..e6379a487c 100644 --- a/internal/framework/service/turnstile/resource.go +++ b/internal/framework/service/turnstile/resource.go @@ -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( @@ -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( @@ -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( @@ -151,6 +154,7 @@ 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 } } @@ -158,6 +162,7 @@ func (r *TurnstileWidgetResource) ImportState(ctx context.Context, req resource. 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])...)