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: add bulk purge api #86

Merged
merged 2 commits into from
Oct 19, 2020
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
1 change: 1 addition & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ func newEcho(manager rpaas.RpaasManager) *echo.Echo {
e.GET("/resources/:instance/route", getRoutes)
e.POST("/resources/:instance/route", updateRoute)
e.POST("/resources/:instance/purge", cachePurge)
e.POST("/resources/:instance/purge/bulk", cachePurgeBulk)
e.Any("/resources/:instance/exec", exec)

return e
Expand Down
37 changes: 37 additions & 0 deletions api/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package api

import (
"encoding/json"
"fmt"
"net/http"

Expand Down Expand Up @@ -36,3 +37,39 @@ func cachePurge(c echo.Context) error {
}
return c.String(http.StatusOK, fmt.Sprintf("Object purged on %d servers", count))
}

func cachePurgeBulk(c echo.Context) error {
if c.Request().ContentLength == 0 {
return echo.NewHTTPError(http.StatusBadRequest, "Request body can't be empty")
}

name := c.Param("instance")
if len(name) == 0 {
return c.String(http.StatusBadRequest, "instance is required")
}
manager, err := getManager(c)
if err != nil {
return err
}

var argsList []rpaas.PurgeCacheArgs
if err = json.NewDecoder(c.Request().Body).Decode(&argsList); err != nil {
pedrokiefer marked this conversation as resolved.
Show resolved Hide resolved
return c.String(http.StatusBadRequest, err.Error())
}

status := http.StatusOK
var results []rpaas.PurgeCacheBulkResult
for _, args := range argsList {
var r rpaas.PurgeCacheBulkResult
count, err := manager.PurgeCache(c.Request().Context(), name, args)
if err != nil {
status = http.StatusInternalServerError
r = rpaas.PurgeCacheBulkResult{Path: args.Path, Error: err.Error()}
} else {
r = rpaas.PurgeCacheBulkResult{Path: args.Path, InstancesPurged: count}
}
results = append(results, r)
}

return c.JSON(status, results)
}
83 changes: 83 additions & 0 deletions api/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,86 @@ func Test_cachePurge(t *testing.T) {
})
}
}

func Test_cachePurgeBulk(t *testing.T) {
testCases := []struct {
description string
instanceName string
requestBody string
expectedCode int
expectedBody string
manager rpaas.RpaasManager
}{
{
description: "returns bad request when request body is empty",
instanceName: "my-instance",
requestBody: "",
expectedCode: http.StatusBadRequest,
expectedBody: `{"message":"Request body can't be empty"}`,
manager: &fake.RpaasManager{},
},
{
description: "returns 400 when manager returns ValidationError",
instanceName: "my-instance",
requestBody: `[{"path":"/index.html","preserve_path":true}]`,
expectedCode: http.StatusInternalServerError,
expectedBody: `[{"path":"/index.html","error":"Some validation failed"}]`,
manager: &fake.RpaasManager{
FakePurgeCache: func(instanceName string, args rpaas.PurgeCacheArgs) (int, error) {
return 0, rpaas.ValidationError{Msg: "Some validation failed"}
},
},
},
{
description: "returns not found when manager returns NotFoundError",
instanceName: "my-instance",
requestBody: `[{"path":"/index.html","preserve_path":true}]`,
expectedCode: http.StatusInternalServerError,
expectedBody: `[{"path":"/index.html","error":"Something was not found"}]`,
manager: &fake.RpaasManager{
FakePurgeCache: func(instanceName string, args rpaas.PurgeCacheArgs) (int, error) {
return 0, rpaas.NotFoundError{Msg: "Something was not found"}
},
},
},
{
description: "returns conflict when manager returns ConflictError",
instanceName: "my-instance",
requestBody: `[{"path":"/index.html","preserve_path":true}]`,
expectedCode: http.StatusInternalServerError,
expectedBody: `[{"path":"/index.html","error":"Something already exists"}]`,
manager: &fake.RpaasManager{
FakePurgeCache: func(instanceName string, args rpaas.PurgeCacheArgs) (int, error) {
return 0, rpaas.ConflictError{Msg: "Something already exists"}
},
},
},
{
description: "returns OK with the total number of servers where the cache was successfully purged",
instanceName: "my-instance",
requestBody: `[{"path":"/index.html","preserve_path":true}]`,
expectedCode: http.StatusOK,
expectedBody: `[{"path":"/index.html","instances_purged":36}]`,
manager: &fake.RpaasManager{
FakePurgeCache: func(instanceName string, args rpaas.PurgeCacheArgs) (int, error) {
return 36, nil
},
},
},
}

for _, tt := range testCases {
t.Run(tt.description, func(t *testing.T) {
srv := newTestingServer(t, tt.manager)
defer srv.Close()
path := fmt.Sprintf("%s/resources/%s/purge/bulk", srv.URL, tt.instanceName)
request, err := http.NewRequest(http.MethodPost, path, strings.NewReader(tt.requestBody))
require.NoError(t, err)
request.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rsp, err := srv.Client().Do(request)
require.NoError(t, err)
assert.Equal(t, tt.expectedCode, rsp.StatusCode)
assert.Equal(t, tt.expectedBody, bodyContent(rsp))
})
}
}
6 changes: 6 additions & 0 deletions internal/pkg/rpaas/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ type PurgeCacheArgs struct {
PreservePath bool `json:"preserve_path" form:"preserve_path"`
}

type PurgeCacheBulkResult struct {
Path string `json:"path"`
InstancesPurged int `json:"instances_purged,omitempty"`
Error string `json:"error,omitempty"`
}

type Plan struct {
Name string `json:"name"`
Description string `json:"description"`
Expand Down