From 037cfda2181e4995e4e2a47db6f1121b532b686b Mon Sep 17 00:00:00 2001 From: Kiran Kamineni Date: Fri, 15 Mar 2019 15:03:01 -0700 Subject: Add support for composite keys Composite keys help us store objects which are unique for a given set of pre-existing objects. Eg: Many profiles can exist for a definition and its key will have a definition name as a part of the composite key. P2: Use a predefined interface for keys instead of generic interfaceP{} P3: Add check for empty strings in stringer interface P5: Add appropriate keys in other packages. Issue-ID: MULTICLOUD-531 Change-Id: I314b1fbd718489ae8a45f0f38915c08ca32f9f43 Signed-off-by: Kiran Kamineni --- src/k8splugin/internal/db/consul.go | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) (limited to 'src/k8splugin/internal/db/consul.go') diff --git a/src/k8splugin/internal/db/consul.go b/src/k8splugin/internal/db/consul.go index a61a4c10..23d2ae88 100644 --- a/src/k8splugin/internal/db/consul.go +++ b/src/k8splugin/internal/db/consul.go @@ -54,7 +54,7 @@ func NewConsulStore(store ConsulKVStore) (Store, error) { // HealthCheck verifies if the database is up and running func (c *ConsulStore) HealthCheck() error { - _, err := c.Read("test", "test", "test") + _, _, err := c.client.Get("test", nil) if err != nil { return pkgerrors.New("[ERROR] Cannot talk to Datastore. Check if it is running/reachable.") } @@ -67,7 +67,13 @@ func (c *ConsulStore) Unmarshal(inp []byte, out interface{}) error { } // Create is used to create a DB entry -func (c *ConsulStore) Create(root, key, tag string, data interface{}) error { +func (c *ConsulStore) Create(root string, key Key, tag string, data interface{}) error { + + //Convert to string as Consul only supports string based keys + k := key.String() + if k == "" { + return pkgerrors.New("Key.String() returned an empty string") + } value, err := Serialize(data) if err != nil { @@ -75,7 +81,7 @@ func (c *ConsulStore) Create(root, key, tag string, data interface{}) error { } p := &api.KVPair{ - Key: key, + Key: k, Value: []byte(value), } _, err = c.client.Put(p, nil) @@ -83,9 +89,16 @@ func (c *ConsulStore) Create(root, key, tag string, data interface{}) error { } // Read method returns the internalID for a particular externalID -func (c *ConsulStore) Read(root, key, tag string) ([]byte, error) { - key = root + "/" + key + "/" + tag - pair, _, err := c.client.Get(key, nil) +func (c *ConsulStore) Read(root string, key Key, tag string) ([]byte, error) { + + //Convert to string as Consul only supports string based keys + k := key.String() + if k == "" { + return nil, pkgerrors.New("Key.String() returned an empty string") + } + + k = root + "/" + k + "/" + tag + pair, _, err := c.client.Get(k, nil) if err != nil { return nil, err } @@ -96,13 +109,19 @@ func (c *ConsulStore) Read(root, key, tag string) ([]byte, error) { } // Delete method removes an internalID from the Database -func (c *ConsulStore) Delete(root, key, tag string) error { - _, err := c.client.Delete(key, nil) +func (c *ConsulStore) Delete(root string, key Key, tag string) error { + + //Convert to string as Consul only supports string based keys + k := key.String() + if k == "" { + return pkgerrors.New("Key.String() returned an empty string") + } + _, err := c.client.Delete(k, nil) return err } // ReadAll is used to get all ExternalIDs in a namespace -func (c *ConsulStore) ReadAll(root, tag string) (map[string][]byte, error) { +func (c *ConsulStore) ReadAll(root string, tag string) (map[string][]byte, error) { pairs, _, err := c.client.List(root, nil) if err != nil { return nil, err -- cgit 1.2.3-korg