From f54fee24c32465478e2c6fc4cd6fc6bfb44fe096 Mon Sep 17 00:00:00 2001 From: Kiran Kamineni Date: Tue, 20 Nov 2018 14:32:50 -0800 Subject: Add upload backend implementation Upload is a seperate API where it takes a binary stream and stores it. The api supports tar.gz file format only. P2: Check if ID is valid before trying upload Add test with an invalid ID Issue-ID: MULTICLOUD-393 Change-Id: Id636a95823a046e1795d3be72d0214e953a8c5fc Signed-off-by: Kiran Kamineni --- src/k8splugin/rb/definition.go | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'src/k8splugin/rb/definition.go') diff --git a/src/k8splugin/rb/definition.go b/src/k8splugin/rb/definition.go index 03fffdda..2d1c2cd0 100644 --- a/src/k8splugin/rb/definition.go +++ b/src/k8splugin/rb/definition.go @@ -17,6 +17,8 @@ package rb import ( + "bytes" + "encoding/base64" "k8splugin/db" "log" @@ -39,6 +41,7 @@ type DefinitionManager interface { List() ([]Definition, error) Get(resID string) (Definition, error) Delete(resID string) error + Upload(resID string, inp []byte) error } // DefinitionClient implements the DefinitionManager @@ -63,7 +66,7 @@ func (v *DefinitionClient) Create(def Definition) (Definition, error) { } key := v.keyPrefix + def.UUID - serData, err := db.Serialize(v) + serData, err := db.Serialize(def) if err != nil { return Definition{}, pkgerrors.Wrap(err, "Serialize Resource Bundle Definition") } @@ -133,3 +136,27 @@ func (v *DefinitionClient) Delete(id string) error { return nil } + +// Upload the contents of resource bundle into database +func (v *DefinitionClient) Upload(id string, inp []byte) error { + + //ignore the returned data here. + _, err := v.Get(id) + if err != nil { + return pkgerrors.Errorf("Invalid ID provided %s", err.Error()) + } + + err = isTarGz(bytes.NewBuffer(inp)) + if err != nil { + return pkgerrors.Errorf("Error in file format %s", err.Error()) + } + + encodedStr := base64.StdEncoding.EncodeToString(inp) + key := v.keyPrefix + id + "/content" + err = db.DBconn.Create(key, encodedStr) + if err != nil { + return pkgerrors.Errorf("Error uploading data to db %s", err.Error()) + } + + return nil +} -- cgit 1.2.3-korg