Skip to content

Commit

Permalink
Add hyperdrive_config_binding to Cloudflare Workers
Browse files Browse the repository at this point in the history
  • Loading branch information
mtlemilio committed Aug 28, 2024
1 parent 0e5e47d commit e1c900c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 10 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21

require (
github.com/agext/levenshtein v1.2.3 // indirect
github.com/cloudflare/cloudflare-go v0.101.0
github.com/cloudflare/cloudflare-go v0.103.0
github.com/fatih/color v1.16.0 // indirect
github.com/google/uuid v1.6.0
github.com/hashicorp/errwrap v1.1.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZ
github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8=
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
github.com/cloudflare/cloudflare-go v0.101.0 h1:SXWNSEDkbdY84iFIZGyTdWQwDfd98ljv0/4UubpleBQ=
github.com/cloudflare/cloudflare-go v0.101.0/go.mod h1:xXQHnoXKR48JlWbFS42i2al3nVqimVhcYvKnIdXLw9g=
github.com/cloudflare/cloudflare-go v0.103.0 h1:XXKzgXeUbAo7UTtM4T5wuD2bJPBtNZv7TlZAEy5QI4k=
github.com/cloudflare/cloudflare-go v0.103.0/go.mod h1:0DrjT4g8wgYFYIxhlqR8xi8dNWfyHFGilUkU3+XV8h0=
github.com/cloudflare/cloudflare-go/v2 v2.4.0 h1:gys/26GoVDklgfq8NYV39WgvOEwzK/XAqYObmnI6iFg=
github.com/cloudflare/cloudflare-go/v2 v2.4.0/go.mod h1:AoIzb05z/rvdJLztPct4tSa+3IqXJJ6c+pbUFMOlTr8=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
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 = "TODO"
)

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 e1c900c

Please sign in to comment.