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/rb/definition.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/rb/definition.go')
-rw-r--r-- | src/k8splugin/internal/rb/definition.go | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/src/k8splugin/internal/rb/definition.go b/src/k8splugin/internal/rb/definition.go index 4eaa9578..2ebbb08a 100644 --- a/src/k8splugin/internal/rb/definition.go +++ b/src/k8splugin/internal/rb/definition.go @@ -49,6 +49,14 @@ type DefinitionManager interface { Upload(resID string, inp []byte) error } +type definitionKey struct { + Key string +} + +func (dk definitionKey) String() string { + return dk.Key +} + // DefinitionClient implements the DefinitionManager // It will also be used to maintain some localized state type DefinitionClient struct { @@ -73,7 +81,7 @@ func (v *DefinitionClient) Create(def Definition) (Definition, error) { if def.UUID == "" { def.UUID, _ = uuid.GenerateUUID() } - key := def.UUID + key := definitionKey{Key: def.UUID} err := db.DBconn.Create(v.storeName, key, v.tagMeta, def) if err != nil { @@ -109,7 +117,8 @@ func (v *DefinitionClient) List() ([]Definition, error) { // Get returns the Resource Bundle Definition for corresponding ID func (v *DefinitionClient) Get(id string) (Definition, error) { - value, err := db.DBconn.Read(v.storeName, id, v.tagMeta) + key := definitionKey{Key: id} + value, err := db.DBconn.Read(v.storeName, key, v.tagMeta) if err != nil { return Definition{}, pkgerrors.Wrap(err, "Get Resource Bundle definition") } @@ -129,13 +138,14 @@ func (v *DefinitionClient) Get(id string) (Definition, error) { // Delete the Resource Bundle definition from database func (v *DefinitionClient) Delete(id string) error { - err := db.DBconn.Delete(v.storeName, id, v.tagMeta) + key := definitionKey{Key: id} + err := db.DBconn.Delete(v.storeName, key, v.tagMeta) if err != nil { return pkgerrors.Wrap(err, "Delete Resource Bundle Definition") } //Delete the content when the delete operation happens - err = db.DBconn.Delete(v.storeName, id, v.tagContent) + err = db.DBconn.Delete(v.storeName, key, v.tagContent) if err != nil { return pkgerrors.Wrap(err, "Delete Resource Bundle Definition Content") } @@ -146,6 +156,7 @@ func (v *DefinitionClient) Delete(id string) error { // Upload the contents of resource bundle into database func (v *DefinitionClient) Upload(id string, inp []byte) error { + key := definitionKey{Key: id} //Check if definition metadata exists def, err := v.Get(id) if err != nil { @@ -192,7 +203,7 @@ func (v *DefinitionClient) Upload(id string, inp []byte) error { //Encode given byte stream to text for storage encodedStr := base64.StdEncoding.EncodeToString(inp) - err = db.DBconn.Create(v.storeName, id, v.tagContent, encodedStr) + err = db.DBconn.Create(v.storeName, key, v.tagContent, encodedStr) if err != nil { return pkgerrors.Errorf("Error uploading data to db: %s", err.Error()) } @@ -205,6 +216,7 @@ func (v *DefinitionClient) Upload(id string, inp []byte) error { // ExtractTarBall code to create the folder structure on disk func (v *DefinitionClient) Download(id string) ([]byte, error) { + key := definitionKey{Key: id} //ignore the returned data here //Check if id is valid _, err := v.Get(id) @@ -212,7 +224,7 @@ func (v *DefinitionClient) Download(id string) ([]byte, error) { return nil, pkgerrors.Errorf("Invalid Definition ID provided: %s", err.Error()) } - value, err := db.DBconn.Read(v.storeName, id, v.tagContent) + value, err := db.DBconn.Read(v.storeName, key, v.tagContent) if err != nil { return nil, pkgerrors.Wrap(err, "Get Resource Bundle definition content") } |