diff options
author | Kiran Kamineni <kiran.k.kamineni@intel.com> | 2019-03-15 15:03:01 -0700 |
---|---|---|
committer | Kiran Kamineni <kiran.k.kamineni@intel.com> | 2019-03-25 14:41:34 -0700 |
commit | 037cfda2181e4995e4e2a47db6f1121b532b686b (patch) | |
tree | f9cb838fc5cc037c01a9f55f561c3b5621236667 /src/k8splugin/internal/db/consul.go | |
parent | 8cdd50b6a06aef5cb0541e74a07b10bd4b01b589 (diff) |
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 <kiran.k.kamineni@intel.com>
Diffstat (limited to 'src/k8splugin/internal/db/consul.go')
-rw-r--r-- | src/k8splugin/internal/db/consul.go | 37 |
1 files changed, 28 insertions, 9 deletions
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 |