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 webassembly_binding in cloudflare_worker_script #780

Merged
merged 3 commits into from
Sep 1, 2020
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
42 changes: 42 additions & 0 deletions cloudflare/resource_cloudflare_worker_script.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cloudflare

import (
"encoding/base64"
"fmt"
"io/ioutil"
"log"
"strings"

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -85,6 +100,11 @@ func resourceCloudflareWorkerScript() *schema.Resource {
Optional: true,
Elem: kvNamespaceBindingResource,
},
"webassembly_binding": {
Type: schema.TypeSet,
Optional: true,
Elem: webAssemblyBindingResource,
},
},
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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),
})
}
}

Expand All @@ -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
}

Expand Down
10 changes: 8 additions & 2 deletions cloudflare/resource_cloudflare_worker_script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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),
),
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions website/docs/r/worker_script.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
```

Expand All @@ -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`
Expand Down