Repository: cloudflare/cloudflare-go
Pull request #2812 (2024-08-01 22:03:05)

Add support for hyperdrive workers bindings
## Description

Adds support for Hyperdrive bindings to Workers.

## Has your change been tested?

Changes covered in tests.

## Types of changes

What sort of change does your code introduce/modify?

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

## Checklist:

- [x] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [x] I have added tests to cover my changes.
- [x] All new and existing tests passed.
- [x] This change is using publicly documented in [cloudflare/api-schemas](https://github.com/cloudflare/api-schemas) 
      and relies on stable APIs.

[1]: https://help.github.com/articles/closing-issues-using-keywords/



Patch:
diff --git a/.changelog/2812.txt b/.changelog/2812.txt
new file mode 100644
index 000000000..f9d178c0e
--- /dev/null
+++ b/.changelog/2812.txt
@@ -0,0 +1,3 @@
+```release-notes:enhancement
+worker_bindings: add support for `hyperdrive` bindings
+```
diff --git a/workers_bindings.go b/workers_bindings.go
index 0516c5b3f..9a49cf865 100644
--- a/workers_bindings.go
+++ b/workers_bindings.go
@@ -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 {
@@ -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{}
 
@@ -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{}
 		}


Test patch:
diff --git a/workers_bindings_test.go b/workers_bindings_test.go
index bb7f141fa..f747eabf1 100644
--- a/workers_bindings_test.go
+++ b/workers_bindings_test.go
@@ -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",
@@ -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) {
@@ -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",
@@ -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() {
diff --git a/workers_test.go b/workers_test.go
index 6175440a1..ef0e09987 100644
--- a/workers_test.go
+++ b/workers_test.go
@@ -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,