Skip to content

Commit

Permalink
Merge pull request #3154 from WalshyDev/walshy/add-wfp-to-workers-script
Browse files Browse the repository at this point in the history
Allow uploading Workers to dispatch namespaces + tags
  • Loading branch information
jacobbednarz committed Mar 15, 2024
2 parents 1d5e302 + be3786f commit 64b49c1
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 22 deletions.
7 changes: 7 additions & 0 deletions .changelog/3154.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
resource/cloudflare_worker_script: Add `dispatch_namespace` to support uploading to a Workers for Platforms namespace
```

```release-note:enhancement
resource/cloudflare_worker_script: Add `tags` to support tagging Workers for Platforms Workers
```
2 changes: 2 additions & 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.
- `d1_database_binding` (Block Set) (see [below for nested schema](#nestedblock--d1_database_binding))
- `dispatch_namespace` (String) Name of the Workers for Platforms dispatch namespace.
- `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.
Expand All @@ -84,6 +85,7 @@ resource "cloudflare_worker_script" "my_script" {
- `r2_bucket_binding` (Block Set) (see [below for nested schema](#nestedblock--r2_bucket_binding))
- `secret_text_binding` (Block Set) (see [below for nested schema](#nestedblock--secret_text_binding))
- `service_binding` (Block Set) (see [below for nested schema](#nestedblock--service_binding))
- `tags` (Set of String)
- `webassembly_binding` (Block Set) (see [below for nested schema](#nestedblock--webassembly_binding))

### Read-Only
Expand Down
8 changes: 8 additions & 0 deletions examples/resources/cloudflare_worker_script/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,11 @@ resource "cloudflare_worker_script" "my_script" {
dataset = "dataset1"
}
}

resource "cloudflare_worker_script" "wfp_user_worker" {
account_id = "f037e56e89293a057740de681ac9abbe"
name = "customer-worker-1"
content = file("script.js")
dispatch_namespace = "my-namespace"
tags = ["free"]
}
67 changes: 46 additions & 21 deletions internal/sdkv2provider/resource_cloudflare_workers_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ func getScriptData(d *schema.ResourceData, client *cloudflare.API) (ScriptData,

type ScriptBindings map[string]cloudflare.WorkerBinding

func getWorkerScriptBindings(ctx context.Context, accountId, scriptName string, client *cloudflare.API) (ScriptBindings, error) {
resp, err := client.ListWorkerBindings(ctx, cloudflare.AccountIdentifier(accountId), cloudflare.ListWorkerBindingsParams{ScriptName: scriptName})
func getWorkerScriptBindings(ctx context.Context, accountId, scriptName string, dispatchNamespace *string, client *cloudflare.API) (ScriptBindings, error) {
resp, err := client.ListWorkerBindings(
ctx,
cloudflare.AccountIdentifier(accountId),
cloudflare.ListWorkerBindingsParams{ScriptName: scriptName, DispatchNamespace: dispatchNamespace},
)
if err != nil {
return nil, fmt.Errorf("cannot list script bindings: %w", err)
}
Expand Down Expand Up @@ -158,6 +162,14 @@ func getCompatibilityFlags(d *schema.ResourceData) []string {
return compatibilityFlags
}

func getTags(d *schema.ResourceData) []string {
tags := make([]string, 0)
for _, item := range d.Get("tags").(*schema.Set).List() {
tags = append(tags, item.(string))
}
return tags
}

func resourceCloudflareWorkerScriptCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*cloudflare.API)
accountID := d.Get(consts.AccountIDSchemaKey).(string)
Expand All @@ -167,8 +179,10 @@ func resourceCloudflareWorkerScriptCreate(ctx context.Context, d *schema.Resourc
return diag.FromErr(err)
}

dispatchNamespace := d.Get("dispatch_namespace").(string)

// make sure that the worker does not already exist
r, _ := client.GetWorker(ctx, cloudflare.AccountIdentifier(accountID), scriptData.Params.ScriptName)
r, _ := client.GetWorkerWithDispatchNamespace(ctx, cloudflare.AccountIdentifier(accountID), scriptData.Params.ScriptName, dispatchNamespace)
if r.WorkerScript.Script != "" {
return diag.FromErr(fmt.Errorf("script already exists"))
}
Expand All @@ -188,15 +202,19 @@ func resourceCloudflareWorkerScriptCreate(ctx context.Context, d *schema.Resourc

placement := getPlacement(d)

tags := getTags(d)

_, 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,
Placement: &placement,
ScriptName: scriptData.Params.ScriptName,
Script: scriptBody,
CompatibilityDate: d.Get("compatibility_date").(string),
CompatibilityFlags: getCompatibilityFlags(d),
Module: d.Get("module").(bool),
Bindings: bindings,
Logpush: &logpush,
Placement: &placement,
DispatchNamespaceName: &dispatchNamespace,
Tags: tags,
})
if err != nil {
return diag.FromErr(errors.Wrap(err, "error creating worker script"))
Expand All @@ -216,7 +234,9 @@ func resourceCloudflareWorkerScriptRead(ctx context.Context, d *schema.ResourceD
return diag.FromErr(err)
}

r, err := client.GetWorker(ctx, cloudflare.AccountIdentifier(accountID), scriptData.Params.ScriptName)
dispatchNamespace := d.Get("dispatch_namespace").(string)

r, err := client.GetWorkerWithDispatchNamespace(ctx, cloudflare.AccountIdentifier(accountID), scriptData.Params.ScriptName, dispatchNamespace)
if err != nil {
// If the resource is deleted, we should set the ID to "" and not
// return an error according to the terraform spec
Expand All @@ -234,7 +254,7 @@ func resourceCloudflareWorkerScriptRead(ctx context.Context, d *schema.ResourceD

parseWorkerBindings(d, existingBindings)

bindings, err := getWorkerScriptBindings(ctx, accountID, d.Get("name").(string), client)
bindings, err := getWorkerScriptBindings(ctx, accountID, d.Get("name").(string), &dispatchNamespace, client)
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -380,15 +400,20 @@ func resourceCloudflareWorkerScriptUpdate(ctx context.Context, d *schema.Resourc

placement := getPlacement(d)

dispatchNamespace := d.Get("dispatch_namespace").(string)
tags := getTags(d)

_, 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,
Placement: &placement,
ScriptName: scriptData.Params.ScriptName,
Script: scriptBody,
CompatibilityDate: d.Get("compatibility_date").(string),
CompatibilityFlags: getCompatibilityFlags(d),
Module: d.Get("module").(bool),
Bindings: bindings,
Logpush: &logpush,
Placement: &placement,
DispatchNamespaceName: &dispatchNamespace,
Tags: tags,
})
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 @@ -308,7 +308,7 @@ func testAccCheckCloudflareWorkerScriptExists(n string, script *cloudflare.Worke
}

name := strings.Replace(n, "cloudflare_worker_script.", "", -1)
foundBindings, err := getWorkerScriptBindings(context.Background(), accountID, name, client)
foundBindings, err := getWorkerScriptBindings(context.Background(), accountID, name, nil, client)
if err != nil {
return fmt.Errorf("cannot list script bindings: %w", err)
}
Expand Down
12 changes: 12 additions & 0 deletions internal/sdkv2provider/schema_cloudflare_workers_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,18 @@ func resourceCloudflareWorkerScriptSchema() map[string]*schema.Schema {
Optional: true,
Elem: placementResource,
},
"dispatch_namespace": {
Type: schema.TypeString,
Optional: true,
Description: "Name of the Workers for Platforms dispatch namespace.",
},
"tags": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
// TODO: dispatch_namespace binding
"plain_text_binding": {
Type: schema.TypeSet,
Optional: true,
Expand Down

0 comments on commit 64b49c1

Please sign in to comment.