From 91fe5c50c2d882a5c831dd473d8db765b2dd8699 Mon Sep 17 00:00:00 2001 From: Lukasz Rajewski Date: Mon, 2 Aug 2021 22:15:35 +0200 Subject: Config List handler added to Config API Config List handler added to Config API Issue-ID: MULTICLOUD-1332 Signed-off-by: Lukasz Rajewski Change-Id: I63355dd6b05e70398cfc89744efa332926286c40 --- src/k8splugin/internal/db/etcd.go | 19 ++++++++++++++++++- src/k8splugin/internal/db/etcd_testing.go | 12 ++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) (limited to 'src/k8splugin/internal/db') diff --git a/src/k8splugin/internal/db/etcd.go b/src/k8splugin/internal/db/etcd.go index 97771a07..a435b435 100644 --- a/src/k8splugin/internal/db/etcd.go +++ b/src/k8splugin/internal/db/etcd.go @@ -36,6 +36,7 @@ type EtcdConfig struct { // EtcdStore Interface needed for mocking type EtcdStore interface { Get(key string) ([]byte, error) + GetAll(key string) ([][]byte, error) Put(key, value string) error Delete(key string) error } @@ -114,7 +115,7 @@ func (e EtcdClient) Get(key string) ([]byte, error) { } getResp, err := e.cli.Get(context.Background(), key) if err != nil { - return nil, pkgerrors.Errorf("Error getitng etcd entry: %s", err.Error()) + return nil, pkgerrors.Errorf("Error getting etcd entry: %s", err.Error()) } if getResp.Count == 0 { return nil, pkgerrors.Errorf("Key doesn't exist") @@ -122,6 +123,22 @@ func (e EtcdClient) Get(key string) ([]byte, error) { return getResp.Kvs[0].Value, nil } +// GetAll sub values from Etcd DB +func (e EtcdClient) GetAll(key string) ([][]byte, error) { + if e.cli == nil { + return nil, pkgerrors.Errorf("Etcd Client not initialized") + } + getResp, err := e.cli.Get(context.Background(), key, clientv3.WithPrefix()) + if err != nil { + return nil, pkgerrors.Errorf("Error getting etcd entry: %s", err.Error()) + } + result := make([][]byte, 0) + for _, v := range getResp.Kvs { + result = append(result, v.Value) + } + return result, nil +} + // Delete values from Etcd DB func (e EtcdClient) Delete(key string) error { diff --git a/src/k8splugin/internal/db/etcd_testing.go b/src/k8splugin/internal/db/etcd_testing.go index 12b17e33..9dfcad82 100644 --- a/src/k8splugin/internal/db/etcd_testing.go +++ b/src/k8splugin/internal/db/etcd_testing.go @@ -14,6 +14,8 @@ limitations under the License. package db import ( + "strings" + pkgerrors "github.com/pkg/errors" ) @@ -39,6 +41,16 @@ func (c *MockEtcdClient) Get(key string) ([]byte, error) { return nil, pkgerrors.Errorf("Key doesn't exist") } +func (c *MockEtcdClient) GetAll(key string) ([][]byte, error) { + result := make([][]byte, 0) + for kvKey, kvValue := range c.Items { + if strings.HasPrefix(kvKey, key) { + result = append(result, []byte(kvValue)) + } + } + return result, nil +} + func (c *MockEtcdClient) Delete(key string) error { delete(c.Items, key) return c.Err -- cgit 1.2.3-korg