aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBin Yang <bin.yang@windriver.com>2019-07-23 07:31:04 +0000
committerGerrit Code Review <gerrit@onap.org>2019-07-23 07:31:04 +0000
commitbcbedee7c3a086d20d802265db3b2b26c359794d (patch)
treee2505e71370257c19bfe375b06971495f4185c9a
parent86ecc9397f67092f780c1c178e1305aa4621953b (diff)
parentb8ecedd40b6be727233bf620a0aa5592f98161e7 (diff)
Merge "Add a listallhandler to definitions api"
-rw-r--r--src/k8splugin/api/api.go1
-rw-r--r--src/k8splugin/api/defhandler.go22
-rw-r--r--src/k8splugin/api/defhandler_test.go80
-rw-r--r--src/k8splugin/internal/rb/definition.go5
4 files changed, 106 insertions, 2 deletions
diff --git a/src/k8splugin/api/api.go b/src/k8splugin/api/api.go
index ea7b5b26..4308db4f 100644
--- a/src/k8splugin/api/api.go
+++ b/src/k8splugin/api/api.go
@@ -70,6 +70,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 {
diff --git a/src/k8splugin/internal/rb/definition.go b/src/k8splugin/internal/rb/definition.go
index 6998eedf..65ae8e00 100644
--- a/src/k8splugin/internal/rb/definition.go
+++ b/src/k8splugin/internal/rb/definition.go
@@ -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)
}
+
}
}