Skip to content

Commit

Permalink
Merge pull request #2812 from OilyLime/hyperdrive-workers-bindings
Browse files Browse the repository at this point in the history
Add support for hyperdrive workers bindings
  • Loading branch information
jacobbednarz committed Aug 2, 2024
2 parents 217ab49 + bd8437d commit 80d52bc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/2812.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-notes:enhancement
worker_bindings: add support for `hyperdrive` bindings
```
29 changes: 29 additions & 0 deletions workers_bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const (
DispatchNamespaceBindingType WorkerBindingType = "dispatch_namespace"
// WorkerD1DataseBindingType is for D1 databases.
WorkerD1DataseBindingType WorkerBindingType = "d1"
// WorkerHyperdriveBindingType is for Hyperdrive config bindings.
WorkerHyperdriveBindingType WorkerBindingType = "hyperdrive"
)

type ListWorkerBindingsParams struct {
Expand Down Expand Up @@ -433,6 +435,28 @@ func (b WorkerD1DatabaseBinding) serialize(bindingName string) (workerBindingMet
}, nil, nil
}

// WorkerHyperdriveBinding is a binding to a Hyperdrive config.
type WorkerHyperdriveBinding struct {
ConfigID string
}

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

func (b WorkerHyperdriveBinding) serialize(bindingName string) (workerBindingMeta, workerBindingBodyWriter, error) {
if b.ConfigID == "" {
return nil, nil, fmt.Errorf(`config ID for binding "%s" cannot be empty`, bindingName)
}

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

// UnsafeBinding is for experimental or deprecated bindings, and allows specifying any binding type or property.
type UnsafeBinding map[string]interface{}

Expand Down Expand Up @@ -562,6 +586,11 @@ func (api *API) ListWorkerBindings(ctx context.Context, rc *ResourceContainer, p
bindingListItem.Binding = WorkerD1DatabaseBinding{
DatabaseID: database_id,
}
case WorkerHyperdriveBindingType:
id := jsonBinding["id"].(string)
bindingListItem.Binding = WorkerHyperdriveBinding{
ConfigID: id,
}
default:
bindingListItem.Binding = WorkerInheritBinding{}
}
Expand Down
20 changes: 18 additions & 2 deletions workers_bindings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestListWorkerBindings(t *testing.T) {
assert.NoError(t, err)

assert.Equal(t, successResponse, res.Response)
assert.Equal(t, 9, len(res.BindingList))
assert.Equal(t, 10, len(res.BindingList))

assert.Equal(t, res.BindingList[0], WorkerBindingListItem{
Name: "MY_KV",
Expand Down Expand Up @@ -106,6 +106,14 @@ func TestListWorkerBindings(t *testing.T) {
},
})
assert.Equal(t, WorkerD1DataseBindingType, res.BindingList[8].Binding.Type())

assert.Equal(t, res.BindingList[9], WorkerBindingListItem{
Name: "MY_HYPERDRIVE",
Binding: WorkerHyperdriveBinding{
ConfigID: "aaf4609248cc493cbc8d3e446e38fdfa",
},
})
assert.Equal(t, WorkerHyperdriveBindingType, res.BindingList[9].Binding.Type())
}

func TestListWorkerBindings_Wfp(t *testing.T) {
Expand All @@ -125,7 +133,7 @@ func TestListWorkerBindings_Wfp(t *testing.T) {
assert.NoError(t, err)

assert.Equal(t, successResponse, res.Response)
assert.Equal(t, 9, len(res.BindingList))
assert.Equal(t, 10, len(res.BindingList))

assert.Equal(t, res.BindingList[0], WorkerBindingListItem{
Name: "MY_KV",
Expand Down Expand Up @@ -191,6 +199,14 @@ func TestListWorkerBindings_Wfp(t *testing.T) {
},
})
assert.Equal(t, WorkerD1DataseBindingType, res.BindingList[8].Binding.Type())

assert.Equal(t, res.BindingList[9], WorkerBindingListItem{
Name: "MY_HYPERDRIVE",
Binding: WorkerHyperdriveBinding{
ConfigID: "aaf4609248cc493cbc8d3e446e38fdfa",
},
})
assert.Equal(t, WorkerHyperdriveBindingType, res.BindingList[9].Binding.Type())
}

func ExampleUnsafeBinding() {
Expand Down
5 changes: 5 additions & 0 deletions workers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ const (
"name": "MY_DATABASE",
"type": "d1",
"database_id": "cef5331f-e5c7-4c8a-a415-7908ae45f92a"
},
{
"name": "MY_HYPERDRIVE",
"type": "hyperdrive",
"id": "aaf4609248cc493cbc8d3e446e38fdfa"
}
],
"success": true,
Expand Down

0 comments on commit 80d52bc

Please sign in to comment.