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

feat(workers): allow enabling logpush on worker uploads #1160

Merged
merged 2 commits into from
Jan 2, 2023
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
3 changes: 3 additions & 0 deletions .changelog/1160.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
workers: Add support for workers logpush enablement on script upload
```
8 changes: 7 additions & 1 deletion workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ type CreateWorkerParams struct {
// ES Module syntax script.
Module bool

// Logpush opts the worker into Workers Logpush logging. A nil value leaves the current setting unchanged.
// https://developers.cloudflare.com/workers/platform/logpush/
Logpush *bool

// Bindings should be a map where the keys are the binding name, and the
// values are the binding content
Bindings map[string]WorkerBinding
Expand Down Expand Up @@ -220,7 +224,7 @@ func (api *API) UploadWorker(ctx context.Context, rc *ResourceContainer, params
err error
)

if params.Module || len(params.Bindings) > 0 {
if params.Module || params.Logpush != nil || len(params.Bindings) > 0 {
jacobbednarz marked this conversation as resolved.
Show resolved Hide resolved
contentType, body, err = formatMultipartBody(params)
if err != nil {
return WorkerScriptResponse{}, err
Expand Down Expand Up @@ -257,8 +261,10 @@ func formatMultipartBody(params CreateWorkerParams) (string, []byte, error) {
BodyPart string `json:"body_part,omitempty"`
MainModule string `json:"main_module,omitempty"`
Bindings []workerBindingMeta `json:"bindings"`
Logpush *bool `json:"logpush,omitempty"`
}{
Bindings: make([]workerBindingMeta, 0, len(params.Bindings)),
Logpush: params.Logpush,
}

if params.Module {
Expand Down
36 changes: 36 additions & 0 deletions workers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ func getFileDetails(r *http.Request, key string) (*multipart.FileHeader, error)
type multipartUpload struct {
Script string
BindingMeta map[string]workerBindingMeta
Logpush *bool
}

func parseMultipartUpload(r *http.Request) (multipartUpload, error) {
Expand All @@ -290,6 +291,7 @@ func parseMultipartUpload(r *http.Request) (multipartUpload, error) {
BodyPart string `json:"body_part,omitempty"`
MainModule string `json:"main_module,omitempty"`
Bindings []workerBindingMeta `json:"bindings"`
Logpush *bool `json:"logpush,omitempty"`
}
err = json.Unmarshal(mdBytes, &metadata)
if err != nil {
Expand Down Expand Up @@ -318,6 +320,7 @@ func parseMultipartUpload(r *http.Request) (multipartUpload, error) {
return multipartUpload{
Script: string(script),
BindingMeta: bindingMeta,
Logpush: metadata.Logpush,
}, nil
}

Expand Down Expand Up @@ -823,3 +826,36 @@ func TestUploadWorker_WithServiceBinding(t *testing.T) {
})
assert.NoError(t, err)
}

func TestUploadWorker_WithLogpush(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/accounts/"+testAccountID+"/workers/scripts/foo", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPut, r.Method, "Expected method 'PUT', got %s", r.Method)
mpUpload, err := parseMultipartUpload(r)
assert.NoError(t, err)

expected := true
assert.Equal(t, &expected, mpUpload.Logpush)

w.Header().Set("content-type", "application/json")
fmt.Fprint(w, uploadWorkerResponseData)
})
res, err := client.UploadWorker(context.Background(), AccountIdentifier(testAccountID), CreateWorkerParams{ScriptName: "foo", Script: workerScript, Logpush: BoolPtr(true)})
formattedTime, _ := time.Parse(time.RFC3339Nano, "2018-06-09T15:17:01.989141Z")
want := WorkerScriptResponse{
successResponse,
false,
WorkerScript{
Script: workerScript,
WorkerMetaData: WorkerMetaData{
ETAG: "279cf40d86d70b82f6cd3ba90a646b3ad995912da446836d7371c21c6a43977a",
Size: 191,
ModifiedOn: formattedTime,
},
}}
if assert.NoError(t, err) {
assert.Equal(t, want, res)
}
}