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

add support for tags to CreateWorkerParams #1368

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/1368.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
workers: add support for tagging Worker scripts
```
31 changes: 30 additions & 1 deletion workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,33 @@ type CreateWorkerParams struct {
CompatibilityFlags []string

Placement *Placement

// Tags are used to better manage CRUD operations at scale.
// https://developers.cloudflare.com/cloudflare-for-platforms/workers-for-platforms/platform/tags/
Tags []string
}

func (p CreateWorkerParams) RequiresMultipart() bool {
switch {
case p.Module:
return true
case p.Logpush != nil:
return true
case p.Placement != nil:
return true
case len(p.Bindings) > 0:
return true
case p.CompatibilityDate != "":
return true
case len(p.CompatibilityFlags) > 0:
return true
case p.TailConsumers != nil:
return true
case len(p.Tags) > 0:
return true
}

return false
}

// WorkerScriptParams provides a worker script and the associated bindings.
Expand Down Expand Up @@ -269,7 +296,7 @@ func (api *API) UploadWorker(ctx context.Context, rc *ResourceContainer, params
err error
)

if params.Module || params.Logpush != nil || params.Placement != nil || len(params.Bindings) > 0 || params.CompatibilityDate != "" || len(params.CompatibilityFlags) > 0 || params.TailConsumers != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if params.RequiresMultipart() {
contentType, body, err = formatMultipartBody(params)
if err != nil {
return WorkerScriptResponse{}, err
Expand Down Expand Up @@ -315,13 +342,15 @@ func formatMultipartBody(params CreateWorkerParams) (string, []byte, error) {
CompatibilityDate string `json:"compatibility_date,omitempty"`
CompatibilityFlags []string `json:"compatibility_flags,omitempty"`
Placement *Placement `json:"placement,omitempty"`
Tags []string `json:"tags"`
}{
Bindings: make([]workerBindingMeta, 0, len(params.Bindings)),
Logpush: params.Logpush,
TailConsumers: params.TailConsumers,
CompatibilityDate: params.CompatibilityDate,
CompatibilityFlags: params.CompatibilityFlags,
Placement: params.Placement,
Tags: params.Tags,
}

if params.Module {
Expand Down
37 changes: 37 additions & 0 deletions workers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ type multipartUpload struct {
CompatibilityDate string
CompatibilityFlags []string
Placement *Placement
Tags []string
}

func parseMultipartUpload(r *http.Request) (multipartUpload, error) {
Expand All @@ -397,6 +398,7 @@ func parseMultipartUpload(r *http.Request) (multipartUpload, error) {
CompatibilityDate string `json:"compatibility_date,omitempty"`
CompatibilityFlags []string `json:"compatibility_flags,omitempty"`
Placement *Placement `json:"placement,omitempty"`
Tags []string `json:"tags"`
}
err = json.Unmarshal(mdBytes, &metadata)
if err != nil {
Expand Down Expand Up @@ -429,6 +431,7 @@ func parseMultipartUpload(r *http.Request) (multipartUpload, error) {
CompatibilityDate: metadata.CompatibilityDate,
CompatibilityFlags: metadata.CompatibilityFlags,
Placement: metadata.Placement,
Tags: metadata.Tags,
}, nil
}

Expand Down Expand Up @@ -1235,6 +1238,40 @@ func TestUploadWorker_ToDispatchNamespace(t *testing.T) {
assert.NoError(t, err)
}

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

namespaceName := "n1"
tags := []string{"hello=there", "another-tag"}

handler := 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)
require.NoError(t, err)

assert.Equal(t, workerScript, mpUpload.Script)

assert.EqualValues(t, tags, mpUpload.Tags)

w.Header().Set("content-type", "application/json")
fmt.Fprint(w, workersScriptResponse(t))
}
mux.HandleFunc(
fmt.Sprintf("/accounts/"+testAccountID+"/workers/dispatch/namespaces/%s/scripts/bar", namespaceName),
handler,
)

_, err := client.UploadWorker(context.Background(), AccountIdentifier(testAccountID), CreateWorkerParams{
ScriptName: "bar",
Script: workerScript,
DispatchNamespaceName: &namespaceName,
Tags: tags,
})
assert.NoError(t, err)
}

func TestUploadWorker_UnsafeBinding(t *testing.T) {
setup()
defer teardown()
Expand Down