Skip to content

Commit

Permalink
Merge pull request cloudflare#964 from rgcostea/gcostea/r2-bucket-bin…
Browse files Browse the repository at this point in the history
…ding

Add R2 bucket binding
  • Loading branch information
jacobbednarz authored Jul 1, 2022
2 parents 939975e + ab0abe9 commit 44f0966
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
29 changes: 29 additions & 0 deletions workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ const (
WorkerPlainTextBindingType WorkerBindingType = "plain_text"
// WorkerServiceBindingType is the type for service bindings.
WorkerServiceBindingType WorkerBindingType = "service"
// WorkerR2BucketBindingType is the type for R2 bucket bindings.
WorkerR2BucketBindingType WorkerBindingType = "r2_bucket"
)

// WorkerBindingListItem a struct representing an individual binding in a list of bindings.
Expand Down Expand Up @@ -325,6 +327,28 @@ func (b WorkerServiceBinding) serialize(bindingName string) (workerBindingMeta,
return meta, nil, nil
}

// WorkerR2BucketBinding is a binding to an R2 bucket.
type WorkerR2BucketBinding struct {
BucketName string
}

// Type returns the type of the binding.
func (b WorkerR2BucketBinding) Type() WorkerBindingType {
return WorkerR2BucketBindingType
}

func (b WorkerR2BucketBinding) serialize(bindingName string) (workerBindingMeta, workerBindingBodyWriter, error) {
if b.BucketName == "" {
return nil, nil, errors.Errorf(`BucketName for binding "%s" cannot be empty`, bindingName)
}

return workerBindingMeta{
"name": bindingName,
"type": b.Type(),
"bucket_name": b.BucketName,
}, nil, nil
}

// Each binding that adds a part to the multipart form body will need
// a unique part name so we just generate a random 128bit hex string.
func getRandomPartName() string {
Expand Down Expand Up @@ -488,6 +512,11 @@ func (api *API) ListWorkerBindings(ctx context.Context, requestParams *WorkerReq
}
case WorkerSecretTextBindingType:
bindingListItem.Binding = WorkerSecretTextBinding{}
case WorkerR2BucketBindingType:
bucketName := jsonBinding["bucket_name"].(string)
bindingListItem.Binding = WorkerR2BucketBinding{
BucketName: bucketName,
}
default:
bindingListItem.Binding = WorkerInheritBinding{}
}
Expand Down
15 changes: 14 additions & 1 deletion workers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ const (
{
"name": "MY_NEW_BINDING",
"type": "some_imaginary_new_binding_type"
},
{
"name": "MY_BUCKET",
"type": "r2_bucket",
"bucket_name": "bucket"
}
],
"success": true,
Expand Down Expand Up @@ -1029,7 +1034,7 @@ func TestWorkers_ListWorkerBindingsMultiScript(t *testing.T) {
assert.NoError(t, err)

assert.Equal(t, successResponse, res.Response)
assert.Equal(t, 6, len(res.BindingList))
assert.Equal(t, 7, len(res.BindingList))

assert.Equal(t, res.BindingList[0], WorkerBindingListItem{
Name: "MY_KV",
Expand Down Expand Up @@ -1075,6 +1080,14 @@ func TestWorkers_ListWorkerBindingsMultiScript(t *testing.T) {
Binding: WorkerInheritBinding{},
})
assert.Equal(t, WorkerInheritBindingType, res.BindingList[5].Binding.Type())

assert.Equal(t, res.BindingList[6], WorkerBindingListItem{
Name: "MY_BUCKET",
Binding: WorkerR2BucketBinding{
BucketName: "bucket",
},
})
assert.Equal(t, WorkerR2BucketBindingType, res.BindingList[6].Binding.Type())
}

func TestWorkers_UpdateWorkerRouteErrorsWhenMixingSingleAndMultiScriptProperties(t *testing.T) {
Expand Down

0 comments on commit 44f0966

Please sign in to comment.