diff options
Diffstat (limited to 'src/k8splugin/api')
-rw-r--r-- | src/k8splugin/api/api.go | 16 | ||||
-rw-r--r-- | src/k8splugin/api/defhandler.go (renamed from src/k8splugin/api/vnfdhandler.go) | 69 | ||||
-rw-r--r-- | src/k8splugin/api/defhandler_test.go (renamed from src/k8splugin/api/vnfdhandler_test.go) | 201 | ||||
-rw-r--r-- | src/k8splugin/api/handler.go | 55 | ||||
-rw-r--r-- | src/k8splugin/api/handler_test.go | 101 |
5 files changed, 232 insertions, 210 deletions
diff --git a/src/k8splugin/api/api.go b/src/k8splugin/api/api.go index 46afadd6..06f5009f 100644 --- a/src/k8splugin/api/api.go +++ b/src/k8splugin/api/api.go @@ -14,7 +14,7 @@ limitations under the License. package api import ( - "k8splugin/vnfd" + "k8splugin/rb" "os" "path/filepath" "plugin" @@ -106,13 +106,13 @@ func NewRouter(kubeconfig string) *mux.Router { vnfInstanceHandler.HandleFunc("/{cloudRegionID}/{namespace}/{externalVNFID}", DeleteHandler).Methods("DELETE") vnfInstanceHandler.HandleFunc("/{cloudRegionID}/{namespace}/{externalVNFID}", GetHandler).Methods("GET") - vnfdRouter := router.PathPrefix("/v1/vnfd").Subrouter() - vh := vnfdHandler{vnfdClient: vnfd.GetVNFDClient()} - vnfdRouter.HandleFunc("", vh.vnfdCreateHandler).Methods("POST") - vnfdRouter.HandleFunc("/{vnfdID}/upload", vh.vnfdUploadHandler).Methods("POST") - vnfdRouter.HandleFunc("", vh.vnfdListHandler).Methods("GET") - vnfdRouter.HandleFunc("/{vnfdID}", vh.vnfdGetHandler).Methods("GET") - vnfdRouter.HandleFunc("/{vnfdID}", vh.vnfdDeleteHandler).Methods("DELETE") + resRouter := router.PathPrefix("/v1/rb").Subrouter() + rbdef := rbDefinitionHandler{client: rb.NewDefinitionClient()} + resRouter.HandleFunc("/definition", rbdef.createHandler).Methods("POST") + resRouter.HandleFunc("/definition/{rbdID}/content", rbdef.uploadHandler).Methods("POST") + resRouter.HandleFunc("/definition", rbdef.listHandler).Methods("GET") + resRouter.HandleFunc("/definition/{rbdID}", rbdef.getHandler).Methods("GET") + resRouter.HandleFunc("/definition/{rbdID}", rbdef.deleteHandler).Methods("DELETE") // (TODO): Fix update method // vnfInstanceHandler.HandleFunc("/{vnfInstanceId}", UpdateHandler).Methods("PUT") diff --git a/src/k8splugin/api/vnfdhandler.go b/src/k8splugin/api/defhandler.go index ff777826..222baaee 100644 --- a/src/k8splugin/api/vnfdhandler.go +++ b/src/k8splugin/api/defhandler.go @@ -18,24 +18,24 @@ package api import ( "encoding/json" + "io/ioutil" + "k8splugin/rb" "net/http" - "k8splugin/vnfd" - "github.com/gorilla/mux" ) // Used to store backend implementations objects // Also simplifies mocking for unit testing purposes -type vnfdHandler struct { - // Interface that implements vnfDefinition operations +type rbDefinitionHandler struct { + // Interface that implements bundle Definition operations // We will set this variable with a mock interface for testing - vnfdClient vnfd.VNFDefinitionInterface + client rb.DefinitionManager } -// vnfdCreateHandler handles creation of the vnfd entry in the database -func (h vnfdHandler) vnfdCreateHandler(w http.ResponseWriter, r *http.Request) { - var v vnfd.VNFDefinition +// createHandler handles creation of the definition entry in the database +func (h rbDefinitionHandler) createHandler(w http.ResponseWriter, r *http.Request) { + var v rb.Definition if r.Body == nil { http.Error(w, "Empty body", http.StatusBadRequest) @@ -54,7 +54,7 @@ func (h vnfdHandler) vnfdCreateHandler(w http.ResponseWriter, r *http.Request) { return } - ret, err := h.vnfdClient.Create(v) + ret, err := h.client.Create(v) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -69,15 +69,36 @@ func (h vnfdHandler) vnfdCreateHandler(w http.ResponseWriter, r *http.Request) { } } -// vnfdUploadHandler handles upload of the vnf tar file into the database +// uploadHandler handles upload of the bundle tar file into the database // Note: This will be implemented in a different patch -func (h vnfdHandler) vnfdUploadHandler(w http.ResponseWriter, r *http.Request) { +func (h rbDefinitionHandler) uploadHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + uuid := vars["rbdID"] + + if r.Body == nil { + http.Error(w, "Empty Body", http.StatusBadRequest) + return + } + + inpBytes, err := ioutil.ReadAll(r.Body) + if err != nil { + http.Error(w, "Unable to read body", http.StatusBadRequest) + return + } + + err = h.client.Upload(uuid, inpBytes) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusOK) } -// vnfdListHandler handles GET (list) operations on the /v1/vnfd endpoint -// Returns a list of vnfd.VNFDefinitions -func (h vnfdHandler) vnfdListHandler(w http.ResponseWriter, r *http.Request) { - ret, err := h.vnfdClient.List() +// listHandler handles GET (list) operations on the endpoint +// Returns a list of rb.Definitions +func (h rbDefinitionHandler) listHandler(w http.ResponseWriter, r *http.Request) { + ret, err := h.client.List() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -92,13 +113,13 @@ func (h vnfdHandler) vnfdListHandler(w http.ResponseWriter, r *http.Request) { } } -// vnfdGetHandler handles GET operations on a particular VNFID -// Returns a vnfd.VNFDefinition -func (h vnfdHandler) vnfdGetHandler(w http.ResponseWriter, r *http.Request) { +// getHandler handles GET operations on a particular ids +// Returns a rb.Definition +func (h rbDefinitionHandler) getHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - vnfdID := vars["vnfdID"] + id := vars["rbdID"] - ret, err := h.vnfdClient.Get(vnfdID) + ret, err := h.client.Get(id) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -113,12 +134,12 @@ func (h vnfdHandler) vnfdGetHandler(w http.ResponseWriter, r *http.Request) { } } -// vnfdDeleteHandler handles DELETE operations on a particular VNFID -func (h vnfdHandler) vnfdDeleteHandler(w http.ResponseWriter, r *http.Request) { +// deleteHandler handles DELETE operations on a particular bundle definition id +func (h rbDefinitionHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - vnfdID := vars["vnfdID"] + id := vars["rbdID"] - err := h.vnfdClient.Delete(vnfdID) + err := h.client.Delete(id) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/src/k8splugin/api/vnfdhandler_test.go b/src/k8splugin/api/defhandler_test.go index e393be6f..9739ab12 100644 --- a/src/k8splugin/api/vnfdhandler_test.go +++ b/src/k8splugin/api/defhandler_test.go @@ -20,7 +20,7 @@ import ( "bytes" "encoding/json" "io" - "k8splugin/vnfd" + "k8splugin/rb" "net/http" "net/http/httptest" "reflect" @@ -32,54 +32,58 @@ import ( //Creating an embedded interface via anonymous variable //This allows us to make mockDB satisfy the DatabaseConnection //interface even if we are not implementing all the methods in it -type mockVNFDefinition struct { - vnfd.VNFDefinitionInterface +type mockRBDefinition struct { + rb.DefinitionManager // Items and err will be used to customize each test - // via a localized instantiation of mockVNFDefinition - Items []vnfd.VNFDefinition + // via a localized instantiation of mockRBDefinition + Items []rb.Definition Err error } -func (m *mockVNFDefinition) Create(inp vnfd.VNFDefinition) (vnfd.VNFDefinition, error) { +func (m *mockRBDefinition) Create(inp rb.Definition) (rb.Definition, error) { if m.Err != nil { - return vnfd.VNFDefinition{}, m.Err + return rb.Definition{}, m.Err } return m.Items[0], nil } -func (m *mockVNFDefinition) List() ([]vnfd.VNFDefinition, error) { +func (m *mockRBDefinition) List() ([]rb.Definition, error) { if m.Err != nil { - return []vnfd.VNFDefinition{}, m.Err + return []rb.Definition{}, m.Err } return m.Items, nil } -func (m *mockVNFDefinition) Get(vnfID string) (vnfd.VNFDefinition, error) { +func (m *mockRBDefinition) Get(id string) (rb.Definition, error) { if m.Err != nil { - return vnfd.VNFDefinition{}, m.Err + return rb.Definition{}, m.Err } return m.Items[0], nil } -func (m *mockVNFDefinition) Delete(vnfID string) error { +func (m *mockRBDefinition) Delete(id string) error { return m.Err } -func TestVnfdCreateHandler(t *testing.T) { +func (m *mockRBDefinition) Upload(id string, inp []byte) error { + return m.Err +} + +func TestRBDefCreateHandler(t *testing.T) { testCases := []struct { label string reader io.Reader - expected vnfd.VNFDefinition + expected rb.Definition expectedCode int - vnfdClient *mockVNFDefinition + rbDefClient *mockRBDefinition }{ { label: "Missing Body Failure", expectedCode: http.StatusBadRequest, - vnfdClient: &mockVNFDefinition{}, + rbDefClient: &mockRBDefinition{}, }, { label: "Create without UUID", @@ -89,18 +93,18 @@ func TestVnfdCreateHandler(t *testing.T) { "description":"test description", "service-type":"firewall" }`)), - expected: vnfd.VNFDefinition{ + expected: rb.Definition{ UUID: "123e4567-e89b-12d3-a456-426655440000", - Name: "testvnf", + Name: "testresourcebundle", Description: "test description", ServiceType: "firewall", }, - vnfdClient: &mockVNFDefinition{ + rbDefClient: &mockRBDefinition{ //Items that will be returned by the mocked Client - Items: []vnfd.VNFDefinition{ + Items: []rb.Definition{ { UUID: "123e4567-e89b-12d3-a456-426655440000", - Name: "testvnf", + Name: "testresourcebundle", Description: "test description", ServiceType: "firewall", }, @@ -111,15 +115,15 @@ func TestVnfdCreateHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { - vh := vnfdHandler{vnfdClient: testCase.vnfdClient} - req, err := http.NewRequest("POST", "/v1/vnfd", testCase.reader) + vh := rbDefinitionHandler{client: testCase.rbDefClient} + req, err := http.NewRequest("POST", "/v1/resource/definition", testCase.reader) if err != nil { t.Fatal(err) } rr := httptest.NewRecorder() - hr := http.HandlerFunc(vh.vnfdCreateHandler) + hr := http.HandlerFunc(vh.createHandler) hr.ServeHTTP(rr, req) //Check returned code @@ -129,11 +133,11 @@ func TestVnfdCreateHandler(t *testing.T) { //Check returned body only if statusCreated if rr.Code == http.StatusCreated { - got := vnfd.VNFDefinition{} + got := rb.Definition{} json.NewDecoder(rr.Body).Decode(&got) if reflect.DeepEqual(testCase.expected, got) == false { - t.Errorf("vnfdCreateHandler returned unexpected body: got %v;"+ + t.Errorf("createHandler returned unexpected body: got %v;"+ " expected %v", got, testCase.expected) } } @@ -141,43 +145,43 @@ func TestVnfdCreateHandler(t *testing.T) { } } -func TestVnfdListHandler(t *testing.T) { +func TestRBDefListHandler(t *testing.T) { testCases := []struct { label string - expected []vnfd.VNFDefinition + expected []rb.Definition expectedCode int - vnfdClient *mockVNFDefinition + rbDefClient *mockRBDefinition }{ { - label: "List VNF Definitions", + label: "List Bundle Definitions", expectedCode: http.StatusOK, - expected: []vnfd.VNFDefinition{ + expected: []rb.Definition{ { UUID: "123e4567-e89b-12d3-a456-426655440000", - Name: "testvnf", + Name: "testresourcebundle", Description: "test description", ServiceType: "firewall", }, { UUID: "123e4567-e89b-12d3-a456-426655441111", - Name: "testvnf2", + Name: "testresourcebundle2", Description: "test description", ServiceType: "dns", }, }, - vnfdClient: &mockVNFDefinition{ + rbDefClient: &mockRBDefinition{ // list of definitions that will be returned by the mockclient - Items: []vnfd.VNFDefinition{ + Items: []rb.Definition{ { UUID: "123e4567-e89b-12d3-a456-426655440000", - Name: "testvnf", + Name: "testresourcebundle", Description: "test description", ServiceType: "firewall", }, { UUID: "123e4567-e89b-12d3-a456-426655441111", - Name: "testvnf2", + Name: "testresourcebundle2", Description: "test description", ServiceType: "dns", }, @@ -188,14 +192,14 @@ func TestVnfdListHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { - vh := vnfdHandler{vnfdClient: testCase.vnfdClient} - req, err := http.NewRequest("GET", "/v1/vnfd", nil) + vh := rbDefinitionHandler{client: testCase.rbDefClient} + req, err := http.NewRequest("GET", "/v1/resource/definition", nil) if err != nil { t.Fatal(err) } rr := httptest.NewRecorder() - hr := http.HandlerFunc(vh.vnfdListHandler) + hr := http.HandlerFunc(vh.listHandler) hr.ServeHTTP(rr, req) //Check returned code @@ -205,11 +209,11 @@ func TestVnfdListHandler(t *testing.T) { //Check returned body only if statusOK if rr.Code == http.StatusOK { - got := []vnfd.VNFDefinition{} + got := []rb.Definition{} json.NewDecoder(rr.Body).Decode(&got) if reflect.DeepEqual(testCase.expected, got) == false { - t.Errorf("vnfdListHandler returned unexpected body: got %v;"+ + t.Errorf("listHandler returned unexpected body: got %v;"+ " expected %v", got, testCase.expected) } } @@ -217,31 +221,31 @@ func TestVnfdListHandler(t *testing.T) { } } -func TestVnfdGetHandler(t *testing.T) { +func TestRBDefGetHandler(t *testing.T) { testCases := []struct { label string - expected vnfd.VNFDefinition + expected rb.Definition inpUUID string expectedCode int - vnfdClient *mockVNFDefinition + rbDefClient *mockRBDefinition }{ { - label: "Get VNF Definition", + label: "Get Bundle Definition", expectedCode: http.StatusOK, - expected: vnfd.VNFDefinition{ + expected: rb.Definition{ UUID: "123e4567-e89b-12d3-a456-426655441111", - Name: "testvnf2", + Name: "testresourcebundle2", Description: "test description", ServiceType: "dns", }, inpUUID: "123e4567-e89b-12d3-a456-426655441111", - vnfdClient: &mockVNFDefinition{ + rbDefClient: &mockRBDefinition{ // list of definitions that will be returned by the mockclient - Items: []vnfd.VNFDefinition{ + Items: []rb.Definition{ { UUID: "123e4567-e89b-12d3-a456-426655441111", - Name: "testvnf2", + Name: "testresourcebundle2", Description: "test description", ServiceType: "dns", }, @@ -249,12 +253,12 @@ func TestVnfdGetHandler(t *testing.T) { }, }, { - label: "Get Non-Exiting VNF Definition", + label: "Get Non-Exiting Bundle Definition", expectedCode: http.StatusInternalServerError, inpUUID: "123e4567-e89b-12d3-a456-426655440000", - vnfdClient: &mockVNFDefinition{ + rbDefClient: &mockRBDefinition{ // list of definitions that will be returned by the mockclient - Items: []vnfd.VNFDefinition{}, + Items: []rb.Definition{}, Err: pkgerrors.New("Internal Error"), }, }, @@ -262,14 +266,14 @@ func TestVnfdGetHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { - vh := vnfdHandler{vnfdClient: testCase.vnfdClient} - req, err := http.NewRequest("GET", "/v1/vnfd/"+testCase.inpUUID, nil) + vh := rbDefinitionHandler{client: testCase.rbDefClient} + req, err := http.NewRequest("GET", "/v1/resource/definition/"+testCase.inpUUID, nil) if err != nil { t.Fatal(err) } rr := httptest.NewRecorder() - hr := http.HandlerFunc(vh.vnfdGetHandler) + hr := http.HandlerFunc(vh.getHandler) hr.ServeHTTP(rr, req) //Check returned code @@ -279,11 +283,11 @@ func TestVnfdGetHandler(t *testing.T) { //Check returned body only if statusOK if rr.Code == http.StatusOK { - got := vnfd.VNFDefinition{} + got := rb.Definition{} json.NewDecoder(rr.Body).Decode(&got) if reflect.DeepEqual(testCase.expected, got) == false { - t.Errorf("vnfdListHandler returned unexpected body: got %v;"+ + t.Errorf("listHandler returned unexpected body: got %v;"+ " expected %v", got, testCase.expected) } } @@ -291,25 +295,25 @@ func TestVnfdGetHandler(t *testing.T) { } } -func TestVnfdDeleteHandler(t *testing.T) { +func TestRBDefDeleteHandler(t *testing.T) { testCases := []struct { label string inpUUID string expectedCode int - vnfdClient *mockVNFDefinition + rbDefClient *mockRBDefinition }{ { - label: "Delete VNF Definition", + label: "Delete Bundle Definition", expectedCode: http.StatusNoContent, inpUUID: "123e4567-e89b-12d3-a456-426655441111", - vnfdClient: &mockVNFDefinition{}, + rbDefClient: &mockRBDefinition{}, }, { - label: "Delete Non-Exiting VNF Definition", + label: "Delete Non-Exiting Bundle Definition", expectedCode: http.StatusInternalServerError, inpUUID: "123e4567-e89b-12d3-a456-426655440000", - vnfdClient: &mockVNFDefinition{ + rbDefClient: &mockRBDefinition{ Err: pkgerrors.New("Internal Error"), }, }, @@ -317,14 +321,75 @@ func TestVnfdDeleteHandler(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { - vh := vnfdHandler{vnfdClient: testCase.vnfdClient} - req, err := http.NewRequest("GET", "/v1/vnfd/"+testCase.inpUUID, nil) + vh := rbDefinitionHandler{client: testCase.rbDefClient} + req, err := http.NewRequest("GET", "/v1/resource/definition/"+testCase.inpUUID, nil) + if err != nil { + t.Fatal(err) + } + + rr := httptest.NewRecorder() + hr := http.HandlerFunc(vh.deleteHandler) + + hr.ServeHTTP(rr, req) + //Check returned code + if rr.Code != testCase.expectedCode { + t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, rr.Code) + } + }) + } +} + +func TestRBDefUploadHandler(t *testing.T) { + + testCases := []struct { + label string + inpUUID string + body io.Reader + expectedCode int + rbDefClient *mockRBDefinition + }{ + { + label: "Upload Bundle Definition Content", + expectedCode: http.StatusOK, + inpUUID: "123e4567-e89b-12d3-a456-426655441111", + body: bytes.NewBuffer([]byte{ + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0xff, 0xf2, 0x48, 0xcd, + }), + rbDefClient: &mockRBDefinition{}, + }, + { + label: "Upload Invalid Bundle Definition Content", + expectedCode: http.StatusInternalServerError, + inpUUID: "123e4567-e89b-12d3-a456-426655440000", + body: bytes.NewBuffer([]byte{ + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0xff, 0xf2, 0x48, 0xcd, + }), + rbDefClient: &mockRBDefinition{ + Err: pkgerrors.New("Internal Error"), + }, + }, + { + label: "Upload Empty Body Content", + expectedCode: http.StatusBadRequest, + inpUUID: "123e4567-e89b-12d3-a456-426655440000", + rbDefClient: &mockRBDefinition{}, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + vh := rbDefinitionHandler{client: testCase.rbDefClient} + req, err := http.NewRequest("POST", + "/v1/resource/definition/"+testCase.inpUUID+"/content", testCase.body) + if err != nil { t.Fatal(err) } rr := httptest.NewRecorder() - hr := http.HandlerFunc(vh.vnfdDeleteHandler) + hr := http.HandlerFunc(vh.uploadHandler) hr.ServeHTTP(rr, req) //Check returned code diff --git a/src/k8splugin/api/handler.go b/src/k8splugin/api/handler.go index 53fa2317..4c49ba78 100644 --- a/src/k8splugin/api/handler.go +++ b/src/k8splugin/api/handler.go @@ -30,6 +30,10 @@ import ( "k8splugin/krd" ) +//TODO: Separate the http handler code and backend code out +var storeName = "rbinst" +var tagData = "data" + // GetVNFClient retrieves the client used to communicate with a Kubernetes Cluster var GetVNFClient = func(kubeConfigPath string) (kubernetes.Clientset, error) { client, err := krd.GetKubeClient(kubeConfigPath) @@ -117,17 +121,9 @@ func CreateHandler(w http.ResponseWriter, r *http.Request) { // TODO: Uncomment when annotations are done // krd.AddNetworkAnnotationsToPod(kubeData, resource.Networks) - // "{"deployment":<>,"service":<>}" - serializedResourceNameMap, err := db.Serialize(resourceNameMap) - if err != nil { - werr := pkgerrors.Wrap(err, "Create VNF deployment JSON Marshalling error") - http.Error(w, werr.Error(), http.StatusInternalServerError) - return - } - // key: cloud1-default-uuid // value: "{"deployment":<>,"service":<>}" - err = db.DBconn.Create(internalVNFID, serializedResourceNameMap) + err = db.DBconn.Create(storeName, internalVNFID, tagData, resourceNameMap) if err != nil { werr := pkgerrors.Wrap(err, "Create VNF deployment DB error") http.Error(w, werr.Error(), http.StatusInternalServerError) @@ -154,27 +150,22 @@ func ListHandler(w http.ResponseWriter, r *http.Request) { namespace := vars["namespace"] prefix := cloudRegionID + "-" + namespace - internalVNFIDs, err := db.DBconn.ReadAll(prefix) + res, err := db.DBconn.ReadAll(storeName, tagData) if err != nil { http.Error(w, pkgerrors.Wrap(err, "Get VNF list error").Error(), http.StatusInternalServerError) return } - if len(internalVNFIDs) == 0 { - w.WriteHeader(http.StatusNotFound) - return - } - // TODO: There is an edge case where if namespace is passed but is missing some characters // trailing, it will print the result with those excluding characters. This is because of // the way I am trimming the Prefix. This fix is needed. var editedList []string - for _, id := range internalVNFIDs { - if len(id) > 0 { - editedList = append(editedList, strings.TrimPrefix(id, prefix)[1:]) + for key, value := range res { + if len(value) > 0 { + editedList = append(editedList, strings.TrimPrefix(key, prefix)[1:]) } } @@ -204,25 +195,20 @@ func DeleteHandler(w http.ResponseWriter, r *http.Request) { // key: cloud1-default-uuid // value: "{"deployment":<>,"service":<>}" - serializedResourceNameMap, err := db.DBconn.Read(internalVNFID) + res, err := db.DBconn.Read(storeName, internalVNFID, tagData) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } - if serializedResourceNameMap == "" { - w.WriteHeader(http.StatusNotFound) - return - } - /* { "deployment": ["cloud1-default-uuid-sisedeploy1", "cloud1-default-uuid-sisedeploy2", ... ] "service": ["cloud1-default-uuid-sisesvc1", "cloud1-default-uuid-sisesvc2", ... ] }, */ - deserializedResourceNameMap := make(map[string][]string) - err = db.DeSerialize(serializedResourceNameMap, &deserializedResourceNameMap) + data := make(map[string][]string) + err = db.DBconn.Unmarshal(res, &data) if err != nil { werr := pkgerrors.Wrap(err, "Unmarshal VNF error") http.Error(w, werr.Error(), http.StatusInternalServerError) @@ -237,14 +223,14 @@ func DeleteHandler(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) return } - err = csar.DestroyVNF(deserializedResourceNameMap, namespace, &kubeclient) + err = csar.DestroyVNF(data, namespace, &kubeclient) if err != nil { werr := pkgerrors.Wrap(err, "Delete VNF error") http.Error(w, werr.Error(), http.StatusInternalServerError) return } - err = db.DBconn.Delete(internalVNFID) + err = db.DBconn.Delete(storeName, internalVNFID, tagData) if err != nil { werr := pkgerrors.Wrap(err, "Delete VNF db record error") http.Error(w, werr.Error(), http.StatusInternalServerError) @@ -337,25 +323,20 @@ func GetHandler(w http.ResponseWriter, r *http.Request) { // key: cloud1-default-uuid // value: "{"deployment":<>,"service":<>}" - serializedResourceNameMap, err := db.DBconn.Read(internalVNFID) + res, err := db.DBconn.Read(storeName, internalVNFID, tagData) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } - if serializedResourceNameMap == "" { - w.WriteHeader(http.StatusNotFound) - return - } - /* { "deployment": ["cloud1-default-uuid-sisedeploy1", "cloud1-default-uuid-sisedeploy2", ... ] "service": ["cloud1-default-uuid-sisesvc1", "cloud1-default-uuid-sisesvc2", ... ] }, */ - deserializedResourceNameMap := make(map[string][]string) - err = db.DeSerialize(serializedResourceNameMap, &deserializedResourceNameMap) + data := make(map[string][]string) + err = db.DBconn.Unmarshal(res, &data) if err != nil { werr := pkgerrors.Wrap(err, "Unmarshal VNF error") http.Error(w, werr.Error(), http.StatusInternalServerError) @@ -366,7 +347,7 @@ func GetHandler(w http.ResponseWriter, r *http.Request) { VNFID: externalVNFID, CloudRegionID: cloudRegionID, Namespace: namespace, - VNFComponents: deserializedResourceNameMap, + VNFComponents: data, } w.Header().Set("Content-Type", "application/json") diff --git a/src/k8splugin/api/handler_test.go b/src/k8splugin/api/handler_test.go index 3336bbc2..a3aeff7a 100644 --- a/src/k8splugin/api/handler_test.go +++ b/src/k8splugin/api/handler_test.go @@ -24,7 +24,6 @@ import ( "reflect" "testing" - "github.com/hashicorp/consul/api" pkgerrors "github.com/pkg/errors" "k8s.io/client-go/kubernetes" @@ -194,37 +193,18 @@ func TestListHandler(t *testing.T) { }, }, { - label: "Get result from DB non-records", - expectedCode: http.StatusNotFound, - mockStore: &db.MockDB{}, - }, - { label: "Get empty list", expectedCode: http.StatusOK, expectedResponse: []string{""}, - mockStore: &db.MockDB{ - Items: api.KVPairs{ - &api.KVPair{ - Key: "", - Value: []byte("{}"), - }, - }, - }, + mockStore: &db.MockDB{}, }, { label: "Succesful get a list of VNF", expectedCode: http.StatusOK, - expectedResponse: []string{"uid1", "uid2"}, + expectedResponse: []string{"uid1"}, mockStore: &db.MockDB{ - Items: api.KVPairs{ - &api.KVPair{ - Key: "uuid1", - Value: []byte("{}"), - }, - &api.KVPair{ - Key: "uuid2", - Value: []byte("{}"), - }, + Items: map[string][]byte{ + "uuid1": []byte("{}"), }, }, }, @@ -275,20 +255,17 @@ func TestDeleteHandler(t *testing.T) { }, { label: "Fail to find VNF record be deleted", - expectedCode: http.StatusNotFound, + expectedCode: http.StatusInternalServerError, mockStore: &db.MockDB{ - Items: api.KVPairs{}, + Items: map[string][]byte{}, }, }, { label: "Fail to unmarshal the DB record", expectedCode: http.StatusInternalServerError, mockStore: &db.MockDB{ - Items: api.KVPairs{ - &api.KVPair{ - Key: "cloudregion1-testnamespace-uuid1", - Value: []byte("{invalid format}"), - }, + Items: map[string][]byte{ + "cloudregion1-testnamespace-uuid1": []byte("{invalid format}"), }, }, }, @@ -297,14 +274,10 @@ func TestDeleteHandler(t *testing.T) { expectedCode: http.StatusInternalServerError, mockGetVNFClientErr: pkgerrors.New("Get VNF client error"), mockStore: &db.MockDB{ - Items: api.KVPairs{ - &api.KVPair{ - Key: "cloudregion1-testnamespace-uuid1", - Value: []byte("{" + - "\"deployment\": [\"deploy1\", \"deploy2\"]," + - "\"service\": [\"svc1\", \"svc2\"]" + - "}"), - }, + Items: map[string][]byte{ + "cloudregion1-testnamespace-uuid1": []byte( + "{\"deployment\": [\"deploy1\", \"deploy2\"]," + + "\"service\": [\"svc1\", \"svc2\"]}"), }, }, }, @@ -312,14 +285,10 @@ func TestDeleteHandler(t *testing.T) { label: "Fail to destroy VNF", expectedCode: http.StatusInternalServerError, mockStore: &db.MockDB{ - Items: api.KVPairs{ - &api.KVPair{ - Key: "cloudregion1-testnamespace-uuid1", - Value: []byte("{" + - "\"deployment\": [\"deploy1\", \"deploy2\"]," + - "\"service\": [\"svc1\", \"svc2\"]" + - "}"), - }, + Items: map[string][]byte{ + "cloudregion1-testnamespace-uuid1": []byte( + "{\"deployment\": [\"deploy1\", \"deploy2\"]," + + "\"service\": [\"svc1\", \"svc2\"]}"), }, }, mockDeleteVNF: &mockCSAR{ @@ -330,14 +299,10 @@ func TestDeleteHandler(t *testing.T) { label: "Succesful delete a VNF", expectedCode: http.StatusAccepted, mockStore: &db.MockDB{ - Items: api.KVPairs{ - &api.KVPair{ - Key: "cloudregion1-testnamespace-uuid1", - Value: []byte("{" + - "\"deployment\": [\"deploy1\", \"deploy2\"]," + - "\"service\": [\"svc1\", \"svc2\"]" + - "}"), - }, + Items: map[string][]byte{ + "cloudregion1-testnamespace-uuid1": []byte( + "{\"deployment\": [\"deploy1\", \"deploy2\"]," + + "\"service\": [\"svc1\", \"svc2\"]}"), }, }, mockDeleteVNF: &mockCSAR{}, @@ -440,18 +405,15 @@ func TestGetHandler(t *testing.T) { }, { label: "Not found DB record", - expectedCode: http.StatusNotFound, + expectedCode: http.StatusInternalServerError, mockStore: &db.MockDB{}, }, { label: "Fail to unmarshal the DB record", expectedCode: http.StatusInternalServerError, mockStore: &db.MockDB{ - Items: api.KVPairs{ - &api.KVPair{ - Key: "cloud1-default-1", - Value: []byte("{invalid-format}"), - }, + Items: map[string][]byte{ + "cloud1-default-1": []byte("{invalid-format}"), }, }, }, @@ -468,18 +430,11 @@ func TestGetHandler(t *testing.T) { }, }, mockStore: &db.MockDB{ - Items: api.KVPairs{ - &api.KVPair{ - Key: "cloud1-default-1", - Value: []byte("{" + - "\"deployment\": [\"deploy1\", \"deploy2\"]," + - "\"service\": [\"svc1\", \"svc2\"]" + - "}"), - }, - &api.KVPair{ - Key: "cloud1-default-2", - Value: []byte("{}"), - }, + Items: map[string][]byte{ + "cloud1-default-1": []byte( + "{\"deployment\": [\"deploy1\", \"deploy2\"]," + + "\"service\": [\"svc1\", \"svc2\"]}"), + "cloud1-default-2": []byte("{}"), }, }, }, |