Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding attribute logpush to the worker script resource #2375

Merged
merged 4 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2375.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/cloudflare_worker_script: Add `logpush` attribute
```
1 change: 1 addition & 0 deletions docs/resources/worker_script.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,16 @@ 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,
CompatibilityDate: d.Get("compatibility_date").(string),
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"))
Expand Down Expand Up @@ -339,13 +342,16 @@ 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,
CompatibilityDate: d.Get("compatibility_date").(string),
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"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -85,14 +87,15 @@ 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),
resource.TestCheckResourceAttr(name, "content", moduleContent),
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"),
),
},
},
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions internal/sdkv2provider/schema_cloudflare_workers_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down