Skip to content

Commit

Permalink
Merge pull request #3825 from danielrs/drivas/script-upload-placement…
Browse files Browse the repository at this point in the history
…-field-changes
  • Loading branch information
jacobbednarz authored Jan 15, 2025
2 parents 42e433d + eaf799a commit ed69723
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
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

0 comments on commit ed69723

Please sign in to comment.