From 934e3e56866f5eaa1394af6d69a9e39131def8d9 Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Fri, 2 Jul 2021 13:34:25 +1000 Subject: [PATCH] resource/cloudflare_worker_cron_trigger: account for missing scripts If the script is removed, the associated relationships (triggers, environment variables, etc) need to be removed from the state to ensure that the dashboard behaviour of total cleanup is handled within Terraform. Closes #1120 --- cloudflare/resource_cloudflare_worker_cron_trigger.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cloudflare/resource_cloudflare_worker_cron_trigger.go b/cloudflare/resource_cloudflare_worker_cron_trigger.go index 86625357141..90cd2c8ed4d 100644 --- a/cloudflare/resource_cloudflare_worker_cron_trigger.go +++ b/cloudflare/resource_cloudflare_worker_cron_trigger.go @@ -3,6 +3,7 @@ package cloudflare import ( "context" "fmt" + "strings" "github.com/cloudflare/cloudflare-go" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" @@ -44,6 +45,7 @@ func resourceCloudflareWorkerCronTriggerUpdate(d *schema.ResourceData, meta inte _, err := client.UpdateWorkerCronTriggers(context.Background(), scriptName, transformSchemaToWorkerCronTriggerStruct(d)) if err != nil { + return fmt.Errorf("failed to update Worker Cron Trigger: %s", err) } @@ -58,6 +60,12 @@ func resourceCloudflareWorkerCronTriggerRead(d *schema.ResourceData, meta interf s, err := client.ListWorkerCronTriggers(context.Background(), scriptName) if err != nil { + // If the script is removed, we also need to remove the triggers. + if strings.Contains(err.Error(), "workers.api.error.script_not_found") { + d.SetId("") + return nil + } + return fmt.Errorf("failed to read Worker Cron Trigger: %s", err) }