Skip to content

Commit

Permalink
Add support for using single-script worker API with an organization
Browse files Browse the repository at this point in the history
ENT accounts have access to the multi-script API but if they want to use the single-script API, we should still support that.
  • Loading branch information
jRiest committed Aug 31, 2018
1 parent 148791a commit bc8641a
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 13 deletions.
38 changes: 26 additions & 12 deletions workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,18 +209,20 @@ func (api *API) uploadWorkerWithName(scriptName string, data string) (WorkerScri
return r, nil
}

func getRoutesPathComponent(api *API) string {
if api.OrganizationID == "" {
return "filters"
}
return "routes"
}

// CreateWorkerRoute creates worker route for a zone
//
// API reference: https://api.cloudflare.com/#worker-filters-create-filter
func (api *API) CreateWorkerRoute(zoneID string, route WorkerRoute) (WorkerRouteResponse, error) {
pathComponent := getRoutesPathComponent(api)
// Check whether a script name is defined in order to determine whether
// to use the single-script or multi-script endpoint.
pathComponent := "filters"
if route.Script != "" {
if api.OrganizationID == "" {
return WorkerRouteResponse{}, errors.New("organization ID required for enterprise only request")
}
pathComponent = "routes"
}

uri := "/zones/" + zoneID + "/workers/" + pathComponent
res, err := api.makeRequest("POST", uri, route)
if err != nil {
Expand All @@ -238,8 +240,9 @@ func (api *API) CreateWorkerRoute(zoneID string, route WorkerRoute) (WorkerRoute
//
// API reference: https://api.cloudflare.com/#worker-filters-delete-filter
func (api *API) DeleteWorkerRoute(zoneID string, routeID string) (WorkerRouteResponse, error) {
pathComponent := getRoutesPathComponent(api)
uri := "/zones/" + zoneID + "/workers/" + pathComponent + "/" + routeID
// For deleting a route, it doesn't matter whether we use the
// single-script or multi-script endpoint
uri := "/zones/" + zoneID + "/workers/filters/" + routeID
res, err := api.makeRequest("DELETE", uri, nil)
if err != nil {
return WorkerRouteResponse{}, errors.Wrap(err, errMakeRequestError)
Expand All @@ -256,7 +259,10 @@ func (api *API) DeleteWorkerRoute(zoneID string, routeID string) (WorkerRouteRes
//
// API reference: https://api.cloudflare.com/#worker-filters-list-filters
func (api *API) ListWorkerRoutes(zoneID string) (WorkerRoutesResponse, error) {
pathComponent := getRoutesPathComponent(api)
pathComponent := "filters"
if api.OrganizationID != "" {
pathComponent = "routes"
}
uri := "/zones/" + zoneID + "/workers/" + pathComponent
res, err := api.makeRequest("GET", uri, nil)
if err != nil {
Expand All @@ -283,7 +289,15 @@ func (api *API) ListWorkerRoutes(zoneID string) (WorkerRoutesResponse, error) {
//
// API reference: https://api.cloudflare.com/#worker-filters-update-filter
func (api *API) UpdateWorkerRoute(zoneID string, routeID string, route WorkerRoute) (WorkerRouteResponse, error) {
pathComponent := getRoutesPathComponent(api)
// Check whether a script name is defined in order to determine whether
// to use the single-script or multi-script endpoint.
pathComponent := "filters"
if route.Script != "" {
if api.OrganizationID == "" {
return WorkerRouteResponse{}, errors.New("organization ID required for enterprise only request")
}
pathComponent = "routes"
}
uri := "/zones/" + zoneID + "/workers/" + pathComponent + "/" + routeID
res, err := api.makeRequest("PUT", uri, route)
if err != nil {
Expand Down
71 changes: 70 additions & 1 deletion workers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,34 @@ func TestWorkers_UploadWorkerWithName(t *testing.T) {
}
}

func TestWorkers_UploadWorkerSingleScriptWithOrg(t *testing.T) {
setup(UsingOrganization("foo"))
defer teardown()

mux.HandleFunc("/zones/foo/workers/script", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "PUT", r.Method, "Expected method 'PUT', got %s", r.Method)
contentTypeHeader := r.Header.Get("content-type")
assert.Equal(t, "application/javascript", contentTypeHeader, "Expected content-type request header to be 'application/javascript', got %s", contentTypeHeader)
w.Header().Set("content-type", "application/javascript")
fmt.Fprintf(w, uploadWorkerResponseData)
})
res, err := client.UploadWorker(&WorkerRequestParams{ZoneID: "foo"}, workerScript)
formattedTime, _ := time.Parse(time.RFC3339Nano, "2018-06-09T15:17:01.989141Z")
want := WorkerScriptResponse{
successResponse,
WorkerScript{
Script: workerScript,
WorkerMetaData: WorkerMetaData{
ETAG: "279cf40d86d70b82f6cd3ba90a646b3ad995912da446836d7371c21c6a43977a",
Size: 191,
ModifiedOn: formattedTime,
},
}}
if assert.NoError(t, err) {
assert.Equal(t, want, res)
}
}

func TestWorkers_UploadWorkerWithNameErrorsWithoutOrgId(t *testing.T) {
setup()
defer teardown()
Expand Down Expand Up @@ -336,6 +364,24 @@ func TestWorkers_CreateWorkerRouteEnt(t *testing.T) {
w.Header().Set("content-type", "application-json")
fmt.Fprintf(w, createWorkerRouteResponse)
})
route := WorkerRoute{Pattern: "app1.example.com/*", Script: "test_script"}
res, err := client.CreateWorkerRoute("foo", route)
want := WorkerRouteResponse{successResponse, WorkerRoute{ID: "e7a57d8746e74ae49c25994dadb421b1"}}
if assert.NoError(t, err) {
assert.Equal(t, want, res)
}

}

func TestWorkers_CreateWorkerRouteSingleScriptWithOrg(t *testing.T) {
setup(UsingOrganization("foo"))
defer teardown()

mux.HandleFunc("/zones/foo/workers/filters", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method, "Expected method 'POST', got %s", r.Method)
w.Header().Set("content-type", "application-json")
fmt.Fprintf(w, createWorkerRouteResponse)
})
route := WorkerRoute{Pattern: "app1.example.com/*", Enabled: true}
res, err := client.CreateWorkerRoute("foo", route)
want := WorkerRouteResponse{successResponse, WorkerRoute{ID: "e7a57d8746e74ae49c25994dadb421b1"}}
Expand Down Expand Up @@ -369,7 +415,7 @@ func TestWorkers_DeleteWorkerRouteEnt(t *testing.T) {
setup(UsingOrganization("foo"))
defer teardown()

mux.HandleFunc("/zones/foo/workers/routes/e7a57d8746e74ae49c25994dadb421b1", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/zones/foo/workers/filters/e7a57d8746e74ae49c25994dadb421b1", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "DELETE", r.Method, "Expected method 'DELETE', got %s", r.Method)
w.Header().Set("content-type", "application-json")
fmt.Fprintf(w, deleteWorkerRouteResponseData)
Expand Down Expand Up @@ -475,3 +521,26 @@ func TestWorkers_UpdateWorkerRouteEnt(t *testing.T) {
}

}

func TestWorkers_UpdateWorkerRouteSingleScriptWithOrg(t *testing.T) {
setup(UsingOrganization("foo"))
defer teardown()

mux.HandleFunc("/zones/foo/workers/filters/e7a57d8746e74ae49c25994dadb421b1", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "PUT", r.Method, "Expected method 'PUT', got %s", r.Method)
w.Header().Set("content-type", "application-json")
fmt.Fprintf(w, updateWorkerRouteEntResponse)
})
route := WorkerRoute{Pattern: "app3.example.com/*", Enabled: true}
res, err := client.UpdateWorkerRoute("foo", "e7a57d8746e74ae49c25994dadb421b1", route)
want := WorkerRouteResponse{successResponse,
WorkerRoute{
ID: "e7a57d8746e74ae49c25994dadb421b1",
Pattern: "app3.example.com/*",
Script: "test_script_1",
}}
if assert.NoError(t, err) {
assert.Equal(t, want, res)
}

}

0 comments on commit bc8641a

Please sign in to comment.