* Add KVList method

* Add KVList method

Add KVList method

* Add pagination support

* Change offset, limit to page, perPage

* Rename constant
Этот коммит содержится в:
Shobhit Gupta
2018-10-03 13:04:37 -07:00
коммит произвёл Joram Wilander
родитель 7468f35cb0
Коммит 8c03e584c1
9 изменённых файлов: 156 добавлений и 0 удалений

Просмотреть файл

@@ -331,6 +331,10 @@ func (api *PluginAPI) KVDelete(key string) *model.AppError {
return api.app.DeletePluginKey(api.id, key)
}
func (api *PluginAPI) KVList(page, perPage int) ([]string, *model.AppError) {
return api.app.ListPluginKeys(api.id, page, perPage)
}
func (api *PluginAPI) PublishWebSocketEvent(event string, payload map[string]interface{}, broadcast *model.WebsocketBroadcast) {
api.app.Publish(&model.WebSocketEvent{
Event: fmt.Sprintf("custom_%v_%v", api.id, event),

Просмотреть файл

@@ -59,3 +59,14 @@ func (a *App) DeletePluginKey(pluginId string, key string) *model.AppError {
return result.Err
}
func (a *App) ListPluginKeys(pluginId string, page, perPage int) ([]string, *model.AppError) {
result := <-a.Srv.Store.Plugin().List(pluginId, page, perPage)
if result.Err != nil {
mlog.Error(result.Err.Error())
return nil, result.Err
}
return result.Data.([]string), nil
}

Просмотреть файл

@@ -5,6 +5,8 @@ package app
import (
"bytes"
"crypto/sha256"
"encoding/base64"
"io/ioutil"
"net/http"
"net/http/httptest"
@@ -17,6 +19,11 @@ import (
"github.com/stretchr/testify/require"
)
func getHashedKey(key string) string {
hash := sha256.New()
hash.Write([]byte(key))
return base64.StdEncoding.EncodeToString(hash.Sum(nil))
}
func TestPluginKeyValueStore(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()
@@ -40,6 +47,41 @@ func TestPluginKeyValueStore(t *testing.T) {
assert.Nil(t, th.App.DeletePluginKey(pluginId, "intkey"))
assert.Nil(t, th.App.DeletePluginKey(pluginId, "postkey"))
assert.Nil(t, th.App.DeletePluginKey(pluginId, "notrealkey"))
// Test ListKeys
assert.Nil(t, th.App.SetPluginKey(pluginId, "key2", []byte("test")))
hashedKey := getHashedKey("key")
hashedKey2 := getHashedKey("key2")
list, err := th.App.ListPluginKeys(pluginId, 0, 1)
assert.Nil(t, err)
assert.Equal(t, 1, len(list))
assert.Equal(t, hashedKey, list[0])
list, err = th.App.ListPluginKeys(pluginId, 1, 1)
assert.Nil(t, err)
assert.Equal(t, 1, len(list))
assert.Equal(t, hashedKey2, list[0])
//List Keys bad input
list, err = th.App.ListPluginKeys(pluginId, 0, 0)
assert.Nil(t, err)
assert.Equal(t, 2, len(list))
list, err = th.App.ListPluginKeys(pluginId, 0, -1)
assert.Nil(t, err)
assert.Equal(t, 2, len(list))
list, err = th.App.ListPluginKeys(pluginId, -1, 1)
assert.Nil(t, err)
assert.Equal(t, 1, len(list))
list, err = th.App.ListPluginKeys(pluginId, -1, 0)
assert.Nil(t, err)
assert.Equal(t, 2, len(list))
list, err = th.App.ListPluginKeys(pluginId, 2, 2)
assert.Nil(t, err)
assert.Equal(t, 0, len(list))
}
func TestServePluginRequest(t *testing.T) {

Просмотреть файл

@@ -196,6 +196,9 @@ type API interface {
// KVDelete will remove a key-value pair. Returns nil for non-existent keys.
KVDelete(key string) *model.AppError
// KVList will list all keys for a plugin.
KVList(page, perPage int) ([]string, *model.AppError)
// PublishWebSocketEvent sends an event to WebSocket connections.
// event is the type and will be prepended with "custom_<pluginid>_"
// payload is the data sent with the event. Interface values must be primitive Go types or mattermost-server/model types

Просмотреть файл

@@ -2151,6 +2151,36 @@ func (s *apiRPCServer) KVDelete(args *Z_KVDeleteArgs, returns *Z_KVDeleteReturns
return nil
}
type Z_KVListArgs struct {
A int
B int
}
type Z_KVListReturns struct {
A []string
B *model.AppError
}
func (g *apiRPCClient) KVList(page, perPage int) ([]string, *model.AppError) {
_args := &Z_KVListArgs{page, perPage}
_returns := &Z_KVListReturns{}
if err := g.client.Call("Plugin.KVList", _args, _returns); err != nil {
log.Printf("RPC call to KVList API failed: %s", err.Error())
}
return _returns.A, _returns.B
}
func (s *apiRPCServer) KVList(args *Z_KVListArgs, returns *Z_KVListReturns) error {
if hook, ok := s.impl.(interface {
KVList(page, perPage int) ([]string, *model.AppError)
}); ok {
returns.A, returns.B = hook.KVList(args.A, args.B)
} else {
return encodableError(fmt.Errorf("API KVList called but not implemented."))
}
return nil
}
type Z_PublishWebSocketEventArgs struct {
A string
B map[string]interface{}

Просмотреть файл

@@ -996,6 +996,31 @@ func (_m *API) KVGet(key string) ([]byte, *model.AppError) {
return r0, r1
}
// KVList provides a mock function with given fields: page, perPage
func (_m *API) KVList(page int, perPage int) ([]string, *model.AppError) {
ret := _m.Called(page, perPage)
var r0 []string
if rf, ok := ret.Get(0).(func(int, int) []string); ok {
r0 = rf(page, perPage)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]string)
}
}
var r1 *model.AppError
if rf, ok := ret.Get(1).(func(int, int) *model.AppError); ok {
r1 = rf(page, perPage)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
}
// KVSet provides a mock function with given fields: key, value
func (_m *API) KVSet(key string, value []byte) *model.AppError {
ret := _m.Called(key, value)

Просмотреть файл

@@ -12,6 +12,10 @@ import (
"github.com/mattermost/mattermost-server/store"
)
const (
DEFAULT_PLUGIN_KEY_FETCH_LIMIT = 10
)
type SqlPluginStore struct {
SqlStore
}
@@ -92,3 +96,23 @@ func (ps SqlPluginStore) Delete(pluginId, key string) store.StoreChannel {
}
})
}
func (ps SqlPluginStore) List(pluginId string, offset int, limit int) store.StoreChannel {
if limit <= 0 {
limit = DEFAULT_PLUGIN_KEY_FETCH_LIMIT
}
if offset <= 0 {
offset = 0
}
return store.Do(func(result *store.StoreResult) {
var keys []string
_, err := ps.GetReplica().Select(&keys, "SELECT PKey FROM PluginKeyValueStore WHERE PluginId = :PluginId order by PKey limit :Limit offset :Offset", map[string]interface{}{"PluginId": pluginId, "Limit": limit, "Offset": offset})
if err != nil {
result.Err = model.NewAppError("SqlPluginStore.List", "store.sql_plugin_store.list.app_error", nil, fmt.Sprintf("plugin_id=%v, err=%v", pluginId, err.Error()), http.StatusInternalServerError)
} else {
result.Data = keys
}
})
}

Просмотреть файл

@@ -501,6 +501,7 @@ type PluginStore interface {
SaveOrUpdate(keyVal *model.PluginKeyValue) StoreChannel
Get(pluginId, key string) StoreChannel
Delete(pluginId, key string) StoreChannel
List(pluginId string, page, perPage int) StoreChannel
}
type RoleStore interface {

Просмотреть файл

@@ -45,6 +45,22 @@ func (_m *PluginStore) Get(pluginId string, key string) store.StoreChannel {
return r0
}
// List provides a mock function with given fields: pluginId, page, perPage
func (_m *PluginStore) List(pluginId string, page int, perPage int) store.StoreChannel {
ret := _m.Called(pluginId, page, perPage)
var r0 store.StoreChannel
if rf, ok := ret.Get(0).(func(string, int, int) store.StoreChannel); ok {
r0 = rf(pluginId, page, perPage)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel)
}
}
return r0
}
// SaveOrUpdate provides a mock function with given fields: keyVal
func (_m *PluginStore) SaveOrUpdate(keyVal *model.PluginKeyValue) store.StoreChannel {
ret := _m.Called(keyVal)