From 6fd27f813b5df8616c06ed5e53ce7a563a684d98 Mon Sep 17 00:00:00 2001 From: Aaron Loyd Date: Wed, 6 Jul 2022 23:58:58 -0500 Subject: [PATCH 1/4] Add support for Workers Service Bindings --- .changelog/1760.txt | 3 +++ docs/resources/worker_script.md | 12 ++++++++++ .../resource_cloudflare_worker_script.go | 19 ++++++++++++++++ .../resource_cloudflare_worker_script_test.go | 8 ++++++- .../schema_cloudflare_worker_script.go | 22 +++++++++++++++++++ templates/resources/worker_script.md | 12 ++++++++++ 6 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 .changelog/1760.txt diff --git a/.changelog/1760.txt b/.changelog/1760.txt new file mode 100644 index 0000000000..68680f5971 --- /dev/null +++ b/.changelog/1760.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/cloudflare_workers_script: add support for `service_binding` bindings +``` \ No newline at end of file diff --git a/docs/resources/worker_script.md b/docs/resources/worker_script.md index 7efc5f0f6a..231fbd0aa4 100644 --- a/docs/resources/worker_script.md +++ b/docs/resources/worker_script.md @@ -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" + } } ``` @@ -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` diff --git a/internal/provider/resource_cloudflare_worker_script.go b/internal/provider/resource_cloudflare_worker_script.go index d1c0113811..1a690796f6 100644 --- a/internal/provider/resource_cloudflare_worker_script.go +++ b/internal/provider/resource_cloudflare_worker_script.go @@ -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: data["environment"].(*string), + } + } } func resourceCloudflareWorkerScriptCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { @@ -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) { @@ -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, + }) } } @@ -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 } diff --git a/internal/provider/resource_cloudflare_worker_script_test.go b/internal/provider/resource_cloudflare_worker_script_test.go index dd472e5a6c..e26e143f0b 100644 --- a/internal/provider/resource_cloudflare_worker_script_test.go +++ b/internal/provider/resource_cloudflare_worker_script_test.go @@ -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"}), resource.TestCheckResourceAttr(name, "name", rnd), resource.TestCheckResourceAttr(name, "content", scriptContent2), ), @@ -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) } diff --git a/internal/provider/schema_cloudflare_worker_script.go b/internal/provider/schema_cloudflare_worker_script.go index fe6a72fbcb..fa35e2429e 100644 --- a/internal/provider/schema_cloudflare_worker_script.go +++ b/internal/provider/schema_cloudflare_worker_script.go @@ -55,6 +55,23 @@ var webAssemblyBindingResource = &schema.Resource{ }, } +var serviceBindingResource = &schema.Resource{ + 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": { @@ -86,5 +103,10 @@ func resourceCloudflareWorkerScriptSchema() map[string]*schema.Schema { Optional: true, Elem: webAssemblyBindingResource, }, + "service_binding": { + Type: schema.TypeSet, + Optional: true, + Elem: serviceBindingResource, + }, } } diff --git a/templates/resources/worker_script.md b/templates/resources/worker_script.md index 7efc5f0f6a..231fbd0aa4 100644 --- a/templates/resources/worker_script.md +++ b/templates/resources/worker_script.md @@ -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" + } } ``` @@ -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` From df727a63a1a25ce06ac33cff1afcceb68c469424 Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Mon, 11 Jul 2022 14:36:52 +1000 Subject: [PATCH 2/4] use BoolPtr, not *string as a cast --- internal/provider/resource_cloudflare_worker_script.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/provider/resource_cloudflare_worker_script.go b/internal/provider/resource_cloudflare_worker_script.go index 1a690796f6..18ebd83ea5 100644 --- a/internal/provider/resource_cloudflare_worker_script.go +++ b/internal/provider/resource_cloudflare_worker_script.go @@ -98,7 +98,7 @@ func parseWorkerBindings(d *schema.ResourceData, bindings ScriptBindings) { data := rawData.(map[string]interface{}) bindings[data["name"].(string)] = cloudflare.WorkerServiceBinding{ Service: data["service"].(string), - Environment: data["environment"].(*string), + Environment: cloudflare.BoolPtr(data["environment"].(string)), } } } From ca10cf7445c50e2d0eeea7e5579b2001e2fb29d2 Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Mon, 11 Jul 2022 14:47:01 +1000 Subject: [PATCH 3/4] fix type casting --- internal/provider/resource_cloudflare_worker_script.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/provider/resource_cloudflare_worker_script.go b/internal/provider/resource_cloudflare_worker_script.go index 18ebd83ea5..7f81bd4b3a 100644 --- a/internal/provider/resource_cloudflare_worker_script.go +++ b/internal/provider/resource_cloudflare_worker_script.go @@ -98,7 +98,7 @@ func parseWorkerBindings(d *schema.ResourceData, bindings ScriptBindings) { data := rawData.(map[string]interface{}) bindings[data["name"].(string)] = cloudflare.WorkerServiceBinding{ Service: data["service"].(string), - Environment: cloudflare.BoolPtr(data["environment"].(string)), + Environment: cloudflare.StringPtr(data["environment"].(string)), } } } From 3ab1d40beec7605caebb4024452da9c0a34cdda3 Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Mon, 11 Jul 2022 15:03:48 +1000 Subject: [PATCH 4/4] fix type conversions --- internal/provider/resource_cloudflare_worker_script.go | 4 ++-- .../provider/resource_cloudflare_worker_script_test.go | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/internal/provider/resource_cloudflare_worker_script.go b/internal/provider/resource_cloudflare_worker_script.go index 7f81bd4b3a..28ede46f72 100644 --- a/internal/provider/resource_cloudflare_worker_script.go +++ b/internal/provider/resource_cloudflare_worker_script.go @@ -214,7 +214,7 @@ func resourceCloudflareWorkerScriptRead(ctx context.Context, d *schema.ResourceD serviceBindings.Add(map[string]interface{}{ "name": name, "service": v.Service, - "environment": v.Environment, + "environment": cloudflare.String(v.Environment), }) } } @@ -309,4 +309,4 @@ func resourceCloudflareWorkerScriptImport(ctx context.Context, d *schema.Resourc _ = resourceCloudflareWorkerScriptRead(ctx, d, meta) return []*schema.ResourceData{d}, nil -} +} \ No newline at end of file diff --git a/internal/provider/resource_cloudflare_worker_script_test.go b/internal/provider/resource_cloudflare_worker_script_test.go index e26e143f0b..2e624d200b 100644 --- a/internal/provider/resource_cloudflare_worker_script_test.go +++ b/internal/provider/resource_cloudflare_worker_script_test.go @@ -82,6 +82,11 @@ resource "cloudflare_workers_kv_namespace" "%[1]s" { title = "%[1]s" } +resource "cloudflare_worker_script" "%[1]s-service" { + name = "%[1]s-service" + content = "%[2]s" +} + resource "cloudflare_worker_script" "%[1]s" { name = "%[1]s" content = "%[2]s" @@ -108,7 +113,7 @@ resource "cloudflare_worker_script" "%[1]s" { service_binding { name = "MY_SERVICE_BINDING" - service = "MY_SERVICE" + service = cloudflare_worker_script.%[1]s-service.name environment = "production" } }`, rnd, scriptContent2, encodedWasm)