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

Add support for durable objects binding #2059

Closed
wants to merge 6 commits into from
Closed
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/2059.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/cloudflare_worker_script: add support for durable objects bindings
```
5 changes: 5 additions & 0 deletions examples/resources/cloudflare_worker_script/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ resource "cloudflare_worker_script" "my_script" {
name = "MY_BUCKET"
bucket_name = "MY_BUCKET_NAME"
}

durable_object_binding {
name = "MY_DURABLE_OBJECT"
class_name = "MyDurableObject"
}
}
8 changes: 8 additions & 0 deletions internal/provider/resource_cloudflare_workers_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ func parseWorkerBindings(d *schema.ResourceData, bindings ScriptBindings) {
BucketName: data["bucket_name"].(string),
}
}

for _, rawData := range d.Get("durable_object_binding").(*schema.Set).List() {
data := rawData.(map[string]interface{})
bindings[data["name"].(string)] = cloudflare.WorkerDurableObjectBinding{
ScriptName: d.Get("name").(string),
ClassName: data["class_name"].(string),
}
}
}

func resourceCloudflareWorkerScriptCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
8 changes: 7 additions & 1 deletion internal/provider/resource_cloudflare_workers_script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
const (
scriptContent1 = `addEventListener('fetch', event => {event.respondWith(new Response('test 1'))});`
scriptContent2 = `addEventListener('fetch', event => {event.respondWith(new Response('test 2'))});`
scriptContent3 = `export class MyDurableObject {constructor(state, env) {} async fetch(request) {return new Response("Hello World");}}`
moduleContent = `export default { fetch() { return new Response('Hello world'); }, };`
encodedWasm = "AGFzbQEAAAAGgYCAgAAA" // wat source: `(module)`, so literally just an empty wasm module
)
Expand Down Expand Up @@ -166,7 +167,12 @@ resource "cloudflare_worker_script" "%[1]s" {
service = cloudflare_worker_script.%[1]s-service.name
environment = "production"
}
}`, rnd, scriptContent2, encodedWasm)

durable_object_binding {
name = "MY_DURABLE_OBJECT"
class_name = "MyDurableObject"
}
}`, rnd, scriptContent3, encodedWasm)
}

func testAccCheckCloudflareWorkerScriptUploadModule(rnd string) string {
Expand Down