From b8ecedd40b6be727233bf620a0aa5592f98161e7 Mon Sep 17 00:00:00 2001 From: Kiran Kamineni Date: Wed, 17 Jul 2019 17:14:07 -0700 Subject: Add a listallhandler to definitions api GET calls to the v1/rb/definition will return all definitions and their versions Issue-ID: MULTICLOUD-715 Change-Id: Ia0951ac83283830e475bf727e7d5ced7aab3add2 Signed-off-by: Kiran Kamineni --- src/k8splugin/api/api.go | 1 + src/k8splugin/api/defhandler.go | 22 +++++++++- src/k8splugin/api/defhandler_test.go | 80 ++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 1 deletion(-) (limited to 'src/k8splugin/api') diff --git a/src/k8splugin/api/api.go b/src/k8splugin/api/api.go index 353972a1..a7caa19b 100644 --- a/src/k8splugin/api/api.go +++ b/src/k8splugin/api/api.go @@ -69,6 +69,7 @@ func NewRouter(defClient rb.DefinitionManager, resRouter.HandleFunc("/definition", defHandler.createHandler).Methods("POST") resRouter.HandleFunc("/definition/{rbname}/{rbversion}/content", defHandler.uploadHandler).Methods("POST") resRouter.HandleFunc("/definition/{rbname}", defHandler.listVersionsHandler).Methods("GET") + resRouter.HandleFunc("/definition", defHandler.listAllHandler).Methods("GET") resRouter.HandleFunc("/definition/{rbname}/{rbversion}", defHandler.getHandler).Methods("GET") resRouter.HandleFunc("/definition/{rbname}/{rbversion}", defHandler.deleteHandler).Methods("DELETE") diff --git a/src/k8splugin/api/defhandler.go b/src/k8splugin/api/defhandler.go index c1110a97..480d4be5 100644 --- a/src/k8splugin/api/defhandler.go +++ b/src/k8splugin/api/defhandler.go @@ -20,9 +20,10 @@ import ( "encoding/json" "io" "io/ioutil" - "github.com/onap/multicloud-k8s/src/k8splugin/internal/rb" "net/http" + "github.com/onap/multicloud-k8s/src/k8splugin/internal/rb" + "github.com/gorilla/mux" ) @@ -122,6 +123,25 @@ func (h rbDefinitionHandler) listVersionsHandler(w http.ResponseWriter, r *http. } } +// listVersionsHandler handles GET (list) operations on the endpoint +// Returns a list of rb.Definitions +func (h rbDefinitionHandler) listAllHandler(w http.ResponseWriter, r *http.Request) { + + ret, err := h.client.List("") + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + err = json.NewEncoder(w).Encode(ret) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} + // getHandler handles GET operations on a particular ids // Returns a rb.Definition func (h rbDefinitionHandler) getHandler(w http.ResponseWriter, r *http.Request) { diff --git a/src/k8splugin/api/defhandler_test.go b/src/k8splugin/api/defhandler_test.go index 077d0708..dcfea1de 100644 --- a/src/k8splugin/api/defhandler_test.go +++ b/src/k8splugin/api/defhandler_test.go @@ -240,6 +240,86 @@ func TestRBDefListVersionsHandler(t *testing.T) { } } +func TestRBDefListAllHandler(t *testing.T) { + + testCases := []struct { + label string + expected []rb.Definition + expectedCode int + rbDefClient *mockRBDefinition + }{ + { + label: "List Bundle Definitions", + expectedCode: http.StatusOK, + expected: []rb.Definition{ + { + RBName: "resourcebundle1", + RBVersion: "v1", + ChartName: "barchart", + Description: "test description for one", + }, + { + RBName: "resourcebundle2", + RBVersion: "version2", + ChartName: "foochart", + Description: "test description for two", + }, + }, + rbDefClient: &mockRBDefinition{ + // list of definitions that will be returned by the mockclient + Items: []rb.Definition{ + { + RBName: "resourcebundle1", + RBVersion: "v1", + ChartName: "barchart", + Description: "test description for one", + }, + { + RBName: "resourcebundle2", + RBVersion: "version2", + ChartName: "foochart", + Description: "test description for two", + }, + }, + }, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + request := httptest.NewRequest("GET", "/v1/rb/definition", nil) + resp := executeRequest(request, NewRouter(testCase.rbDefClient, nil, nil, nil, nil, nil)) + + //Check returned code + if resp.StatusCode != testCase.expectedCode { + t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode) + } + + //Check returned body only if statusOK + if resp.StatusCode == http.StatusOK { + got := []rb.Definition{} + json.NewDecoder(resp.Body).Decode(&got) + + // Since the order of returned slice is not guaranteed + // Check both and return error if both don't match + sort.Slice(got, func(i, j int) bool { + return got[i].RBVersion < got[j].RBVersion + }) + // Sort both as it is not expected that testCase.expected + // is sorted + sort.Slice(testCase.expected, func(i, j int) bool { + return testCase.expected[i].RBVersion < testCase.expected[j].RBVersion + }) + + if reflect.DeepEqual(testCase.expected, got) == false { + t.Errorf("listHandler returned unexpected body: got %v;"+ + " expected %v", got, testCase.expected) + } + } + }) + } +} + func TestRBDefGetHandler(t *testing.T) { testCases := []struct { -- cgit 1.2.3-korg