Skip to content

Commit

Permalink
Merge pull request #1825 from yonran/cloudflare_worker_script.r2_buck…
Browse files Browse the repository at this point in the history
…et_binding
  • Loading branch information
jacobbednarz authored Aug 22, 2022
2 parents f6e8e25 + 87f35f3 commit e30b99a
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/1825.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resources/worker_script: add support for r2_bucket_binding
```
10 changes: 10 additions & 0 deletions docs/resources/worker_script.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ resource "cloudflare_worker_script" "my_script" {
service = "MY_SERVICE"
environment = "production"
}
r2_bucket_binding {
name = "MY_BUCKET"
bucket_name = "MY_BUCKET_NAME"
}
}
```

Expand Down Expand Up @@ -81,6 +86,11 @@ The following arguments are supported:
- `service` - (Required) The name of the Worker to bind to.
- `environment` - (Optional) The name of the Worker environment to bind to.

**r2_bucket_binding** supports:

- `name` - (Required) The global variable for the binding in your Worker code.
- `bucket_name` - (Required) The name of the Bucket to bind to.

## Import

To import a script, use a script name, e.g. `script_name`
Expand Down
17 changes: 17 additions & 0 deletions internal/provider/resource_cloudflare_worker_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ func parseWorkerBindings(d *schema.ResourceData, bindings ScriptBindings) {
Environment: cloudflare.StringPtr(data["environment"].(string)),
}
}

for _, rawData := range d.Get("r2_bucket_binding").(*schema.Set).List() {
data := rawData.(map[string]interface{})
bindings[data["name"].(string)] = cloudflare.WorkerR2BucketBinding{
BucketName: data["bucket_name"].(string),
}
}
}

func resourceCloudflareWorkerScriptCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down Expand Up @@ -179,6 +186,7 @@ func resourceCloudflareWorkerScriptRead(ctx context.Context, d *schema.ResourceD
secretTextBindings := &schema.Set{F: schema.HashResource(secretTextBindingResource)}
webAssemblyBindings := &schema.Set{F: schema.HashResource(webAssemblyBindingResource)}
serviceBindings := &schema.Set{F: schema.HashResource(serviceBindingResource)}
r2BucketBindings := &schema.Set{F: schema.HashResource(r2BucketBindingResource)}

for name, binding := range bindings {
switch v := binding.(type) {
Expand Down Expand Up @@ -217,6 +225,11 @@ func resourceCloudflareWorkerScriptRead(ctx context.Context, d *schema.ResourceD
"service": v.Service,
"environment": cloudflare.String(v.Environment),
})
case cloudflare.WorkerR2BucketBinding:
r2BucketBindings.Add(map[string]interface{}{
"name": name,
"bucket_name": v.BucketName,
})
}
}

Expand Down Expand Up @@ -244,6 +257,10 @@ func resourceCloudflareWorkerScriptRead(ctx context.Context, d *schema.ResourceD
return diag.FromErr(fmt.Errorf("cannot set service bindings (%s): %w", d.Id(), err))
}

if err := d.Set("r2_bucket_binding", r2BucketBindings); err != nil {
return diag.FromErr(fmt.Errorf("cannot set r2 bucket bindings (%s): %w", d.Id(), err))
}

return nil
}

Expand Down
24 changes: 23 additions & 1 deletion internal/provider/resource_cloudflare_worker_script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestAccCloudflareWorkerScript_MultiScriptEnt(t *testing.T) {
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAccount(t)
testAccCheckCloudflareWorkerScriptCreateBucket(t, rnd)
},
ProviderFactories: providerFactories,
CheckDestroy: testAccCheckCloudflareWorkerScriptDestroy,
Expand All @@ -51,7 +52,7 @@ func TestAccCloudflareWorkerScript_MultiScriptEnt(t *testing.T) {
{
Config: testAccCheckCloudflareWorkerScriptConfigMultiScriptUpdateBinding(rnd),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudflareWorkerScriptExists(name, &script, []string{"MY_KV_NAMESPACE", "MY_PLAIN_TEXT", "MY_SECRET_TEXT", "MY_WASM", "MY_SERVICE_BINDING"}),
testAccCheckCloudflareWorkerScriptExists(name, &script, []string{"MY_KV_NAMESPACE", "MY_PLAIN_TEXT", "MY_SECRET_TEXT", "MY_WASM", "MY_SERVICE_BINDING", "MY_BUCKET"}),
resource.TestCheckResourceAttr(name, "name", rnd),
resource.TestCheckResourceAttr(name, "content", scriptContent2),
),
Expand All @@ -60,6 +61,22 @@ func TestAccCloudflareWorkerScript_MultiScriptEnt(t *testing.T) {
})
}

// Create a bucket before creating a worker script binding.
// When a cloudflare_r2_bucket resource is added, we can switch to that instead
func testAccCheckCloudflareWorkerScriptCreateBucket(t *testing.T, rnd string) {
client := testAccProvider.Meta().(*cloudflare.API)
err := client.CreateR2Bucket(context.Background(), cloudflare.AccountIdentifier(accountID), cloudflare.CreateR2BucketParameters{Name: rnd})
if err != nil {
t.Fatalf("unable to create test bucket named %s: %v", rnd, err)
}
t.Cleanup(func() {
err := client.DeleteR2Bucket(context.Background(), cloudflare.AccountIdentifier(accountID), rnd)
if err != nil {
t.Errorf("Failed to clean up bucket named %s: %v", rnd, err)
}
})
}

func testAccCheckCloudflareWorkerScriptConfigMultiScriptInitial(rnd string) string {
return fmt.Sprintf(`
resource "cloudflare_worker_script" "%[1]s" {
Expand Down Expand Up @@ -111,6 +128,11 @@ resource "cloudflare_worker_script" "%[1]s" {
module = "%[3]s"
}
r2_bucket_binding {
name = "MY_BUCKET"
bucket_name = "%[1]s"
}
service_binding {
name = "MY_SERVICE_BINDING"
service = cloudflare_worker_script.%[1]s-service.name
Expand Down
18 changes: 18 additions & 0 deletions internal/provider/schema_cloudflare_worker_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ var serviceBindingResource = &schema.Resource{
},
}

var r2BucketBindingResource = &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"bucket_name": {
Type: schema.TypeString,
Required: true,
},
},
}

func resourceCloudflareWorkerScriptSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"name": {
Expand Down Expand Up @@ -108,5 +121,10 @@ func resourceCloudflareWorkerScriptSchema() map[string]*schema.Schema {
Optional: true,
Elem: serviceBindingResource,
},
"r2_bucket_binding": {
Type: schema.TypeSet,
Optional: true,
Elem: r2BucketBindingResource,
},
}
}

0 comments on commit e30b99a

Please sign in to comment.