diff options
author | Eric Multanen <eric.w.multanen@intel.com> | 2020-05-27 11:21:42 -0700 |
---|---|---|
committer | Eric Multanen <eric.w.multanen@intel.com> | 2020-06-03 17:15:58 -0700 |
commit | 8e7a20777f2100324526a34b4b6c943b512df0fb (patch) | |
tree | eca4847a8361da6f3f97a363f8d65d8a3c6a08b2 /src/ncm | |
parent | 0c22bafd470e36647157b6d01221cb17841f360a (diff) |
Move cluster management into its own microservice
Split out the cluster provider and cluster APIs
and packages into a separate microservice to align
with the architecture.
Issue-ID: MULTICLOUD-1029
Signed-off-by: Eric Multanen <eric.w.multanen@intel.com>
Change-Id: I08f357b5a488004a2389b72a178ae33e101d1540
Diffstat (limited to 'src/ncm')
-rw-r--r-- | src/ncm/api/api.go | 29 | ||||
-rw-r--r-- | src/ncm/api/clusterhandler.go | 485 | ||||
-rw-r--r-- | src/ncm/api/clusterhandler_test.go | 1432 | ||||
-rw-r--r-- | src/ncm/go.mod | 10 | ||||
-rw-r--r-- | src/ncm/pkg/cluster/cluster.go | 602 | ||||
-rw-r--r-- | src/ncm/pkg/module/module.go | 3 | ||||
-rw-r--r-- | src/ncm/pkg/networkintents/network.go | 2 | ||||
-rw-r--r-- | src/ncm/pkg/networkintents/providernet.go | 2 | ||||
-rw-r--r-- | src/ncm/pkg/scheduler/scheduler.go | 2 |
9 files changed, 5 insertions, 2562 deletions
diff --git a/src/ncm/api/api.go b/src/ncm/api/api.go index a304516c..6dd958a1 100644 --- a/src/ncm/api/api.go +++ b/src/ncm/api/api.go @@ -20,7 +20,6 @@ import ( "reflect" "github.com/gorilla/mux" - "github.com/onap/multicloud-k8s/src/ncm/pkg/cluster" "github.com/onap/multicloud-k8s/src/ncm/pkg/module" "github.com/onap/multicloud-k8s/src/ncm/pkg/networkintents" "github.com/onap/multicloud-k8s/src/ncm/pkg/scheduler" @@ -33,13 +32,6 @@ var moduleClient *module.Client // return the testClient, otherwise return the client. func setClient(client, testClient interface{}) interface{} { switch cl := client.(type) { - case *cluster.ClusterClient: - if testClient != nil && reflect.TypeOf(testClient).Implements(reflect.TypeOf((*cluster.ClusterManager)(nil)).Elem()) { - c, ok := testClient.(cluster.ClusterManager) - if ok { - return c - } - } case *networkintents.NetworkClient: if testClient != nil && reflect.TypeOf(testClient).Implements(reflect.TypeOf((*networkintents.NetworkManager)(nil)).Elem()) { c, ok := testClient.(networkintents.NetworkManager) @@ -75,27 +67,6 @@ func NewRouter(testClient interface{}) *mux.Router { router := mux.NewRouter().PathPrefix("/v2").Subrouter() - clusterHandler := clusterHandler{ - client: setClient(moduleClient.Cluster, testClient).(cluster.ClusterManager), - } - 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", clusterHandler.getClusterHandler).Queries("label", "{label}") - 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") - networkHandler := networkHandler{ client: setClient(moduleClient.Network, testClient).(networkintents.NetworkManager), } diff --git a/src/ncm/api/clusterhandler.go b/src/ncm/api/clusterhandler.go deleted file mode 100644 index 08040408..00000000 --- a/src/ncm/api/clusterhandler.go +++ /dev/null @@ -1,485 +0,0 @@ -/* - * 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" - - clusterPkg "github.com/onap/multicloud-k8s/src/ncm/pkg/cluster" - - "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 clusterPkg.ClusterManager -} - -// Create handles creation of the ClusterProvider entry in the database -func (h clusterHandler) createClusterProviderHandler(w http.ResponseWriter, r *http.Request) { - var p clusterPkg.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 clusterPkg.Cluster - var q clusterPkg.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"] - - label := r.URL.Query().Get("label") - if len(label) != 0 { - ret, err := h.client.GetClustersWithLabel(provider, 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 - } - return - } - - // handle the get all clusters case - return a list of only the json parts - if len(name) == 0 { - var retList []clusterPkg.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, clusterPkg.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 - } - kcBytes, err := base64.StdEncoding.DecodeString(retKubeconfig.Kubeconfig) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - _, err = pw.Write(kcBytes) - 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) - kcBytes, err := base64.StdEncoding.DecodeString(retKubeconfig.Kubeconfig) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - _, err = w.Write(kcBytes) - 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 clusterPkg.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 clusterPkg.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/ncm/api/clusterhandler_test.go b/src/ncm/api/clusterhandler_test.go deleted file mode 100644 index a9290ad9..00000000 --- a/src/ncm/api/clusterhandler_test.go +++ /dev/null @@ -1,1432 +0,0 @@ -/* - * 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" - - "github.com/onap/multicloud-k8s/src/ncm/pkg/cluster" - "github.com/onap/multicloud-k8s/src/orchestrator/pkg/appcontext" - types "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module/types" - - 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 []cluster.ClusterProvider - ClusterItems []cluster.Cluster - ClusterContentItems []cluster.ClusterContent - ClusterContextItems []appcontext.AppContext - ClusterLabelItems []cluster.ClusterLabel - ClusterKvPairsItems []cluster.ClusterKvPairs - ClusterList []string - Err error -} - -func (m *mockClusterManager) CreateClusterProvider(inp cluster.ClusterProvider) (cluster.ClusterProvider, error) { - if m.Err != nil { - return cluster.ClusterProvider{}, m.Err - } - - return m.ClusterProviderItems[0], nil -} - -func (m *mockClusterManager) GetClusterProvider(name string) (cluster.ClusterProvider, error) { - if m.Err != nil { - return cluster.ClusterProvider{}, m.Err - } - - return m.ClusterProviderItems[0], nil -} - -func (m *mockClusterManager) GetClusterProviders() ([]cluster.ClusterProvider, error) { - if m.Err != nil { - return []cluster.ClusterProvider{}, m.Err - } - - return m.ClusterProviderItems, nil -} - -func (m *mockClusterManager) DeleteClusterProvider(name string) error { - return m.Err -} - -func (m *mockClusterManager) CreateCluster(provider string, inp cluster.Cluster, inq cluster.ClusterContent) (cluster.Cluster, error) { - if m.Err != nil { - return cluster.Cluster{}, m.Err - } - - return m.ClusterItems[0], nil -} - -func (m *mockClusterManager) GetCluster(provider, name string) (cluster.Cluster, error) { - if m.Err != nil { - return cluster.Cluster{}, m.Err - } - - return m.ClusterItems[0], nil -} - -func (m *mockClusterManager) GetClusterContent(provider, name string) (cluster.ClusterContent, error) { - if m.Err != nil { - return cluster.ClusterContent{}, m.Err - } - - return m.ClusterContentItems[0], nil -} - -func (m *mockClusterManager) GetClusterContext(provider, name string) (appcontext.AppContext, error) { - if m.Err != nil { - return appcontext.AppContext{}, m.Err - } - - return m.ClusterContextItems[0], nil -} - -func (m *mockClusterManager) GetClusters(provider string) ([]cluster.Cluster, error) { - if m.Err != nil { - return []cluster.Cluster{}, m.Err - } - - return m.ClusterItems, nil -} - -func (m *mockClusterManager) GetClustersWithLabel(provider, label string) ([]string, error) { - if m.Err != nil { - return []string{}, m.Err - } - - return m.ClusterList, nil -} - -func (m *mockClusterManager) DeleteCluster(provider, name string) error { - return m.Err -} - -func (m *mockClusterManager) CreateClusterLabel(provider, clusterName string, inp cluster.ClusterLabel) (cluster.ClusterLabel, error) { - if m.Err != nil { - return cluster.ClusterLabel{}, m.Err - } - - return m.ClusterLabelItems[0], nil -} - -func (m *mockClusterManager) GetClusterLabel(provider, clusterName, label string) (cluster.ClusterLabel, error) { - if m.Err != nil { - return cluster.ClusterLabel{}, m.Err - } - - return m.ClusterLabelItems[0], nil -} - -func (m *mockClusterManager) GetClusterLabels(provider, clusterName string) ([]cluster.ClusterLabel, error) { - if m.Err != nil { - return []cluster.ClusterLabel{}, m.Err - } - - return m.ClusterLabelItems, nil -} - -func (m *mockClusterManager) DeleteClusterLabel(provider, clusterName, label string) error { - return m.Err -} - -func (m *mockClusterManager) CreateClusterKvPairs(provider, clusterName string, inp cluster.ClusterKvPairs) (cluster.ClusterKvPairs, error) { - if m.Err != nil { - return cluster.ClusterKvPairs{}, m.Err - } - - return m.ClusterKvPairsItems[0], nil -} - -func (m *mockClusterManager) GetClusterKvPairs(provider, clusterName, kvpair string) (cluster.ClusterKvPairs, error) { - if m.Err != nil { - return cluster.ClusterKvPairs{}, m.Err - } - - return m.ClusterKvPairsItems[0], nil -} - -func (m *mockClusterManager) GetAllClusterKvPairs(provider, clusterName string) ([]cluster.ClusterKvPairs, error) { - if m.Err != nil { - return []cluster.ClusterKvPairs{}, m.Err - } - - return m.ClusterKvPairsItems, nil -} - -func (m *mockClusterManager) DeleteClusterKvPairs(provider, clusterName, kvpair string) error { - return m.Err -} - -func TestClusterProviderCreateHandler(t *testing.T) { - testCases := []struct { - label string - reader io.Reader - expected cluster.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: cluster.ClusterProvider{ - Metadata: types.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: []cluster.ClusterProvider{ - { - Metadata: types.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(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 := cluster.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 []cluster.ClusterProvider - name, version string - expectedCode int - clusterClient *mockClusterManager - }{ - { - label: "Get Cluster Provider", - expectedCode: http.StatusOK, - expected: []cluster.ClusterProvider{ - { - Metadata: types.Metadata{ - Name: "testClusterProvider1", - Description: "testClusterProvider 1 description", - UserData1: "some user data 1", - UserData2: "some user data 2", - }, - }, - { - Metadata: types.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: []cluster.ClusterProvider{ - { - Metadata: types.Metadata{ - Name: "testClusterProvider1", - Description: "testClusterProvider 1 description", - UserData1: "some user data 1", - UserData2: "some user data 2", - }, - }, - { - Metadata: types.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(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 := []cluster.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 cluster.ClusterProvider - name, version string - expectedCode int - clusterClient *mockClusterManager - }{ - { - label: "Get Cluster Provider", - expectedCode: http.StatusOK, - expected: cluster.ClusterProvider{ - Metadata: types.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: []cluster.ClusterProvider{ - { - Metadata: types.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: []cluster.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(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 := cluster.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(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 cluster.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: cluster.Cluster{ - Metadata: types.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: []cluster.ClusterProvider{ - { - Metadata: types.Metadata{ - Name: "clusterProvider1", - Description: "ClusterProvider 1 description", - UserData1: "some user data 1", - UserData2: "some user data 2", - }, - }, - }, - ClusterItems: []cluster.Cluster{ - { - Metadata: types.Metadata{ - Name: "clusterTest", - Description: "testCluster", - UserData1: "some user data 1", - UserData2: "some user data 2", - }, - }, - }, - ClusterContentItems: []cluster.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(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 := cluster.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 []cluster.Cluster - name, version string - expectedCode int - clusterClient *mockClusterManager - }{ - { - label: "Get Clusters", - expectedCode: http.StatusOK, - expected: []cluster.Cluster{ - { - Metadata: types.Metadata{ - Name: "testCluster1", - Description: "testCluster 1 description", - UserData1: "some user data 1", - UserData2: "some user data 2", - }, - }, - { - Metadata: types.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: []cluster.Cluster{ - { - Metadata: types.Metadata{ - Name: "testCluster1", - Description: "testCluster 1 description", - UserData1: "some user data 1", - UserData2: "some user data 2", - }, - }, - { - Metadata: types.Metadata{ - Name: "testCluster2", - Description: "testCluster 2 description", - UserData1: "some user data A", - UserData2: "some user data B", - }, - }, - }, - ClusterContentItems: []cluster.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(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 := []cluster.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 cluster.Cluster - name, version string - accept string - expectedCode int - clusterClient *mockClusterManager - }{ - { - label: "Get Cluster with Accept: application/json", - accept: "application/json", - expectedCode: http.StatusOK, - expected: cluster.Cluster{ - Metadata: types.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: []cluster.Cluster{ - { - Metadata: types.Metadata{ - Name: "testCluster", - Description: "testCluster description", - UserData1: "some user data 1", - UserData2: "some user data 2", - }, - }, - }, - ClusterContentItems: []cluster.ClusterContent{ - { - Kubeconfig: "dGVzdCBjb250ZW50cwpvZiBhIGZpbGUgYXR0YWNoZWQKdG8gdGhlIGNyZWF0aW9uCm9mIGNsdXN0ZXJUZXN0Cg==", - }, - }, - }, - }, - { - label: "Get Non-Existing Cluster", - accept: "application/json", - expectedCode: http.StatusInternalServerError, - name: "nonexistingcluster", - clusterClient: &mockClusterManager{ - ClusterItems: []cluster.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(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 := cluster.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: []cluster.Cluster{ - { - Metadata: types.Metadata{ - Name: "testCluster", - Description: "testCluster description", - UserData1: "some user data 1", - UserData2: "some user data 2", - }, - }, - }, - ClusterContentItems: []cluster.ClusterContent{ - { - Kubeconfig: "dGVzdCBjb250ZW50cwpvZiBhIGZpbGUgYXR0YWNoZWQKdG8gdGhlIGNyZWF0aW9uCm9mIGNsdXN0ZXJUZXN0Cg==", - }, - }, - }, - }, - { - label: "Get Non-Existing Cluster", - accept: "application/octet-stream", - expectedCode: http.StatusInternalServerError, - name: "nonexistingcluster", - clusterClient: &mockClusterManager{ - ClusterItems: []cluster.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(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(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 cluster.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: cluster.ClusterLabel{ - LabelName: "test-label", - }, - clusterClient: &mockClusterManager{ - //Items that will be returned by the mocked Client - ClusterLabelItems: []cluster.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(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 := cluster.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 []cluster.ClusterLabel - name, version string - expectedCode int - clusterClient *mockClusterManager - }{ - { - label: "Get Cluster Labels", - expectedCode: http.StatusOK, - expected: []cluster.ClusterLabel{ - { - LabelName: "test-label1", - }, - { - LabelName: "test-label-two", - }, - { - LabelName: "test-label-3", - }, - }, - clusterClient: &mockClusterManager{ - //Items that will be returned by the mocked Client - ClusterLabelItems: []cluster.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(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 := []cluster.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 cluster.ClusterLabel - name, version string - expectedCode int - clusterClient *mockClusterManager - }{ - { - label: "Get Cluster Label", - expectedCode: http.StatusOK, - expected: cluster.ClusterLabel{ - LabelName: "testlabel", - }, - name: "testlabel", - clusterClient: &mockClusterManager{ - //Items that will be returned by the mocked Client - ClusterLabelItems: []cluster.ClusterLabel{ - { - LabelName: "testlabel", - }, - }, - }, - }, - { - label: "Get Non-Existing Cluster Label", - expectedCode: http.StatusInternalServerError, - name: "nonexistingclusterlabel", - clusterClient: &mockClusterManager{ - ClusterLabelItems: []cluster.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(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 := cluster.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(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 cluster.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: cluster.ClusterKvPairs{ - Metadata: types.Metadata{ - Name: "ClusterKvPair1", - Description: "test cluster kv pairs", - UserData1: "some user data 1", - UserData2: "some user data 2", - }, - Spec: cluster.ClusterKvSpec{ - Kv: []map[string]interface{}{ - { - "key1": "value1", - }, - { - "key2": "value2", - }, - }, - }, - }, - clusterClient: &mockClusterManager{ - //Items that will be returned by the mocked Client - ClusterKvPairsItems: []cluster.ClusterKvPairs{ - { - Metadata: types.Metadata{ - Name: "ClusterKvPair1", - Description: "test cluster kv pairs", - UserData1: "some user data 1", - UserData2: "some user data 2", - }, - Spec: cluster.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(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 := cluster.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 []cluster.ClusterKvPairs - name, version string - expectedCode int - clusterClient *mockClusterManager - }{ - { - label: "Get Cluster KvPairs", - expectedCode: http.StatusOK, - expected: []cluster.ClusterKvPairs{ - { - Metadata: types.Metadata{ - Name: "ClusterKvPair1", - Description: "test cluster kv pairs", - UserData1: "some user data 1", - UserData2: "some user data 2", - }, - Spec: cluster.ClusterKvSpec{ - Kv: []map[string]interface{}{ - { - "key1": "value1", - }, - { - "key2": "value2", - }, - }, - }, - }, - { - Metadata: types.Metadata{ - Name: "ClusterKvPair2", - Description: "test cluster kv pairs", - UserData1: "some user data A", - UserData2: "some user data B", - }, - Spec: cluster.ClusterKvSpec{ - Kv: []map[string]interface{}{ - { - "keyA": "valueA", - }, - { - "keyB": "valueB", - }, - }, - }, - }, - }, - clusterClient: &mockClusterManager{ - //Items that will be returned by the mocked Client - ClusterKvPairsItems: []cluster.ClusterKvPairs{ - { - Metadata: types.Metadata{ - Name: "ClusterKvPair1", - Description: "test cluster kv pairs", - UserData1: "some user data 1", - UserData2: "some user data 2", - }, - Spec: cluster.ClusterKvSpec{ - Kv: []map[string]interface{}{ - { - "key1": "value1", - }, - { - "key2": "value2", - }, - }, - }, - }, - { - Metadata: types.Metadata{ - Name: "ClusterKvPair2", - Description: "test cluster kv pairs", - UserData1: "some user data A", - UserData2: "some user data B", - }, - Spec: cluster.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(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 := []cluster.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 cluster.ClusterKvPairs - name, version string - expectedCode int - clusterClient *mockClusterManager - }{ - { - label: "Get Cluster KV Pairs", - expectedCode: http.StatusOK, - expected: cluster.ClusterKvPairs{ - Metadata: types.Metadata{ - Name: "ClusterKvPair2", - Description: "test cluster kv pairs", - UserData1: "some user data A", - UserData2: "some user data B", - }, - Spec: cluster.ClusterKvSpec{ - Kv: []map[string]interface{}{ - { - "keyA": "valueA", - }, - { - "keyB": "valueB", - }, - }, - }, - }, - name: "ClusterKvPair2", - clusterClient: &mockClusterManager{ - //Items that will be returned by the mocked Client - ClusterKvPairsItems: []cluster.ClusterKvPairs{ - { - Metadata: types.Metadata{ - Name: "ClusterKvPair2", - Description: "test cluster kv pairs", - UserData1: "some user data A", - UserData2: "some user data B", - }, - Spec: cluster.ClusterKvSpec{ - Kv: []map[string]interface{}{ - { - "keyA": "valueA", - }, - { - "keyB": "valueB", - }, - }, - }, - }, - }, - }, - }, - { - label: "Get Non-Existing Cluster KV Pairs", - expectedCode: http.StatusInternalServerError, - name: "nonexistingclusterkvpairs", - clusterClient: &mockClusterManager{ - ClusterKvPairsItems: []cluster.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(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 := cluster.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(testCase.clusterClient)) - - //Check returned code - if resp.StatusCode != testCase.expectedCode { - t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode) - } - }) - } -} diff --git a/src/ncm/go.mod b/src/ncm/go.mod index 41f2b5ff..233d8880 100644 --- a/src/ncm/go.mod +++ b/src/ncm/go.mod @@ -2,17 +2,10 @@ module github.com/onap/multicloud-k8s/src/ncm require ( github.com/ghodss/yaml v1.0.0 - github.com/go-stack/stack v1.8.0 // indirect - github.com/golang/snappy v0.0.1 // indirect github.com/gorilla/handlers v1.3.0 - github.com/gorilla/mux v1.6.2 + github.com/gorilla/mux v1.7.3 github.com/k8snetworkplumbingwg/network-attachment-definition-client v0.0.0-20200127152046-0ee521d56061 - github.com/onap/multicloud-k8s/src/orchestrator v0.0.0-20200527175204-ef27eb4d63f1 - github.com/onap/multicloud-k8s/src/rsync v0.0.0-20200529003854-0a7bf256bde5 - github.com/opencontainers/go-digest v1.0.0 // indirect github.com/pkg/errors v0.8.1 - github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c // indirect - github.com/xdg/stringprep v1.0.0 // indirect google.golang.org/grpc v1.27.1 gopkg.in/yaml.v2 v2.2.8 k8s.io/api v0.0.0-20190831074750-7364b6bdad65 @@ -23,6 +16,7 @@ require ( ) replace ( + github.com/onap/multicloud-k8s/src/clm => ../clm github.com/onap/multicloud-k8s/src/orchestrator => ../orchestrator github.com/onap/multicloud-k8s/src/rsync => ../rsync k8s.io/api => k8s.io/api v0.0.0-20190409021203-6e4e0e4f393b diff --git a/src/ncm/pkg/cluster/cluster.go b/src/ncm/pkg/cluster/cluster.go deleted file mode 100644 index f656688c..00000000 --- a/src/ncm/pkg/cluster/cluster.go +++ /dev/null @@ -1,602 +0,0 @@ -/* - * 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 cluster - -import ( - ncmtypes "github.com/onap/multicloud-k8s/src/ncm/pkg/module/types" - appcontext "github.com/onap/multicloud-k8s/src/orchestrator/pkg/appcontext" - "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" - mtypes "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module/types" - - pkgerrors "github.com/pkg/errors" -) - -// ClusterProvider contains the parameters needed for ClusterProviders -type ClusterProvider struct { - Metadata mtypes.Metadata `json:"metadata"` -} - -type Cluster struct { - Metadata mtypes.Metadata `json:"metadata"` -} - -type ClusterContent struct { - Kubeconfig string `json:"kubeconfig"` -} - -type ClusterLabel struct { - LabelName string `json:"label-name"` -} - -type ClusterKvPairs struct { - Metadata mtypes.Metadata `json:"metadata"` - Spec ClusterKvSpec `json:"spec"` -} - -type ClusterKvSpec struct { - Kv []map[string]interface{} `json:"kv"` -} - -// ClusterProviderKey is the key structure that is used in the database -type ClusterProviderKey struct { - ClusterProviderName string `json:"provider"` -} - -// ClusterKey is the key structure that is used in the database -type ClusterKey struct { - ClusterProviderName string `json:"provider"` - ClusterName string `json:"cluster"` -} - -// ClusterLabelKey is the key structure that is used in the database -type ClusterLabelKey struct { - ClusterProviderName string `json:"provider"` - ClusterName string `json:"cluster"` - ClusterLabelName string `json:"label"` -} - -// LabelKey is the key structure that is used in the database -type LabelKey struct { - ClusterProviderName string `json:"provider"` - ClusterLabelName string `json:"label"` -} - -// ClusterKvPairsKey is the key structure that is used in the database -type ClusterKvPairsKey struct { - ClusterProviderName string `json:"provider"` - ClusterName string `json:"cluster"` - ClusterKvPairsName string `json:"kvname"` -} - -const SEPARATOR = "+" -const CONTEXT_CLUSTER_APP = "network-intents" -const CONTEXT_CLUSTER_RESOURCE = "network-intents" - -// ClusterManager is an interface exposes the Cluster functionality -type ClusterManager interface { - CreateClusterProvider(pr ClusterProvider) (ClusterProvider, error) - GetClusterProvider(name string) (ClusterProvider, error) - GetClusterProviders() ([]ClusterProvider, error) - DeleteClusterProvider(name string) error - CreateCluster(provider string, pr Cluster, qr ClusterContent) (Cluster, error) - GetCluster(provider, name string) (Cluster, error) - GetClusterContent(provider, name string) (ClusterContent, error) - GetClusterContext(provider, name string) (appcontext.AppContext, error) - GetClusters(provider string) ([]Cluster, error) - GetClustersWithLabel(provider, label string) ([]string, error) - DeleteCluster(provider, name string) error - CreateClusterLabel(provider, cluster string, pr ClusterLabel) (ClusterLabel, error) - GetClusterLabel(provider, cluster, label string) (ClusterLabel, error) - GetClusterLabels(provider, cluster string) ([]ClusterLabel, error) - DeleteClusterLabel(provider, cluster, label string) error - CreateClusterKvPairs(provider, cluster string, pr ClusterKvPairs) (ClusterKvPairs, error) - GetClusterKvPairs(provider, cluster, kvpair string) (ClusterKvPairs, error) - GetAllClusterKvPairs(provider, cluster string) ([]ClusterKvPairs, error) - DeleteClusterKvPairs(provider, cluster, kvpair string) error -} - -// ClusterClient implements the Manager -// It will also be used to maintain some localized state -type ClusterClient struct { - db ncmtypes.ClientDbInfo -} - -// NewClusterClient returns an instance of the ClusterClient -// which implements the Manager -func NewClusterClient() *ClusterClient { - return &ClusterClient{ - db: ncmtypes.ClientDbInfo{ - StoreName: "cluster", - TagMeta: "clustermetadata", - TagContent: "clustercontent", - TagContext: "clustercontext", - }, - } -} - -// CreateClusterProvider - create a new Cluster Provider -func (v *ClusterClient) CreateClusterProvider(p ClusterProvider) (ClusterProvider, error) { - - //Construct key and tag to select the entry - key := ClusterProviderKey{ - ClusterProviderName: p.Metadata.Name, - } - - //Check if this ClusterProvider already exists - _, err := v.GetClusterProvider(p.Metadata.Name) - if err == nil { - return ClusterProvider{}, pkgerrors.New("ClusterProvider already exists") - } - - err = db.DBconn.Insert(v.db.StoreName, key, nil, v.db.TagMeta, p) - if err != nil { - return ClusterProvider{}, pkgerrors.Wrap(err, "Creating DB Entry") - } - - return p, nil -} - -// GetClusterProvider returns the ClusterProvider for corresponding name -func (v *ClusterClient) GetClusterProvider(name string) (ClusterProvider, error) { - - //Construct key and tag to select the entry - key := ClusterProviderKey{ - ClusterProviderName: name, - } - - value, err := db.DBconn.Find(v.db.StoreName, key, v.db.TagMeta) - if err != nil { - return ClusterProvider{}, pkgerrors.Wrap(err, "Get ClusterProvider") - } - - //value is a byte array - if value != nil { - cp := ClusterProvider{} - err = db.DBconn.Unmarshal(value[0], &cp) - if err != nil { - return ClusterProvider{}, pkgerrors.Wrap(err, "Unmarshalling Value") - } - return cp, nil - } - - return ClusterProvider{}, pkgerrors.New("Error getting ClusterProvider") -} - -// GetClusterProviderList returns all of the ClusterProvider for corresponding name -func (v *ClusterClient) GetClusterProviders() ([]ClusterProvider, error) { - - //Construct key and tag to select the entry - key := ClusterProviderKey{ - ClusterProviderName: "", - } - - var resp []ClusterProvider - values, err := db.DBconn.Find(v.db.StoreName, key, v.db.TagMeta) - if err != nil { - return []ClusterProvider{}, pkgerrors.Wrap(err, "Get ClusterProviders") - } - - for _, value := range values { - cp := ClusterProvider{} - err = db.DBconn.Unmarshal(value, &cp) - if err != nil { - return []ClusterProvider{}, pkgerrors.Wrap(err, "Unmarshalling Value") - } - resp = append(resp, cp) - } - - return resp, nil -} - -// DeleteClusterProvider the ClusterProvider from database -func (v *ClusterClient) DeleteClusterProvider(name string) error { - - //Construct key and tag to select the entry - key := ClusterProviderKey{ - ClusterProviderName: name, - } - - err := db.DBconn.Remove(v.db.StoreName, key) - if err != nil { - return pkgerrors.Wrap(err, "Delete ClusterProvider Entry;") - } - - return nil -} - -// CreateCluster - create a new Cluster for a cluster-provider -func (v *ClusterClient) CreateCluster(provider string, p Cluster, q ClusterContent) (Cluster, error) { - - //Construct key and tag to select the entry - key := ClusterKey{ - ClusterProviderName: provider, - ClusterName: p.Metadata.Name, - } - - //Verify ClusterProvider already exists - _, err := v.GetClusterProvider(provider) - if err != nil { - return Cluster{}, pkgerrors.New("ClusterProvider does not exist") - } - - //Check if this Cluster already exists - _, err = v.GetCluster(provider, p.Metadata.Name) - if err == nil { - return Cluster{}, pkgerrors.New("Cluster already exists") - } - - err = db.DBconn.Insert(v.db.StoreName, key, nil, v.db.TagMeta, p) - if err != nil { - return Cluster{}, pkgerrors.Wrap(err, "Creating DB Entry") - } - err = db.DBconn.Insert(v.db.StoreName, key, nil, v.db.TagContent, q) - if err != nil { - return Cluster{}, pkgerrors.Wrap(err, "Creating DB Entry") - } - - return p, nil -} - -// GetCluster returns the Cluster for corresponding provider and name -func (v *ClusterClient) GetCluster(provider, name string) (Cluster, error) { - //Construct key and tag to select the entry - key := ClusterKey{ - ClusterProviderName: provider, - ClusterName: name, - } - - value, err := db.DBconn.Find(v.db.StoreName, key, v.db.TagMeta) - if err != nil { - return Cluster{}, pkgerrors.Wrap(err, "Get Cluster") - } - - //value is a byte array - if value != nil { - cl := Cluster{} - err = db.DBconn.Unmarshal(value[0], &cl) - if err != nil { - return Cluster{}, pkgerrors.Wrap(err, "Unmarshalling Value") - } - return cl, nil - } - - return Cluster{}, pkgerrors.New("Error getting Cluster") -} - -// GetClusterContent returns the ClusterContent for corresponding provider and name -func (v *ClusterClient) GetClusterContent(provider, name string) (ClusterContent, error) { - //Construct key and tag to select the entry - key := ClusterKey{ - ClusterProviderName: provider, - ClusterName: name, - } - - value, err := db.DBconn.Find(v.db.StoreName, key, v.db.TagContent) - if err != nil { - return ClusterContent{}, pkgerrors.Wrap(err, "Get Cluster Content") - } - - //value is a byte array - if value != nil { - cc := ClusterContent{} - err = db.DBconn.Unmarshal(value[0], &cc) - if err != nil { - return ClusterContent{}, pkgerrors.Wrap(err, "Unmarshalling Value") - } - return cc, nil - } - - return ClusterContent{}, pkgerrors.New("Error getting Cluster Content") -} - -// GetClusterContext returns the AppContext for corresponding provider and name -func (v *ClusterClient) GetClusterContext(provider, name string) (appcontext.AppContext, error) { - //Construct key and tag to select the entry - key := ClusterKey{ - ClusterProviderName: provider, - ClusterName: name, - } - - value, err := db.DBconn.Find(v.db.StoreName, key, v.db.TagContext) - if err != nil { - return appcontext.AppContext{}, pkgerrors.Wrap(err, "Get Cluster Context") - } - - //value is a byte array - if value != nil { - ctxVal := string(value[0]) - var cc appcontext.AppContext - _, err = cc.LoadAppContext(ctxVal) - if err != nil { - return appcontext.AppContext{}, pkgerrors.Wrap(err, "Reinitializing Cluster AppContext") - } - return cc, nil - } - - return appcontext.AppContext{}, pkgerrors.New("Error getting Cluster AppContext") -} - -// GetClusters returns all the Clusters for corresponding provider -func (v *ClusterClient) GetClusters(provider string) ([]Cluster, error) { - //Construct key and tag to select the entry - key := ClusterKey{ - ClusterProviderName: provider, - ClusterName: "", - } - - values, err := db.DBconn.Find(v.db.StoreName, key, v.db.TagMeta) - if err != nil { - return []Cluster{}, pkgerrors.Wrap(err, "Get Clusters") - } - - var resp []Cluster - - for _, value := range values { - cp := Cluster{} - err = db.DBconn.Unmarshal(value, &cp) - if err != nil { - return []Cluster{}, pkgerrors.Wrap(err, "Unmarshalling Value") - } - resp = append(resp, cp) - } - - return resp, nil -} - -// GetClustersWithLabel returns all the Clusters with Labels for provider -// Support Query like /cluster-providers/{Provider}/clusters?label={label} -func (v *ClusterClient) GetClustersWithLabel(provider, label string) ([]string, error) { - //Construct key and tag to select the entry - key := LabelKey{ - ClusterProviderName: provider, - ClusterLabelName: label, - } - - values, err := db.DBconn.Find(v.db.StoreName, key, "cluster") - if err != nil { - return []string{}, pkgerrors.Wrap(err, "Get Clusters by label") - } - var resp []string - - for _, value := range values { - cp := string(value) - resp = append(resp, cp) - } - - return resp, nil -} - -// DeleteCluster the Cluster from database -func (v *ClusterClient) DeleteCluster(provider, name string) error { - //Construct key and tag to select the entry - key := ClusterKey{ - ClusterProviderName: provider, - ClusterName: name, - } - _, err := v.GetClusterContext(provider, name) - if err == nil { - return pkgerrors.Errorf("Cannot delete cluster until context is deleted: %v, %v", provider, name) - } - - err = db.DBconn.Remove(v.db.StoreName, key) - if err != nil { - return pkgerrors.Wrap(err, "Delete Cluster Entry;") - } - - return nil -} - -// CreateClusterLabel - create a new Cluster Label mongo document for a cluster-provider/cluster -func (v *ClusterClient) CreateClusterLabel(provider string, cluster string, p ClusterLabel) (ClusterLabel, error) { - //Construct key and tag to select the entry - key := ClusterLabelKey{ - ClusterProviderName: provider, - ClusterName: cluster, - ClusterLabelName: p.LabelName, - } - - //Verify Cluster already exists - _, err := v.GetCluster(provider, cluster) - if err != nil { - return ClusterLabel{}, pkgerrors.New("Cluster does not exist") - } - - //Check if this ClusterLabel already exists - _, err = v.GetClusterLabel(provider, cluster, p.LabelName) - if err == nil { - return ClusterLabel{}, pkgerrors.New("Cluster Label already exists") - } - - err = db.DBconn.Insert(v.db.StoreName, key, nil, v.db.TagMeta, p) - if err != nil { - return ClusterLabel{}, pkgerrors.Wrap(err, "Creating DB Entry") - } - - return p, nil -} - -// GetClusterLabel returns the Cluster for corresponding provider, cluster and label -func (v *ClusterClient) GetClusterLabel(provider, cluster, label string) (ClusterLabel, error) { - //Construct key and tag to select the entry - key := ClusterLabelKey{ - ClusterProviderName: provider, - ClusterName: cluster, - ClusterLabelName: label, - } - - value, err := db.DBconn.Find(v.db.StoreName, key, v.db.TagMeta) - if err != nil { - return ClusterLabel{}, pkgerrors.Wrap(err, "Get Cluster") - } - - //value is a byte array - if value != nil { - cl := ClusterLabel{} - err = db.DBconn.Unmarshal(value[0], &cl) - if err != nil { - return ClusterLabel{}, pkgerrors.Wrap(err, "Unmarshalling Value") - } - return cl, nil - } - - return ClusterLabel{}, pkgerrors.New("Error getting Cluster") -} - -// GetClusterLabels returns the Cluster Labels for corresponding provider and cluster -func (v *ClusterClient) GetClusterLabels(provider, cluster string) ([]ClusterLabel, error) { - //Construct key and tag to select the entry - key := ClusterLabelKey{ - ClusterProviderName: provider, - ClusterName: cluster, - ClusterLabelName: "", - } - - values, err := db.DBconn.Find(v.db.StoreName, key, v.db.TagMeta) - if err != nil { - return []ClusterLabel{}, pkgerrors.Wrap(err, "Get Cluster Labels") - } - - var resp []ClusterLabel - - for _, value := range values { - cp := ClusterLabel{} - err = db.DBconn.Unmarshal(value, &cp) - if err != nil { - return []ClusterLabel{}, pkgerrors.Wrap(err, "Unmarshalling Value") - } - resp = append(resp, cp) - } - - return resp, nil -} - -// Delete the Cluster Label from database -func (v *ClusterClient) DeleteClusterLabel(provider, cluster, label string) error { - //Construct key and tag to select the entry - key := ClusterLabelKey{ - ClusterProviderName: provider, - ClusterName: cluster, - ClusterLabelName: label, - } - - err := db.DBconn.Remove(v.db.StoreName, key) - if err != nil { - return pkgerrors.Wrap(err, "Delete ClusterLabel Entry;") - } - - return nil -} - -// CreateClusterKvPairs - Create a New Cluster KV pairs document -func (v *ClusterClient) CreateClusterKvPairs(provider string, cluster string, p ClusterKvPairs) (ClusterKvPairs, error) { - key := ClusterKvPairsKey{ - ClusterProviderName: provider, - ClusterName: cluster, - ClusterKvPairsName: p.Metadata.Name, - } - - //Verify Cluster already exists - _, err := v.GetCluster(provider, cluster) - if err != nil { - return ClusterKvPairs{}, pkgerrors.New("Cluster does not exist") - } - - //Check if this ClusterKvPairs already exists - _, err = v.GetClusterKvPairs(provider, cluster, p.Metadata.Name) - if err == nil { - return ClusterKvPairs{}, pkgerrors.New("Cluster KV Pair already exists") - } - - err = db.DBconn.Insert(v.db.StoreName, key, nil, v.db.TagMeta, p) - if err != nil { - return ClusterKvPairs{}, pkgerrors.Wrap(err, "Creating DB Entry") - } - - return p, nil -} - -// GetClusterKvPairs returns the Cluster KeyValue pair for corresponding provider, cluster and KV pair name -func (v *ClusterClient) GetClusterKvPairs(provider, cluster, kvpair string) (ClusterKvPairs, error) { - //Construct key and tag to select entry - key := ClusterKvPairsKey{ - ClusterProviderName: provider, - ClusterName: cluster, - ClusterKvPairsName: kvpair, - } - - value, err := db.DBconn.Find(v.db.StoreName, key, v.db.TagMeta) - if err != nil { - return ClusterKvPairs{}, pkgerrors.Wrap(err, "Get Cluster") - } - - //value is a byte array - if value != nil { - ckvp := ClusterKvPairs{} - err = db.DBconn.Unmarshal(value[0], &ckvp) - if err != nil { - return ClusterKvPairs{}, pkgerrors.Wrap(err, "Unmarshalling Value") - } - return ckvp, nil - } - - return ClusterKvPairs{}, pkgerrors.New("Error getting Cluster") -} - -// GetAllClusterKvPairs returns the Cluster Kv Pairs for corresponding provider and cluster -func (v *ClusterClient) GetAllClusterKvPairs(provider, cluster string) ([]ClusterKvPairs, error) { - //Construct key and tag to select the entry - key := ClusterKvPairsKey{ - ClusterProviderName: provider, - ClusterName: cluster, - ClusterKvPairsName: "", - } - - values, err := db.DBconn.Find(v.db.StoreName, key, v.db.TagMeta) - if err != nil { - return []ClusterKvPairs{}, pkgerrors.Wrap(err, "Get Cluster KV Pairs") - } - - var resp []ClusterKvPairs - - for _, value := range values { - cp := ClusterKvPairs{} - err = db.DBconn.Unmarshal(value, &cp) - if err != nil { - return []ClusterKvPairs{}, pkgerrors.Wrap(err, "Unmarshalling Value") - } - resp = append(resp, cp) - } - - return resp, nil -} - -// DeleteClusterKvPairs the ClusterKvPairs from database -func (v *ClusterClient) DeleteClusterKvPairs(provider, cluster, kvpair string) error { - //Construct key and tag to select entry - key := ClusterKvPairsKey{ - ClusterProviderName: provider, - ClusterName: cluster, - ClusterKvPairsName: kvpair, - } - - err := db.DBconn.Remove(v.db.StoreName, key) - if err != nil { - return pkgerrors.Wrap(err, "Delete ClusterKvPairs Entry;") - } - - return nil -} diff --git a/src/ncm/pkg/module/module.go b/src/ncm/pkg/module/module.go index 10b241f6..39200102 100644 --- a/src/ncm/pkg/module/module.go +++ b/src/ncm/pkg/module/module.go @@ -17,14 +17,12 @@ package module import ( - "github.com/onap/multicloud-k8s/src/ncm/pkg/cluster" "github.com/onap/multicloud-k8s/src/ncm/pkg/networkintents" "github.com/onap/multicloud-k8s/src/ncm/pkg/scheduler" ) // Client for using the services in the ncm type Client struct { - Cluster *cluster.ClusterClient Network *networkintents.NetworkClient ProviderNet *networkintents.ProviderNetClient Scheduler *scheduler.SchedulerClient @@ -34,7 +32,6 @@ type Client struct { // NewClient creates a new client for using the services func NewClient() *Client { c := &Client{} - c.Cluster = cluster.NewClusterClient() c.Network = networkintents.NewNetworkClient() c.ProviderNet = networkintents.NewProviderNetClient() c.Scheduler = scheduler.NewSchedulerClient() diff --git a/src/ncm/pkg/networkintents/network.go b/src/ncm/pkg/networkintents/network.go index e8480e0f..de8ee504 100644 --- a/src/ncm/pkg/networkintents/network.go +++ b/src/ncm/pkg/networkintents/network.go @@ -17,7 +17,7 @@ package networkintents import ( - clusterPkg "github.com/onap/multicloud-k8s/src/ncm/pkg/cluster" + clusterPkg "github.com/onap/multicloud-k8s/src/clm/pkg/cluster" ncmtypes "github.com/onap/multicloud-k8s/src/ncm/pkg/module/types" nettypes "github.com/onap/multicloud-k8s/src/ncm/pkg/networkintents/types" "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" diff --git a/src/ncm/pkg/networkintents/providernet.go b/src/ncm/pkg/networkintents/providernet.go index 0eb763c1..072e07f6 100644 --- a/src/ncm/pkg/networkintents/providernet.go +++ b/src/ncm/pkg/networkintents/providernet.go @@ -17,7 +17,7 @@ package networkintents import ( - clusterPkg "github.com/onap/multicloud-k8s/src/ncm/pkg/cluster" + clusterPkg "github.com/onap/multicloud-k8s/src/clm/pkg/cluster" ncmtypes "github.com/onap/multicloud-k8s/src/ncm/pkg/module/types" nettypes "github.com/onap/multicloud-k8s/src/ncm/pkg/networkintents/types" "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" diff --git a/src/ncm/pkg/scheduler/scheduler.go b/src/ncm/pkg/scheduler/scheduler.go index e72085b7..29d67662 100644 --- a/src/ncm/pkg/scheduler/scheduler.go +++ b/src/ncm/pkg/scheduler/scheduler.go @@ -21,9 +21,9 @@ import ( "encoding/json" "time" + clusterPkg "github.com/onap/multicloud-k8s/src/clm/pkg/cluster" "github.com/onap/multicloud-k8s/src/ncm/internal/grpc" oc "github.com/onap/multicloud-k8s/src/ncm/internal/ovncontroller" - clusterPkg "github.com/onap/multicloud-k8s/src/ncm/pkg/cluster" ncmtypes "github.com/onap/multicloud-k8s/src/ncm/pkg/module/types" nettypes "github.com/onap/multicloud-k8s/src/ncm/pkg/networkintents/types" appcontext "github.com/onap/multicloud-k8s/src/orchestrator/pkg/appcontext" |