Skip to content

Commit

Permalink
Merge pull request #3821 from OilyLime/hyperdrive-worker-binding
Browse files Browse the repository at this point in the history
[WIP] Add hyperdrive_config_binding to Cloudflare Workers
  • Loading branch information
jacobbednarz authored Sep 18, 2024
2 parents a0129b8 + e1d4166 commit 3a4df23
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 28 deletions.
3 changes: 3 additions & 0 deletions .changelog/3821.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 `hyperdrive` binding type
```
17 changes: 9 additions & 8 deletions docs/data-sources/rulesets.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

<a id="nestedobjatt--rulesets--rules--action_parameters--cache_reserve"></a>
### Nested Schema for `rulesets.rules.action_parameters.version`

Read-Only:

- `eligible` (Boolean)
- `minimum_file_size` (Number)

<a id="nestedobjatt--rulesets--rules--action_parameters--version--custom_key"></a>
### Nested Schema for `rulesets.rules.action_parameters.version.custom_key`

Expand Down Expand Up @@ -232,6 +224,15 @@ Read-Only:



<a id="nestedobjatt--rulesets--rules--action_parameters--cache_reserve"></a>
### Nested Schema for `rulesets.rules.action_parameters.version`

Read-Only:

- `eligible` (Boolean)
- `minimum_file_size` (Number)


<a id="nestedobjatt--rulesets--rules--action_parameters--edge_ttl"></a>
### Nested Schema for `rulesets.rules.action_parameters.version`

Expand Down
30 changes: 17 additions & 13 deletions docs/resources/ruleset.md
Original file line number Diff line number Diff line change
Expand Up @@ -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\")"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.

<a id="nestedblock--rules--action_parameters--cache_reserve"></a>
### 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.


<a id="nestedblock--rules--action_parameters--cache_key--custom_key"></a>
### Nested Schema for `rules.action_parameters.cache_key.custom_key`

Expand Down Expand Up @@ -654,6 +646,18 @@ Optional:



<a id="nestedblock--rules--action_parameters--cache_reserve"></a>
### 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.


<a id="nestedblock--rules--action_parameters--edge_ttl"></a>
### Nested Schema for `rules.action_parameters.edge_ttl`

Expand Down
10 changes: 10 additions & 0 deletions docs/resources/worker_script.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -110,6 +111,15 @@ Required:
- `name` (String) The global variable for the binding in your Worker code.


<a id="nestedblock--hyperdrive_config_binding"></a>
### 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.


<a id="nestedblock--kv_namespace_binding"></a>
### Nested Schema for `kv_namespace_binding`

Expand Down
10 changes: 10 additions & 0 deletions docs/resources/workers_script.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -110,6 +111,15 @@ Required:
- `name` (String) The global variable for the binding in your Worker code.


<a id="nestedblock--hyperdrive_config_binding"></a>
### 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.


<a id="nestedblock--kv_namespace_binding"></a>
### Nested Schema for `kv_namespace_binding`

Expand Down
19 changes: 19 additions & 0 deletions internal/sdkv2provider/resource_cloudflare_workers_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
})
}
}

Expand Down Expand Up @@ -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
Expand Down
20 changes: 13 additions & 7 deletions internal/sdkv2provider/resource_cloudflare_workers_script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions internal/sdkv2provider/schema_cloudflare_workers_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -264,5 +279,10 @@ func resourceCloudflareWorkerScriptSchema() map[string]*schema.Schema {
Optional: true,
Elem: d1BindingResource,
},
"hyperdrive_config_binding": {
Type: schema.TypeSet,
Optional: true,
Elem: hyperdriveConfigBindingsResource,
},
}
}

0 comments on commit 3a4df23

Please sign in to comment.