From bb092fcf07e5242525c87413c1afdcacc30f1bd8 Mon Sep 17 00:00:00 2001 From: ldesgoui Date: Sun, 30 Aug 2020 14:19:16 +0200 Subject: [PATCH 1/3] add support for worker_script.webassembly_binding --- .../resource_cloudflare_worker_script.go | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/cloudflare/resource_cloudflare_worker_script.go b/cloudflare/resource_cloudflare_worker_script.go index 2e26e040ad..9b29ac9fc1 100644 --- a/cloudflare/resource_cloudflare_worker_script.go +++ b/cloudflare/resource_cloudflare_worker_script.go @@ -1,7 +1,9 @@ package cloudflare import ( + "encoding/base64" "fmt" + "io/ioutil" "log" "strings" @@ -50,6 +52,19 @@ var secretTextBindingResource = &schema.Resource{ }, } +var webAssemblyBindingResource = &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + }, + "module": { + Type: schema.TypeString, + Required: true, + }, + }, +} + func resourceCloudflareWorkerScript() *schema.Resource { return &schema.Resource{ Create: resourceCloudflareWorkerScriptCreate, @@ -85,6 +100,11 @@ func resourceCloudflareWorkerScript() *schema.Resource { Optional: true, Elem: kvNamespaceBindingResource, }, + "webassembly_binding": { + Type: schema.TypeSet, + Optional: true, + Elem: webAssemblyBindingResource, + }, }, } } @@ -147,6 +167,14 @@ func parseWorkerBindings(d *schema.ResourceData, bindings ScriptBindings) { Text: data["text"].(string), } } + + for _, rawData := range d.Get("webassembly_binding").(*schema.Set).List() { + data := rawData.(map[string]interface{}) + module := base64.NewDecoder(base64.StdEncoding, strings.NewReader(data["module"].(string))) + bindings[data["name"].(string)] = cloudflare.WorkerWebAssemblyBinding{ + Module: module, + } + } } func resourceCloudflareWorkerScriptCreate(d *schema.ResourceData, meta interface{}) error { @@ -222,6 +250,7 @@ func resourceCloudflareWorkerScriptRead(d *schema.ResourceData, meta interface{} kvNamespaceBindings := &schema.Set{F: schema.HashResource(kvNamespaceBindingResource)} plainTextBindings := &schema.Set{F: schema.HashResource(plainTextBindingResource)} secretTextBindings := &schema.Set{F: schema.HashResource(secretTextBindingResource)} + webAssemblyBindings := &schema.Set{F: schema.HashResource(webAssemblyBindingResource)} for name, binding := range bindings { switch v := binding.(type) { @@ -245,6 +274,15 @@ func resourceCloudflareWorkerScriptRead(d *schema.ResourceData, meta interface{} "name": name, "text": value, }) + case cloudflare.WorkerWebAssemblyBinding: + module, err := ioutil.ReadAll(v.Module) + if err != nil { + return errors.Wrap(err, fmt.Sprintf("cannot read contents of wasm bindings (%s)", name)) + } + webAssemblyBindings.Add(map[string]interface{}{ + "name": name, + "module": base64.StdEncoding.EncodeToString(module), + }) } } @@ -264,6 +302,10 @@ func resourceCloudflareWorkerScriptRead(d *schema.ResourceData, meta interface{} return fmt.Errorf("cannot set secret text bindings (%s): %v", d.Id(), err) } + if err := d.Set("webassembly_binding", webAssemblyBindings); err != nil { + return fmt.Errorf("cannot set webassembly bindings (%s): %v", d.Id(), err) + } + return nil } From 4ce8240d6ef5c631e75644a198e78a2f0daa8381 Mon Sep 17 00:00:00 2001 From: ldesgoui Date: Sun, 30 Aug 2020 14:31:39 +0200 Subject: [PATCH 2/3] include webassembly_binding in existing tests --- cloudflare/resource_cloudflare_worker_script_test.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cloudflare/resource_cloudflare_worker_script_test.go b/cloudflare/resource_cloudflare_worker_script_test.go index c5071b95a3..a1dc88e82e 100644 --- a/cloudflare/resource_cloudflare_worker_script_test.go +++ b/cloudflare/resource_cloudflare_worker_script_test.go @@ -13,6 +13,7 @@ import ( const ( scriptContent1 = `addEventListener('fetch', event => {event.respondWith(new Response('test 1'))});` scriptContent2 = `addEventListener('fetch', event => {event.respondWith(new Response('test 2'))});` + encodedWasm = "AGFzbQEAAAAGgYCAgAAA" // wat source: `(module)`, so literally just an empty wasm module ) func TestAccCloudflareWorkerScript_MultiScriptEnt(t *testing.T) { @@ -49,7 +50,7 @@ func TestAccCloudflareWorkerScript_MultiScriptEnt(t *testing.T) { { Config: testAccCheckCloudflareWorkerScriptConfigMultiScriptUpdateBinding(rnd), Check: resource.ComposeTestCheckFunc( - testAccCheckCloudflareWorkerScriptExists(name, &script, []string{"MY_KV_NAMESPACE", "MY_PLAIN_TEXT", "MY_SECRET_TEXT"}), + testAccCheckCloudflareWorkerScriptExists(name, &script, []string{"MY_KV_NAMESPACE", "MY_PLAIN_TEXT", "MY_SECRET_TEXT", "MY_WASM"}), resource.TestCheckResourceAttr(name, "name", rnd), resource.TestCheckResourceAttr(name, "content", scriptContent2), ), @@ -98,7 +99,12 @@ resource "cloudflare_worker_script" "%[1]s" { name = "MY_SECRET_TEXT" text = "%[1]s" } -}`, rnd, scriptContent2) + + webassembly_binding { + name = "MY_WASM" + module = "%[3]s" + } +}`, rnd, scriptContent2, encodedWasm) } func getRequestParamsFromResource(rs *terraform.ResourceState) cloudflare.WorkerRequestParams { From 22e6bc175a33f3407d6fc0fc4519c2a0ba265945 Mon Sep 17 00:00:00 2001 From: ldesgoui Date: Sun, 30 Aug 2020 14:34:54 +0200 Subject: [PATCH 3/3] add documentation for webassembly_binding --- website/docs/r/worker_script.html.markdown | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/website/docs/r/worker_script.html.markdown b/website/docs/r/worker_script.html.markdown index e184f2fc2b..d1236e1ced 100644 --- a/website/docs/r/worker_script.html.markdown +++ b/website/docs/r/worker_script.html.markdown @@ -36,6 +36,11 @@ resource "cloudflare_worker_script" "my_script" { name = "MY_EXAMPLE_SECRET_TEXT" text = var.secret_foo_value } + + webassembly_binding { + name = "MY_EXAMPLE_WASM" + module = filebase64("example.wasm") + } } ``` @@ -61,6 +66,11 @@ The following arguments are supported: * `name` - (Required) The global variable for the binding in your Worker code. * `text` - (Required) The secret text you want to store. +**webassembly_binding** supports: + +* `name` - (Required) The global variable for the binding in your Worker code. +* `module` - (Required) The base64 encoded wasm module you want to store. + ## Import To import a script, use a script name, e.g. `script_name`