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 Workers Service Bindings #1760

Merged
merged 4 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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/1760.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/cloudflare_workers_script: add support for `service_binding` bindings
```
12 changes: 12 additions & 0 deletions docs/resources/worker_script.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ resource "cloudflare_worker_script" "my_script" {
name = "MY_EXAMPLE_WASM"
module = filebase64("example.wasm")
}

jacobbednarz marked this conversation as resolved.
Show resolved Hide resolved
service_binding {
name = "MY_SERVICE_BINDING"
service = "MY_SERVICE"
environment = "production"
}
}
```

Expand Down Expand Up @@ -69,6 +75,12 @@ The following arguments are supported:
- `name` - (Required) The global variable for the binding in your Worker code.
- `module` - (Required) The base64 encoded wasm module you want to store.

**service_binding** supports:

- `name` - (Required) The global variable for the binding in your Worker code.
- `service` - (Required) The name of the Worker to bind to.
- `environment` - (Optional) The name of the Worker environment to bind to.

## Import

To import a script, use a script name, e.g. `script_name`
Expand Down
19 changes: 19 additions & 0 deletions internal/provider/resource_cloudflare_worker_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ func parseWorkerBindings(d *schema.ResourceData, bindings ScriptBindings) {
Module: module,
}
}

for _, rawData := range d.Get("service_binding").(*schema.Set).List() {
data := rawData.(map[string]interface{})
bindings[data["name"].(string)] = cloudflare.WorkerServiceBinding{
Service: data["service"].(string),
Environment: cloudflare.BoolPtr(data["environment"].(string)),
jacobbednarz marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

func resourceCloudflareWorkerScriptCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down Expand Up @@ -169,6 +177,7 @@ func resourceCloudflareWorkerScriptRead(ctx context.Context, d *schema.ResourceD
plainTextBindings := &schema.Set{F: schema.HashResource(plainTextBindingResource)}
secretTextBindings := &schema.Set{F: schema.HashResource(secretTextBindingResource)}
webAssemblyBindings := &schema.Set{F: schema.HashResource(webAssemblyBindingResource)}
serviceBindings := &schema.Set{F: schema.HashResource(serviceBindingResource)}

for name, binding := range bindings {
switch v := binding.(type) {
Expand Down Expand Up @@ -201,6 +210,12 @@ func resourceCloudflareWorkerScriptRead(ctx context.Context, d *schema.ResourceD
"name": name,
"module": base64.StdEncoding.EncodeToString(module),
})
case cloudflare.WorkerServiceBinding:
serviceBindings.Add(map[string]interface{}{
"name": name,
"service": v.Service,
"environment": v.Environment,
})
}
}

Expand All @@ -224,6 +239,10 @@ func resourceCloudflareWorkerScriptRead(ctx context.Context, d *schema.ResourceD
return diag.FromErr(fmt.Errorf("cannot set webassembly bindings (%s): %w", d.Id(), err))
}

if err := d.Set("service_binding", serviceBindings); err != nil {
return diag.FromErr(fmt.Errorf("cannot set service bindings (%s): %w", d.Id(), err))
}

return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,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", "MY_WASM"}),
testAccCheckCloudflareWorkerScriptExists(name, &script, []string{"MY_KV_NAMESPACE", "MY_PLAIN_TEXT", "MY_SECRET_TEXT", "MY_WASM", "MY_SERVICE_BINDING"}),
jacobbednarz marked this conversation as resolved.
Show resolved Hide resolved
resource.TestCheckResourceAttr(name, "name", rnd),
resource.TestCheckResourceAttr(name, "content", scriptContent2),
),
Expand Down Expand Up @@ -105,6 +105,12 @@ resource "cloudflare_worker_script" "%[1]s" {
name = "MY_WASM"
module = "%[3]s"
}

service_binding {
name = "MY_SERVICE_BINDING"
service = "MY_SERVICE"
environment = "production"
}
}`, rnd, scriptContent2, encodedWasm)
}

Expand Down
22 changes: 22 additions & 0 deletions internal/provider/schema_cloudflare_worker_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ var webAssemblyBindingResource = &schema.Resource{
},
}

var serviceBindingResource = &schema.Resource{
jacobbednarz marked this conversation as resolved.
Show resolved Hide resolved
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"service": {
Type: schema.TypeString,
Required: true,
},
"environment": {
Type: schema.TypeString,
Optional: true,
},
},
}

func resourceCloudflareWorkerScriptSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"name": {
Expand Down Expand Up @@ -86,5 +103,10 @@ func resourceCloudflareWorkerScriptSchema() map[string]*schema.Schema {
Optional: true,
Elem: webAssemblyBindingResource,
},
"service_binding": {
Type: schema.TypeSet,
Optional: true,
Elem: serviceBindingResource,
},
}
}
12 changes: 12 additions & 0 deletions templates/resources/worker_script.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ resource "cloudflare_worker_script" "my_script" {
name = "MY_EXAMPLE_WASM"
module = filebase64("example.wasm")
}

service_binding {
name = "MY_SERVICE_BINDING"
service = "MY_SERVICE"
environment = "production"
}
}
```

Expand Down Expand Up @@ -69,6 +75,12 @@ The following arguments are supported:
- `name` - (Required) The global variable for the binding in your Worker code.
- `module` - (Required) The base64 encoded wasm module you want to store.

**service_binding** supports:

- `name` - (Required) The global variable for the binding in your Worker code.
- `service` - (Required) The name of the Worker to bind to.
- `environment` - (Optional) The name of the Worker environment to bind to.

## Import

To import a script, use a script name, e.g. `script_name`
Expand Down