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

fix: when create a route without ID in request body by Admin API, it can't be displayed in Manager API #1063

Merged
merged 25 commits into from
Dec 25, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
8e20d02
fix: when the ID in body of a resource is empty, it cannot be display…
johzchen Dec 15, 2020
7a48b87
test: add test case
johzchen Dec 16, 2020
b2bcbf6
fix: unit test fail
johzchen Dec 18, 2020
c2631b0
Merge branch 'master' into fix-no-id
johzchen Dec 20, 2020
274c16f
fix: error
johzchen Dec 20, 2020
934517e
Merge branch 'master' into fix-no-id
nic-chen Dec 22, 2020
2f49325
Merge branch 'master' into fix-no-id
nic-chen Dec 22, 2020
9c9457e
Merge branch 'master' into fix-no-id
nic-chen Dec 24, 2020
28d9189
Merge branch 'master' into fix-no-id
nic-chen Dec 24, 2020
8307d35
test: update test cases
johzchen Dec 24, 2020
27dcb2c
test: update test cases
johzchen Dec 24, 2020
5a2dfe2
Merge branch 'master' into fix-no-id
nic-chen Dec 24, 2020
04155fc
Merge branch 'master' into fix-no-id
nic-chen Dec 24, 2020
94786f3
Merge branch 'master' into fix-no-id
nic-chen Dec 25, 2020
1556502
chore: naming style
johzchen Dec 25, 2020
e89acac
Merge branch 'master' into fix-no-id
nic-chen Dec 25, 2020
fb1d485
test: add test cases for create route by POST
johzchen Dec 25, 2020
e9f619b
Merge branch 'fix-no-id' of github.com:nic-chen/incubator-apisix-dash…
johzchen Dec 25, 2020
08abcc8
fix: unit test failed
johzchen Dec 25, 2020
54f564a
fix: manager api host port for e2e
johzchen Dec 25, 2020
8720671
chore: sleep more time
johzchen Dec 25, 2020
048abbd
chore: code refactor
johzchen Dec 25, 2020
c48df31
fix: error
johzchen Dec 25, 2020
ff232d2
fix: unit test failed
johzchen Dec 25, 2020
3c6a54c
Merge branch 'master' into fix-no-id
nic-chen Dec 25, 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
10 changes: 7 additions & 3 deletions api/internal/core/storage/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,19 @@ func (s *EtcdV3Storage) Get(ctx context.Context, key string) (string, error) {
return string(resp.Kvs[0].Value), nil
}

func (s *EtcdV3Storage) List(ctx context.Context, key string) ([]string, error) {
func (s *EtcdV3Storage) List(ctx context.Context, key string) ([]Data, error) {
resp, err := Client.Get(ctx, key, clientv3.WithPrefix())
if err != nil {
log.Errorf("etcd get failed: %s", err)
return nil, fmt.Errorf("etcd get failed: %s", err)
}
var ret []string
var ret []Data
for i := range resp.Kvs {
ret = append(ret, string(resp.Kvs[i].Value))
data := Data{
Key: string(resp.Kvs[i].Key),
Value: string(resp.Kvs[i].Value),
}
ret = append(ret, data)
}

return ret, nil
Expand Down
7 changes: 6 additions & 1 deletion api/internal/core/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import "context"

type Interface interface {
Get(ctx context.Context, key string) (string, error)
List(ctx context.Context, key string) ([]string, error)
List(ctx context.Context, key string) ([]Data, error)
Create(ctx context.Context, key, val string) error
Update(ctx context.Context, key, val string) error
BatchDelete(ctx context.Context, keys []string) error
Expand All @@ -33,6 +33,11 @@ type WatchResponse struct {
Canceled bool
}

type Data struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

What about Keypair?

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, it is clearer.

Key string
Value string
}

type Event struct {
Type EventType
Key string
Expand Down
26 changes: 20 additions & 6 deletions api/internal/core/storage/storage_mock.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
// Code generated by mockery v1.0.0. DO NOT EDIT.

/*
nic-chen marked this conversation as resolved.
Show resolved Hide resolved
* 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 storage

import (
Expand Down Expand Up @@ -63,15 +77,15 @@ func (_m *MockInterface) Get(ctx context.Context, key string) (string, error) {
}

// List provides a mock function with given fields: ctx, key
func (_m *MockInterface) List(ctx context.Context, key string) ([]string, error) {
func (_m *MockInterface) List(ctx context.Context, key string) ([]Data, error) {
ret := _m.Called(ctx, key)

var r0 []string
if rf, ok := ret.Get(0).(func(context.Context, string) []string); ok {
var r0 []Data
if rf, ok := ret.Get(0).(func(context.Context, string) []Data); ok {
r0 = rf(ctx, key)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]string)
r0 = ret.Get(0).([]Data)
}
}

Expand Down
19 changes: 11 additions & 8 deletions api/internal/core/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type GenericStore struct {
type GenericStoreOption struct {
BasePath string
ObjType reflect.Type
KeyFunc func(obj interface{}) string
KeyFunc func(obj interface{}, key string) string
juzhiyuan marked this conversation as resolved.
Show resolved Hide resolved
StockCheck func(obj interface{}, stockObj interface{}) error
Validator Validator
}
Expand Down Expand Up @@ -96,14 +96,16 @@ func (s *GenericStore) Init() error {
return err
}
for i := range ret {
if ret[i] == "init_dir" {
if ret[i].Value == "init_dir" {
continue
}
objPtr, err := s.StringToObjPtr(ret[i])
objPtr, err := s.StringToObjPtr(ret[i].Value)
if err != nil {
return err
}
s.cache.Store(s.opt.KeyFunc(objPtr), objPtr)

key := ret[i].Key[len(s.opt.BasePath)+1:]
s.cache.Store(s.opt.KeyFunc(objPtr, key), objPtr)
}

c, cancel := context.WithCancel(context.TODO())
Expand All @@ -122,7 +124,8 @@ func (s *GenericStore) Init() error {
log.Warnf("value convert to obj failed: %s", err)
continue
}
s.cache.Store(event.Events[i].Key[len(s.opt.BasePath)+1:], objPtr)
key := event.Events[i].Key[len(s.opt.BasePath)+1:]
s.cache.Store(s.opt.KeyFunc(objPtr, key), objPtr)
case storage.EventTypeDelete:
s.cache.Delete(event.Events[i].Key[len(s.opt.BasePath)+1:])
}
Expand Down Expand Up @@ -247,7 +250,7 @@ func (s *GenericStore) Create(ctx context.Context, obj interface{}) error {
return err
}

key := s.opt.KeyFunc(obj)
key := s.opt.KeyFunc(obj, "")
if key == "" {
return fmt.Errorf("key is required")
}
Expand All @@ -274,7 +277,7 @@ func (s *GenericStore) Update(ctx context.Context, obj interface{}, createIfNotE
return err
}

key := s.opt.KeyFunc(obj)
key := s.opt.KeyFunc(obj, "")
if key == "" {
return fmt.Errorf("key is required")
}
Expand Down Expand Up @@ -332,7 +335,7 @@ func (s *GenericStore) StringToObjPtr(str string) (interface{}, error) {
}

func (s *GenericStore) GetObjStorageKey(obj interface{}) string {
return s.GetStorageKey(s.opt.KeyFunc(obj))
return s.GetStorageKey(s.opt.KeyFunc(obj, ""))
}

func (s *GenericStore) GetStorageKey(key string) string {
Expand Down
44 changes: 28 additions & 16 deletions api/internal/core/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
)

func TestNewGenericStore(t *testing.T) {
dfFunc := func(obj interface{}) string { return "" }
dfFunc := func(obj interface{}, key string) string { return "" }
tests := []struct {
giveOpt GenericStoreOption
giveCache map[string]interface{}
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestGenericStore_Init(t *testing.T) {
caseDesc string
giveStore *GenericStore
giveListErr error
giveListRet []string
giveListRet []storage.Data
giveWatchCh chan storage.WatchResponse
giveResp storage.WatchResponse
wantErr error
Expand All @@ -129,14 +129,20 @@ func TestGenericStore_Init(t *testing.T) {
opt: GenericStoreOption{
BasePath: "test",
ObjType: reflect.TypeOf(TestStruct{}),
KeyFunc: func(obj interface{}) string {
KeyFunc: func(obj interface{}, key string) string {
return obj.(*TestStruct).Field1
},
},
},
giveListRet: []string{
`{"Field1":"demo1-f1", "Field2":"demo1-f2"}`,
`{"Field1":"demo2-f1", "Field2":"demo2-f2"}`,
giveListRet: []storage.Data{
{
Key: "test/demo1-f1",
Value: `{"Field1":"demo1-f1", "Field2":"demo1-f2"}`,
},
{
Key: "test/demo2-f1",
Value: `{"Field1":"demo2-f1", "Field2":"demo2-f2"}`,
},
},
giveWatchCh: make(chan storage.WatchResponse),
giveResp: storage.WatchResponse{
Expand Down Expand Up @@ -178,14 +184,20 @@ func TestGenericStore_Init(t *testing.T) {
opt: GenericStoreOption{
BasePath: "test",
ObjType: reflect.TypeOf(TestStruct{}),
KeyFunc: func(obj interface{}) string {
KeyFunc: func(obj interface{}, key string) string {
return obj.(*TestStruct).Field1
},
},
},
giveListRet: []string{
`{"Field1","demo1-f1", "Field2":"demo1-f2"}`,
`{"Field1":"demo2-f1", "Field2":"demo2-f2"}`,
giveListRet: []storage.Data{
{
Key: "test/demo1-f1",
Value: `{"Field1","demo1-f1", "Field2":"demo1-f2"}`,
},
{
Key: "test/demo2-f1",
Value: `{"Field1":"demo2-f1", "Field2":"demo2-f2"}`,
},
},
wantErr: fmt.Errorf("json unmarshal failed: invalid character ',' after object key"),
wantListCalled: true,
Expand Down Expand Up @@ -509,7 +521,7 @@ func TestGenericStore_Create(t *testing.T) {
giveStore: &GenericStore{
opt: GenericStoreOption{
BasePath: "test/path",
KeyFunc: func(obj interface{}) string {
KeyFunc: func(obj interface{}, key string) string {
return obj.(*TestStruct).Field1
},
},
Expand All @@ -525,7 +537,7 @@ func TestGenericStore_Create(t *testing.T) {
giveStore: &GenericStore{
opt: GenericStoreOption{
BasePath: "test/path",
KeyFunc: func(obj interface{}) string {
KeyFunc: func(obj interface{}, key string) string {
return obj.(*TestStruct).Field1
},
},
Expand All @@ -546,7 +558,7 @@ func TestGenericStore_Create(t *testing.T) {
giveStore: &GenericStore{
opt: GenericStoreOption{
BasePath: "test/path",
KeyFunc: func(obj interface{}) string {
KeyFunc: func(obj interface{}, key string) string {
return obj.(*TestStruct).Field1
},
},
Expand Down Expand Up @@ -632,7 +644,7 @@ func TestGenericStore_Update(t *testing.T) {
giveStore: &GenericStore{
opt: GenericStoreOption{
BasePath: "test/path",
KeyFunc: func(obj interface{}) string {
KeyFunc: func(obj interface{}, key string) string {
return obj.(*TestStruct).Field1
},
},
Expand All @@ -651,7 +663,7 @@ func TestGenericStore_Update(t *testing.T) {
giveStore: &GenericStore{
opt: GenericStoreOption{
BasePath: "test/path",
KeyFunc: func(obj interface{}) string {
KeyFunc: func(obj interface{}, key string) string {
return obj.(*TestStruct).Field1
},
},
Expand All @@ -672,7 +684,7 @@ func TestGenericStore_Update(t *testing.T) {
giveStore: &GenericStore{
opt: GenericStoreOption{
BasePath: "test/path",
KeyFunc: func(obj interface{}) string {
KeyFunc: func(obj interface{}, key string) string {
return obj.(*TestStruct).Field1
},
},
Expand Down
29 changes: 22 additions & 7 deletions api/internal/core/store/storehub.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func InitStores() error {
err := InitStore(HubKeyConsumer, GenericStoreOption{
BasePath: "/apisix/consumers",
ObjType: reflect.TypeOf(entity.Consumer{}),
KeyFunc: func(obj interface{}) string {
KeyFunc: func(obj interface{}, key string) string {
r := obj.(*entity.Consumer)
return r.Username
},
Expand All @@ -88,8 +88,11 @@ func InitStores() error {
err = InitStore(HubKeyRoute, GenericStoreOption{
BasePath: "/apisix/routes",
ObjType: reflect.TypeOf(entity.Route{}),
KeyFunc: func(obj interface{}) string {
KeyFunc: func(obj interface{}, key string) string {
r := obj.(*entity.Route)
if r.ID == nil && key != "" {
r.ID = key
}
return utils.InterfaceToString(r.ID)
},
})
Expand All @@ -100,8 +103,11 @@ func InitStores() error {
err = InitStore(HubKeyService, GenericStoreOption{
BasePath: "/apisix/services",
ObjType: reflect.TypeOf(entity.Service{}),
KeyFunc: func(obj interface{}) string {
KeyFunc: func(obj interface{}, key string) string {
r := obj.(*entity.Service)
if r.ID == nil && key != "" {
r.ID = key
}
return utils.InterfaceToString(r.ID)
},
})
Expand All @@ -112,8 +118,11 @@ func InitStores() error {
err = InitStore(HubKeySsl, GenericStoreOption{
BasePath: "/apisix/ssl",
ObjType: reflect.TypeOf(entity.SSL{}),
KeyFunc: func(obj interface{}) string {
KeyFunc: func(obj interface{}, key string) string {
r := obj.(*entity.SSL)
if r.ID == nil && key != "" {
r.ID = key
}
return utils.InterfaceToString(r.ID)
},
})
Expand All @@ -124,8 +133,11 @@ func InitStores() error {
err = InitStore(HubKeyUpstream, GenericStoreOption{
BasePath: "/apisix/upstreams",
ObjType: reflect.TypeOf(entity.Upstream{}),
KeyFunc: func(obj interface{}) string {
KeyFunc: func(obj interface{}, key string) string {
r := obj.(*entity.Upstream)
if r.ID == nil && key != "" {
r.ID = key
}
return utils.InterfaceToString(r.ID)
},
})
Expand All @@ -136,8 +148,11 @@ func InitStores() error {
err = InitStore(HubKeyScript, GenericStoreOption{
BasePath: "/apisix/scripts",
ObjType: reflect.TypeOf(entity.Script{}),
KeyFunc: func(obj interface{}) string {
KeyFunc: func(obj interface{}, key string) string {
r := obj.(*entity.Script)
if r.ID == "" && key != "" {
r.ID = key
}
return r.ID
},
})
Expand All @@ -148,7 +163,7 @@ func InitStores() error {
err = InitStore(HubKeyServerInfo, GenericStoreOption{
BasePath: "/apisix/data_plane/server_info",
ObjType: reflect.TypeOf(entity.ServerInfo{}),
KeyFunc: func(obj interface{}) string {
KeyFunc: func(obj interface{}, key string) string {
r := obj.(*entity.ServerInfo)
return utils.InterfaceToString(r.ID)
},
Expand Down
25 changes: 18 additions & 7 deletions api/internal/core/store/validate_mock.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
// Code generated by mockery v1.0.0. DO NOT EDIT.

/*
* 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 store

import mock "github.com/stretchr/testify/mock"
Expand All @@ -13,12 +27,9 @@ type MockValidator struct {
func (_m *MockValidator) Validate(obj interface{}) error {
ret := _m.Called(obj)

var r0 error
if rf, ok := ret.Get(0).(func(interface{}) error); ok {
r0 = rf(obj)
} else {
r0 = ret.Error(0)
return rf(obj)
}

return r0
return ret.Error(0)
}
Loading