Skip to content
This repository has been archived by the owner on Jun 21, 2024. It is now read-only.

Commit

Permalink
Add D1 support for workers
Browse files Browse the repository at this point in the history
  • Loading branch information
drewschrauf committed Feb 2, 2023
1 parent 037addb commit 38bf5d7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,5 @@ require (
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/cloudflare/cloudflare-go v0.60.0 => github.com/qwilr/cloudflare-go v0.0.0-20230202044323-b60d455c7943
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkY
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cloudflare/cloudflare-go v0.60.0 h1:Lwt9Nok5LHAkFY4g5jOjbDLAdKpb5qDhQpvU0ie1eNc=
github.com/cloudflare/cloudflare-go v0.60.0/go.mod h1:qmuJW7P3KCRXYSTHkV7uS9qTeacr8gkqE60ekUwapqQ=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4/go.mod h1:6pvJx4me5XPnfI9Z40ddWsdw2W/uZgQLFXToKeRcDiI=
Expand Down Expand Up @@ -199,6 +197,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/qwilr/cloudflare-go v0.0.0-20230202044323-b60d455c7943 h1:ByZupghVrVnHmW5xMe90DCvoTa9SaIiKYrmTXisG9Ys=
github.com/qwilr/cloudflare-go v0.0.0-20230202044323-b60d455c7943/go.mod h1:qmuJW7P3KCRXYSTHkV7uS9qTeacr8gkqE60ekUwapqQ=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg=
github.com/sebdah/goldie v1.0.0/go.mod h1:jXP4hmWywNEwZzhMuv2ccnqTSFpuq8iyQhtQdkkZBH4=
Expand Down
17 changes: 17 additions & 0 deletions internal/sdkv2provider/resource_cloudflare_workers_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ func parseWorkerBindings(d *schema.ResourceData, bindings ScriptBindings) {
}
}

for _, rawData := range d.Get("d1_database_binding").(*schema.Set).List() {
data := rawData.(map[string]interface{})
bindings[data["name"].(string)] = cloudflare.WorkerD1DatabaseBinding{
ID: data["id"].(string),
}
}

for _, rawData := range d.Get("analytics_engine_binding").(*schema.Set).List() {
data := rawData.(map[string]interface{})
bindings[data["name"].(string)] = cloudflare.WorkerAnalyticsEngineBinding{
Expand Down Expand Up @@ -209,6 +216,7 @@ func resourceCloudflareWorkerScriptRead(ctx context.Context, d *schema.ResourceD
webAssemblyBindings := &schema.Set{F: schema.HashResource(webAssemblyBindingResource)}
serviceBindings := &schema.Set{F: schema.HashResource(serviceBindingResource)}
r2BucketBindings := &schema.Set{F: schema.HashResource(r2BucketBindingResource)}
d1DatabaseBindings := &schema.Set{F: schema.HashResource(d1DatabaseBindingResource)}
analyticsEngineBindings := &schema.Set{F: schema.HashResource(analyticsEngineBindingResource)}

for name, binding := range bindings {
Expand Down Expand Up @@ -253,6 +261,11 @@ func resourceCloudflareWorkerScriptRead(ctx context.Context, d *schema.ResourceD
"name": name,
"bucket_name": v.BucketName,
})
case cloudflare.WorkerD1DatabaseBinding:
d1DatabaseBindings.Add(map[string]interface{}{
"name": name,
"id": v.ID,
})
case cloudflare.WorkerAnalyticsEngineBinding:
analyticsEngineBindings.Add(map[string]interface{}{
"name": name,
Expand Down Expand Up @@ -289,6 +302,10 @@ func resourceCloudflareWorkerScriptRead(ctx context.Context, d *schema.ResourceD
return diag.FromErr(fmt.Errorf("cannot set r2 bucket 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))
}

if err := d.Set("analytics_engine_binding", analyticsEngineBindings); err != nil {
return diag.FromErr(fmt.Errorf("cannot set analytics engine bindings (%s): %w", d.Id(), err))
}
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 @@ -101,6 +101,21 @@ var r2BucketBindingResource = &schema.Resource{
},
}

var d1DatabaseBindingResource = &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "The global variable for the binding in your Worker code.",
},
"id": {
Type: schema.TypeString,
Required: true,
Description: "ID of the Database to bind to.",
},
},
}

var analyticsEngineBindingResource = &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Expand Down Expand Up @@ -169,6 +184,11 @@ func resourceCloudflareWorkerScriptSchema() map[string]*schema.Schema {
Optional: true,
Elem: r2BucketBindingResource,
},
"d1_database_binding": {
Type: schema.TypeSet,
Optional: true,
Elem: d1DatabaseBindingResource,
},
"analytics_engine_binding": {
Type: schema.TypeSet,
Optional: true,
Expand Down

0 comments on commit 38bf5d7

Please sign in to comment.