diff --git a/.changelog/2375.txt b/.changelog/2375.txt new file mode 100644 index 0000000000..b81a06f2f3 --- /dev/null +++ b/.changelog/2375.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/cloudflare_worker_script: Add `logpush` attribute +``` \ No newline at end of file diff --git a/docs/resources/worker_script.md b/docs/resources/worker_script.md index 3857ca63ad..b185e8b3fd 100644 --- a/docs/resources/worker_script.md +++ b/docs/resources/worker_script.md @@ -75,6 +75,7 @@ resource "cloudflare_worker_script" "my_script" { - `compatibility_date` (String) The date to use for the compatibility flag. - `compatibility_flags` (Set of String) Compatibility flags used for Worker Scripts. - `kv_namespace_binding` (Block Set) (see [below for nested schema](#nestedblock--kv_namespace_binding)) +- `logpush` (Boolean) Enabling allows Worker events to be sent to a defined Logpush destination. - `module` (Boolean) Whether to upload Worker as a module. - `plain_text_binding` (Block Set) (see [below for nested schema](#nestedblock--plain_text_binding)) - `queue_binding` (Block Set) (see [below for nested schema](#nestedblock--queue_binding)) diff --git a/internal/sdkv2provider/resource_cloudflare_workers_script.go b/internal/sdkv2provider/resource_cloudflare_workers_script.go index 6bee162513..2e5d86f367 100644 --- a/internal/sdkv2provider/resource_cloudflare_workers_script.go +++ b/internal/sdkv2provider/resource_cloudflare_workers_script.go @@ -165,6 +165,8 @@ func resourceCloudflareWorkerScriptCreate(ctx context.Context, d *schema.Resourc parseWorkerBindings(d, bindings) + logpush := d.Get("logpush").(bool) + _, err = client.UploadWorker(ctx, cloudflare.AccountIdentifier(accountID), cloudflare.CreateWorkerParams{ ScriptName: scriptData.Params.ScriptName, Script: scriptBody, @@ -172,6 +174,7 @@ func resourceCloudflareWorkerScriptCreate(ctx context.Context, d *schema.Resourc CompatibilityFlags: getCompatibilityFlags(d), Module: d.Get("module").(bool), Bindings: bindings, + Logpush: &logpush, }) if err != nil { return diag.FromErr(errors.Wrap(err, "error creating worker script")) @@ -339,6 +342,8 @@ func resourceCloudflareWorkerScriptUpdate(ctx context.Context, d *schema.Resourc parseWorkerBindings(d, bindings) + logpush := d.Get("logpush").(bool) + _, err = client.UploadWorker(ctx, cloudflare.AccountIdentifier(accountID), cloudflare.CreateWorkerParams{ ScriptName: scriptData.Params.ScriptName, Script: scriptBody, @@ -346,6 +351,7 @@ func resourceCloudflareWorkerScriptUpdate(ctx context.Context, d *schema.Resourc CompatibilityFlags: getCompatibilityFlags(d), Module: d.Get("module").(bool), Bindings: bindings, + Logpush: &logpush, }) if err != nil { return diag.FromErr(errors.Wrap(err, "error updating worker script")) diff --git a/internal/sdkv2provider/resource_cloudflare_workers_script_test.go b/internal/sdkv2provider/resource_cloudflare_workers_script_test.go index 9e67edee8f..4728470e76 100644 --- a/internal/sdkv2provider/resource_cloudflare_workers_script_test.go +++ b/internal/sdkv2provider/resource_cloudflare_workers_script_test.go @@ -75,6 +75,8 @@ func TestAccCloudflareWorkerScript_ModuleUpload(t *testing.T) { var script cloudflare.WorkerScript rnd := generateRandomResourceName() name := "cloudflare_worker_script." + rnd + r2AccesKeyID := os.Getenv("CLOUDFLARE_R2_ACCESS_KEY_ID") + r2AccesKeySecret := os.Getenv("CLOUDFLARE_R2_ACCESS_KEY_SECRET") resource.Test(t, resource.TestCase{ PreCheck: func() { @@ -85,7 +87,7 @@ func TestAccCloudflareWorkerScript_ModuleUpload(t *testing.T) { CheckDestroy: testAccCheckCloudflareWorkerScriptDestroy, Steps: []resource.TestStep{ { - Config: testAccCheckCloudflareWorkerScriptUploadModule(rnd, accountID), + Config: testAccCheckCloudflareWorkerScriptUploadModule(rnd, accountID, r2AccesKeyID, r2AccesKeySecret), Check: resource.ComposeTestCheckFunc( testAccCheckCloudflareWorkerScriptExists(name, &script, nil), resource.TestCheckResourceAttr(name, "name", rnd), @@ -93,6 +95,7 @@ func TestAccCloudflareWorkerScript_ModuleUpload(t *testing.T) { resource.TestCheckResourceAttr(name, "compatibility_date", compatibilityDate), resource.TestCheckResourceAttr(name, "compatibility_flags.#", "2"), resource.TestCheckResourceAttr(name, "compatibility_flags.0", compatibilityFlags[0]), + resource.TestCheckResourceAttr(name, "logpush", "true"), ), }, }, @@ -195,16 +198,28 @@ resource "cloudflare_worker_script" "%[1]s" { }`, rnd, scriptContent2, encodedWasm, accountID) } -func testAccCheckCloudflareWorkerScriptUploadModule(rnd, accountID string) string { +func testAccCheckCloudflareWorkerScriptUploadModule(rnd, accountID, r2AccessKeyID, r2AccessKeySecret string) string { return fmt.Sprintf(` + resource "cloudflare_logpush_job" "%[1]s" { + enabled = true + account_id = "%[3]s" + name = "%[1]s" + logpull_options = "fields=Event,EventTimestampMs,Outcome,Exceptions,Logs,ScriptName" + destination_conf = "r2://terraform-acctest/date={DATE}?account-id=%[3]s&access-key-id=%[6]s&secret-access-key=%[7]s" + dataset = "workers_trace_events" + } + resource "cloudflare_worker_script" "%[1]s" { account_id = "%[3]s" name = "%[1]s" content = "%[2]s" module = true - compatibility_date = "%[4]s" - compatibility_flags = ["%[5]s"] -}`, rnd, moduleContent, accountID, compatibilityDate, strings.Join(compatibilityFlags, `","`)) + compatibility_date = "%[4]s" + compatibility_flags = ["%[5]s"] + logpush = true + + depends_on = [cloudflare_logpush_job.%[1]s] +}`, rnd, moduleContent, accountID, compatibilityDate, strings.Join(compatibilityFlags, `","`), r2AccessKeyID, r2AccessKeySecret) } func testAccCheckCloudflareWorkerScriptExists(n string, script *cloudflare.WorkerScript, bindings []string) resource.TestCheckFunc { diff --git a/internal/sdkv2provider/schema_cloudflare_workers_script.go b/internal/sdkv2provider/schema_cloudflare_workers_script.go index fbcf199e55..efcd7f9299 100644 --- a/internal/sdkv2provider/schema_cloudflare_workers_script.go +++ b/internal/sdkv2provider/schema_cloudflare_workers_script.go @@ -168,6 +168,11 @@ func resourceCloudflareWorkerScriptSchema() map[string]*schema.Schema { }, Computed: true, }, + "logpush": { + Type: schema.TypeBool, + Optional: true, + Description: "Enabling allows Worker events to be sent to a defined Logpush destination.", + }, "plain_text_binding": { Type: schema.TypeSet, Optional: true,