diff --git a/.changelog/3821.txt b/.changelog/3821.txt new file mode 100644 index 0000000000..05d07d6269 --- /dev/null +++ b/.changelog/3821.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/cloudflare_workers_script: Add support for `hyperdrive` binding type +``` \ No newline at end of file diff --git a/docs/data-sources/rulesets.md b/docs/data-sources/rulesets.md index 22e42cf6b6..eb6f3e3887 100644 --- a/docs/data-sources/rulesets.md +++ b/docs/data-sources/rulesets.md @@ -164,14 +164,6 @@ Read-Only: - `custom_key` (List of Object) (see [below for nested schema](#nestedobjatt--rulesets--rules--action_parameters--version--custom_key)) - `ignore_query_strings_order` (Boolean) - -### Nested Schema for `rulesets.rules.action_parameters.version` - -Read-Only: - -- `eligible` (Boolean) -- `minimum_file_size` (Number) - ### Nested Schema for `rulesets.rules.action_parameters.version.custom_key` @@ -232,6 +224,15 @@ Read-Only: + +### Nested Schema for `rulesets.rules.action_parameters.version` + +Read-Only: + +- `eligible` (Boolean) +- `minimum_file_size` (Number) + + ### Nested Schema for `rulesets.rules.action_parameters.version` diff --git a/docs/resources/ruleset.md b/docs/resources/ruleset.md index f23fe2965d..ea4fc61be5 100644 --- a/docs/resources/ruleset.md +++ b/docs/resources/ruleset.md @@ -319,6 +319,10 @@ resource "cloudflare_ruleset" "cache_settings_example" { } } } + cache_reserve = { + eligible = true + minimum_file_size = 100000 + } origin_error_page_passthru = false } expression = "(http.host eq \"example.host.com\")" @@ -495,7 +499,7 @@ Optional: - `browser_ttl` (Block List) List of browser TTL parameters to apply to the request. (see [below for nested schema](#nestedblock--rules--action_parameters--browser_ttl)) - `cache` (Boolean) Whether to cache if expression matches. - `cache_key` (Block List) List of cache key parameters to apply to the request. (see [below for nested schema](#nestedblock--rules--action_parameters--cache_key)) -- `cache_reserve` (Block List) List of cache reserve parameters to apply to the request (see [below for nested schema](#nestedblock--rules--action_parameters--cache_reserve)) +- `cache_reserve` (Block List) List of cache reserve parameters to apply to the request. (see [below for nested schema](#nestedblock--rules--action_parameters--cache_reserve)) - `content` (String) Content of the custom error response. - `content_type` (String) Content-Type of the custom error response. - `cookie_fields` (Set of String) List of cookie values to include as part of custom fields logging. @@ -582,18 +586,6 @@ Optional: - `custom_key` (Block List) Custom key parameters for the request. (see [below for nested schema](#nestedblock--rules--action_parameters--cache_key--custom_key)) - `ignore_query_strings_order` (Boolean) Ignore query strings order. - -### Nested Schema for `rules.action_parameters.cache_reserve` - -Required: - -- `eligible` (Boolean) Determines whether Cloudflare will write the eligible resource to cache reserve. - -Optional: - -- `minimum_file_size` (Number) The minimum file size, in bytes, eligible for storage in cache reserve. If omitted and "eligible" is true, Cloudflare will use 0 bytes by default. - - ### Nested Schema for `rules.action_parameters.cache_key.custom_key` @@ -654,6 +646,18 @@ Optional: + +### Nested Schema for `rules.action_parameters.cache_reserve` + +Required: + +- `eligible` (Boolean) Determines whether Cloudflare will write the eligible resource to cache reserve. + +Optional: + +- `minimum_file_size` (Number) The minimum file size, in bytes, eligible for storage in cache reserve. If omitted and "eligible" is true, Cloudflare will use 0 bytes by default. + + ### Nested Schema for `rules.action_parameters.edge_ttl` diff --git a/docs/resources/worker_script.md b/docs/resources/worker_script.md index aca11b84fd..726855d911 100644 --- a/docs/resources/worker_script.md +++ b/docs/resources/worker_script.md @@ -76,6 +76,7 @@ resource "cloudflare_worker_script" "my_script" { - `compatibility_flags` (Set of String) Compatibility flags used for Worker Scripts. - `d1_database_binding` (Block Set) (see [below for nested schema](#nestedblock--d1_database_binding)) - `dispatch_namespace` (String) Name of the Workers for Platforms dispatch namespace. +- `hyperdrive_config_binding` (Block Set) (see [below for nested schema](#nestedblock--hyperdrive_config_binding)) - `kv_namespace_binding` (Block Set) (see [below for nested schema](#nestedblock--kv_namespace_binding)) - `logpush` (Boolean) Enabling allows Worker events to be sent to a defined Logpush destination. - `module` (Boolean) Whether to upload Worker as a module. @@ -110,6 +111,15 @@ Required: - `name` (String) The global variable for the binding in your Worker code. + +### Nested Schema for `hyperdrive_config_binding` + +Required: + +- `binding` (String) The global variable for the binding in your Worker code. +- `id` (String) The ID of the Hyperdrive config to use. + + ### Nested Schema for `kv_namespace_binding` diff --git a/docs/resources/workers_script.md b/docs/resources/workers_script.md index 236c7887f4..8b50bd56a2 100644 --- a/docs/resources/workers_script.md +++ b/docs/resources/workers_script.md @@ -76,6 +76,7 @@ resource "cloudflare_workers_script" "my_script" { - `compatibility_flags` (Set of String) Compatibility flags used for Worker Scripts. - `d1_database_binding` (Block Set) (see [below for nested schema](#nestedblock--d1_database_binding)) - `dispatch_namespace` (String) Name of the Workers for Platforms dispatch namespace. +- `hyperdrive_config_binding` (Block Set) (see [below for nested schema](#nestedblock--hyperdrive_config_binding)) - `kv_namespace_binding` (Block Set) (see [below for nested schema](#nestedblock--kv_namespace_binding)) - `logpush` (Boolean) Enabling allows Worker events to be sent to a defined Logpush destination. - `module` (Boolean) Whether to upload Worker as a module. @@ -110,6 +111,15 @@ Required: - `name` (String) The global variable for the binding in your Worker code. + +### Nested Schema for `hyperdrive_config_binding` + +Required: + +- `binding` (String) The global variable for the binding in your Worker code. +- `id` (String) The ID of the Hyperdrive config to use. + + ### Nested Schema for `kv_namespace_binding` diff --git a/internal/sdkv2provider/resource_cloudflare_workers_script.go b/internal/sdkv2provider/resource_cloudflare_workers_script.go index d511097a34..a57b1468a4 100644 --- a/internal/sdkv2provider/resource_cloudflare_workers_script.go +++ b/internal/sdkv2provider/resource_cloudflare_workers_script.go @@ -158,6 +158,15 @@ func parseWorkerBindings(d *schema.ResourceData, bindings ScriptBindings) { DatabaseID: data["database_id"].(string), } } + + for _, rawData := range d.Get("hyperdrive_config_binding").(*schema.Set).List() { + data := rawData.(map[string]interface{}) + + bindings[data["binding"].(string)] = cloudflare.WorkerHyperdriveBinding{ + Binding: data["binding"].(string), + ConfigID: data["id"].(string), + } + } } func getPlacement(d *schema.ResourceData) cloudflare.Placement { @@ -285,6 +294,7 @@ func resourceCloudflareWorkerScriptRead(ctx context.Context, d *schema.ResourceD analyticsEngineBindings := &schema.Set{F: schema.HashResource(analyticsEngineBindingResource)} queueBindings := &schema.Set{F: schema.HashResource(queueBindingResource)} d1DatabaseBindings := &schema.Set{F: schema.HashResource(d1BindingResource)} + hyperdriveConfigBindings := &schema.Set{F: schema.HashResource(hyperdriveConfigBindingsResource)} for name, binding := range bindings { switch v := binding.(type) { @@ -343,6 +353,11 @@ func resourceCloudflareWorkerScriptRead(ctx context.Context, d *schema.ResourceD "name": name, "database_id": v.DatabaseID, }) + case cloudflare.WorkerHyperdriveBinding: + hyperdriveConfigBindings.Add(map[string]interface{}{ + "binding": name, + "id": v.ConfigID, + }) } } @@ -388,6 +403,10 @@ func resourceCloudflareWorkerScriptRead(ctx context.Context, d *schema.ResourceD return diag.FromErr(fmt.Errorf("cannot set d1 database bindings (%s): %w", d.Id(), err)) } + if err := d.Set("hyperdrive_config_binding", hyperdriveConfigBindings); err != nil { + return diag.FromErr(fmt.Errorf("cannot set hyperdrive config 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 c413d6d3b3..9f678b8b24 100644 --- a/internal/sdkv2provider/resource_cloudflare_workers_script_test.go +++ b/internal/sdkv2provider/resource_cloudflare_workers_script_test.go @@ -17,12 +17,13 @@ import ( ) const ( - scriptContent1 = `addEventListener('fetch', event => {event.respondWith(new Response('test 1'))});` - scriptContent2 = `addEventListener('fetch', event => {event.respondWith(new Response('test 2'))});` - 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 = "ce8b95dc-b376-4ff8-9b9e-1801ed6d745d" + scriptContent1 = `addEventListener('fetch', event => {event.respondWith(new Response('test 1'))});` + scriptContent2 = `addEventListener('fetch', event => {event.respondWith(new Response('test 2'))});` + 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 = "ce8b95dc-b376-4ff8-9b9e-1801ed6d745d" + hyperdriveConfigID = "d08bc4a1c3c140aa95e4ceec535f832e" ) var ( @@ -248,7 +249,12 @@ resource "cloudflare_workers_script" "%[1]s" { queue = cloudflare_queue.%[1]s.name } -}`, rnd, scriptContent2, encodedWasm, accountID) + hyperdrive_config_binding { + binding = "MY_HYPERDRIVE" + id = "%[5]s" + } + +}`, rnd, scriptContent2, encodedWasm, accountID, hyperdriveConfigID) } func testAccCheckCloudflareWorkerScriptUploadModule(rnd, accountID, r2AccessKeyID, r2AccessKeySecret string) string { diff --git a/internal/sdkv2provider/schema_cloudflare_workers_script.go b/internal/sdkv2provider/schema_cloudflare_workers_script.go index 29873e87b1..842940160b 100644 --- a/internal/sdkv2provider/schema_cloudflare_workers_script.go +++ b/internal/sdkv2provider/schema_cloudflare_workers_script.go @@ -149,6 +149,21 @@ var d1BindingResource = &schema.Resource{ }, } +var hyperdriveConfigBindingsResource = &schema.Resource{ + Schema: map[string]*schema.Schema{ + "binding": { + Type: schema.TypeString, + Required: true, + Description: "The global variable for the binding in your Worker code.", + }, + "id": { + Type: schema.TypeString, + Required: true, + Description: "The ID of the Hyperdrive config to use.", + }, + }, +} + var placementResource = &schema.Resource{ Schema: map[string]*schema.Schema{ "mode": { @@ -264,5 +279,10 @@ func resourceCloudflareWorkerScriptSchema() map[string]*schema.Schema { Optional: true, Elem: d1BindingResource, }, + "hyperdrive_config_binding": { + Type: schema.TypeSet, + Optional: true, + Elem: hyperdriveConfigBindingsResource, + }, } }