Add a listallhandler to definitions api 89/91689/2
authorKiran Kamineni <kiran.k.kamineni@intel.com>
Thu, 18 Jul 2019 00:14:07 +0000 (17:14 -0700)
committerKiran Kamineni <kiran.k.kamineni@intel.com>
Thu, 18 Jul 2019 23:24:34 +0000 (16:24 -0700)
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 <kiran.k.kamineni@intel.com>
src/k8splugin/api/api.go
src/k8splugin/api/defhandler.go
src/k8splugin/api/defhandler_test.go
src/k8splugin/internal/rb/definition.go

index 353972a..a7caa19 100644 (file)
@@ -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")
 
index c1110a9..480d4be 100644 (file)
@@ -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) {
index 077d070..dcfea1d 100644 (file)
@@ -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 {
index 6998eed..65ae8e0 100644 (file)
@@ -121,10 +121,13 @@ func (v *DefinitionClient) List(name string) ([]Definition, error) {
                                log.Printf("[Definition] Error Unmarshaling value for: %s", key)
                                continue
                        }
+
                        //Select only the definitions that match name provided
-                       if def.RBName == name {
+                       //If name is empty, return all
+                       if def.RBName == name || name == "" {
                                results = append(results, def)
                        }
+
                }
        }