Skip to content

Commit

Permalink
Merge pull request #1368 from aaronlisman/alisman/script-upload-tags
Browse files Browse the repository at this point in the history
add support for tags to CreateWorkerParams
  • Loading branch information
jacobbednarz authored Aug 10, 2023
2 parents a0f7a6b + 4863561 commit 9e66410
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
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 {
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

0 comments on commit 9e66410

Please sign in to comment.