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: support patch in route module in order to support publish/offline #1049

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 32 additions & 0 deletions api/internal/handler/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,41 @@ func (h *Handler) ApplyRoute(r *gin.Engine) {
r.DELETE("/apisix/admin/routes/:ids", wgin.Wraps(h.BatchDelete,
wrapper.InputType(reflect.TypeOf(BatchDelete{}))))

r.PATCH("/apisix/admin/routes/:id", consts.ErrorWrapper(Patch))
r.PATCH("/apisix/admin/routes/:id/*path", consts.ErrorWrapper(Patch))

r.GET("/apisix/admin/notexist/routes", consts.ErrorWrapper(Exist))
}

func Patch(c *gin.Context) (interface{}, error) {
reqBody, _ := c.GetRawData()
ID := c.Param("id")
subPath := c.Param("path")

routeStore := store.GetStore(store.HubKeyRoute)
stored, err := routeStore.Get(ID)
if err != nil {
return handler.SpecCodeResponse(err), err
}

res, err := utils.MergePatch(stored, subPath, reqBody)
if err != nil {
return handler.SpecCodeResponse(err), err
}

var route entity.Route
err = json.Unmarshal(res, &route)
if err != nil {
return handler.SpecCodeResponse(err), err
}

if err := routeStore.Update(c, &route, false); err != nil {
return handler.SpecCodeResponse(err), err
}

return nil, nil
}

type GetInput struct {
ID string `auto_read:"id,path" validate:"required"`
}
Expand Down
42 changes: 42 additions & 0 deletions api/test/e2e/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,48 @@ func TestRoute_Update_Routes_With_Hosts(t *testing.T) {
}
}

func TestRoute_Patch(t *testing.T) {
tests := []HttpTestCase{
{
caseDesc: "route patch",
Copy link
Member

Choose a reason for hiding this comment

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

we should always create the related resource first.

eg: the route resource

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

Object: ManagerApiExpect(t),
Method: http.MethodPatch,
Path: "/apisix/admin/routes/r1",
Body: `{"status":1}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
}, {
caseDesc: "route patch",
Object: ManagerApiExpect(t),
Method: http.MethodPatch,
Path: "/apisix/admin/routes/r1",
Body: `{"status":0}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
}, {
caseDesc: "route patch",
Object: ManagerApiExpect(t),
Method: http.MethodPatch,
Path: "/apisix/admin/routes/r1",
Body: `{"name":"r1_route"}`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
Sleep: sleepTime,
}, {
caseDesc: "route patch with path",
Object: ManagerApiExpect(t),
Method: http.MethodPatch,
Path: "/apisix/admin/routes/r1/status",
Body: `1`,
Headers: map[string]string{"Authorization": token},
ExpectStatus: http.StatusOK,
},
}
for _, tc := range tests {
testCaseCheck(tc)
}
}

func TestRoute_Delete_Routes_With_Hosts(t *testing.T) {
tests := []HttpTestCase{
{
Expand Down