From d082784e42ebfb32c015f1cb763b8a635b0e73c2 Mon Sep 17 00:00:00 2001 From: Chris Lyons <52037738+mephmanx@users.noreply.github.com> Date: Thu, 23 Nov 2023 16:57:31 +0000 Subject: [PATCH 1/6] adding_d1_support Signed-off-by: Chris Lyons <52037738+mephmanx@users.noreply.github.com> --- .../resource_cloudflare_workers_script.go | 18 +++++++++++++++++ ...resource_cloudflare_workers_script_test.go | 7 ++++++- .../schema_cloudflare_workers_script.go | 20 +++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/internal/sdkv2provider/resource_cloudflare_workers_script.go b/internal/sdkv2provider/resource_cloudflare_workers_script.go index 8ef3c9458e..4c846794ab 100644 --- a/internal/sdkv2provider/resource_cloudflare_workers_script.go +++ b/internal/sdkv2provider/resource_cloudflare_workers_script.go @@ -129,6 +129,14 @@ func parseWorkerBindings(d *schema.ResourceData, bindings ScriptBindings) { Queue: data["queue"].(string), } } + + for _, rawData := range d.Get("d1_database_binding").(*schema.Set).List() { + data := rawData.(map[string]interface{}) + + bindings[data["name"].(string)] = cloudflare.WorkerD1DatabaseBinding{ + DatabaseID: data["database_id"].(string), + } + } } func getPlacement(d *schema.ResourceData) cloudflare.Placement { @@ -239,6 +247,7 @@ func resourceCloudflareWorkerScriptRead(ctx context.Context, d *schema.ResourceD r2BucketBindings := &schema.Set{F: schema.HashResource(r2BucketBindingResource)} analyticsEngineBindings := &schema.Set{F: schema.HashResource(analyticsEngineBindingResource)} queueBindings := &schema.Set{F: schema.HashResource(queueBindingResource)} + d1DatabaseBindings := &schema.Set{F: schema.HashResource(d1BindingResource)} for name, binding := range bindings { switch v := binding.(type) { @@ -292,6 +301,11 @@ func resourceCloudflareWorkerScriptRead(ctx context.Context, d *schema.ResourceD "binding": name, "queue": v.Queue, }) + case cloudflare.WorkerD1DatabaseBinding: + d1DatabaseBindings.Add(map[string]interface{} { + "name": name, + "database_id": v.DatabaseID, + }) } } @@ -331,6 +345,10 @@ func resourceCloudflareWorkerScriptRead(ctx context.Context, d *schema.ResourceD return diag.FromErr(fmt.Errorf("cannot set queue bindings (%s): %w", d.Id(), err)) } + if err := d.Set("d1_database_binding", d1DatabaseBindings); err != nil { + return diag.FromErr(fmt.Errorf("cannot set d1 database bindings (%s): %w", d.Id(), err)) + } + d.SetId(scriptData.ID) return nil diff --git a/internal/sdkv2provider/resource_cloudflare_workers_script_test.go b/internal/sdkv2provider/resource_cloudflare_workers_script_test.go index 5a871f110d..0d9573bee7 100644 --- a/internal/sdkv2provider/resource_cloudflare_workers_script_test.go +++ b/internal/sdkv2provider/resource_cloudflare_workers_script_test.go @@ -64,7 +64,7 @@ func TestAccCloudflareWorkerScript_MultiScriptEnt(t *testing.T) { { Config: testAccCheckCloudflareWorkerScriptConfigMultiScriptUpdateBinding(rnd, accountID), Check: resource.ComposeTestCheckFunc( - testAccCheckCloudflareWorkerScriptExists(name, &script, []string{"MY_KV_NAMESPACE", "MY_PLAIN_TEXT", "MY_SECRET_TEXT", "MY_WASM", "MY_SERVICE_BINDING", "MY_BUCKET", "MY_QUEUE"}), + testAccCheckCloudflareWorkerScriptExists(name, &script, []string{"MY_KV_NAMESPACE", "MY_PLAIN_TEXT", "MY_SECRET_TEXT", "MY_WASM", "MY_SERVICE_BINDING", "MY_BUCKET", "MY_QUEUE", "MY_DATABASE"}), resource.TestCheckResourceAttr(name, "name", rnd), resource.TestCheckResourceAttr(name, "content", scriptContent2), ), @@ -241,6 +241,11 @@ resource "cloudflare_worker_script" "%[1]s" { environment = "production" } + d1_database_binding { + name = "MY_DATABASE" + database_id = "cef5331f-e5c7-4c8a-a415-7908ae45f92a" + } + queue_binding { binding = "MY_QUEUE" queue = cloudflare_queue.%[1]s.name diff --git a/internal/sdkv2provider/schema_cloudflare_workers_script.go b/internal/sdkv2provider/schema_cloudflare_workers_script.go index f1df59ec65..60df53a408 100644 --- a/internal/sdkv2provider/schema_cloudflare_workers_script.go +++ b/internal/sdkv2provider/schema_cloudflare_workers_script.go @@ -134,6 +134,21 @@ var queueBindingResource = &schema.Resource{ }, } +var d1BindingResource = &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + Description: "The global variable for the binding in your Worker code.", + }, + "database_id": { + Type: schema.TypeString, + Required: true, + Description: "Database ID of D1 database to use.", + }, + }, +} + var placementResource = &schema.Resource{ Schema: map[string]*schema.Schema{ "mode": { @@ -232,5 +247,10 @@ func resourceCloudflareWorkerScriptSchema() map[string]*schema.Schema { Optional: true, Elem: queueBindingResource, }, + "d1_database_binding": { + Type: schema.TypeSet, + Optional: true, + Elem: d1BindingResource, + }, } } From 370bbb30875107cdb8011fbe57ba8e4f1b8dbcfe Mon Sep 17 00:00:00 2001 From: Chris Lyons <52037738+mephmanx@users.noreply.github.com> Date: Thu, 23 Nov 2023 17:02:57 +0000 Subject: [PATCH 2/6] adding_d1_support changelog Signed-off-by: Chris Lyons <52037738+mephmanx@users.noreply.github.com> --- .changelog/2960.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/2960.txt diff --git a/.changelog/2960.txt b/.changelog/2960.txt new file mode 100644 index 0000000000..b9f550ac10 --- /dev/null +++ b/.changelog/2960.txt @@ -0,0 +1,3 @@ +```release-note:adding_d1_support +resource/cloudflare_worker_script: Adding D1 support. +``` \ No newline at end of file From d108b1cd4e846b11bada9bed29b46fa084f2dff1 Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Tue, 28 Nov 2023 14:42:44 +1100 Subject: [PATCH 3/6] use hardcoded d1 UUID for tests --- .../resource_cloudflare_workers_script_test.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/internal/sdkv2provider/resource_cloudflare_workers_script_test.go b/internal/sdkv2provider/resource_cloudflare_workers_script_test.go index 0d9573bee7..d4cc007fb7 100644 --- a/internal/sdkv2provider/resource_cloudflare_workers_script_test.go +++ b/internal/sdkv2provider/resource_cloudflare_workers_script_test.go @@ -22,6 +22,7 @@ const ( moduleContent = `export default { fetch() { return new Response('Hello world'); }, };` encodedWasm = "AGFzbQEAAAAGgYCAgAAA" // wat source: `(module)`, so literally just an empty wasm module compatibilityDate = "2023-03-19" + d1DatabaseID = "f558dd73-d16b-4918-8e3d-1f3a3b557606" ) var ( @@ -64,7 +65,7 @@ func TestAccCloudflareWorkerScript_MultiScriptEnt(t *testing.T) { { Config: testAccCheckCloudflareWorkerScriptConfigMultiScriptUpdateBinding(rnd, accountID), Check: resource.ComposeTestCheckFunc( - testAccCheckCloudflareWorkerScriptExists(name, &script, []string{"MY_KV_NAMESPACE", "MY_PLAIN_TEXT", "MY_SECRET_TEXT", "MY_WASM", "MY_SERVICE_BINDING", "MY_BUCKET", "MY_QUEUE", "MY_DATABASE"}), + testAccCheckCloudflareWorkerScriptExists(name, &script, []string{"MY_KV_NAMESPACE", "MY_PLAIN_TEXT", "MY_SECRET_TEXT", "MY_WASM", "MY_SERVICE_BINDING", "MY_BUCKET", "MY_QUEUE"}), resource.TestCheckResourceAttr(name, "name", rnd), resource.TestCheckResourceAttr(name, "content", scriptContent2), ), @@ -94,7 +95,7 @@ func TestAccCloudflareWorkerScript_ModuleUpload(t *testing.T) { { Config: testAccCheckCloudflareWorkerScriptUploadModule(rnd, accountID, r2AccesKeyID, r2AccesKeySecret), Check: resource.ComposeTestCheckFunc( - testAccCheckCloudflareWorkerScriptExists(name, &script, nil), + testAccCheckCloudflareWorkerScriptExists(name, &script, []string{"MY_DATABASE"}), resource.TestCheckResourceAttr(name, "name", rnd), resource.TestCheckResourceAttr(name, "content", moduleContent), resource.TestCheckResourceAttr(name, "compatibility_date", compatibilityDate), @@ -139,6 +140,7 @@ func testAccCheckCloudflareWorkerScriptCreateBucket(t *testing.T, rnd string) { cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithEndpointResolverWithOptions(r2Resolver), config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accessKeyId, accessKeySecret, "")), + config.WithDefaultRegion("auto"), ) if err != nil { t.Error(err) @@ -241,11 +243,6 @@ resource "cloudflare_worker_script" "%[1]s" { environment = "production" } - d1_database_binding { - name = "MY_DATABASE" - database_id = "cef5331f-e5c7-4c8a-a415-7908ae45f92a" - } - queue_binding { binding = "MY_QUEUE" queue = cloudflare_queue.%[1]s.name @@ -277,8 +274,13 @@ resource "cloudflare_worker_script" "%[1]s" { mode = "smart" } + d1_database_binding { + name = "MY_DATABASE" + database_id = "%[8]s" + } + depends_on = [cloudflare_logpush_job.%[1]s] -}`, rnd, moduleContent, accountID, compatibilityDate, strings.Join(compatibilityFlags, `","`), r2AccessKeyID, r2AccessKeySecret) +}`, rnd, moduleContent, accountID, compatibilityDate, strings.Join(compatibilityFlags, `","`), r2AccessKeyID, r2AccessKeySecret, d1DatabaseID) } func testAccCheckCloudflareWorkerScriptExists(n string, script *cloudflare.WorkerScript, bindings []string) resource.TestCheckFunc { From 0e0a46d552788ec96281c7809f2608385f140758 Mon Sep 17 00:00:00 2001 From: Chris Lyons <52037738+mephmanx@users.noreply.github.com> Date: Wed, 27 Dec 2023 12:58:20 +0000 Subject: [PATCH 4/6] fixing tabs with gofmt Signed-off-by: Chris Lyons <52037738+mephmanx@users.noreply.github.com> --- internal/sdkv2provider/resource_cloudflare_workers_script.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/sdkv2provider/resource_cloudflare_workers_script.go b/internal/sdkv2provider/resource_cloudflare_workers_script.go index 4c846794ab..2e33d79a43 100644 --- a/internal/sdkv2provider/resource_cloudflare_workers_script.go +++ b/internal/sdkv2provider/resource_cloudflare_workers_script.go @@ -302,8 +302,8 @@ func resourceCloudflareWorkerScriptRead(ctx context.Context, d *schema.ResourceD "queue": v.Queue, }) case cloudflare.WorkerD1DatabaseBinding: - d1DatabaseBindings.Add(map[string]interface{} { - "name": name, + d1DatabaseBindings.Add(map[string]interface{}{ + "name": name, "database_id": v.DatabaseID, }) } From d721182188a673138b7163eaba2e6c2604461f90 Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Thu, 28 Dec 2023 09:20:01 +1100 Subject: [PATCH 5/6] update changelog --- .changelog/2960.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.changelog/2960.txt b/.changelog/2960.txt index b9f550ac10..81a0e3b048 100644 --- a/.changelog/2960.txt +++ b/.changelog/2960.txt @@ -1,3 +1,3 @@ -```release-note:adding_d1_support -resource/cloudflare_worker_script: Adding D1 support. -``` \ No newline at end of file +```release-note:enhancement +resource/cloudflare_worker_script: adds D1 binding support +``` From d8c476d62aeeef86bb1ed9f5d9f5785830ad9394 Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Thu, 28 Dec 2023 09:37:23 +1100 Subject: [PATCH 6/6] don't wipe out static test DB --- internal/framework/service/d1/resource_test.go | 6 ++++++ .../resource_cloudflare_workers_script_test.go | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/internal/framework/service/d1/resource_test.go b/internal/framework/service/d1/resource_test.go index 18a883dd05..e975f8a427 100644 --- a/internal/framework/service/d1/resource_test.go +++ b/internal/framework/service/d1/resource_test.go @@ -34,6 +34,12 @@ func init() { } for _, database := range databases { + // hardcoded D1 identifier until we can solve the cyclic import + // issues and automatically create this resource. + if database.UUID == "ce8b95dc-b376-4ff8-9b9e-1801ed6d745d" { + continue + } + err := client.DeleteD1Database(ctx, cloudflare.AccountIdentifier(accountID), database.UUID) if err != nil { return fmt.Errorf("failed to delete D1 database %q: %w", database.Name, err) diff --git a/internal/sdkv2provider/resource_cloudflare_workers_script_test.go b/internal/sdkv2provider/resource_cloudflare_workers_script_test.go index d4cc007fb7..f1970eb0e9 100644 --- a/internal/sdkv2provider/resource_cloudflare_workers_script_test.go +++ b/internal/sdkv2provider/resource_cloudflare_workers_script_test.go @@ -22,7 +22,7 @@ const ( moduleContent = `export default { fetch() { return new Response('Hello world'); }, };` encodedWasm = "AGFzbQEAAAAGgYCAgAAA" // wat source: `(module)`, so literally just an empty wasm module compatibilityDate = "2023-03-19" - d1DatabaseID = "f558dd73-d16b-4918-8e3d-1f3a3b557606" + d1DatabaseID = "ce8b95dc-b376-4ff8-9b9e-1801ed6d745d" ) var (