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 global rules for Manager API #1057

Merged
merged 34 commits into from
Dec 28, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
274ea69
feat: support global rules
johzchen Dec 15, 2020
794b538
test: add e2e test cases for global rule
johzchen Dec 15, 2020
1f24f93
fix: code format
johzchen Dec 15, 2020
a6333ec
test: add test cases to confirm global rule has been removed.
johzchen Dec 18, 2020
f4ba47b
Merge branch 'master' into global-rules
johzchen Dec 18, 2020
b6443a6
fix: according to review
johzchen Dec 18, 2020
d04dd03
Merge branch 'master' into global-rules
juzhiyuan Dec 19, 2020
0fc804d
Merge branch 'master' into global-rules
johzchen Dec 19, 2020
b9b2486
fix: lint
johzchen Dec 20, 2020
3025af5
test: update test cases for plugins configured both in route and glob…
johzchen Dec 20, 2020
ea52ed4
Merge branch 'master' into global-rules
juzhiyuan Dec 22, 2020
6407c6b
Merge branch 'master' into global-rules
juzhiyuan Dec 22, 2020
d67727e
Merge branch 'master' into global-rules
nic-chen Dec 22, 2020
3af747b
fix: resful style
johzchen Dec 22, 2020
0f83dd1
fix: global rule unit test
johzchen Dec 22, 2020
61ae97b
Merge branch 'master' into global-rules
membphis Dec 23, 2020
2052302
Merge branch 'master' into global-rules
nic-chen Dec 24, 2020
3c58e5c
test: update test cases
johzchen Dec 24, 2020
312e297
Merge branch 'master' into global-rules
nic-chen Dec 24, 2020
7b9860a
Merge branch 'master' into global-rules
juzhiyuan Dec 24, 2020
ec714d0
Merge branch 'master' into global-rules
nic-chen Dec 25, 2020
f722420
Merge branch 'master' into global-rules
nic-chen Dec 25, 2020
f4207a3
feat: support patch for global rule
johzchen Dec 25, 2020
0d8a6b5
Merge branch 'master' into global-rules
nic-chen Dec 25, 2020
ce2e586
Merge branch 'master' into global-rules
nic-chen Dec 25, 2020
2aeb8a7
Merge branch 'master' into global-rules
johzchen Dec 26, 2020
6104851
Merge branch 'master' into global-rules
nic-chen Dec 27, 2020
78141e1
fix CI failed
johzchen Dec 27, 2020
c3e19f6
fix CI failed
johzchen Dec 27, 2020
2cdda71
Merge branch 'master' into global-rules
juzhiyuan Dec 27, 2020
ff66be3
Merge branch 'master' into global-rules
nic-chen Dec 28, 2020
bc454a7
Merge branch 'master' into global-rules
nic-chen Dec 28, 2020
749d956
fix CI failed
johzchen Dec 28, 2020
56a549a
fix CI failed
johzchen Dec 28, 2020
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
11 changes: 6 additions & 5 deletions api/filter/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ import (
)

var resources = map[string]string{
"routes": "route",
"upstreams": "upstream",
"services": "service",
"consumers": "consumer",
"ssl": "ssl",
"routes": "route",
"upstreams": "upstream",
"services": "service",
"consumers": "consumer",
"ssl": "ssl",
"global_rules": "global_rule",
}

func parseCert(crt, key string) ([]string, error) {
Expand Down
6 changes: 6 additions & 0 deletions api/internal/core/entity/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ type Script struct {
Script interface{} `json:"script,omitempty"`
}

// swagger:model GlobalPlugins
type GlobalPlugins struct {
ID interface{} `json:"id"`
Plugins map[string]interface{} `json:"plugins,omitempty"`
}

type ServerInfo struct {
BaseInfo
LastReportTime int64 `json:"last_report_time,omitempty"`
Expand Down
24 changes: 22 additions & 2 deletions api/internal/core/store/storehub.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
HubKeySsl HubKey = "ssl"
HubKeyUpstream HubKey = "upstream"
HubKeyScript HubKey = "script"
HubKeyGlobalRule HubKey = "global_rule"
HubKeyServerInfo HubKey = `server_info`
)

Expand All @@ -42,8 +43,15 @@ var (
)

func InitStore(key HubKey, opt GenericStoreOption) error {
if key == HubKeyConsumer || key == HubKeyRoute ||
key == HubKeyService || key == HubKeySsl || key == HubKeyUpstream {
hubsNeedCheck := map[HubKey]bool{
HubKeyConsumer: true,
HubKeyRoute: true,
HubKeySsl: true,
HubKeyService: true,
HubKeyUpstream: true,
HubKeyGlobalRule: true,
}
if _, ok := hubsNeedCheck[key]; ok {
validator, err := NewAPISIXJsonSchemaValidator("main." + string(key))
if err != nil {
return err
Expand Down Expand Up @@ -145,6 +153,18 @@ func InitStores() error {
return err
}

err = InitStore(HubKeyGlobalRule, GenericStoreOption{
BasePath: "/apisix/global_rules",
ObjType: reflect.TypeOf(entity.GlobalPlugins{}),
KeyFunc: func(obj interface{}) string {
r := obj.(*entity.GlobalPlugins)
return utils.InterfaceToString(r.ID)
},
})
if err != nil {
return err
}

err = InitStore(HubKeyServerInfo, GenericStoreOption{
BasePath: "/apisix/data_plane/server_info",
ObjType: reflect.TypeOf(entity.ServerInfo{}),
Expand Down
140 changes: 140 additions & 0 deletions api/internal/handler/global_rule/global_rule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* 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 (
"reflect"
"strings"

"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"
)

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.DELETE("/apisix/admin/global_rules/:ids", wgin.Wraps(h.BatchDelete,
membphis marked this conversation as resolved.
Show resolved Hide resolved
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
}

type BatchDeleteInput struct {
IDs string `auto_read:"ids,path"`
}

func (h *Handler) BatchDelete(c droplet.Context) (interface{}, error) {
input := c.Input().(*BatchDeleteInput)

if err := h.globalRuleStore.BatchDelete(c.Context(), strings.Split(input.IDs, ",")); err != nil {
return handler.SpecCodeResponse(err), err
}

return nil, nil
}
Loading