From 88ecb1f9dfeded36e7fd74c776daefcaf67f8ae2 Mon Sep 17 00:00:00 2001 From: Lukasz Rajewski Date: Thu, 3 Feb 2022 19:18:07 +0100 Subject: ConfigAPI and Query API improvements - Config Template create from the definition content - Missing CRUD Config handlers added - Improved Rollback and Config delete - Query API name filtering improved Issue-ID: MULTICLOUD-1437 Signed-off-by: Lukasz Rajewski Change-Id: Iec8ec6d03746085f294d9318a252f1ae45d3b9c8 --- src/k8splugin/internal/db/etcd.go | 23 ++++++++++++++++++++--- src/k8splugin/internal/db/etcd_testing.go | 12 +++++++++++- 2 files changed, 31 insertions(+), 4 deletions(-) (limited to 'src/k8splugin/internal/db') diff --git a/src/k8splugin/internal/db/etcd.go b/src/k8splugin/internal/db/etcd.go index e455cc1a..5ce8135a 100644 --- a/src/k8splugin/internal/db/etcd.go +++ b/src/k8splugin/internal/db/etcd.go @@ -36,7 +36,8 @@ type EtcdConfig struct { // EtcdStore Interface needed for mocking type EtcdStore interface { Get(key string) ([]byte, error) - GetAll(key string) ([][]byte, error) + GetKeys(key string) ([]string, error) + GetValues(key string) ([][]byte, error) Put(key, value string) error Delete(key string) error DeletePrefix(keyPrefix string) error @@ -124,8 +125,24 @@ 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) { +// GetKeys sub values from Etcd DB +func (e EtcdClient) GetKeys(key string) ([]string, 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([]string, 0) + for _, v := range getResp.Kvs { + result = append(result, string(v.Key)) + } + return result, nil +} + +// GetValues sub values from Etcd DB +func (e EtcdClient) GetValues(key string) ([][]byte, error) { if e.cli == nil { return nil, pkgerrors.Errorf("Etcd Client not initialized") } diff --git a/src/k8splugin/internal/db/etcd_testing.go b/src/k8splugin/internal/db/etcd_testing.go index 4b4dfe3e..2f62d365 100644 --- a/src/k8splugin/internal/db/etcd_testing.go +++ b/src/k8splugin/internal/db/etcd_testing.go @@ -41,7 +41,17 @@ func (c *MockEtcdClient) Get(key string) ([]byte, error) { return nil, pkgerrors.Errorf("Key doesn't exist") } -func (c *MockEtcdClient) GetAll(key string) ([][]byte, error) { +func (c *MockEtcdClient) GetKeys(key string) ([]string, error) { + result := make([]string, 0) + for kvKey := range c.Items { + if strings.HasPrefix(kvKey, key) { + result = append(result, kvKey) + } + } + return result, nil +} + +func (c *MockEtcdClient) GetValues(key string) ([][]byte, error) { result := make([][]byte, 0) for kvKey, kvValue := range c.Items { if strings.HasPrefix(kvKey, key) { -- cgit 1.2.3-korg