Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script upload response placement field changes #3825

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changelog/3825.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:note
workers: The `placement_mode` attribute in script upload responses has been deprecated. The new attribute `placement.mode` should be used instead.
```

```release-note:enhancement
workers: Add new `placement` attribute object in script upload responses. It contains the `mode` and `status` attributes.
```
7 changes: 2 additions & 5 deletions convert_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package cloudflare

import (
"reflect"
"time"
)

Expand All @@ -42,10 +41,8 @@ import (
// var _ *uint16 = AnyPtr(uint16(16)).(*uint16)
// var _ *uint32 = AnyPtr(uint32(32)).(*uint32)
// var _ *uint64 = AnyPtr(uint64(64)).(*uint64)
func AnyPtr(v interface{}) interface{} {
r := reflect.New(reflect.TypeOf(v))
reflect.ValueOf(r.Interface()).Elem().Set(reflect.ValueOf(v))
return r.Interface()
func AnyPtr[T any](v T) *T {
return &v
}

// BytePtr is a helper routine that allocates a new byte value to store v and
Expand Down
25 changes: 23 additions & 2 deletions workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ type WorkerMetaData struct {
TailConsumers *[]WorkersTailConsumer `json:"tail_consumers,omitempty"`
LastDeployedFrom *string `json:"last_deployed_from,omitempty"`
DeploymentId *string `json:"deployment_id,omitempty"`
PlacementMode *PlacementMode `json:"placement_mode,omitempty"`
PipelineHash *string `json:"pipeline_hash,omitempty"`
PlacementFields
PipelineHash *string `json:"pipeline_hash,omitempty"`
}

// WorkerListResponse wrapper struct for API response to worker script list API call.
Expand Down Expand Up @@ -228,8 +228,29 @@ const (
PlacementModeSmart PlacementMode = "smart"
)

type PlacementStatus string

// Placement contains all the worker placement information.
type Placement struct {

// Mode is the placement mode for the worker (e.g. "smart").
Mode PlacementMode `json:"mode"`

// Status is the status of the placement (readonly).
Status PlacementStatus `json:"status,omitempty"`
}

// PlacementFields contains all the worker placement fields (deprecated and nested).
// This struct is meant to be embedded, it exists for locality of deprecated and regular fields.
type PlacementFields struct {

// PlacementMode.
//
// Deprecated: Use Placement.Mode instead.
PlacementMode *PlacementMode `json:"placement_mode,omitempty"`

// Placement.
Placement *Placement `json:"placement,omitempty"`
}

// DeleteWorker deletes a single Worker.
Expand Down
19 changes: 15 additions & 4 deletions workers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ type (
CompatibilityDate *string `json:"compatibility_date,omitempty"`
Logpush *bool `json:"logpush,omitempty"`
TailConsumers *[]WorkersTailConsumer `json:"tail_consumers,omitempty"`
PlacementMode *string `json:"placement_mode,omitempty"`
PlacementFields
}
workersTestResponseOpt func(r *WorkersTestScriptResponse)
)
Expand Down Expand Up @@ -307,8 +307,16 @@ func withWorkerLogpush(logpush *bool) workersTestResponseOpt {
}

//nolint:unused
func withWorkerPlacementMode(mode *string) workersTestResponseOpt {
return func(r *WorkersTestScriptResponse) { r.PlacementMode = mode }
func withWorkerPlacementMode(mode *PlacementMode) workersTestResponseOpt {
return func(r *WorkersTestScriptResponse) {
if mode == nil {
r.PlacementMode = nil
r.Placement = nil
} else {
r.PlacementMode = mode
r.Placement = &Placement{Mode: *mode}
}
}
}

//nolint:unused
Expand Down Expand Up @@ -1268,7 +1276,7 @@ func TestUploadWorker_WithSmartPlacementEnabled(t *testing.T) {
defer teardown()

placementMode := PlacementModeSmart
response := workersScriptResponse(t, withWorkerScript(expectedWorkersModuleWorkerScript), withWorkerPlacementMode(StringPtr("smart")))
response := workersScriptResponse(t, withWorkerScript(expectedWorkersModuleWorkerScript), withWorkerPlacementMode(AnyPtr(PlacementModeSmart)))

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPut, r.Method, "Expected method 'PUT', got %s", r.Method)
Expand All @@ -1293,6 +1301,8 @@ func TestUploadWorker_WithSmartPlacementEnabled(t *testing.T) {
})
assert.NoError(t, err)
assert.Equal(t, placementMode, *worker.PlacementMode)
assert.NotNil(t, worker.Placement)
assert.Equal(t, placementMode, worker.Placement.Mode)
})

t.Run("Test disabling placement", func(t *testing.T) {
Expand All @@ -1308,6 +1318,7 @@ func TestUploadWorker_WithSmartPlacementEnabled(t *testing.T) {
})
assert.NoError(t, err)
assert.Nil(t, worker.PlacementMode)
assert.Nil(t, worker.Placement)
})
}

Expand Down
Loading