summaryrefslogtreecommitdiffstats
path: root/src/orchestrator/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/orchestrator/api')
-rw-r--r--src/orchestrator/api/api.go26
-rw-r--r--src/orchestrator/api/clusterhandler.go468
-rw-r--r--src/orchestrator/api/clusterhandler_test.go1412
-rw-r--r--src/orchestrator/api/controllerhandler_test.go6
-rw-r--r--src/orchestrator/api/projecthandler_test.go6
5 files changed, 1910 insertions, 8 deletions
diff --git a/src/orchestrator/api/api.go b/src/orchestrator/api/api.go
index f0342433..5e05b31b 100644
--- a/src/orchestrator/api/api.go
+++ b/src/orchestrator/api/api.go
@@ -22,8 +22,7 @@ import (
var moduleClient *moduleLib.Client
// NewRouter creates a router that registers the various urls that are supported
-
-func NewRouter(projectClient moduleLib.ProjectManager, compositeAppClient moduleLib.CompositeAppManager, ControllerClient moduleLib.ControllerManager) *mux.Router {
+func NewRouter(projectClient moduleLib.ProjectManager, compositeAppClient moduleLib.CompositeAppManager, ControllerClient moduleLib.ControllerManager, clusterClient moduleLib.ClusterManager) *mux.Router {
router := mux.NewRouter().PathPrefix("/v2").Subrouter()
moduleClient = moduleLib.NewClient()
@@ -39,6 +38,12 @@ func NewRouter(projectClient moduleLib.ProjectManager, compositeAppClient module
controlHandler := controllerHandler{
client: ControllerClient,
}
+ if clusterClient == nil {
+ clusterClient = moduleClient.Cluster
+ }
+ clusterHandler := clusterHandler{
+ client: clusterClient,
+ }
router.HandleFunc("/projects", projHandler.createHandler).Methods("POST")
router.HandleFunc("/projects/{project-name}", projHandler.getHandler).Methods("GET")
router.HandleFunc("/projects/{project-name}", projHandler.deleteHandler).Methods("DELETE")
@@ -58,5 +63,22 @@ func NewRouter(projectClient moduleLib.ProjectManager, compositeAppClient module
router.HandleFunc("/controllers", controlHandler.createHandler).Methods("PUT")
router.HandleFunc("/controllers/{controller-name}", controlHandler.getHandler).Methods("GET")
router.HandleFunc("/controllers/{controller-name}", controlHandler.deleteHandler).Methods("DELETE")
+ router.HandleFunc("/cluster-providers", clusterHandler.createClusterProviderHandler).Methods("POST")
+ router.HandleFunc("/cluster-providers", clusterHandler.getClusterProviderHandler).Methods("GET")
+ router.HandleFunc("/cluster-providers/{name}", clusterHandler.getClusterProviderHandler).Methods("GET")
+ router.HandleFunc("/cluster-providers/{name}", clusterHandler.deleteClusterProviderHandler).Methods("DELETE")
+ router.HandleFunc("/cluster-providers/{provider-name}/clusters", clusterHandler.createClusterHandler).Methods("POST")
+ router.HandleFunc("/cluster-providers/{provider-name}/clusters", clusterHandler.getClusterHandler).Methods("GET")
+ router.HandleFunc("/cluster-providers/{provider-name}/clusters/{name}", clusterHandler.getClusterHandler).Methods("GET")
+ router.HandleFunc("/cluster-providers/{provider-name}/clusters/{name}", clusterHandler.deleteClusterHandler).Methods("DELETE")
+ router.HandleFunc("/cluster-providers/{provider-name}/clusters/{cluster-name}/labels", clusterHandler.createClusterLabelHandler).Methods("POST")
+ router.HandleFunc("/cluster-providers/{provider-name}/clusters/{cluster-name}/labels", clusterHandler.getClusterLabelHandler).Methods("GET")
+ router.HandleFunc("/cluster-providers/{provider-name}/clusters/{cluster-name}/labels/{label}", clusterHandler.getClusterLabelHandler).Methods("GET")
+ router.HandleFunc("/cluster-providers/{provider-name}/clusters/{cluster-name}/labels/{label}", clusterHandler.deleteClusterLabelHandler).Methods("DELETE")
+ router.HandleFunc("/cluster-providers/{provider-name}/clusters/{cluster-name}/kv-pairs", clusterHandler.createClusterKvPairsHandler).Methods("POST")
+ router.HandleFunc("/cluster-providers/{provider-name}/clusters/{cluster-name}/kv-pairs", clusterHandler.getClusterKvPairsHandler).Methods("GET")
+ router.HandleFunc("/cluster-providers/{provider-name}/clusters/{cluster-name}/kv-pairs/{kvpair}", clusterHandler.getClusterKvPairsHandler).Methods("GET")
+ router.HandleFunc("/cluster-providers/{provider-name}/clusters/{cluster-name}/kv-pairs/{kvpair}", clusterHandler.deleteClusterKvPairsHandler).Methods("DELETE")
+
return router
}
diff --git a/src/orchestrator/api/clusterhandler.go b/src/orchestrator/api/clusterhandler.go
new file mode 100644
index 00000000..7e769f31
--- /dev/null
+++ b/src/orchestrator/api/clusterhandler.go
@@ -0,0 +1,468 @@
+/*
+ * Copyright 2020 Intel Corporation, Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package api
+
+import (
+ "bytes"
+ "encoding/base64"
+ "encoding/json"
+ "io"
+ "io/ioutil"
+ "mime"
+ "mime/multipart"
+ "net/http"
+ "net/textproto"
+
+ moduleLib "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module"
+
+ "github.com/gorilla/mux"
+)
+
+// Used to store backend implementations objects
+// Also simplifies mocking for unit testing purposes
+type clusterHandler struct {
+ // Interface that implements Cluster operations
+ // We will set this variable with a mock interface for testing
+ client moduleLib.ClusterManager
+}
+
+// Create handles creation of the ClusterProvider entry in the database
+func (h clusterHandler) createClusterProviderHandler(w http.ResponseWriter, r *http.Request) {
+ var p moduleLib.ClusterProvider
+
+ err := json.NewDecoder(r.Body).Decode(&p)
+
+ switch {
+ case err == io.EOF:
+ http.Error(w, "Empty body", http.StatusBadRequest)
+ return
+ case err != nil:
+ http.Error(w, err.Error(), http.StatusUnprocessableEntity)
+ return
+ }
+
+ // Name is required.
+ if p.Metadata.Name == "" {
+ http.Error(w, "Missing name in POST request", http.StatusBadRequest)
+ return
+ }
+
+ ret, err := h.client.CreateClusterProvider(p)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusCreated)
+ err = json.NewEncoder(w).Encode(ret)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+}
+
+// Get handles GET operations on a particular ClusterProvider Name
+// Returns a ClusterProvider
+func (h clusterHandler) getClusterProviderHandler(w http.ResponseWriter, r *http.Request) {
+ vars := mux.Vars(r)
+ name := vars["name"]
+ var ret interface{}
+ var err error
+
+ if len(name) == 0 {
+ ret, err = h.client.GetClusterProviders()
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+ } else {
+ ret, err = h.client.GetClusterProvider(name)
+ 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
+ }
+}
+
+// Delete handles DELETE operations on a particular ClusterProvider Name
+func (h clusterHandler) deleteClusterProviderHandler(w http.ResponseWriter, r *http.Request) {
+ vars := mux.Vars(r)
+ name := vars["name"]
+
+ err := h.client.DeleteClusterProvider(name)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ w.WriteHeader(http.StatusNoContent)
+}
+
+// Create handles creation of the Cluster entry in the database
+func (h clusterHandler) createClusterHandler(w http.ResponseWriter, r *http.Request) {
+ vars := mux.Vars(r)
+ provider := vars["provider-name"]
+ var p moduleLib.Cluster
+ var q moduleLib.ClusterContent
+
+ // Implemenation using multipart form
+ // Review and enable/remove at a later date
+ // Set Max size to 16mb here
+ err := r.ParseMultipartForm(16777216)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusUnprocessableEntity)
+ return
+ }
+
+ jsn := bytes.NewBuffer([]byte(r.FormValue("metadata")))
+ err = json.NewDecoder(jsn).Decode(&p)
+ switch {
+ case err == io.EOF:
+ http.Error(w, "Empty body", http.StatusBadRequest)
+ return
+ case err != nil:
+ http.Error(w, err.Error(), http.StatusUnprocessableEntity)
+ return
+ }
+
+ //Read the file section and ignore the header
+ file, _, err := r.FormFile("file")
+ if err != nil {
+ http.Error(w, "Unable to process file", http.StatusUnprocessableEntity)
+ return
+ }
+
+ defer file.Close()
+
+ //Convert the file content to base64 for storage
+ content, err := ioutil.ReadAll(file)
+ if err != nil {
+ http.Error(w, "Unable to read file", http.StatusUnprocessableEntity)
+ return
+ }
+
+ q.Kubeconfig = base64.StdEncoding.EncodeToString(content)
+
+ // Name is required.
+ if p.Metadata.Name == "" {
+ http.Error(w, "Missing name in POST request", http.StatusBadRequest)
+ return
+ }
+
+ ret, err := h.client.CreateCluster(provider, p, q)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ // w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusCreated)
+ err = json.NewEncoder(w).Encode(ret)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+}
+
+// Get handles GET operations on a particular Cluster Name
+// Returns a Cluster
+func (h clusterHandler) getClusterHandler(w http.ResponseWriter, r *http.Request) {
+ vars := mux.Vars(r)
+ provider := vars["provider-name"]
+ name := vars["name"]
+
+ // handle the get all clusters case - return a list of only the json parts
+ if len(name) == 0 {
+ var retList []moduleLib.Cluster
+
+ ret, err := h.client.GetClusters(provider)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ for _, cl := range ret {
+ retList = append(retList, moduleLib.Cluster{Metadata: cl.Metadata})
+ }
+
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+ err = json.NewEncoder(w).Encode(retList)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+ return
+ }
+
+ accepted, _, err := mime.ParseMediaType(r.Header.Get("Accept"))
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusNotAcceptable)
+ return
+ }
+
+ retCluster, err := h.client.GetCluster(provider, name)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ retKubeconfig, err := h.client.GetClusterContent(provider, name)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ switch accepted {
+ case "multipart/form-data":
+ mpw := multipart.NewWriter(w)
+ w.Header().Set("Content-Type", mpw.FormDataContentType())
+ w.WriteHeader(http.StatusOK)
+ pw, err := mpw.CreatePart(textproto.MIMEHeader{"Content-Type": {"application/json"}, "Content-Disposition": {"form-data; name=metadata"}})
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+ if err := json.NewEncoder(pw).Encode(retCluster); err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+ pw, err = mpw.CreatePart(textproto.MIMEHeader{"Content-Type": {"application/octet-stream"}, "Content-Disposition": {"form-data; name=file; filename=kubeconfig"}})
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+ kc_bytes, err := base64.StdEncoding.DecodeString(retKubeconfig.Kubeconfig)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+ _, err = pw.Write(kc_bytes)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+ case "application/json":
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+ err = json.NewEncoder(w).Encode(retCluster)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+ case "application/octet-stream":
+ w.Header().Set("Content-Type", "application/octet-stream")
+ w.WriteHeader(http.StatusOK)
+ kc_bytes, err := base64.StdEncoding.DecodeString(retKubeconfig.Kubeconfig)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+ _, err = w.Write(kc_bytes)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+ default:
+ http.Error(w, "set Accept: multipart/form-data, application/json or application/octet-stream", http.StatusMultipleChoices)
+ return
+ }
+}
+
+// Delete handles DELETE operations on a particular Cluster Name
+func (h clusterHandler) deleteClusterHandler(w http.ResponseWriter, r *http.Request) {
+ vars := mux.Vars(r)
+ provider := vars["provider-name"]
+ name := vars["name"]
+
+ err := h.client.DeleteCluster(provider, name)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ w.WriteHeader(http.StatusNoContent)
+}
+
+// Create handles creation of the ClusterLabel entry in the database
+func (h clusterHandler) createClusterLabelHandler(w http.ResponseWriter, r *http.Request) {
+ vars := mux.Vars(r)
+ provider := vars["provider-name"]
+ cluster := vars["cluster-name"]
+ var p moduleLib.ClusterLabel
+
+ err := json.NewDecoder(r.Body).Decode(&p)
+
+ // LabelName is required.
+ if p.LabelName == "" {
+ http.Error(w, "Missing label name in POST request", http.StatusBadRequest)
+ return
+ }
+
+ ret, err := h.client.CreateClusterLabel(provider, cluster, p)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusCreated)
+ err = json.NewEncoder(w).Encode(ret)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+}
+
+// Get handles GET operations on a particular Cluster Label
+// Returns a ClusterLabel
+func (h clusterHandler) getClusterLabelHandler(w http.ResponseWriter, r *http.Request) {
+ vars := mux.Vars(r)
+ provider := vars["provider-name"]
+ cluster := vars["cluster-name"]
+ label := vars["label"]
+
+ var ret interface{}
+ var err error
+
+ if len(label) == 0 {
+ ret, err = h.client.GetClusterLabels(provider, cluster)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+ } else {
+ ret, err = h.client.GetClusterLabel(provider, cluster, label)
+ 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
+ }
+}
+
+// Delete handles DELETE operations on a particular ClusterLabel Name
+func (h clusterHandler) deleteClusterLabelHandler(w http.ResponseWriter, r *http.Request) {
+ vars := mux.Vars(r)
+ provider := vars["provider-name"]
+ cluster := vars["cluster-name"]
+ label := vars["label"]
+
+ err := h.client.DeleteClusterLabel(provider, cluster, label)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ w.WriteHeader(http.StatusNoContent)
+}
+
+// Create handles creation of the ClusterKvPairs entry in the database
+func (h clusterHandler) createClusterKvPairsHandler(w http.ResponseWriter, r *http.Request) {
+ vars := mux.Vars(r)
+ provider := vars["provider-name"]
+ cluster := vars["cluster-name"]
+ var p moduleLib.ClusterKvPairs
+
+ err := json.NewDecoder(r.Body).Decode(&p)
+
+ // KvPairsName is required.
+ if p.Metadata.Name == "" {
+ http.Error(w, "Missing Key Value pair name in POST request", http.StatusBadRequest)
+ return
+ }
+
+ ret, err := h.client.CreateClusterKvPairs(provider, cluster, p)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusCreated)
+ err = json.NewEncoder(w).Encode(ret)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+}
+
+// Get handles GET operations on a particular Cluster Key Value Pair
+// Returns a ClusterKvPairs
+func (h clusterHandler) getClusterKvPairsHandler(w http.ResponseWriter, r *http.Request) {
+ vars := mux.Vars(r)
+ provider := vars["provider-name"]
+ cluster := vars["cluster-name"]
+ kvpair := vars["kvpair"]
+
+ var ret interface{}
+ var err error
+
+ if len(kvpair) == 0 {
+ ret, err = h.client.GetAllClusterKvPairs(provider, cluster)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+ } else {
+ ret, err = h.client.GetClusterKvPairs(provider, cluster, kvpair)
+ 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
+ }
+}
+
+// Delete handles DELETE operations on a particular Cluster Name
+func (h clusterHandler) deleteClusterKvPairsHandler(w http.ResponseWriter, r *http.Request) {
+ vars := mux.Vars(r)
+ provider := vars["provider-name"]
+ cluster := vars["cluster-name"]
+ kvpair := vars["kvpair"]
+
+ err := h.client.DeleteClusterKvPairs(provider, cluster, kvpair)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ w.WriteHeader(http.StatusNoContent)
+}
diff --git a/src/orchestrator/api/clusterhandler_test.go b/src/orchestrator/api/clusterhandler_test.go
new file mode 100644
index 00000000..db1b6f9f
--- /dev/null
+++ b/src/orchestrator/api/clusterhandler_test.go
@@ -0,0 +1,1412 @@
+/*
+ * Copyright 2020 Intel Corporation, Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package api
+
+import (
+ "bytes"
+ "encoding/json"
+ "io"
+ "mime/multipart"
+ "net/http"
+ "net/http/httptest"
+ "net/textproto"
+ "reflect"
+ "testing"
+
+ moduleLib "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module"
+
+ pkgerrors "github.com/pkg/errors"
+)
+
+//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 mockClusterManager struct {
+ // Items and err will be used to customize each test
+ // via a localized instantiation of mockClusterManager
+ ClusterProviderItems []moduleLib.ClusterProvider
+ ClusterItems []moduleLib.Cluster
+ ClusterContentItems []moduleLib.ClusterContent
+ ClusterLabelItems []moduleLib.ClusterLabel
+ ClusterKvPairsItems []moduleLib.ClusterKvPairs
+ Err error
+}
+
+func (m *mockClusterManager) CreateClusterProvider(inp moduleLib.ClusterProvider) (moduleLib.ClusterProvider, error) {
+ if m.Err != nil {
+ return moduleLib.ClusterProvider{}, m.Err
+ }
+
+ return m.ClusterProviderItems[0], nil
+}
+
+func (m *mockClusterManager) GetClusterProvider(name string) (moduleLib.ClusterProvider, error) {
+ if m.Err != nil {
+ return moduleLib.ClusterProvider{}, m.Err
+ }
+
+ return m.ClusterProviderItems[0], nil
+}
+
+func (m *mockClusterManager) GetClusterProviders() ([]moduleLib.ClusterProvider, error) {
+ if m.Err != nil {
+ return []moduleLib.ClusterProvider{}, m.Err
+ }
+
+ return m.ClusterProviderItems, nil
+}
+
+func (m *mockClusterManager) DeleteClusterProvider(name string) error {
+ return m.Err
+}
+
+func (m *mockClusterManager) CreateCluster(provider string, inp moduleLib.Cluster, inq moduleLib.ClusterContent) (moduleLib.Cluster, error) {
+ if m.Err != nil {
+ return moduleLib.Cluster{}, m.Err
+ }
+
+ return m.ClusterItems[0], nil
+}
+
+func (m *mockClusterManager) GetCluster(provider, name string) (moduleLib.Cluster, error) {
+ if m.Err != nil {
+ return moduleLib.Cluster{}, m.Err
+ }
+
+ return m.ClusterItems[0], nil
+}
+
+func (m *mockClusterManager) GetClusterContent(provider, name string) (moduleLib.ClusterContent, error) {
+ if m.Err != nil {
+ return moduleLib.ClusterContent{}, m.Err
+ }
+
+ return m.ClusterContentItems[0], nil
+}
+
+func (m *mockClusterManager) GetClusters(provider string) ([]moduleLib.Cluster, error) {
+ if m.Err != nil {
+ return []moduleLib.Cluster{}, m.Err
+ }
+
+ return m.ClusterItems, nil
+}
+
+func (m *mockClusterManager) DeleteCluster(provider, name string) error {
+ return m.Err
+}
+
+func (m *mockClusterManager) CreateClusterLabel(provider, cluster string, inp moduleLib.ClusterLabel) (moduleLib.ClusterLabel, error) {
+ if m.Err != nil {
+ return moduleLib.ClusterLabel{}, m.Err
+ }
+
+ return m.ClusterLabelItems[0], nil
+}
+
+func (m *mockClusterManager) GetClusterLabel(provider, cluster, label string) (moduleLib.ClusterLabel, error) {
+ if m.Err != nil {
+ return moduleLib.ClusterLabel{}, m.Err
+ }
+
+ return m.ClusterLabelItems[0], nil
+}
+
+func (m *mockClusterManager) GetClusterLabels(provider, cluster string) ([]moduleLib.ClusterLabel, error) {
+ if m.Err != nil {
+ return []moduleLib.ClusterLabel{}, m.Err
+ }
+
+ return m.ClusterLabelItems, nil
+}
+
+func (m *mockClusterManager) DeleteClusterLabel(provider, cluster, label string) error {
+ return m.Err
+}
+
+func (m *mockClusterManager) CreateClusterKvPairs(provider, cluster string, inp moduleLib.ClusterKvPairs) (moduleLib.ClusterKvPairs, error) {
+ if m.Err != nil {
+ return moduleLib.ClusterKvPairs{}, m.Err
+ }
+
+ return m.ClusterKvPairsItems[0], nil
+}
+
+func (m *mockClusterManager) GetClusterKvPairs(provider, cluster, kvpair string) (moduleLib.ClusterKvPairs, error) {
+ if m.Err != nil {
+ return moduleLib.ClusterKvPairs{}, m.Err
+ }
+
+ return m.ClusterKvPairsItems[0], nil
+}
+
+func (m *mockClusterManager) GetAllClusterKvPairs(provider, cluster string) ([]moduleLib.ClusterKvPairs, error) {
+ if m.Err != nil {
+ return []moduleLib.ClusterKvPairs{}, m.Err
+ }
+
+ return m.ClusterKvPairsItems, nil
+}
+
+func (m *mockClusterManager) DeleteClusterKvPairs(provider, cluster, kvpair string) error {
+ return m.Err
+}
+
+func TestClusterProviderCreateHandler(t *testing.T) {
+ testCases := []struct {
+ label string
+ reader io.Reader
+ expected moduleLib.ClusterProvider
+ expectedCode int
+ clusterClient *mockClusterManager
+ }{
+ {
+ label: "Missing Cluster Provider Body Failure",
+ expectedCode: http.StatusBadRequest,
+ clusterClient: &mockClusterManager{},
+ },
+ {
+ label: "Create Cluster Provider",
+ expectedCode: http.StatusCreated,
+ reader: bytes.NewBuffer([]byte(`{
+ "metadata": {
+ "name": "clusterProviderTest",
+ "description": "testClusterProvider",
+ "userData1": "some user data 1",
+ "userData2": "some user data 2"
+ }
+ }`)),
+ expected: moduleLib.ClusterProvider{
+ Metadata: moduleLib.Metadata{
+ Name: "clusterProviderTest",
+ Description: "testClusterProvider",
+ UserData1: "some user data 1",
+ UserData2: "some user data 2",
+ },
+ },
+ clusterClient: &mockClusterManager{
+ //Items that will be returned by the mocked Client
+ ClusterProviderItems: []moduleLib.ClusterProvider{
+ {
+ Metadata: moduleLib.Metadata{
+ Name: "clusterProviderTest",
+ Description: "testClusterProvider",
+ UserData1: "some user data 1",
+ UserData2: "some user data 2",
+ },
+ },
+ },
+ },
+ },
+ {
+ label: "Missing ClusterProvider Name in Request Body",
+ reader: bytes.NewBuffer([]byte(`{
+ "metadata": {
+ "description": "this is a test cluster provider",
+ "userData1": "some user data 1",
+ "userData2": "some user data 2"
+ }
+ }`)),
+ expectedCode: http.StatusBadRequest,
+ clusterClient: &mockClusterManager{},
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.label, func(t *testing.T) {
+ request := httptest.NewRequest("POST", "/v2/cluster-providers", testCase.reader)
+ resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient))
+
+ //Check returned code
+ if resp.StatusCode != testCase.expectedCode {
+ t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
+ }
+
+ //Check returned body only if statusCreated
+ if resp.StatusCode == http.StatusCreated {
+ got := moduleLib.ClusterProvider{}
+ json.NewDecoder(resp.Body).Decode(&got)
+
+ if reflect.DeepEqual(testCase.expected, got) == false {
+ t.Errorf("createHandler returned unexpected body: got %v;"+
+ " expected %v", got, testCase.expected)
+ }
+ }
+ })
+ }
+}
+
+func TestClusterProviderGetAllHandler(t *testing.T) {
+
+ testCases := []struct {
+ label string
+ expected []moduleLib.ClusterProvider
+ name, version string
+ expectedCode int
+ clusterClient *mockClusterManager
+ }{
+ {
+ label: "Get Cluster Provider",
+ expectedCode: http.StatusOK,
+ expected: []moduleLib.ClusterProvider{
+ {
+ Metadata: moduleLib.Metadata{
+ Name: "testClusterProvider1",
+ Description: "testClusterProvider 1 description",
+ UserData1: "some user data 1",
+ UserData2: "some user data 2",
+ },
+ },
+ {
+ Metadata: moduleLib.Metadata{
+ Name: "testClusterProvider2",
+ Description: "testClusterProvider 2 description",
+ UserData1: "some user data A",
+ UserData2: "some user data B",
+ },
+ },
+ },
+ clusterClient: &mockClusterManager{
+ //Items that will be returned by the mocked Client
+ ClusterProviderItems: []moduleLib.ClusterProvider{
+ {
+ Metadata: moduleLib.Metadata{
+ Name: "testClusterProvider1",
+ Description: "testClusterProvider 1 description",
+ UserData1: "some user data 1",
+ UserData2: "some user data 2",
+ },
+ },
+ {
+ Metadata: moduleLib.Metadata{
+ Name: "testClusterProvider2",
+ Description: "testClusterProvider 2 description",
+ UserData1: "some user data A",
+ UserData2: "some user data B",
+ },
+ },
+ },
+ },
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.label, func(t *testing.T) {
+ request := httptest.NewRequest("GET", "/v2/cluster-providers", nil)
+ resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient))
+
+ //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 := []moduleLib.ClusterProvider{}
+ json.NewDecoder(resp.Body).Decode(&got)
+
+ if reflect.DeepEqual(testCase.expected, got) == false {
+ t.Errorf("listHandler returned unexpected body: got %v;"+
+ " expected %v", got, testCase.expected)
+ }
+ }
+ })
+ }
+}
+
+func TestClusterProviderGetHandler(t *testing.T) {
+
+ testCases := []struct {
+ label string
+ expected moduleLib.ClusterProvider
+ name, version string
+ expectedCode int
+ clusterClient *mockClusterManager
+ }{
+ {
+ label: "Get Cluster Provider",
+ expectedCode: http.StatusOK,
+ expected: moduleLib.ClusterProvider{
+ Metadata: moduleLib.Metadata{
+ Name: "testClusterProvider",
+ Description: "testClusterProvider description",
+ UserData1: "some user data 1",
+ UserData2: "some user data 2",
+ },
+ },
+ name: "testClusterProvider",
+ clusterClient: &mockClusterManager{
+ //Items that will be returned by the mocked Client
+ ClusterProviderItems: []moduleLib.ClusterProvider{
+ {
+ Metadata: moduleLib.Metadata{
+ Name: "testClusterProvider",
+ Description: "testClusterProvider description",
+ UserData1: "some user data 1",
+ UserData2: "some user data 2",
+ },
+ },
+ },
+ },
+ },
+ {
+ label: "Get Non-Existing Cluster Provider",
+ expectedCode: http.StatusInternalServerError,
+ name: "nonexistingclusterprovider",
+ clusterClient: &mockClusterManager{
+ ClusterProviderItems: []moduleLib.ClusterProvider{},
+ Err: pkgerrors.New("Internal Error"),
+ },
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.label, func(t *testing.T) {
+ request := httptest.NewRequest("GET", "/v2/cluster-providers/"+testCase.name, nil)
+ resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient))
+
+ //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 := moduleLib.ClusterProvider{}
+ json.NewDecoder(resp.Body).Decode(&got)
+
+ if reflect.DeepEqual(testCase.expected, got) == false {
+ t.Errorf("listHandler returned unexpected body: got %v;"+
+ " expected %v", got, testCase.expected)
+ }
+ }
+ })
+ }
+}
+
+func TestClusterProviderDeleteHandler(t *testing.T) {
+
+ testCases := []struct {
+ label string
+ name string
+ version string
+ expectedCode int
+ clusterClient *mockClusterManager
+ }{
+ {
+ label: "Delete Cluster Provider",
+ expectedCode: http.StatusNoContent,
+ name: "testClusterProvider",
+ clusterClient: &mockClusterManager{},
+ },
+ {
+ label: "Delete Non-Existing Cluster Provider",
+ expectedCode: http.StatusInternalServerError,
+ name: "testClusterProvider",
+ clusterClient: &mockClusterManager{
+ Err: pkgerrors.New("Internal Error"),
+ },
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.label, func(t *testing.T) {
+ request := httptest.NewRequest("DELETE", "/v2/cluster-providers/"+testCase.name, nil)
+ resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient))
+
+ //Check returned code
+ if resp.StatusCode != testCase.expectedCode {
+ t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
+ }
+ })
+ }
+}
+
+func TestClusterCreateHandler(t *testing.T) {
+ testCases := []struct {
+ label string
+ metadata string
+ kubeconfig string
+ expected moduleLib.Cluster
+ expectedCode int
+ clusterClient *mockClusterManager
+ }{
+ {
+ label: "Missing Cluster Body Failure",
+ expectedCode: http.StatusBadRequest,
+ clusterClient: &mockClusterManager{},
+ },
+ {
+ label: "Create Cluster",
+ expectedCode: http.StatusCreated,
+ metadata: `
+{
+ "metadata": {
+ "name": "clusterTest",
+ "description": "this is test cluster",
+ "userData1": "some cluster data abc",
+ "userData2": "some cluster data def"
+ }
+}`,
+ kubeconfig: `test contents
+of a file attached
+to the creation
+of clusterTest
+`,
+ expected: moduleLib.Cluster{
+ Metadata: moduleLib.Metadata{
+ Name: "clusterTest",
+ Description: "testCluster",
+ UserData1: "some user data 1",
+ UserData2: "some user data 2",
+ },
+ },
+ clusterClient: &mockClusterManager{
+ //Items that will be returned by the mocked Client
+ ClusterProviderItems: []moduleLib.ClusterProvider{
+ {
+ Metadata: moduleLib.Metadata{
+ Name: "clusterProvider1",
+ Description: "ClusterProvider 1 description",
+ UserData1: "some user data 1",
+ UserData2: "some user data 2",
+ },
+ },
+ },
+ ClusterItems: []moduleLib.Cluster{
+ {
+ Metadata: moduleLib.Metadata{
+ Name: "clusterTest",
+ Description: "testCluster",
+ UserData1: "some user data 1",
+ UserData2: "some user data 2",
+ },
+ },
+ },
+ ClusterContentItems: []moduleLib.ClusterContent{
+ {
+ Kubeconfig: "dGVzdCBjb250ZW50cwpvZiBhIGZpbGUgYXR0YWNoZWQKdG8gdGhlIGNyZWF0aW9uCm9mIGNsdXN0ZXJUZXN0Cg==",
+ },
+ },
+ },
+ },
+ {
+ label: "Missing Cluster Name in Request Body",
+ expectedCode: http.StatusBadRequest,
+ metadata: `
+{
+ "metadata": {
+ "description": "this is test cluster",
+ "userData1": "some cluster data abc",
+ "userData2": "some cluster data def"
+ }
+}`,
+ kubeconfig: `test contents
+of a file attached
+to the creation
+of clusterTest
+`,
+ clusterClient: &mockClusterManager{},
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.label, func(t *testing.T) {
+ // Create the multipart test Request body
+ body := new(bytes.Buffer)
+ multiwr := multipart.NewWriter(body)
+ multiwr.SetBoundary("------------------------f77f80a7092eb312")
+ pw, _ := multiwr.CreatePart(textproto.MIMEHeader{"Content-Type": {"application/json"}, "Content-Disposition": {"form-data; name=metadata"}})
+ pw.Write([]byte(testCase.metadata))
+ pw, _ = multiwr.CreateFormFile("file", "kubeconfig")
+ pw.Write([]byte(testCase.kubeconfig))
+ multiwr.Close()
+
+ request := httptest.NewRequest("POST", "/v2/cluster-providers/clusterProvider1/clusters", bytes.NewBuffer(body.Bytes()))
+ request.Header.Set("Content-Type", multiwr.FormDataContentType())
+ resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient))
+
+ //Check returned code
+ if resp.StatusCode != testCase.expectedCode {
+ t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
+ }
+
+ //Check returned body only if statusCreated
+ if resp.StatusCode == http.StatusCreated {
+ got := moduleLib.Cluster{}
+ json.NewDecoder(resp.Body).Decode(&got)
+
+ if reflect.DeepEqual(testCase.expected, got) == false {
+ t.Errorf("createHandler returned unexpected body: got %v;"+
+ " expected %v", got, testCase.expected)
+ }
+ }
+ })
+ }
+}
+
+func TestClusterGetAllHandler(t *testing.T) {
+
+ testCases := []struct {
+ label string
+ expected []moduleLib.Cluster
+ name, version string
+ expectedCode int
+ clusterClient *mockClusterManager
+ }{
+ {
+ label: "Get Clusters",
+ expectedCode: http.StatusOK,
+ expected: []moduleLib.Cluster{
+ {
+ Metadata: moduleLib.Metadata{
+ Name: "testCluster1",
+ Description: "testCluster 1 description",
+ UserData1: "some user data 1",
+ UserData2: "some user data 2",
+ },
+ },
+ {
+ Metadata: moduleLib.Metadata{
+ Name: "testCluster2",
+ Description: "testCluster 2 description",
+ UserData1: "some user data A",
+ UserData2: "some user data B",
+ },
+ },
+ },
+ clusterClient: &mockClusterManager{
+ //Items that will be returned by the mocked Client
+ ClusterItems: []moduleLib.Cluster{
+ {
+ Metadata: moduleLib.Metadata{
+ Name: "testCluster1",
+ Description: "testCluster 1 description",
+ UserData1: "some user data 1",
+ UserData2: "some user data 2",
+ },
+ },
+ {
+ Metadata: moduleLib.Metadata{
+ Name: "testCluster2",
+ Description: "testCluster 2 description",
+ UserData1: "some user data A",
+ UserData2: "some user data B",
+ },
+ },
+ },
+ ClusterContentItems: []moduleLib.ClusterContent{
+ // content here doesn't matter - just needs to be present
+ {
+ Kubeconfig: "dGVzdCBjb250ZW50cwpvZiBhIGZpbGUgYXR0YWNoZWQKdG8gdGhlIGNyZWF0aW9uCm9mIGNsdXN0ZXJUZXN0Cg==",
+ },
+ {
+ Kubeconfig: "dGVzdCBjb250ZW50cwpvZiBhIGZpbGUgYXR0YWNoZWQKdG8gdGhlIGNyZWF0aW9uCm9mIGNsdXN0ZXJUZXN0Cg==",
+ },
+ },
+ },
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.label, func(t *testing.T) {
+ request := httptest.NewRequest("GET", "/v2/cluster-providers/clusterProvder1/clusters", nil)
+ resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient))
+
+ //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 := []moduleLib.Cluster{}
+ json.NewDecoder(resp.Body).Decode(&got)
+
+ if reflect.DeepEqual(testCase.expected, got) == false {
+ t.Errorf("listHandler returned unexpected body: got %v;"+
+ " expected %v", got, testCase.expected)
+ }
+ }
+ })
+ }
+}
+
+func TestClusterGetHandler(t *testing.T) {
+
+ testCases := []struct {
+ label string
+ expected moduleLib.Cluster
+ name, version string
+ accept string
+ expectedCode int
+ clusterClient *mockClusterManager
+ }{
+ {
+ label: "Get Cluster with Accept: application/json",
+ accept: "application/json",
+ expectedCode: http.StatusOK,
+ expected: moduleLib.Cluster{
+ Metadata: moduleLib.Metadata{
+ Name: "testCluster",
+ Description: "testCluster description",
+ UserData1: "some user data 1",
+ UserData2: "some user data 2",
+ },
+ },
+ name: "testCluster",
+ clusterClient: &mockClusterManager{
+ //Items that will be returned by the mocked Client
+ ClusterItems: []moduleLib.Cluster{
+ {
+ Metadata: moduleLib.Metadata{
+ Name: "testCluster",
+ Description: "testCluster description",
+ UserData1: "some user data 1",
+ UserData2: "some user data 2",
+ },
+ },
+ },
+ ClusterContentItems: []moduleLib.ClusterContent{
+ {
+ Kubeconfig: "dGVzdCBjb250ZW50cwpvZiBhIGZpbGUgYXR0YWNoZWQKdG8gdGhlIGNyZWF0aW9uCm9mIGNsdXN0ZXJUZXN0Cg==",
+ },
+ },
+ },
+ },
+ {
+ label: "Get Non-Existing Cluster",
+ accept: "application/json",
+ expectedCode: http.StatusInternalServerError,
+ name: "nonexistingcluster",
+ clusterClient: &mockClusterManager{
+ ClusterItems: []moduleLib.Cluster{},
+ Err: pkgerrors.New("Internal Error"),
+ },
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.label, func(t *testing.T) {
+ request := httptest.NewRequest("GET", "/v2/cluster-providers/clusterProvider1/clusters/"+testCase.name, nil)
+ if len(testCase.accept) > 0 {
+ request.Header.Set("Accept", testCase.accept)
+ }
+ resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient))
+
+ //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 := moduleLib.Cluster{}
+ json.NewDecoder(resp.Body).Decode(&got)
+
+ if reflect.DeepEqual(testCase.expected, got) == false {
+ t.Errorf("listHandler returned unexpected body: got %v;"+
+ " expected %v", got, testCase.expected)
+ }
+ }
+ })
+ }
+}
+
+func TestClusterGetContentHandler(t *testing.T) {
+
+ testCases := []struct {
+ label string
+ expected string
+ name, version string
+ accept string
+ expectedCode int
+ clusterClient *mockClusterManager
+ }{
+ {
+ label: "Get Cluster Content with Accept: application/octet-stream",
+ accept: "application/octet-stream",
+ expectedCode: http.StatusOK,
+ expected: `test contents
+of a file attached
+to the creation
+of clusterTest
+`,
+ name: "testCluster",
+ clusterClient: &mockClusterManager{
+ //Items that will be returned by the mocked Client
+ ClusterItems: []moduleLib.Cluster{
+ {
+ Metadata: moduleLib.Metadata{
+ Name: "testCluster",
+ Description: "testCluster description",
+ UserData1: "some user data 1",
+ UserData2: "some user data 2",
+ },
+ },
+ },
+ ClusterContentItems: []moduleLib.ClusterContent{
+ {
+ Kubeconfig: "dGVzdCBjb250ZW50cwpvZiBhIGZpbGUgYXR0YWNoZWQKdG8gdGhlIGNyZWF0aW9uCm9mIGNsdXN0ZXJUZXN0Cg==",
+ },
+ },
+ },
+ },
+ {
+ label: "Get Non-Existing Cluster",
+ accept: "application/octet-stream",
+ expectedCode: http.StatusInternalServerError,
+ name: "nonexistingcluster",
+ clusterClient: &mockClusterManager{
+ ClusterItems: []moduleLib.Cluster{},
+ Err: pkgerrors.New("Internal Error"),
+ },
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.label, func(t *testing.T) {
+ request := httptest.NewRequest("GET", "/v2/cluster-providers/clusterProvider1/clusters/"+testCase.name, nil)
+ if len(testCase.accept) > 0 {
+ request.Header.Set("Accept", testCase.accept)
+ }
+ resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient))
+
+ //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 {
+ body := new(bytes.Buffer)
+ body.ReadFrom(resp.Body)
+ got := body.String()
+
+ if reflect.DeepEqual(testCase.expected, got) == false {
+ t.Errorf("listHandler returned unexpected body: got %v;"+
+ " expected %v", got, testCase.expected)
+ }
+ }
+ })
+ }
+}
+
+func TestClusterDeleteHandler(t *testing.T) {
+
+ testCases := []struct {
+ label string
+ name string
+ version string
+ expectedCode int
+ clusterClient *mockClusterManager
+ }{
+ {
+ label: "Delete Cluster",
+ expectedCode: http.StatusNoContent,
+ name: "testCluster",
+ clusterClient: &mockClusterManager{},
+ },
+ {
+ label: "Delete Non-Existing Cluster",
+ expectedCode: http.StatusInternalServerError,
+ name: "testCluster",
+ clusterClient: &mockClusterManager{
+ Err: pkgerrors.New("Internal Error"),
+ },
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.label, func(t *testing.T) {
+ request := httptest.NewRequest("DELETE", "/v2/cluster-providers/clusterProvider1/clusters/"+testCase.name, nil)
+ resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient))
+
+ //Check returned code
+ if resp.StatusCode != testCase.expectedCode {
+ t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
+ }
+ })
+ }
+}
+
+func TestClusterLabelCreateHandler(t *testing.T) {
+ testCases := []struct {
+ label string
+ reader io.Reader
+ expected moduleLib.ClusterLabel
+ expectedCode int
+ clusterClient *mockClusterManager
+ }{
+ {
+ label: "Missing Cluster Label Body Failure",
+ expectedCode: http.StatusBadRequest,
+ clusterClient: &mockClusterManager{},
+ },
+ {
+ label: "Create Cluster Label",
+ expectedCode: http.StatusCreated,
+ reader: bytes.NewBuffer([]byte(`{
+ "label-name": "test-label"
+ }`)),
+ expected: moduleLib.ClusterLabel{
+ LabelName: "test-label",
+ },
+ clusterClient: &mockClusterManager{
+ //Items that will be returned by the mocked Client
+ ClusterLabelItems: []moduleLib.ClusterLabel{
+ {
+ LabelName: "test-label",
+ },
+ },
+ },
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.label, func(t *testing.T) {
+ request := httptest.NewRequest("POST", "/v2/cluster-providers/cp1/clusters/cl1/labels", testCase.reader)
+ resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient))
+
+ //Check returned code
+ if resp.StatusCode != testCase.expectedCode {
+ t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
+ }
+
+ //Check returned body only if statusCreated
+ if resp.StatusCode == http.StatusCreated {
+ got := moduleLib.ClusterLabel{}
+ json.NewDecoder(resp.Body).Decode(&got)
+
+ if reflect.DeepEqual(testCase.expected, got) == false {
+ t.Errorf("createHandler returned unexpected body: got %v;"+
+ " expected %v", got, testCase.expected)
+ }
+ }
+ })
+ }
+}
+
+func TestClusterLabelsGetHandler(t *testing.T) {
+
+ testCases := []struct {
+ label string
+ expected []moduleLib.ClusterLabel
+ name, version string
+ expectedCode int
+ clusterClient *mockClusterManager
+ }{
+ {
+ label: "Get Cluster Labels",
+ expectedCode: http.StatusOK,
+ expected: []moduleLib.ClusterLabel{
+ {
+ LabelName: "test-label1",
+ },
+ {
+ LabelName: "test-label-two",
+ },
+ {
+ LabelName: "test-label-3",
+ },
+ },
+ clusterClient: &mockClusterManager{
+ //Items that will be returned by the mocked Client
+ ClusterLabelItems: []moduleLib.ClusterLabel{
+ {
+ LabelName: "test-label1",
+ },
+ {
+ LabelName: "test-label-two",
+ },
+ {
+ LabelName: "test-label-3",
+ },
+ },
+ },
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.label, func(t *testing.T) {
+ request := httptest.NewRequest("GET", "/v2/cluster-providers/cp1/clusters/cl1/labels", nil)
+ resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient))
+
+ //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 := []moduleLib.ClusterLabel{}
+ json.NewDecoder(resp.Body).Decode(&got)
+
+ if reflect.DeepEqual(testCase.expected, got) == false {
+ t.Errorf("listHandler returned unexpected body: got %v;"+
+ " expected %v", got, testCase.expected)
+ }
+ }
+ })
+ }
+}
+
+func TestClusterLabelGetHandler(t *testing.T) {
+
+ testCases := []struct {
+ label string
+ expected moduleLib.ClusterLabel
+ name, version string
+ expectedCode int
+ clusterClient *mockClusterManager
+ }{
+ {
+ label: "Get Cluster Label",
+ expectedCode: http.StatusOK,
+ expected: moduleLib.ClusterLabel{
+ LabelName: "testlabel",
+ },
+ name: "testlabel",
+ clusterClient: &mockClusterManager{
+ //Items that will be returned by the mocked Client
+ ClusterLabelItems: []moduleLib.ClusterLabel{
+ {
+ LabelName: "testlabel",
+ },
+ },
+ },
+ },
+ {
+ label: "Get Non-Existing Cluster Label",
+ expectedCode: http.StatusInternalServerError,
+ name: "nonexistingclusterlabel",
+ clusterClient: &mockClusterManager{
+ ClusterLabelItems: []moduleLib.ClusterLabel{},
+ Err: pkgerrors.New("Internal Error"),
+ },
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.label, func(t *testing.T) {
+ request := httptest.NewRequest("GET", "/v2/cluster-providers/clusterProvider1/clusters/cl1/labels/"+testCase.name, nil)
+ resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient))
+
+ //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 := moduleLib.ClusterLabel{}
+ json.NewDecoder(resp.Body).Decode(&got)
+
+ if reflect.DeepEqual(testCase.expected, got) == false {
+ t.Errorf("listHandler returned unexpected body: got %v;"+
+ " expected %v", got, testCase.expected)
+ }
+ }
+ })
+ }
+}
+
+func TestClusterLabelDeleteHandler(t *testing.T) {
+
+ testCases := []struct {
+ label string
+ name string
+ version string
+ expectedCode int
+ clusterClient *mockClusterManager
+ }{
+ {
+ label: "Delete Cluster Label",
+ expectedCode: http.StatusNoContent,
+ name: "testClusterLabel",
+ clusterClient: &mockClusterManager{},
+ },
+ {
+ label: "Delete Non-Existing Cluster Label",
+ expectedCode: http.StatusInternalServerError,
+ name: "testClusterLabel",
+ clusterClient: &mockClusterManager{
+ Err: pkgerrors.New("Internal Error"),
+ },
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.label, func(t *testing.T) {
+ request := httptest.NewRequest("DELETE", "/v2/cluster-providers/cp1/clusters/cl1/labels/"+testCase.name, nil)
+ resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient))
+
+ //Check returned code
+ if resp.StatusCode != testCase.expectedCode {
+ t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
+ }
+ })
+ }
+}
+
+func TestClusterKvPairsCreateHandler(t *testing.T) {
+ testCases := []struct {
+ label string
+ reader io.Reader
+ expected moduleLib.ClusterKvPairs
+ expectedCode int
+ clusterClient *mockClusterManager
+ }{
+ {
+ label: "Missing Cluster KvPairs Body Failure",
+ expectedCode: http.StatusBadRequest,
+ clusterClient: &mockClusterManager{},
+ },
+ {
+ label: "Create Cluster KvPairs",
+ expectedCode: http.StatusCreated,
+ reader: bytes.NewBuffer([]byte(`{
+ "metadata": {
+ "name": "ClusterKvPair1",
+ "description": "test cluster kv pairs",
+ "userData1": "some user data 1",
+ "userData2": "some user data 2"
+ },
+ "spec": {
+ "kv": [
+ {
+ "key1": "value1"
+ },
+ {
+ "key2": "value2"
+ }
+ ]
+ }
+ }`)),
+ expected: moduleLib.ClusterKvPairs{
+ Metadata: moduleLib.Metadata{
+ Name: "ClusterKvPair1",
+ Description: "test cluster kv pairs",
+ UserData1: "some user data 1",
+ UserData2: "some user data 2",
+ },
+ Spec: moduleLib.ClusterKvSpec{
+ Kv: []map[string]interface{}{
+ {
+ "key1": "value1",
+ },
+ {
+ "key2": "value2",
+ },
+ },
+ },
+ },
+ clusterClient: &mockClusterManager{
+ //Items that will be returned by the mocked Client
+ ClusterKvPairsItems: []moduleLib.ClusterKvPairs{
+ {
+ Metadata: moduleLib.Metadata{
+ Name: "ClusterKvPair1",
+ Description: "test cluster kv pairs",
+ UserData1: "some user data 1",
+ UserData2: "some user data 2",
+ },
+ Spec: moduleLib.ClusterKvSpec{
+ Kv: []map[string]interface{}{
+ {
+ "key1": "value1",
+ },
+ {
+ "key2": "value2",
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.label, func(t *testing.T) {
+ request := httptest.NewRequest("POST", "/v2/cluster-providers/cp1/clusters/cl1/kv-pairs", testCase.reader)
+ resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient))
+
+ //Check returned code
+ if resp.StatusCode != testCase.expectedCode {
+ t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
+ }
+
+ //Check returned body only if statusCreated
+ if resp.StatusCode == http.StatusCreated {
+ got := moduleLib.ClusterKvPairs{}
+ json.NewDecoder(resp.Body).Decode(&got)
+
+ if reflect.DeepEqual(testCase.expected, got) == false {
+ t.Errorf("createHandler returned unexpected body: got %v;"+
+ " expected %v", got, testCase.expected)
+ }
+ }
+ })
+ }
+}
+
+func TestClusterKvPairsGetAllHandler(t *testing.T) {
+
+ testCases := []struct {
+ label string
+ expected []moduleLib.ClusterKvPairs
+ name, version string
+ expectedCode int
+ clusterClient *mockClusterManager
+ }{
+ {
+ label: "Get Cluster KvPairs",
+ expectedCode: http.StatusOK,
+ expected: []moduleLib.ClusterKvPairs{
+ {
+ Metadata: moduleLib.Metadata{
+ Name: "ClusterKvPair1",
+ Description: "test cluster kv pairs",
+ UserData1: "some user data 1",
+ UserData2: "some user data 2",
+ },
+ Spec: moduleLib.ClusterKvSpec{
+ Kv: []map[string]interface{}{
+ {
+ "key1": "value1",
+ },
+ {
+ "key2": "value2",
+ },
+ },
+ },
+ },
+ {
+ Metadata: moduleLib.Metadata{
+ Name: "ClusterKvPair2",
+ Description: "test cluster kv pairs",
+ UserData1: "some user data A",
+ UserData2: "some user data B",
+ },
+ Spec: moduleLib.ClusterKvSpec{
+ Kv: []map[string]interface{}{
+ {
+ "keyA": "valueA",
+ },
+ {
+ "keyB": "valueB",
+ },
+ },
+ },
+ },
+ },
+ clusterClient: &mockClusterManager{
+ //Items that will be returned by the mocked Client
+ ClusterKvPairsItems: []moduleLib.ClusterKvPairs{
+ {
+ Metadata: moduleLib.Metadata{
+ Name: "ClusterKvPair1",
+ Description: "test cluster kv pairs",
+ UserData1: "some user data 1",
+ UserData2: "some user data 2",
+ },
+ Spec: moduleLib.ClusterKvSpec{
+ Kv: []map[string]interface{}{
+ {
+ "key1": "value1",
+ },
+ {
+ "key2": "value2",
+ },
+ },
+ },
+ },
+ {
+ Metadata: moduleLib.Metadata{
+ Name: "ClusterKvPair2",
+ Description: "test cluster kv pairs",
+ UserData1: "some user data A",
+ UserData2: "some user data B",
+ },
+ Spec: moduleLib.ClusterKvSpec{
+ Kv: []map[string]interface{}{
+ {
+ "keyA": "valueA",
+ },
+ {
+ "keyB": "valueB",
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.label, func(t *testing.T) {
+ request := httptest.NewRequest("GET", "/v2/cluster-providers/cp1/clusters/cl1/kv-pairs", nil)
+ resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient))
+
+ //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 := []moduleLib.ClusterKvPairs{}
+ json.NewDecoder(resp.Body).Decode(&got)
+
+ if reflect.DeepEqual(testCase.expected, got) == false {
+ t.Errorf("listHandler returned unexpected body: got %v;"+
+ " expected %v", got, testCase.expected)
+ }
+ }
+ })
+ }
+}
+
+func TestClusterKvPairsGetHandler(t *testing.T) {
+
+ testCases := []struct {
+ label string
+ expected moduleLib.ClusterKvPairs
+ name, version string
+ expectedCode int
+ clusterClient *mockClusterManager
+ }{
+ {
+ label: "Get Cluster KV Pairs",
+ expectedCode: http.StatusOK,
+ expected: moduleLib.ClusterKvPairs{
+ Metadata: moduleLib.Metadata{
+ Name: "ClusterKvPair2",
+ Description: "test cluster kv pairs",
+ UserData1: "some user data A",
+ UserData2: "some user data B",
+ },
+ Spec: moduleLib.ClusterKvSpec{
+ Kv: []map[string]interface{}{
+ {
+ "keyA": "valueA",
+ },
+ {
+ "keyB": "valueB",
+ },
+ },
+ },
+ },
+ name: "ClusterKvPair2",
+ clusterClient: &mockClusterManager{
+ //Items that will be returned by the mocked Client
+ ClusterKvPairsItems: []moduleLib.ClusterKvPairs{
+ {
+ Metadata: moduleLib.Metadata{
+ Name: "ClusterKvPair2",
+ Description: "test cluster kv pairs",
+ UserData1: "some user data A",
+ UserData2: "some user data B",
+ },
+ Spec: moduleLib.ClusterKvSpec{
+ Kv: []map[string]interface{}{
+ {
+ "keyA": "valueA",
+ },
+ {
+ "keyB": "valueB",
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ {
+ label: "Get Non-Existing Cluster KV Pairs",
+ expectedCode: http.StatusInternalServerError,
+ name: "nonexistingclusterkvpairs",
+ clusterClient: &mockClusterManager{
+ ClusterKvPairsItems: []moduleLib.ClusterKvPairs{},
+ Err: pkgerrors.New("Internal Error"),
+ },
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.label, func(t *testing.T) {
+ request := httptest.NewRequest("GET", "/v2/cluster-providers/clusterProvider1/clusters/cl1/kv-pairs/"+testCase.name, nil)
+ resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient))
+
+ //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 := moduleLib.ClusterKvPairs{}
+ json.NewDecoder(resp.Body).Decode(&got)
+
+ if reflect.DeepEqual(testCase.expected, got) == false {
+ t.Errorf("listHandler returned unexpected body: got %v;"+
+ " expected %v", got, testCase.expected)
+ }
+ }
+ })
+ }
+}
+
+func TestClusterKvPairsDeleteHandler(t *testing.T) {
+
+ testCases := []struct {
+ label string
+ name string
+ version string
+ expectedCode int
+ clusterClient *mockClusterManager
+ }{
+ {
+ label: "Delete Cluster KV Pairs",
+ expectedCode: http.StatusNoContent,
+ name: "testClusterKvPairs",
+ clusterClient: &mockClusterManager{},
+ },
+ {
+ label: "Delete Non-Existing Cluster KV Pairs",
+ expectedCode: http.StatusInternalServerError,
+ name: "testClusterKvPairs",
+ clusterClient: &mockClusterManager{
+ Err: pkgerrors.New("Internal Error"),
+ },
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.label, func(t *testing.T) {
+ request := httptest.NewRequest("DELETE", "/v2/cluster-providers/cp1/clusters/cl1/kv-pairs/"+testCase.name, nil)
+ resp := executeRequest(request, NewRouter(nil, nil, nil, testCase.clusterClient))
+
+ //Check returned code
+ if resp.StatusCode != testCase.expectedCode {
+ t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
+ }
+ })
+ }
+}
diff --git a/src/orchestrator/api/controllerhandler_test.go b/src/orchestrator/api/controllerhandler_test.go
index f0804107..c91943ad 100644
--- a/src/orchestrator/api/controllerhandler_test.go
+++ b/src/orchestrator/api/controllerhandler_test.go
@@ -110,7 +110,7 @@ func TestControllerCreateHandler(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.label, func(t *testing.T) {
request := httptest.NewRequest("POST", "/v2/controllers", testCase.reader)
- resp := executeRequest(request, NewRouter(nil, nil, testCase.controllerClient))
+ resp := executeRequest(request, NewRouter(nil, nil, testCase.controllerClient, nil))
//Check returned code
if resp.StatusCode != testCase.expectedCode {
@@ -173,7 +173,7 @@ func TestControllerGetHandler(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.label, func(t *testing.T) {
request := httptest.NewRequest("GET", "/v2/controllers/"+testCase.name, nil)
- resp := executeRequest(request, NewRouter(nil, nil, testCase.controllerClient))
+ resp := executeRequest(request, NewRouter(nil, nil, testCase.controllerClient, nil))
//Check returned code
if resp.StatusCode != testCase.expectedCode {
@@ -222,7 +222,7 @@ func TestControllerDeleteHandler(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.label, func(t *testing.T) {
request := httptest.NewRequest("DELETE", "/v2/controllers/"+testCase.name, nil)
- resp := executeRequest(request, NewRouter(nil, nil, testCase.controllerClient))
+ resp := executeRequest(request, NewRouter(nil, nil, testCase.controllerClient, nil))
//Check returned code
if resp.StatusCode != testCase.expectedCode {
diff --git a/src/orchestrator/api/projecthandler_test.go b/src/orchestrator/api/projecthandler_test.go
index 1e273349..c6da4e05 100644
--- a/src/orchestrator/api/projecthandler_test.go
+++ b/src/orchestrator/api/projecthandler_test.go
@@ -119,7 +119,7 @@ func TestProjectCreateHandler(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.label, func(t *testing.T) {
request := httptest.NewRequest("POST", "/v2/projects", testCase.reader)
- resp := executeRequest(request, NewRouter(testCase.projectClient, nil, nil))
+ resp := executeRequest(request, NewRouter(testCase.projectClient, nil, nil, nil))
//Check returned code
if resp.StatusCode != testCase.expectedCode {
@@ -188,7 +188,7 @@ func TestProjectGetHandler(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.label, func(t *testing.T) {
request := httptest.NewRequest("GET", "/v2/projects/"+testCase.name, nil)
- resp := executeRequest(request, NewRouter(testCase.projectClient, nil, nil))
+ resp := executeRequest(request, NewRouter(testCase.projectClient, nil, nil, nil))
//Check returned code
if resp.StatusCode != testCase.expectedCode {
@@ -237,7 +237,7 @@ func TestProjectDeleteHandler(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.label, func(t *testing.T) {
request := httptest.NewRequest("DELETE", "/v2/projects/"+testCase.name, nil)
- resp := executeRequest(request, NewRouter(testCase.projectClient, nil, nil))
+ resp := executeRequest(request, NewRouter(testCase.projectClient, nil, nil, nil))
//Check returned code
if resp.StatusCode != testCase.expectedCode {