-
Notifications
You must be signed in to change notification settings - Fork 527
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 global rules for Manager API #1057
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
274ea69
feat: support global rules
johzchen 794b538
test: add e2e test cases for global rule
johzchen 1f24f93
fix: code format
johzchen a6333ec
test: add test cases to confirm global rule has been removed.
johzchen f4ba47b
Merge branch 'master' into global-rules
johzchen b6443a6
fix: according to review
johzchen d04dd03
Merge branch 'master' into global-rules
juzhiyuan 0fc804d
Merge branch 'master' into global-rules
johzchen b9b2486
fix: lint
johzchen 3025af5
test: update test cases for plugins configured both in route and glob…
johzchen ea52ed4
Merge branch 'master' into global-rules
juzhiyuan 6407c6b
Merge branch 'master' into global-rules
juzhiyuan d67727e
Merge branch 'master' into global-rules
nic-chen 3af747b
fix: resful style
johzchen 0f83dd1
fix: global rule unit test
johzchen 61ae97b
Merge branch 'master' into global-rules
membphis 2052302
Merge branch 'master' into global-rules
nic-chen 3c58e5c
test: update test cases
johzchen 312e297
Merge branch 'master' into global-rules
nic-chen 7b9860a
Merge branch 'master' into global-rules
juzhiyuan ec714d0
Merge branch 'master' into global-rules
nic-chen f722420
Merge branch 'master' into global-rules
nic-chen f4207a3
feat: support patch for global rule
johzchen 0d8a6b5
Merge branch 'master' into global-rules
nic-chen ce2e586
Merge branch 'master' into global-rules
nic-chen 2aeb8a7
Merge branch 'master' into global-rules
johzchen 6104851
Merge branch 'master' into global-rules
nic-chen 78141e1
fix CI failed
johzchen c3e19f6
fix CI failed
johzchen 2cdda71
Merge branch 'master' into global-rules
juzhiyuan ff66be3
Merge branch 'master' into global-rules
nic-chen bc454a7
Merge branch 'master' into global-rules
nic-chen 749d956
fix CI failed
johzchen 56a549a
fix CI failed
johzchen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package global_rule | ||
|
||
import ( | ||
"encoding/json" | ||
"reflect" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/shiningrush/droplet" | ||
"github.com/shiningrush/droplet/wrapper" | ||
wgin "github.com/shiningrush/droplet/wrapper/gin" | ||
|
||
"github.com/apisix/manager-api/internal/core/entity" | ||
"github.com/apisix/manager-api/internal/core/store" | ||
"github.com/apisix/manager-api/internal/handler" | ||
"github.com/apisix/manager-api/internal/utils" | ||
"github.com/apisix/manager-api/internal/utils/consts" | ||
) | ||
|
||
type Handler struct { | ||
globalRuleStore store.Interface | ||
} | ||
|
||
func NewHandler() (handler.RouteRegister, error) { | ||
return &Handler{ | ||
globalRuleStore: store.GetStore(store.HubKeyGlobalRule), | ||
}, nil | ||
} | ||
|
||
func (h *Handler) ApplyRoute(r *gin.Engine) { | ||
// global plugins | ||
r.GET("/apisix/admin/global_rules/:id", wgin.Wraps(h.Get, | ||
wrapper.InputType(reflect.TypeOf(GetInput{})))) | ||
r.GET("/apisix/admin/global_rules", wgin.Wraps(h.List, | ||
wrapper.InputType(reflect.TypeOf(ListInput{})))) | ||
r.PUT("/apisix/admin/global_rules/:id", wgin.Wraps(h.Set, | ||
wrapper.InputType(reflect.TypeOf(entity.GlobalPlugins{})))) | ||
r.PUT("/apisix/admin/global_rules", wgin.Wraps(h.Set, | ||
wrapper.InputType(reflect.TypeOf(entity.GlobalPlugins{})))) | ||
|
||
r.PATCH("/apisix/admin/global_rules/:id", consts.ErrorWrapper(Patch)) | ||
r.PATCH("/apisix/admin/global_rules/:id/*path", consts.ErrorWrapper(Patch)) | ||
|
||
r.DELETE("/apisix/admin/global_rules/:id", wgin.Wraps(h.BatchDelete, | ||
wrapper.InputType(reflect.TypeOf(BatchDeleteInput{})))) | ||
} | ||
|
||
type GetInput struct { | ||
ID string `auto_read:"id,path" validate:"required"` | ||
} | ||
|
||
func (h *Handler) Get(c droplet.Context) (interface{}, error) { | ||
input := c.Input().(*GetInput) | ||
|
||
r, err := h.globalRuleStore.Get(input.ID) | ||
if err != nil { | ||
return handler.SpecCodeResponse(err), err | ||
} | ||
return r, nil | ||
} | ||
|
||
type ListInput struct { | ||
store.Pagination | ||
} | ||
|
||
// swagger:operation GET /apisix/admin/global_rules getGlobalRuleList | ||
// | ||
// Return the global rule list according to the specified page number and page size. | ||
// | ||
// --- | ||
// produces: | ||
// - application/json | ||
// parameters: | ||
// - name: page | ||
// in: query | ||
// description: page number | ||
// required: false | ||
// type: integer | ||
// - name: page_size | ||
// in: query | ||
// description: page size | ||
// required: false | ||
// type: integer | ||
// responses: | ||
// '0': | ||
// description: list response | ||
// schema: | ||
// type: array | ||
// items: | ||
// "$ref": "#/definitions/GlobalPlugins" | ||
// default: | ||
// description: unexpected error | ||
// schema: | ||
// "$ref": "#/definitions/ApiError" | ||
func (h *Handler) List(c droplet.Context) (interface{}, error) { | ||
input := c.Input().(*ListInput) | ||
|
||
ret, err := h.globalRuleStore.List(store.ListInput{ | ||
PageSize: input.PageSize, | ||
PageNumber: input.PageNumber, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return ret, nil | ||
} | ||
|
||
func (h *Handler) Set(c droplet.Context) (interface{}, error) { | ||
input := c.Input().(*entity.GlobalPlugins) | ||
|
||
if err := h.globalRuleStore.Create(c.Context(), input); err != nil { | ||
return handler.SpecCodeResponse(err), err | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
func Patch(c *gin.Context) (interface{}, error) { | ||
reqBody, _ := c.GetRawData() | ||
ID := c.Param("id") | ||
subPath := c.Param("path") | ||
|
||
routeStore := store.GetStore(store.HubKeyGlobalRule) | ||
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 globalRule entity.GlobalPlugins | ||
err = json.Unmarshal(res, &globalRule) | ||
if err != nil { | ||
return handler.SpecCodeResponse(err), err | ||
} | ||
|
||
if err := routeStore.Update(c, &globalRule, false); err != nil { | ||
return handler.SpecCodeResponse(err), err | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
type BatchDeleteInput struct { | ||
ID string `auto_read:"id,path"` | ||
} | ||
|
||
func (h *Handler) BatchDelete(c droplet.Context) (interface{}, error) { | ||
input := c.Input().(*BatchDeleteInput) | ||
|
||
if err := h.globalRuleStore.BatchDelete(c.Context(), []string{input.ID}); err != nil { | ||
return handler.SpecCodeResponse(err), err | ||
} | ||
|
||
return nil, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ShiningRush please take a review