diff options
Diffstat (limited to 'src/orchestrator')
29 files changed, 5479 insertions, 283 deletions
diff --git a/src/orchestrator/api/add_intents_handler.go b/src/orchestrator/api/add_intents_handler.go new file mode 100644 index 00000000..dfe1a496 --- /dev/null +++ b/src/orchestrator/api/add_intents_handler.go @@ -0,0 +1,136 @@ +/* + * 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 ( + "encoding/json" + "io" + "net/http" + + moduleLib "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module" + + "github.com/gorilla/mux" +) + +type intentHandler struct { + client moduleLib.IntentManager +} + +func (h intentHandler) addIntentHandler(w http.ResponseWriter, r *http.Request) { + var i moduleLib.Intent + + err := json.NewDecoder(r.Body).Decode(&i) + switch { + case err == io.EOF: + http.Error(w, "Empty body", http.StatusBadRequest) + return + + case err != nil: + http.Error(w, err.Error(), http.StatusUnprocessableEntity) + return + } + + if i.MetaData.Name == "" { + http.Error(w, "Missing Intent in POST request", http.StatusBadRequest) + return + } + + vars := mux.Vars(r) + p := vars["project-name"] + ca := vars["composite-app-name"] + v := vars["composite-app-version"] + d := vars["deployment-intent-group-name"] + + intent, addError := h.client.AddIntent(i, p, ca, v, d) + if addError != nil { + http.Error(w, addError.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusCreated) + err = json.NewEncoder(w).Encode(intent) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} + +func (h intentHandler) getIntentHandler(w http.ResponseWriter, r *http.Request) { + + vars := mux.Vars(r) + + i := vars["intent-name"] + if i == "" { + http.Error(w, "Missing intentName in GET request", http.StatusBadRequest) + return + } + + p := vars["project-name"] + if p == "" { + http.Error(w, "Missing projectName in GET request", http.StatusBadRequest) + return + } + ca := vars["composite-app-name"] + if ca == "" { + http.Error(w, "Missing compositeAppName in GET request", http.StatusBadRequest) + return + } + + v := vars["composite-app-version"] + if v == "" { + http.Error(w, "Missing version of compositeApp in GET request", http.StatusBadRequest) + return + } + + di := vars["deployment-intent-group-name"] + if di == "" { + http.Error(w, "Missing name of DeploymentIntentGroup in GET request", http.StatusBadRequest) + return + } + + intent, err := h.client.GetIntent(i, p, ca, v, di) + 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(intent) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} + +func (h intentHandler) deleteIntentHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + + i := vars["intent-name"] + p := vars["project-name"] + ca := vars["composite-app-name"] + v := vars["composite-app-version"] + di := vars["deployment-intent-group-name"] + + err := h.client.DeleteIntent(i, p, ca, v, di) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + w.WriteHeader(http.StatusNoContent) +} diff --git a/src/orchestrator/api/api.go b/src/orchestrator/api/api.go index 1cb4299e..f3e3b177 100644 --- a/src/orchestrator/api/api.go +++ b/src/orchestrator/api/api.go @@ -1,51 +1,145 @@ /* -Copyright 2020 Intel Corporation. -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. -*/ - + * 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 ( - moduleLib "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module" - "github.com/gorilla/mux" + moduleLib "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module" ) var moduleClient *moduleLib.Client // NewRouter creates a router that registers the various urls that are supported -func NewRouter(projectClient moduleLib.ProjectManager, compositeAppClient moduleLib.CompositeAppManager) *mux.Router { +func NewRouter(projectClient moduleLib.ProjectManager, + compositeAppClient moduleLib.CompositeAppManager, + ControllerClient moduleLib.ControllerManager, + clusterClient moduleLib.ClusterManager, + genericPlacementIntentClient moduleLib.GenericPlacementIntentManager, + appIntentClient moduleLib.AppIntentManager, + deploymentIntentGrpClient moduleLib.DeploymentIntentGroupManager, + intentClient moduleLib.IntentManager) *mux.Router { router := mux.NewRouter().PathPrefix("/v2").Subrouter() + moduleClient = moduleLib.NewClient() + + //setting routes for project if projectClient == nil { projectClient = moduleClient.Project + } projHandler := projectHandler{ client: projectClient, } + if ControllerClient == nil { + ControllerClient = moduleClient.Controller + } + 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") + //setting routes for compositeApp if compositeAppClient == nil { compositeAppClient = moduleClient.CompositeApp } compAppHandler := compositeAppHandler{ client: compositeAppClient, } - router.HandleFunc("/projects/{project-name}/composite-apps", compAppHandler.createHandler).Methods("POST") router.HandleFunc("/projects/{project-name}/composite-apps/{composite-app-name}/{version}", compAppHandler.getHandler).Methods("GET") router.HandleFunc("/projects/{project-name}/composite-apps/{composite-app-name}/{version}", compAppHandler.deleteHandler).Methods("DELETE") + router.HandleFunc("/controllers", controlHandler.createHandler).Methods("POST") + 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") + //setting routes for genericPlacementIntent + if genericPlacementIntentClient == nil { + genericPlacementIntentClient = moduleClient.GenericPlacementIntent + } + + genericPlacementIntentHandler := genericPlacementIntentHandler{ + client: genericPlacementIntentClient, + } + router.HandleFunc("/projects/{project-name}/composite-apps/{composite-app-name}/{composite-app-version}/generic-placement-intents", genericPlacementIntentHandler.createGenericPlacementIntentHandler).Methods("POST") + router.HandleFunc("/projects/{project-name}/composite-apps/{composite-app-name}/{composite-app-version}/generic-placement-intents/{intent-name}", genericPlacementIntentHandler.getGenericPlacementHandler).Methods("GET") + router.HandleFunc("/projects/{project-name}/composite-apps/{composite-app-name}/{composite-app-version}/generic-placement-intents/{intent-name}", genericPlacementIntentHandler.deleteGenericPlacementHandler).Methods("DELETE") + + //setting routes for AppIntent + if appIntentClient == nil { + appIntentClient = moduleClient.AppIntent + } + + appIntentHandler := appIntentHandler{ + client: appIntentClient, + } + + router.HandleFunc("/projects/{project-name}/composite-apps/{composite-app-name}/{composite-app-version}/generic-placement-intents/{intent-name}/app-intents", appIntentHandler.createAppIntentHandler).Methods("POST") + router.HandleFunc("/projects/{project-name}/composite-apps/{composite-app-name}/{composite-app-version}/generic-placement-intents/{intent-name}/app-intents/{app-intent-name}", appIntentHandler.getAppIntentHandler).Methods("GET") + router.HandleFunc("/projects/{project-name}/composite-apps/{composite-app-name}/{composite-app-version}/generic-placement-intents/{intent-name}/app-intents/{app-intent-name}", appIntentHandler.deleteAppIntentHandler).Methods("DELETE") + + //setting routes for deploymentIntentGroup + if deploymentIntentGrpClient == nil { + deploymentIntentGrpClient = moduleClient.DeploymentIntentGroup + } + + deploymentIntentGrpHandler := deploymentIntentGroupHandler{ + client: deploymentIntentGrpClient, + } + router.HandleFunc("/projects/{project-name}/composite-apps/{composite-app-name}/{composite-app-version}/deployment-intent-groups", deploymentIntentGrpHandler.createDeploymentIntentGroupHandler).Methods("POST") + router.HandleFunc("/projects/{project-name}/composite-apps/{composite-app-name}/{composite-app-version}/deployment-intent-groups/{deployment-intent-group-name}", deploymentIntentGrpHandler.getDeploymentIntentGroupHandler).Methods("GET") + router.HandleFunc("/projects/{project-name}/composite-apps/{composite-app-name}/{composite-app-version}/deployment-intent-groups/{deployment-intent-group-name}", deploymentIntentGrpHandler.deleteDeploymentIntentGroupHandler).Methods("DELETE") + + // setting routes for AddingIntents + if intentClient == nil { + intentClient = moduleClient.Intent + } + + intentHandler := intentHandler{ + client: intentClient, + } + + router.HandleFunc("/projects/{project-name}/composite-apps/{composite-app-name}/{composite-app-version}/deployment-intent-groups/{deployment-intent-group-name}/intents", intentHandler.addIntentHandler).Methods("POST") + router.HandleFunc("/projects/{project-name}/composite-apps/{composite-app-name}/{composite-app-version}/deployment-intent-groups/{deployment-intent-group-name}/intents/{intent-name}", intentHandler.getIntentHandler).Methods("GET") + router.HandleFunc("/projects/{project-name}/composite-apps/{composite-app-name}/{composite-app-version}/deployment-intent-groups/{deployment-intent-group-name}/intents/{intent-name}", intentHandler.deleteIntentHandler).Methods("DELETE") + return router } diff --git a/src/orchestrator/api/app_intent_handler.go b/src/orchestrator/api/app_intent_handler.go new file mode 100644 index 00000000..ab510c8e --- /dev/null +++ b/src/orchestrator/api/app_intent_handler.go @@ -0,0 +1,138 @@ +/* + * 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 ( + "encoding/json" + "github.com/gorilla/mux" + moduleLib "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module" + "io" + "net/http" +) + +/* Used to store backend implementation objects +Also simplifies mocking for unit testing purposes +*/ +type appIntentHandler struct { + client moduleLib.AppIntentManager +} + +// createAppIntentHandler handles the create operation of intent +func (h appIntentHandler) createAppIntentHandler(w http.ResponseWriter, r *http.Request) { + + var a moduleLib.AppIntent + + err := json.NewDecoder(r.Body).Decode(&a) + switch { + case err == io.EOF: + http.Error(w, "Empty body", http.StatusBadRequest) + return + case err != nil: + http.Error(w, err.Error(), http.StatusUnprocessableEntity) + return + } + + if a.MetaData.Name == "" { + http.Error(w, "Missing AppIntentName in POST request", http.StatusBadRequest) + return + } + + vars := mux.Vars(r) + projectName := vars["project-name"] + compositeAppName := vars["composite-app-name"] + version := vars["composite-app-version"] + intent := vars["intent-name"] + + appIntent, createErr := h.client.CreateAppIntent(a, projectName, compositeAppName, version, intent) + if createErr != nil { + http.Error(w, createErr.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusCreated) + err = json.NewEncoder(w).Encode(appIntent) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} + +func (h appIntentHandler) getAppIntentHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + + p := vars["project-name"] + if p == "" { + http.Error(w, "Missing projectName in GET request", http.StatusBadRequest) + return + } + ca := vars["composite-app-name"] + if ca == "" { + http.Error(w, "Missing compositeAppName in GET request", http.StatusBadRequest) + return + } + + v := vars["composite-app-version"] + if v == "" { + http.Error(w, "Missing version of compositeApp in GET request", http.StatusBadRequest) + return + } + + i := vars["intent-name"] + if i == "" { + http.Error(w, "Missing genericPlacementIntentName in GET request", http.StatusBadRequest) + return + } + + ai := vars["app-intent-name"] + if ai == "" { + http.Error(w, "Missing appIntentName in GET request", http.StatusBadRequest) + return + } + + appIntent, err := h.client.GetAppIntent(ai, p, ca, v, i) + 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(appIntent) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + +} + +func (h appIntentHandler) deleteAppIntentHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + + p := vars["project-name"] + ca := vars["composite-app-name"] + v := vars["composite-app-version"] + i := vars["intent-name"] + ai := vars["app-intent-name"] + + err := h.client.DeleteAppIntent(ai, p, ca, v, i) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + w.WriteHeader(http.StatusNoContent) +} diff --git a/src/orchestrator/api/clusterhandler.go b/src/orchestrator/api/clusterhandler.go new file mode 100644 index 00000000..ac4191e1 --- /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 + } + 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 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..a32bf021 --- /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, nil, nil, nil, nil)) + + //Check returned code + if resp.StatusCode != testCase.expectedCode { + t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode) + } + + //Check returned body only if 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, nil, nil, nil, nil)) + + //Check returned code + if resp.StatusCode != testCase.expectedCode { + t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode) + } + + //Check returned body only if statusOK + if resp.StatusCode == http.StatusOK { + got := []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, nil, nil, nil, nil)) + + //Check returned code + if resp.StatusCode != testCase.expectedCode { + t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode) + } + + //Check returned body only if statusOK + if resp.StatusCode == http.StatusOK { + got := 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, nil, nil, nil, nil)) + + //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, nil, nil, nil, nil)) + + //Check returned code + if resp.StatusCode != testCase.expectedCode { + t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode) + } + + //Check returned body only if 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, nil, nil, nil, nil)) + + //Check returned code + if resp.StatusCode != testCase.expectedCode { + t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode) + } + + //Check returned body only if statusOK + if resp.StatusCode == http.StatusOK { + got := []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, nil, nil, nil, nil)) + + //Check returned code + if resp.StatusCode != testCase.expectedCode { + t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode) + } + + //Check returned body only if statusOK + if resp.StatusCode == http.StatusOK { + got := 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, nil, nil, nil, nil)) + + //Check returned code + if resp.StatusCode != testCase.expectedCode { + t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode) + } + + //Check returned body only if statusOK + if resp.StatusCode == http.StatusOK { + 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, nil, nil, nil, nil)) + + //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, nil, nil, nil, nil)) + + //Check returned code + if resp.StatusCode != testCase.expectedCode { + t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode) + } + + //Check returned body only if 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, nil, nil, nil, nil)) + + //Check returned code + if resp.StatusCode != testCase.expectedCode { + t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode) + } + + //Check returned body only if statusOK + if resp.StatusCode == http.StatusOK { + got := []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, nil, nil, nil, nil)) + + //Check returned code + if resp.StatusCode != testCase.expectedCode { + t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode) + } + + //Check returned body only if statusOK + if resp.StatusCode == http.StatusOK { + got := 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, nil, nil, nil, nil)) + + //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, nil, nil, nil, nil)) + + //Check returned code + if resp.StatusCode != testCase.expectedCode { + t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode) + } + + //Check returned body only if 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, nil, nil, nil, nil)) + + //Check returned code + if resp.StatusCode != testCase.expectedCode { + t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode) + } + + //Check returned body only if statusOK + if resp.StatusCode == http.StatusOK { + got := []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, nil, nil, nil, nil)) + + //Check returned code + if resp.StatusCode != testCase.expectedCode { + t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode) + } + + //Check returned body only if statusOK + if resp.StatusCode == http.StatusOK { + got := 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, nil, nil, nil, nil)) + + //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.go b/src/orchestrator/api/controllerhandler.go new file mode 100644 index 00000000..4f98c023 --- /dev/null +++ b/src/orchestrator/api/controllerhandler.go @@ -0,0 +1,104 @@ +/* + * 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 ( + "encoding/json" + "io" + "net/http" + + "github.com/gorilla/mux" + moduleLib "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module" +) + +// Used to store backend implementations objects +// Also simplifies mocking for unit testing purposes +type controllerHandler struct { + // Interface that implements controller operations + // We will set this variable with a mock interface for testing + client moduleLib.ControllerManager +} + +// Create handles creation of the controller entry in the database +func (h controllerHandler) createHandler(w http.ResponseWriter, r *http.Request) { + var m moduleLib.Controller + + err := json.NewDecoder(r.Body).Decode(&m) + 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 m.Name == "" { + http.Error(w, "Missing name in POST request", http.StatusBadRequest) + return + } + + ret, err := h.client.CreateController(m) + 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 controller Name +// Returns a controller +func (h controllerHandler) getHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + name := vars["controller-name"] + + ret, err := h.client.GetController(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 controller Name +func (h controllerHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + name := vars["controller-name"] + + err := h.client.DeleteController(name) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusNoContent) +} diff --git a/src/orchestrator/api/controllerhandler_test.go b/src/orchestrator/api/controllerhandler_test.go new file mode 100644 index 00000000..dd542de7 --- /dev/null +++ b/src/orchestrator/api/controllerhandler_test.go @@ -0,0 +1,233 @@ +/* + * 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" + "net/http" + "net/http/httptest" + "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 mockControllerManager struct { + // Items and err will be used to customize each test + // via a localized instantiation of mockControllerManager + Items []moduleLib.Controller + Err error +} + +func (m *mockControllerManager) CreateController(inp moduleLib.Controller) (moduleLib.Controller, error) { + if m.Err != nil { + return moduleLib.Controller{}, m.Err + } + + return m.Items[0], nil +} + +func (m *mockControllerManager) GetController(name string) (moduleLib.Controller, error) { + if m.Err != nil { + return moduleLib.Controller{}, m.Err + } + + return m.Items[0], nil +} + +func (m *mockControllerManager) DeleteController(name string) error { + return m.Err +} + +func TestControllerCreateHandler(t *testing.T) { + testCases := []struct { + label string + reader io.Reader + expected moduleLib.Controller + expectedCode int + controllerClient *mockControllerManager + }{ + { + label: "Missing Body Failure", + expectedCode: http.StatusBadRequest, + controllerClient: &mockControllerManager{}, + }, + { + label: "Create Controller", + expectedCode: http.StatusCreated, + reader: bytes.NewBuffer([]byte(`{ + "name":"testController", + "ip-address":"10.188.234.1", + "port":8080 + }`)), + expected: moduleLib.Controller{ + Name: "testController", + Host: "10.188.234.1", + Port: 8080, + }, + controllerClient: &mockControllerManager{ + //Items that will be returned by the mocked Client + Items: []moduleLib.Controller{ + { + Name: "testController", + Host: "10.188.234.1", + Port: 8080, + }, + }, + }, + }, + { + label: "Missing Controller Name in Request Body", + reader: bytes.NewBuffer([]byte(`{ + "description":"test description" + }`)), + expectedCode: http.StatusBadRequest, + controllerClient: &mockControllerManager{}, + }, + } + + 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, nil, nil, nil, nil, nil)) + + //Check returned code + if resp.StatusCode != testCase.expectedCode { + t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode) + } + + //Check returned body only if statusCreated + if resp.StatusCode == http.StatusCreated { + got := moduleLib.Controller{} + 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 TestControllerGetHandler(t *testing.T) { + + testCases := []struct { + label string + expected moduleLib.Controller + name, version string + expectedCode int + controllerClient *mockControllerManager + }{ + { + label: "Get Controller", + expectedCode: http.StatusOK, + expected: moduleLib.Controller{ + Name: "testController", + Host: "10.188.234.1", + Port: 8080, + }, + name: "testController", + controllerClient: &mockControllerManager{ + Items: []moduleLib.Controller{ + { + Name: "testController", + Host: "10.188.234.1", + Port: 8080, + }, + }, + }, + }, + { + label: "Get Non-Existing Controller", + expectedCode: http.StatusInternalServerError, + name: "nonexistingController", + controllerClient: &mockControllerManager{ + Items: []moduleLib.Controller{}, + Err: pkgerrors.New("Internal Error"), + }, + }, + } + + 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, nil, nil, nil, nil, nil)) + + //Check returned code + if resp.StatusCode != testCase.expectedCode { + t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode) + } + + //Check returned body only if statusOK + if resp.StatusCode == http.StatusOK { + got := moduleLib.Controller{} + 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 TestControllerDeleteHandler(t *testing.T) { + + testCases := []struct { + label string + name string + version string + expectedCode int + controllerClient *mockControllerManager + }{ + { + label: "Delete Controller", + expectedCode: http.StatusNoContent, + name: "testController", + controllerClient: &mockControllerManager{}, + }, + { + label: "Delete Non-Existing Controller", + expectedCode: http.StatusInternalServerError, + name: "testController", + controllerClient: &mockControllerManager{ + Err: pkgerrors.New("Internal Error"), + }, + }, + } + + 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, nil, nil, nil, nil, nil)) + + //Check returned code + if resp.StatusCode != testCase.expectedCode { + t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode) + } + }) + } +} diff --git a/src/orchestrator/api/deployment_intent_groups_handler.go b/src/orchestrator/api/deployment_intent_groups_handler.go new file mode 100644 index 00000000..3f5b3969 --- /dev/null +++ b/src/orchestrator/api/deployment_intent_groups_handler.go @@ -0,0 +1,133 @@ +/* + * 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 ( + "encoding/json" + "io" + "net/http" + + moduleLib "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module" + + "github.com/gorilla/mux" +) + +/* Used to store backend implementation objects +Also simplifies mocking for unit testing purposes +*/ +type deploymentIntentGroupHandler struct { + client moduleLib.DeploymentIntentGroupManager +} + +// createDeploymentIntentGroupHandler handles the create operation of DeploymentIntentGroup +func (h deploymentIntentGroupHandler) createDeploymentIntentGroupHandler(w http.ResponseWriter, r *http.Request) { + + var d moduleLib.DeploymentIntentGroup + + err := json.NewDecoder(r.Body).Decode(&d) + switch { + case err == io.EOF: + http.Error(w, "Empty body", http.StatusBadRequest) + return + case err != nil: + http.Error(w, err.Error(), http.StatusUnprocessableEntity) + return + } + + if d.MetaData.Name == "" { + http.Error(w, "Missing deploymentIntentGroupName in POST request", http.StatusBadRequest) + return + } + + vars := mux.Vars(r) + projectName := vars["project-name"] + compositeAppName := vars["composite-app-name"] + version := vars["composite-app-version"] + + dIntent, createErr := h.client.CreateDeploymentIntentGroup(d, projectName, compositeAppName, version) + if createErr != nil { + http.Error(w, createErr.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusCreated) + err = json.NewEncoder(w).Encode(dIntent) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} + +func (h deploymentIntentGroupHandler) getDeploymentIntentGroupHandler(w http.ResponseWriter, r *http.Request) { + + vars := mux.Vars(r) + + p := vars["project-name"] + if p == "" { + http.Error(w, "Missing projectName in GET request", http.StatusBadRequest) + return + } + ca := vars["composite-app-name"] + if ca == "" { + http.Error(w, "Missing compositeAppName in GET request", http.StatusBadRequest) + return + } + + v := vars["composite-app-version"] + if v == "" { + http.Error(w, "Missing version of compositeApp in GET request", http.StatusBadRequest) + return + } + + di := vars["deployment-intent-group-name"] + if v == "" { + http.Error(w, "Missing name of DeploymentIntentGroup in GET request", http.StatusBadRequest) + return + } + + dIntentGrp, err := h.client.GetDeploymentIntentGroup(di, p, ca, v) + 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(dIntentGrp) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + +} + +func (h deploymentIntentGroupHandler) deleteDeploymentIntentGroupHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + + p := vars["project-name"] + ca := vars["composite-app-name"] + v := vars["composite-app-version"] + di := vars["deployment-intent-group-name"] + + err := h.client.DeleteDeploymentIntentGroup(di, p, ca, v) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + w.WriteHeader(http.StatusNoContent) +} diff --git a/src/orchestrator/api/generic_placement_intent_handler.go b/src/orchestrator/api/generic_placement_intent_handler.go new file mode 100644 index 00000000..e186735c --- /dev/null +++ b/src/orchestrator/api/generic_placement_intent_handler.go @@ -0,0 +1,130 @@ +/* + * 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 ( + "encoding/json" + "io" + "net/http" + + moduleLib "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module" + + "github.com/gorilla/mux" +) + +/* Used to store backend implementation objects +Also simplifies mocking for unit testing purposes +*/ +type genericPlacementIntentHandler struct { + client moduleLib.GenericPlacementIntentManager +} + +// createGenericPlacementIntentHandler handles the create operation of intent +func (h genericPlacementIntentHandler) createGenericPlacementIntentHandler(w http.ResponseWriter, r *http.Request) { + + var g moduleLib.GenericPlacementIntent + + err := json.NewDecoder(r.Body).Decode(&g) + switch { + case err == io.EOF: + http.Error(w, "Empty body", http.StatusBadRequest) + return + case err != nil: + http.Error(w, err.Error(), http.StatusUnprocessableEntity) + return + } + + if g.MetaData.Name == "" { + http.Error(w, "Missing genericPlacementIntentName in POST request", http.StatusBadRequest) + return + } + + vars := mux.Vars(r) + projectName := vars["project-name"] + compositeAppName := vars["composite-app-name"] + version := vars["composite-app-version"] + + gPIntent, createErr := h.client.CreateGenericPlacementIntent(g, projectName, compositeAppName, version) + if createErr != nil { + http.Error(w, createErr.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusCreated) + err = json.NewEncoder(w).Encode(gPIntent) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} + +// getGenericPlacementHandler handles the GET operations on intent +func (h genericPlacementIntentHandler) getGenericPlacementHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + intentName := vars["intent-name"] + if intentName == "" { + http.Error(w, "Missing genericPlacementIntentName in GET request", http.StatusBadRequest) + return + } + projectName := vars["project-name"] + if projectName == "" { + http.Error(w, "Missing projectName in GET request", http.StatusBadRequest) + return + } + compositeAppName := vars["composite-app-name"] + if compositeAppName == "" { + http.Error(w, "Missing compositeAppName in GET request", http.StatusBadRequest) + return + } + + version := vars["composite-app-version"] + if version == "" { + http.Error(w, "Missing version in GET request", http.StatusBadRequest) + return + } + + gPIntent, err := h.client.GetGenericPlacementIntent(intentName, projectName, compositeAppName, version) + 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(gPIntent) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} + +// deleteGenericPlacementHandler handles the delete operations on intent +func (h genericPlacementIntentHandler) deleteGenericPlacementHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + i := vars["intent-name"] + p := vars["project-name"] + ca := vars["composite-app-name"] + v := vars["composite-app-version"] + + err := h.client.DeleteGenericPlacementIntent(i, p, ca, v) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + w.WriteHeader(http.StatusNoContent) +} diff --git a/src/orchestrator/api/projecthandler_test.go b/src/orchestrator/api/projecthandler_test.go index c76764b3..eccccb90 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)) + resp := executeRequest(request, NewRouter(testCase.projectClient, nil, nil, nil, nil, 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)) + resp := executeRequest(request, NewRouter(testCase.projectClient, nil, nil, nil, nil, 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)) + resp := executeRequest(request, NewRouter(testCase.projectClient, nil, nil, nil, nil, nil, nil, nil)) //Check returned code if resp.StatusCode != testCase.expectedCode { diff --git a/src/orchestrator/cmd/main.go b/src/orchestrator/cmd/main.go index 087caba3..9296a54c 100644 --- a/src/orchestrator/cmd/main.go +++ b/src/orchestrator/cmd/main.go @@ -22,12 +22,12 @@ import ( "os/signal" "time" + "github.com/gorilla/handlers" "github.com/onap/multicloud-k8s/src/orchestrator/api" "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/auth" "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/config" - "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" contextDb "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/contextdb" - "github.com/gorilla/handlers" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" ) func main() { @@ -40,14 +40,14 @@ func main() { log.Println(err) log.Fatalln("Exiting...") } - err = contextDb.InitializeContextDatabase() + err = contextDb.InitializeContextDatabase() if err != nil { log.Println("Unable to initialize database connection...") log.Println(err) log.Fatalln("Exiting...") } - httpRouter := api.NewRouter(nil, nil) + httpRouter := api.NewRouter(nil, nil, nil, nil, nil, nil, nil, nil) loggedRouter := handlers.LoggingHandler(os.Stdout, httpRouter) log.Println("Starting Kubernetes Multicloud API") diff --git a/src/orchestrator/go.mod b/src/orchestrator/go.mod index d6fada43..547fa8ed 100644 --- a/src/orchestrator/go.mod +++ b/src/orchestrator/go.mod @@ -1,6 +1,7 @@ module github.com/onap/multicloud-k8s/src/orchestrator require ( + github.com/coreos/etcd v3.3.12+incompatible github.com/docker/engine v0.0.0-20190620014054-c513a4c6c298 github.com/ghodss/yaml v1.0.0 github.com/gogo/protobuf v1.3.1 // indirect @@ -32,3 +33,5 @@ replace ( k8s.io/client-go => k8s.io/client-go v11.0.1-0.20190409021438-1a26190bd76a+incompatible k8s.io/cloud-provider => k8s.io/cloud-provider v0.0.0-20190409023720-1bc0c81fa51d ) + +go 1.13 diff --git a/src/orchestrator/go.sum b/src/orchestrator/go.sum index d2015406..aeab3b50 100644 --- a/src/orchestrator/go.sum +++ b/src/orchestrator/go.sum @@ -172,6 +172,7 @@ github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3Rllmb github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/onap/multicloud-k8s v0.0.0-20191115005109-f168ebb73d8d h1:3uFucXVv6gqa3H1u85CjoLOvGraREfD8/NL7m/9W9tc= github.com/onap/multicloud-k8s v0.0.0-20200131010833-90e13d101cf0 h1:2qDo6s4pdg/g7Vj6QGrCK02EP4jjwVehgEObnAfipSM= +github.com/onap/multicloud-k8s v0.0.0-20200229013830-7b566f287523 h1:hVu6djUEav5nKQvVZZa3FT71ZD9QbCcTI3dM+1chvFU= github.com/onap/multicloud-k8s/src/k8splugin v0.0.0-20191115005109-f168ebb73d8d h1:ucIEjqzNVeFPnQofeuBfUqro0OnilX//fajEFxuLsgA= github.com/onap/multicloud-k8s/src/k8splugin v0.0.0-20191115005109-f168ebb73d8d/go.mod h1:EnQd/vQGZR1/55IihaHxiux4ZUig/zfXZux7bfmU0S8= github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= diff --git a/src/orchestrator/pkg/infra/db/README.md b/src/orchestrator/pkg/infra/db/README.md index cba1b7ea..ff482b98 100644 --- a/src/orchestrator/pkg/infra/db/README.md +++ b/src/orchestrator/pkg/infra/db/README.md @@ -11,81 +11,92 @@ type Store interface { // Unmarshal implements any unmarshaling needed for the database Unmarshal(inp []byte, out interface{}) error - // Creates a new master table with key and links data with tag and - // creates a pointer to the newly added data in the master table - Create(table string, key Key, tag string, data interface{}) error + // Inserts and Updates a tag with key and also adds query fields if provided + Insert(coll string, key Key, query interface{}, tag string, data interface{}) error - // Reads data for a particular key with specific tag. - Read(table string, key Key, tag string) ([]byte, error) + // Find the document(s) with key and get the tag values from the document(s) + Find(coll string, key Key, tag string) ([][]byte, error) - // Update data for particular key with specific tag - Update(table string, key Key, tag string, data interface{}) error - - // Deletes a specific tag data for key. - // TODO: If tag is empty, it will delete all tags under key. - Delete(table string, key Key, tag string) error - - // Reads all master tables and data from the specified tag in table - ReadAll(table string, tag string) (map[string][]byte, error) + // Removes the document(s) matching the key + Remove(coll string, key Key) error } ``` -Therefore, `mongo.go`, `consul.go` implement the above interface and can be used as the backend as needed based on initial configuration. +With this interface multiple database types can be supported by providing backends. ## Details on Mongo Implementation `mongo.go` implements the above interface using the `go.mongodb.org/mongo-driver` package. The code converts incoming binary data and creates a new document in the database. -### Create +### Insert Arguments: ```go collection string key interface +query interface tag string data []byte ``` -Create inserts the provided `data` into the `collection` which returns an auto-generated (by `mongodb`) ID which we then associate with the `key` that is provided as one of the arguments. +Insert function inserts the provided `data` into the `collection` as a document in MongoDB. `FindOneAndUpdate` mongo API is used to achieve this with the `upsert` option set to `true`. With this if the record doesn't exist it is created and if it exists it is updated with new data for the tag. -We use the `FindOneAndUpdate` mongo API to achieve this with the `upsert` option set to `true`. -We create the following documents in mongodb for each new definition added to the database: +Key and Query parameters are assumed to be json structures with each element as part of the key. Those key-value pairs are used as the key for the document. +Internally this API takes all the fields in the Key structure and adds them as fields in the document. Query parameter works just like key and it is used to add additional fields to the document. -There is a Master Key document that contains references to other documents which are related to this `key`. +With this key the document can be quried with Mongo `Find` function for both the key fields and Query fields. -#### Master Key Entry -```json -{ - "_id" : ObjectId("5e0a8554b78a15f71d2dce7e"), - "key" : { "rbname" : "edgex", "rbversion" : "v1"}, - "defmetadata" : ObjectId("5e0a8554be261ecb57f067eb"), - "defcontent" : ObjectId("5e0a8377bcfcdd0f01dc7b0d") +This API also adds another field called "Key" field to the document. The "Key" field is concatenation of the key part of the Key parameter. Internally this is used to figure out the type of the document. + +Assumption is that all the elememts of the key structure are strings. + +#### Example of Key Structure +```go +type CompositeAppKey struct { + CompositeAppName string `json:"compositeappname"` + Version string `json:"version"` + Project string `json:"project"` } ``` -#### Metadata Key Entry -```json -{ - "_id" : ObjectId("5e0a8554be261ecb57f067eb"), - "defmetadata" : { "rbname" : "edgex", "rbversion" : "v1", "chartname" : "", "description" : "", "labels" : null } +#### Example of Key Values +```go +key := CompositeAppKey{ + CompositeAppName: "ca1", + Version: "v1", + Project: "testProject", + } +``` + +#### Example of Query Structure +```go +type Query struct { + Userdata1 string `json:"userdata1"` } ``` -#### Definition Content +#### Example of Document store in MongoDB ```json { - "_id" : ObjectId("5e0a8377bcfcdd0f01dc7b0d"), - "defcontent" : "H4sICCVd3FwAA3Byb2ZpbGUxLnRhcgDt1NEKgjAUxvFd7ylG98aWOsGXiYELxLRwJvj2rbyoIPDGiuD/uzmwM9iB7Vvruvrgw7CdXHsUn6Ejm2W3aopcP9eZLYRJM1voPN+ZndAm16kVSn9onheXMLheKeGqfdM0rq07/3bfUv9PJUkiR9+H+tSVajRymM6+lEqN7njxoVSbU+z2deX388r9nWzkr8fGSt5d79pnLOZfm0f+dRrzb7P4DZD/LyDJAAAAAAAAAAAAAAAA/+0Ksq1N5QAoAAA=" + "_id":"ObjectId(" "5e54c206f53ca130893c8020" ")", + "compositeappname":"ca1", + "project":"testProject", + "version":"v1", + "compositeAppmetadata":{ + "metadata":{ + "name":"ca1", + "description":"Test ca", + "userdata1":"Data1", + "userdata2":"Data2" + }, + "spec":{ + "version":"v1" + } + }, + "key":"{compositeappname,project,version,}" } ``` -### Unmarshal - -Data in mongo is stored as `bson` which is a compressed form of `json`. We need mongo to convert the stored `bson` data to regular `json` -that we can use in our code when returned. - -We just use the `bson.Unmarshal` API to achieve this. - -### Read +### Find Arguments: ```go @@ -94,30 +105,45 @@ key interface tag string ``` -Read is straight forward and it uses the `FindOne` API to find our Mongo document based on the provided `key` and then gets the corresponding data for the given `tag`. It will return []byte which can then be passed to the `Unmarshal` function to get the desired GO object. +Find function return one or more tag data based on the Key value. If key has all the fields defined then an exact match is looked for based on the key passed in. +If some of the field value in structure are empty strings then this function returns all the documents which have the same type. (ANY operation) -### Delete +#### Example of Exact Match based on fields Key Values +```go +key := CompositeAppKey{ + CompositeAppName: "ca1", + Version: "v1", + Project: "testProject", + } +``` -Delete is similar to Read and deletes all the objectIDs being stored for a given `key` in the collection. +#### Example of Match based on some fields +This example will return all the compositeApp under project testProject. +```go +key := CompositeAppKey{ + Project: "testProject", + CompositeAppName: "", + Version: "", + + } +``` -## Testing Interfaces +NOTE: Key structure can be different from the original key and can include Query fields also. ANY operation is not supported for Query fields. -The following interface exists to allow for the development of unit tests which don't require mongo to be running. -It is mentioned so in the code as well. +### Remove +Arguments: ```go -// MongoCollection defines the a subset of MongoDB operations -// Note: This interface is defined mainly for mock testing -type MongoCollection interface { - InsertOne(ctx context.Context, document interface{}, - opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error) - FindOne(ctx context.Context, filter interface{}, - opts ...*options.FindOneOptions) *mongo.SingleResult - FindOneAndUpdate(ctx context.Context, filter interface{}, - update interface{}, opts ...*options.FindOneAndUpdateOptions) *mongo.SingleResult - DeleteOne(ctx context.Context, filter interface{}, - opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) - Find(ctx context.Context, filter interface{}, - opts ...*options.FindOptions) (*mongo.Cursor, error) -} -```
\ No newline at end of file +collection string +key interface +``` +Similar to find. This will remove one or more documents based on the key structure. + +### Unmarshal + +Data in mongo is stored as `bson` which is a compressed form of `json`. We need mongo to convert the stored `bson` data to regular `json` +that we can use in our code when returned. + +`bson.Unmarshal` API is used to achieve this. + + diff --git a/src/orchestrator/pkg/infra/db/mock.go b/src/orchestrator/pkg/infra/db/mock.go index 1dbca4b4..79366d10 100644 --- a/src/orchestrator/pkg/infra/db/mock.go +++ b/src/orchestrator/pkg/infra/db/mock.go @@ -15,6 +15,7 @@ package db import ( "encoding/json" + "fmt" pkgerrors "github.com/pkg/errors" ) @@ -44,6 +45,10 @@ func (m *MockDB) Create(table string, key Key, tag string, data interface{}) err return m.Err } +func (m *MockDB) Insert(table string, key Key, query interface{}, tag string, data interface{}) error { + return m.Err +} + func (m *MockDB) Update(table string, key Key, tag string, data interface{}) error { return m.Err } @@ -62,8 +67,9 @@ func (m *MockDB) Read(table string, key Key, tag string) ([]byte, error) { return nil, m.Err } + str := fmt.Sprintf("%v", key) for k, v := range m.Items { - if k == key.String() { + if k == str { return v[tag], nil } } @@ -71,24 +77,22 @@ func (m *MockDB) Read(table string, key Key, tag string) ([]byte, error) { return nil, m.Err } -func (m *MockDB) Delete(table string, key Key, tag string) error { - return m.Err -} - -func (m *MockDB) ReadAll(table string, tag string) (map[string][]byte, error) { +func (m *MockDB) Find(table string, key Key, tag string) ([][]byte, error) { if m.Err != nil { return nil, m.Err } - ret := make(map[string][]byte) - + str := fmt.Sprintf("%v", key) for k, v := range m.Items { - for k1, v1 := range v { - if k1 == tag { - ret[k] = v1 - } + if k == str { + + return [][]byte{v[tag]}, nil } } - return ret, nil + return nil, m.Err +} + +func (m *MockDB) Delete(table string, key Key, tag string) error { + return m.Err } diff --git a/src/orchestrator/pkg/infra/db/mongo.go b/src/orchestrator/pkg/infra/db/mongo.go index 32d0b549..30eb899f 100644 --- a/src/orchestrator/pkg/infra/db/mongo.go +++ b/src/orchestrator/pkg/infra/db/mongo.go @@ -17,7 +17,9 @@ package db import ( + "encoding/json" "log" + "sort" "golang.org/x/net/context" @@ -41,8 +43,12 @@ type MongoCollection interface { update interface{}, opts ...*options.FindOneAndUpdateOptions) *mongo.SingleResult DeleteOne(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) + DeleteMany(ctx context.Context, filter interface{}, + opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error) + UpdateOne(ctx context.Context, filter interface{}, update interface{}, + opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) } // MongoStore is an implementation of the db.Store interface @@ -339,58 +345,218 @@ func (m *MongoStore) Delete(coll string, key Key, tag string) error { return nil } -// ReadAll is used to get all documents in db of a particular tag -func (m *MongoStore) ReadAll(coll, tag string) (map[string][]byte, error) { - if !m.validateParams(coll, tag) { - return nil, pkgerrors.New("Missing collection or tag name") +func (m *MongoStore) findFilter(key Key) (primitive.M, error) { + + var bsonMap bson.M + st, err := json.Marshal(key) + if err != nil { + return primitive.M{}, pkgerrors.Errorf("Error Marshalling key: %s", err.Error()) + } + err = json.Unmarshal([]byte(st), &bsonMap) + if err != nil { + return primitive.M{}, pkgerrors.Errorf("Error Unmarshalling key to Bson Map: %s", err.Error()) + } + filter := bson.M{ + "$and": []bson.M{bsonMap}, + } + return filter, nil +} + +func (m *MongoStore) findFilterWithKey(key Key) (primitive.M, error) { + + var bsonMap bson.M + var bsonMapFinal bson.M + st, err := json.Marshal(key) + if err != nil { + return primitive.M{}, pkgerrors.Errorf("Error Marshalling key: %s", err.Error()) + } + err = json.Unmarshal([]byte(st), &bsonMap) + if err != nil { + return primitive.M{}, pkgerrors.Errorf("Error Unmarshalling key to Bson Map: %s", err.Error()) + } + bsonMapFinal = make(bson.M) + for k, v := range bsonMap { + if v == "" { + if _, ok := bsonMapFinal["key"]; !ok { + // add type of key to filter + s, err := m.createKeyField(key) + if err != nil { + return primitive.M{}, err + } + bsonMapFinal["key"] = s + } + } else { + bsonMapFinal[k] = v + } + } + filter := bson.M{ + "$and": []bson.M{bsonMapFinal}, + } + return filter, nil +} + +func (m *MongoStore) updateFilter(key interface{}) (primitive.M, error) { + + var n map[string]string + st, err := json.Marshal(key) + if err != nil { + return primitive.M{}, pkgerrors.Errorf("Error Marshalling key: %s", err.Error()) + } + err = json.Unmarshal([]byte(st), &n) + if err != nil { + return primitive.M{}, pkgerrors.Errorf("Error Unmarshalling key to Bson Map: %s", err.Error()) + } + p := make(bson.M, len(n)) + for k, v := range n { + p[k] = v + } + filter := bson.M{ + "$set": p, + } + return filter, nil +} + +func (m *MongoStore) createKeyField(key interface{}) (string, error) { + + var n map[string]string + st, err := json.Marshal(key) + if err != nil { + return "", pkgerrors.Errorf("Error Marshalling key: %s", err.Error()) + } + err = json.Unmarshal([]byte(st), &n) + if err != nil { + return "", pkgerrors.Errorf("Error Unmarshalling key to Bson Map: %s", err.Error()) + } + var keys []string + for k := range n { + keys = append(keys, k) + } + sort.Strings(keys) + s := "{" + for _, k := range keys { + s = s + k + "," + } + s = s + "}" + return s, nil +} + +// Insert is used to insert/add element to a document +func (m *MongoStore) Insert(coll string, key Key, query interface{}, tag string, data interface{}) error { + if data == nil || !m.validateParams(coll, key, tag) { + return pkgerrors.New("No Data to store") } c := getCollection(coll, m) ctx := context.Background() - //Get all master tables in this collection - filter := bson.D{ - {"key", bson.D{ - {"$exists", true}, - }}, + filter, err := m.findFilter(key) + if err != nil { + return err } - cursor, err := c.Find(ctx, filter) + // Create and add key tag + s, err := m.createKeyField(key) if err != nil { - return nil, pkgerrors.Errorf("Error reading from database: %s", err.Error()) + return err } - defer cursorClose(ctx, cursor) + _, err = decodeBytes( + c.FindOneAndUpdate( + ctx, + filter, + bson.D{ + {"$set", bson.D{ + {tag, data}, + {"key", s}, + }}, + }, + options.FindOneAndUpdate().SetUpsert(true).SetReturnDocument(options.After))) - //Iterate over all the master tables - result := make(map[string][]byte) - for cursorNext(ctx, cursor) { - d := cursor.Current + if err != nil { + return pkgerrors.Errorf("Error updating master table: %s", err.Error()) + } + if query == nil { + return nil + } - //Read key of each master table - key, ok := d.Lookup("key").DocumentOK() - if !ok { - //Throw error if key is not found - pkgerrors.New("Unable to read key from mastertable") - } + // Update to add Query fields + update, err := m.updateFilter(query) + if err != nil { + return err + } + _, err = c.UpdateOne( + ctx, + filter, + update) - //Get objectID of tag document - tid, ok := d.Lookup(tag).ObjectIDOK() - if !ok { - log.Printf("Did not find tag: %s", tag) - continue - } + if err != nil { + return pkgerrors.Errorf("Error updating Query fields: %s", err.Error()) + } + return nil +} - //Find tag document and unmarshal it into []byte - tagData, err := decodeBytes(c.FindOne(ctx, bson.D{{"_id", tid}})) - if err != nil { - log.Printf("Unable to decode tag data %s", err.Error()) - continue - } - result[key.String()] = tagData.Lookup(tag).Value +// Find method returns the data stored for this key and for this particular tag +func (m *MongoStore) Find(coll string, key Key, tag string) ([][]byte, error) { + + //result, err := m.findInternal(coll, key, tag, "") + //return result, err + if !m.validateParams(coll, key, tag) { + return nil, pkgerrors.New("Mandatory fields are missing") } + c := getCollection(coll, m) + ctx := context.Background() + + filter, err := m.findFilterWithKey(key) + if err != nil { + return nil, err + } + // Find only the field requested + projection := bson.D{ + {tag, 1}, + {"_id", 0}, + } + + cursor, err := c.Find(context.Background(), filter, options.Find().SetProjection(projection)) + if err != nil { + return nil, pkgerrors.Errorf("Error finding element: %s", err.Error()) + } + defer cursorClose(ctx, cursor) + var data []byte + var result [][]byte + for cursorNext(ctx, cursor) { + d := cursor.Current + switch d.Lookup(tag).Type { + case bson.TypeString: + data = []byte(d.Lookup(tag).StringValue()) + default: + r, err := d.LookupErr(tag) + if err != nil { + // Throw error if not found + pkgerrors.New("Unable to read data ") + } + data = r.Value + } + result = append(result, data) + } if len(result) == 0 { return result, pkgerrors.Errorf("Did not find any objects with tag: %s", tag) } - return result, nil } + +// Remove method to remove the documet by key +func (m *MongoStore) Remove(coll string, key Key) error { + if !m.validateParams(coll, key) { + return pkgerrors.New("Mandatory fields are missing") + } + c := getCollection(coll, m) + ctx := context.Background() + filter, err := m.findFilterWithKey(key) + if err != nil { + return err + } + _, err = c.DeleteMany(ctx, filter) + if err != nil { + return pkgerrors.Errorf("Error Deleting from database: %s", err.Error()) + } + return nil +} diff --git a/src/orchestrator/pkg/infra/db/mongo_test.go b/src/orchestrator/pkg/infra/db/mongo_test.go index 171c908f..9f813ca4 100644 --- a/src/orchestrator/pkg/infra/db/mongo_test.go +++ b/src/orchestrator/pkg/infra/db/mongo_test.go @@ -19,7 +19,6 @@ package db import ( "bytes" "context" - "reflect" "strings" "testing" @@ -70,6 +69,16 @@ func (c *mockCollection) Find(ctx context.Context, filter interface{}, return c.mCursor, c.Err } +func (c *mockCollection) DeleteMany(ctx context.Context, filter interface{}, + opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) { + return nil, c.Err +} + +func (c *mockCollection) UpdateOne(ctx context.Context, filter interface{}, update interface{}, + opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) { + return nil, c.Err +} + func TestCreate(t *testing.T) { testCases := []struct { label string @@ -454,144 +463,3 @@ func TestDelete(t *testing.T) { }) } } - -func TestReadAll(t *testing.T) { - testCases := []struct { - label string - input map[string]interface{} - mockColl *mockCollection - bson bson.Raw - expectedError string - expected map[string][]byte - }{ - { - label: "Successfully Read all entries", - input: map[string]interface{}{ - "coll": "collname", - "tag": "metadata", - }, - mockColl: &mockCollection{ - mCursor: &mongo.Cursor{ - // Binary form of - // { - // "_id" : ObjectId("5c115156777ff85654248ae1"), - // "key" : bson.D{{"name","testdef"},{"version","v1"}}, - // "metadata" : ObjectId("5c115156c9755047e318bbfd") - // } - - Current: bson.Raw{ - '\x58', '\x00', '\x00', '\x00', '\x03', '\x6b', '\x65', '\x79', - '\x00', '\x27', '\x00', '\x00', '\x00', '\x02', '\x6e', '\x61', - '\x6d', '\x65', '\x00', '\x08', '\x00', '\x00', '\x00', '\x74', - '\x65', '\x73', '\x74', '\x64', '\x65', '\x66', '\x00', '\x02', - '\x76', '\x65', '\x72', '\x73', '\x69', '\x6f', '\x6e', '\x00', - '\x03', '\x00', '\x00', '\x00', '\x76', '\x31', '\x00', '\x00', - '\x07', '\x6d', '\x65', '\x74', '\x61', '\x64', '\x61', '\x74', - '\x61', '\x00', '\x5c', '\x11', '\x51', '\x56', '\x77', '\x7f', - '\xf8', '\x56', '\x54', '\x24', '\x8a', '\xe1', '\x07', '\x5f', - '\x69', '\x64', '\x00', '\x5c', '\x11', '\x51', '\x56', '\x77', - '\x7f', '\xf8', '\x56', '\x54', '\x24', '\x8a', '\xe1', '\x00', - }, - }, - mCursorCount: 1, - }, - expected: map[string][]byte{ - `{"name": "testdef","version": "v1"}`: []byte{ - 92, 17, 81, 86, 119, 127, 248, 86, 84, 36, 138, 225}, - }, - }, - { - label: "UnSuccessfully Read of all entries", - input: map[string]interface{}{ - "coll": "collname", - "tag": "tagName", - }, - mockColl: &mockCollection{ - Err: pkgerrors.New("DB Error"), - }, - expectedError: "DB Error", - }, - { - label: "UnSuccessfull Readall, tag not found", - input: map[string]interface{}{ - "coll": "collname", - "tag": "tagName", - }, - mockColl: &mockCollection{ - mCursor: &mongo.Cursor{ - // Binary form of - // { - // "_id" : ObjectId("5c115156777ff85654248ae1"), - // "key" : bson.D{{"name","testdef"},{"version","v1"}}, - // "metadata" : ObjectId("5c115156c9755047e318bbfd") - // } - Current: bson.Raw{ - '\x58', '\x00', '\x00', '\x00', '\x03', '\x6b', '\x65', '\x79', - '\x00', '\x27', '\x00', '\x00', '\x00', '\x02', '\x6e', '\x61', - '\x6d', '\x65', '\x00', '\x08', '\x00', '\x00', '\x00', '\x74', - '\x65', '\x73', '\x74', '\x64', '\x65', '\x66', '\x00', '\x02', - '\x76', '\x65', '\x72', '\x73', '\x69', '\x6f', '\x6e', '\x00', - '\x03', '\x00', '\x00', '\x00', '\x76', '\x31', '\x00', '\x00', - '\x07', '\x6d', '\x65', '\x74', '\x61', '\x64', '\x61', '\x74', - '\x61', '\x00', '\x5c', '\x11', '\x51', '\x56', '\x77', '\x7f', - '\xf8', '\x56', '\x54', '\x24', '\x8a', '\xe1', '\x07', '\x5f', - '\x69', '\x64', '\x00', '\x5c', '\x11', '\x51', '\x56', '\x77', - '\x7f', '\xf8', '\x56', '\x54', '\x24', '\x8a', '\xe1', '\x00', - }, - }, - mCursorCount: 1, - }, - expectedError: "Did not find any objects with tag", - }, - { - label: "Missing input fields", - input: map[string]interface{}{ - "coll": "", - "tag": "", - }, - expectedError: "Missing collection or tag name", - mockColl: &mockCollection{}, - }, - } - - for _, testCase := range testCases { - t.Run(testCase.label, func(t *testing.T) { - m, _ := NewMongoStore("name", &mongo.Database{}) - // Override the getCollection function with our mocked version - getCollection = func(coll string, m *MongoStore) MongoCollection { - return testCase.mockColl - } - - decodeBytes = func(sr *mongo.SingleResult) (bson.Raw, error) { - return testCase.mockColl.mCursor.Current, testCase.mockColl.Err - } - - cursorNext = func(ctx context.Context, cursor *mongo.Cursor) bool { - if testCase.mockColl.mCursorCount > 0 { - testCase.mockColl.mCursorCount -= 1 - return true - } - return false - } - - cursorClose = func(ctx context.Context, cursor *mongo.Cursor) error { - return nil - } - - got, err := m.ReadAll(testCase.input["coll"].(string), testCase.input["tag"].(string)) - if err != nil { - if testCase.expectedError == "" { - t.Fatalf("Readall method returned an un-expected (%s)", err) - } - if !strings.Contains(string(err.Error()), testCase.expectedError) { - t.Fatalf("Readall method returned an error (%s)", err) - } - } else { - if reflect.DeepEqual(got, testCase.expected) == false { - t.Fatalf("Readall returned unexpected data: %v, expected: %v", - got, testCase.expected) - } - } - }) - } -} diff --git a/src/orchestrator/pkg/infra/db/store.go b/src/orchestrator/pkg/infra/db/store.go index 1a9632e7..0cf3ef65 100644 --- a/src/orchestrator/pkg/infra/db/store.go +++ b/src/orchestrator/pkg/infra/db/store.go @@ -29,7 +29,6 @@ var DBconn Store // that wants to use the Store interface. This allows various // db backends and key types. type Key interface { - String() string } // Store is an interface for accessing the database @@ -54,8 +53,14 @@ type Store interface { // TODO: If tag is empty, it will delete all tags under key. Delete(table string, key Key, tag string) error - // Reads all master tables and data from the specified tag in table - ReadAll(table string, tag string) (map[string][]byte, error) + // Inserts and Updates a tag with key and also adds query fields if provided + Insert(coll string, key Key, query interface{}, tag string, data interface{}) error + + // Find the document(s) with key and get the tag values from the document(s) + Find(coll string, key Key, tag string) ([][]byte, error) + + // Removes the document(s) matching the key + Remove(coll string, key Key) error } // CreateDBClient creates the DB client diff --git a/src/orchestrator/pkg/module/add_intents.go b/src/orchestrator/pkg/module/add_intents.go new file mode 100644 index 00000000..a657cce7 --- /dev/null +++ b/src/orchestrator/pkg/module/add_intents.go @@ -0,0 +1,184 @@ +/* + * 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 Addlicable 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 module + +import ( + "encoding/json" + "reflect" + + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" + + pkgerrors "github.com/pkg/errors" +) + +// Intent shall have 2 fields - MetaData and Spec +type Intent struct { + MetaData IntentMetaData `json:"metadata"` + Spec IntentSpecData `json:"spec"` +} + +// IntentMetaData has Name, Description, userdata1, userdata2 +type IntentMetaData struct { + Name string `json:"name"` + Description string `json:"description"` + UserData1 string `json:"userData1"` + UserData2 string `json:"userData2"` +} + +// IntentSpecData has Intent +type IntentSpecData struct { + Intent IntentObj `json:"intent"` +} + +// IntentObj has name of the generic placement intent +type IntentObj struct { + Generic string `json:"generic"` +} + +// ListOfIntents is a list of intents +type ListOfIntents struct { + ListOfIntents []map[string]string `json:"intent"` +} + +// IntentManager is an interface which exposes the IntentManager functionality +type IntentManager interface { + AddIntent(a Intent, p string, ca string, v string, di string) (Intent, error) + GetIntent(i string, p string, ca string, v string, di string) (Intent, error) + DeleteIntent(i string, p string, ca string, v string, di string) error +} + +// IntentKey consists of Name if the intent, Project name, CompositeApp name, +// CompositeApp version +type IntentKey struct { + Name string `json:"name"` + Project string `json:"project"` + CompositeApp string `json:"compositeapp"` + Version string `json:"version"` + DeploymentIntentGroup string `json:"deployment-intent-group-name"` +} + +// We will use json marshalling to convert to string to +// preserve the underlying structure. +func (ik IntentKey) String() string { + out, err := json.Marshal(ik) + if err != nil { + return "" + } + return string(out) +} + +// IntentClient implements the AddIntentManager interface +type IntentClient struct { + storeName string + tagMetaData string +} + +// NewIntentClient returns an instance of AddIntentClient +func NewIntentClient() *IntentClient { + return &IntentClient{ + storeName: "orchestrator", + tagMetaData: "addintent", + } +} + +// AddIntent adds a given intent to the deployment-intent-group and stores in the db. Other input parameters for it - projectName, compositeAppName, version, DeploymentIntentgroupName +func (c *IntentClient) AddIntent(a Intent, p string, ca string, v string, di string) (Intent, error) { + + //Check for the AddIntent already exists here. + res, err := c.GetIntent(a.MetaData.Name, p, ca, v, di) + if !reflect.DeepEqual(res, Intent{}) { + return Intent{}, pkgerrors.New("AppIntent already exists") + } + + //Check if project exists + _, err = NewProjectClient().GetProject(p) + if err != nil { + return Intent{}, pkgerrors.New("Unable to find the project") + } + + //check if compositeApp exists + _, err = NewCompositeAppClient().GetCompositeApp(ca, v, p) + if err != nil { + return Intent{}, pkgerrors.New("Unable to find the composite-app") + } + + //check if DeploymentIntentGroup exists + _, err = NewDeploymentIntentGroupClient().GetDeploymentIntentGroup(di, p, ca, v) + if err != nil { + return Intent{}, pkgerrors.New("Unable to find the intent") + } + + akey := IntentKey{ + Name: a.MetaData.Name, + Project: p, + CompositeApp: ca, + Version: v, + DeploymentIntentGroup: di, + } + + err = db.DBconn.Insert(c.storeName, akey, nil, c.tagMetaData, a) + if err != nil { + return Intent{}, pkgerrors.Wrap(err, "Create DB entry error") + } + return a, nil +} + +// GetIntent returns an Intent +func (c *IntentClient) GetIntent(i string, p string, ca string, v string, di string) (Intent, error) { + + k := IntentKey{ + Name: i, + Project: p, + CompositeApp: ca, + Version: v, + DeploymentIntentGroup: di, + } + + result, err := db.DBconn.Find(c.storeName, k, c.tagMetaData) + if err != nil { + return Intent{}, pkgerrors.Wrap(err, "Get AppIntent error") + } + + if result != nil { + a := Intent{} + err = db.DBconn.Unmarshal(result[0], &a) + if err != nil { + return Intent{}, pkgerrors.Wrap(err, "Unmarshalling AppIntent") + } + return a, nil + + } + return Intent{}, pkgerrors.New("Error getting AppIntent") +} + +// DeleteIntent deletes a given intent tied to project, composite app and deployment intent group +func (c IntentClient) DeleteIntent(i string, p string, ca string, v string, di string) error { + k := IntentKey{ + Name: i, + Project: p, + CompositeApp: ca, + Version: v, + DeploymentIntentGroup: di, + } + + err := db.DBconn.Remove(c.storeName, k) + if err != nil { + return pkgerrors.Wrap(err, "Delete Project entry;") + } + return nil + +} diff --git a/src/orchestrator/pkg/module/app_intent.go b/src/orchestrator/pkg/module/app_intent.go new file mode 100644 index 00000000..70c80dac --- /dev/null +++ b/src/orchestrator/pkg/module/app_intent.go @@ -0,0 +1,195 @@ +/* + * 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 module + +import ( + "encoding/json" + "reflect" + + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" + + pkgerrors "github.com/pkg/errors" +) + +// AppIntent has two components - metadata, spec +type AppIntent struct { + MetaData MetaData `json:"metadata"` + Spec SpecData `json:"spec"` +} + +// MetaData has - name, description, userdata1, userdata2 +type MetaData struct { + Name string `json:"name"` + Description string `json:"description"` + UserData1 string `json:"userData1"` + UserData2 string `json:"userData2"` +} + +// AllOf consists of AnyOfArray and ClusterNames array +type AllOf struct { + ClusterName string `json:"cluster-name,omitempty"` + ClusterLabelName string `json:"cluster-label-name,omitempty"` + AnyOfArray []AnyOf `json:"anyOf,omitempty"` +} + +// AnyOf consists of Array of ClusterLabelNames +type AnyOf struct { + ClusterName string `json:"cluster-name,omitempty"` + ClusterLabelName string `json:"cluster-label-name,omitempty"` +} + +// IntentStruc consists of AllOfArray and AnyOfArray +type IntentStruc struct { + AllOfArray []AllOf `json:"allOf,omitempty"` + AnyOfArray []AnyOf `json:"anyOf,omitempty"` +} + +// SpecData consists of appName and intent +type SpecData struct { + AppName string `json:"app-name"` + Intent IntentStruc `json:"intent"` +} + +// AppIntentManager is an interface which exposes the +// AppIntentManager functionalities +type AppIntentManager interface { + CreateAppIntent(a AppIntent, p string, ca string, v string, i string) (AppIntent, error) + GetAppIntent(ai string, p string, ca string, v string, i string) (AppIntent, error) + DeleteAppIntent(ai string, p string, ca string, v string, i string) error +} + +// AppIntentKey is used as primary key +type AppIntentKey struct { + Name string `json:"name"` + Project string `json:"project"` + CompositeApp string `json:"compositeapp"` + Version string `json:"version"` + Intent string `json:"intent-name"` +} + +// We will use json marshalling to convert to string to +// preserve the underlying structure. +func (ak AppIntentKey) String() string { + out, err := json.Marshal(ak) + if err != nil { + return "" + } + return string(out) +} + +// AppIntentClient implements the AppIntentManager interface +type AppIntentClient struct { + storeName string + tagMetaData string +} + +// NewAppIntentClient returns an instance of AppIntentClient +func NewAppIntentClient() *AppIntentClient { + return &AppIntentClient{ + storeName: "orchestrator", + tagMetaData: "appintent", + } +} + +// CreateAppIntent creates an entry for AppIntent in the db. Other input parameters for it - projectName, compositeAppName, version, intentName. +func (c *AppIntentClient) CreateAppIntent(a AppIntent, p string, ca string, v string, i string) (AppIntent, error) { + + //Check for the AppIntent already exists here. + res, err := c.GetAppIntent(a.MetaData.Name, p, ca, v, i) + if !reflect.DeepEqual(res, AppIntent{}) { + return AppIntent{}, pkgerrors.New("AppIntent already exists") + } + + //Check if project exists + _, err = NewProjectClient().GetProject(p) + if err != nil { + return AppIntent{}, pkgerrors.New("Unable to find the project") + } + + // check if compositeApp exists + _, err = NewCompositeAppClient().GetCompositeApp(ca, v, p) + if err != nil { + return AppIntent{}, pkgerrors.New("Unable to find the composite-app") + } + + // check if Intent exists + _, err = NewGenericPlacementIntentClient().GetGenericPlacementIntent(i, p, ca, v) + if err != nil { + return AppIntent{}, pkgerrors.New("Unable to find the intent") + } + + akey := AppIntentKey{ + Name: a.MetaData.Name, + Project: p, + CompositeApp: ca, + Version: v, + Intent: i, + } + + err = db.DBconn.Insert(c.storeName, akey, nil, c.tagMetaData, a) + if err != nil { + return AppIntent{}, pkgerrors.Wrap(err, "Create DB entry error") + } + + return a, nil +} + +// GetAppIntent shall take arguments - name of the app intent, name of the project, name of the composite app, version of the composite app and intent name. It shall return the AppIntent +func (c *AppIntentClient) GetAppIntent(ai string, p string, ca string, v string, i string) (AppIntent, error) { + + k := AppIntentKey{ + Name: ai, + Project: p, + CompositeApp: ca, + Version: v, + Intent: i, + } + + result, err := db.DBconn.Find(c.storeName, k, c.tagMetaData) + if err != nil { + return AppIntent{}, pkgerrors.Wrap(err, "Get AppIntent error") + } + + if result != nil { + a := AppIntent{} + err = db.DBconn.Unmarshal(result[0], &a) + if err != nil { + return AppIntent{}, pkgerrors.Wrap(err, "Unmarshalling AppIntent") + } + return a, nil + + } + return AppIntent{}, pkgerrors.New("Error getting AppIntent") +} + +// DeleteAppIntent delete an AppIntent +func (c *AppIntentClient) DeleteAppIntent(ai string, p string, ca string, v string, i string) error { + k := AppIntentKey{ + Name: ai, + Project: p, + CompositeApp: ca, + Version: v, + Intent: i, + } + + err := db.DBconn.Remove(c.storeName, k) + if err != nil { + return pkgerrors.Wrap(err, "Delete Project entry;") + } + return nil + +} diff --git a/src/orchestrator/pkg/module/app_intent_test.go b/src/orchestrator/pkg/module/app_intent_test.go new file mode 100644 index 00000000..5a4f7693 --- /dev/null +++ b/src/orchestrator/pkg/module/app_intent_test.go @@ -0,0 +1,271 @@ +/* + * 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 module + +import ( + "reflect" + "strings" + "testing" + + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" +) + +func TestCreateAppIntent(t *testing.T) { + testCases := []struct { + label string + inputAppIntent AppIntent + inputProject string + inputCompositeApp string + inputCompositeAppVersion string + inputGenericPlacementIntent string + expectedError string + mockdb *db.MockDB + expected AppIntent + }{ + { + label: "Create AppIntent", + inputAppIntent: AppIntent{ + MetaData: MetaData{ + Name: "testAppIntent", + Description: "A sample AppIntent", + UserData1: "userData1", + UserData2: "userData2", + }, + Spec: SpecData{ + AppName: "SampleApp", + Intent: IntentStruc{ + AllOfArray: []AllOf{ + { + ClusterName: "edge1", + //ClusterLabelName: "edge1", + }, + { + ClusterName: "edge2", + //ClusterLabelName: "edge2", + }, + { + AnyOfArray: []AnyOf{ + {ClusterLabelName: "east-us1"}, + {ClusterLabelName: "east-us2"}, + //{ClusterName: "east-us1"}, + //{ClusterName: "east-us2"}, + }, + }, + }, + + AnyOfArray: []AnyOf{}, + }, + }, + }, + + inputProject: "testProject", + inputCompositeApp: "testCompositeApp", + inputCompositeAppVersion: "testCompositeAppVersion", + inputGenericPlacementIntent: "testIntent", + expected: AppIntent{ + MetaData: MetaData{ + Name: "testAppIntent", + Description: "A sample AppIntent", + UserData1: "userData1", + UserData2: "userData2", + }, + Spec: SpecData{ + AppName: "SampleApp", + Intent: IntentStruc{ + AllOfArray: []AllOf{ + { + ClusterName: "edge1", + //ClusterLabelName: "edge1", + }, + { + ClusterName: "edge2", + //ClusterLabelName: "edge2", + }, + { + AnyOfArray: []AnyOf{ + {ClusterLabelName: "east-us1"}, + {ClusterLabelName: "east-us2"}, + //{ClusterName: "east-us1"}, + //{ClusterName: "east-us2"}, + }, + }, + }, + AnyOfArray: []AnyOf{}, + }, + }, + }, + expectedError: "", + mockdb: &db.MockDB{ + Items: map[string]map[string][]byte{ + ProjectKey{ProjectName: "testProject"}.String(): { + "projectmetadata": []byte( + "{\"project-name\":\"testProject\"," + + "\"description\":\"Test project for unit testing\"}"), + }, + CompositeAppKey{CompositeAppName: "testCompositeApp", + Version: "testCompositeAppVersion", Project: "testProject"}.String(): { + "compositeAppmetadata": []byte( + "{\"metadata\":{" + + "\"name\":\"testCompositeApp\"," + + "\"description\":\"description\"," + + "\"userData1\":\"user data\"," + + "\"userData2\":\"user data\"" + + "}," + + "\"spec\":{" + + "\"version\":\"version of the composite app\"}}"), + }, + GenericPlacementIntentKey{ + Name: "testIntent", + Project: "testProject", + CompositeApp: "testCompositeApp", + Version: "testCompositeAppVersion", + }.String(): { + "genericplacementintent": []byte( + "{\"metadata\":{\"Name\":\"testIntent\"," + + "\"Description\":\"A sample intent for testing\"," + + "\"UserData1\": \"userData1\"," + + "\"UserData2\": \"userData2\"}," + + "\"spec\":{\"Logical-Cloud\": \"logicalCloud1\"}}"), + }, + }, + }, + }, + } + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + db.DBconn = testCase.mockdb + appIntentCli := NewAppIntentClient() + got, err := appIntentCli.CreateAppIntent(testCase.inputAppIntent, testCase.inputProject, testCase.inputCompositeApp, testCase.inputCompositeAppVersion, testCase.inputGenericPlacementIntent) + if err != nil { + if testCase.expectedError == "" { + t.Fatalf("CreateAppIntent returned an unexpected error %s, ", err) + } + if strings.Contains(err.Error(), testCase.expectedError) == false { + t.Fatalf("CreateAppIntent returned an unexpected error %s", err) + } + } else { + if reflect.DeepEqual(testCase.expected, got) == false { + t.Errorf("CreateAppIntent returned unexpected body: got %v; "+" expected %v", got, testCase.expected) + } + } + }) + } +} + +func TestGetAppIntent(t *testing.T) { + testCases := []struct { + label string + expectedError string + expected AppIntent + mockdb *db.MockDB + appIntentName string + projectName string + compositeAppName string + compositeAppVersion string + genericPlacementIntent string + }{ + { + label: "Get Intent", + appIntentName: "testAppIntent", + projectName: "testProject", + compositeAppName: "testCompositeApp", + compositeAppVersion: "testCompositeAppVersion", + genericPlacementIntent: "testIntent", + expected: AppIntent{ + MetaData: MetaData{ + Name: "testAppIntent", + Description: "testAppIntent", + UserData1: "userData1", + UserData2: "userData2", + }, + Spec: SpecData{ + AppName: "SampleApp", + Intent: IntentStruc{ + AllOfArray: []AllOf{ + { + ClusterName: "edge1", + }, + { + ClusterName: "edge2", + }, + { + AnyOfArray: []AnyOf{ + {ClusterLabelName: "east-us1"}, + {ClusterLabelName: "east-us2"}, + }, + }, + }, + }, + }, + }, + expectedError: "", + mockdb: &db.MockDB{ + Items: map[string]map[string][]byte{ + AppIntentKey{ + Name: "testAppIntent", + Project: "testProject", + CompositeApp: "testCompositeApp", + Version: "testCompositeAppVersion", + Intent: "testIntent", + }.String(): { + "appintent": []byte( + "{\"metadata\":{\"Name\":\"testAppIntent\"," + + "\"Description\":\"testAppIntent\"," + + "\"UserData1\": \"userData1\"," + + "\"UserData2\": \"userData2\"}," + + "\"spec\":{\"app-name\": \"SampleApp\"," + + "\"intent\": {" + + "\"allOf\":[" + + "{\"cluster-name\":\"edge1\"}," + + "{\"cluster-name\":\"edge2\"}," + + "{" + + "\"anyOf\":[" + + "{" + + "\"cluster-label-name\":\"east-us1\"}," + + "{" + + "\"cluster-label-name\":\"east-us2\"}" + + "]}]" + + "}}}"), + }, + }, + }, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + db.DBconn = testCase.mockdb + appIntentCli := NewAppIntentClient() + got, err := appIntentCli.GetAppIntent(testCase.appIntentName, testCase.projectName, testCase.compositeAppName, testCase.compositeAppVersion, + testCase.genericPlacementIntent) + if err != nil { + if testCase.expectedError == "" { + t.Fatalf("GetAppIntent returned an unexpected error: %s", err) + } + if strings.Contains(err.Error(), testCase.expectedError) == false { + t.Fatalf("GetAppIntent returned an unexpected error: %s", err) + } + } else { + if reflect.DeepEqual(testCase.expected, got) == false { + t.Errorf("GetAppIntent returned unexpected body: got %v;"+ + " expected %v", got, testCase.expected) + } + } + + }) + } +} diff --git a/src/orchestrator/pkg/module/cluster.go b/src/orchestrator/pkg/module/cluster.go new file mode 100644 index 00000000..c9ddad6e --- /dev/null +++ b/src/orchestrator/pkg/module/cluster.go @@ -0,0 +1,540 @@ +/* + * 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 module + +import ( + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" + + pkgerrors "github.com/pkg/errors" +) + +// ClusterProvider contains the parameters needed for ClusterProviders +// It implements the interface for managing the ClusterProviders +type Metadata struct { + Name string `json:"name"` + Description string `json:"description"` + UserData1 string `json:"userData1"` + UserData2 string `json:"userData2"` +} + +type ClusterProvider struct { + Metadata Metadata `json:"metadata"` +} + +type Cluster struct { + Metadata Metadata `json:"metadata"` +} + +type ClusterContent struct { + Kubeconfig string `json:"kubeconfig"` +} + +type ClusterLabel struct { + LabelName string `json:"label-name"` +} + +type ClusterKvPairs struct { + Metadata 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"` +} + +// 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"` +} + +// Manager 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) + GetClusters(provider string) ([]Cluster, 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 { + storeName string + tagMeta string + tagContent string +} + +// NewClusterClient returns an instance of the ClusterClient +// which implements the Manager +func NewClusterClient() *ClusterClient { + return &ClusterClient{ + storeName: "cluster", + tagMeta: "clustermetadata", + tagContent: "clustercontent", + } +} + +// 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.storeName, key, nil, v.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.storeName, key, v.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, "Unmarshaling 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.storeName, key, v.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, "Unmarshaling 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.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.storeName, key, nil, v.tagMeta, p) + if err != nil { + return Cluster{}, pkgerrors.Wrap(err, "Creating DB Entry") + } + err = db.DBconn.Insert(v.storeName, key, nil, v.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.storeName, key, v.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, "Unmarshaling 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.storeName, key, v.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, "Unmarshaling Value") + } + return cc, nil + } + + return ClusterContent{}, pkgerrors.New("Error getting Cluster Content") +} + +// 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.storeName, key, v.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, "Unmarshaling 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 := db.DBconn.Remove(v.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.storeName, key, nil, v.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.storeName, key, v.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, "Unmarshaling 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.storeName, key, v.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, "Unmarshaling 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.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.storeName, key, nil, v.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.storeName, key, v.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, "Unmarshaling 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.storeName, key, v.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, "Unmarshaling 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.storeName, key) + if err != nil { + return pkgerrors.Wrap(err, "Delete ClusterKvPairs Entry;") + } + + return nil +} diff --git a/src/orchestrator/pkg/module/controller.go b/src/orchestrator/pkg/module/controller.go new file mode 100644 index 00000000..35d6f892 --- /dev/null +++ b/src/orchestrator/pkg/module/controller.go @@ -0,0 +1,135 @@ +/* + * 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 module + +import ( + "encoding/json" + + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" + + pkgerrors "github.com/pkg/errors" +) + +// Controller contains the parameters needed for Controllers +// It implements the interface for managing the Controllers +type Controller struct { + Name string `json:"name"` + + Host string `json:"host"` + + Port int64 `json:"port"` +} + +// ControllerKey is the key structure that is used in the database +type ControllerKey struct { + ControllerName string `json:"controller-name"` +} + +// We will use json marshalling to convert to string to +// preserve the underlying structure. +func (mk ControllerKey) String() string { + out, err := json.Marshal(mk) + if err != nil { + return "" + } + + return string(out) +} + +// ControllerManager is an interface exposes the Controller functionality +type ControllerManager interface { + CreateController(ms Controller) (Controller, error) + GetController(name string) (Controller, error) + DeleteController(name string) error +} + +// ControllerClient implements the Manager +// It will also be used to maintain some localized state +type ControllerClient struct { + collectionName string + tagMeta string +} + +// NewControllerClient returns an instance of the ControllerClient +// which implements the Manager +func NewControllerClient() *ControllerClient { + return &ControllerClient{ + collectionName: "controller", + tagMeta: "controllermetadata", + } +} + +// CreateController a new collection based on the Controller +func (mc *ControllerClient) CreateController(m Controller) (Controller, error) { + + //Construct the composite key to select the entry + key := ControllerKey{ + ControllerName: m.Name, + } + + //Check if this Controller already exists + _, err := mc.GetController(m.Name) + if err == nil { + return Controller{}, pkgerrors.New("Controller already exists") + } + + err = db.DBconn.Create(mc.collectionName, key, mc.tagMeta, m) + if err != nil { + return Controller{}, pkgerrors.Wrap(err, "Creating DB Entry") + } + + return m, nil +} + +// GetController returns the Controller for corresponding name +func (mc *ControllerClient) GetController(name string) (Controller, error) { + + //Construct the composite key to select the entry + key := ControllerKey{ + ControllerName: name, + } + value, err := db.DBconn.Read(mc.collectionName, key, mc.tagMeta) + if err != nil { + return Controller{}, pkgerrors.Wrap(err, "Get Controller") + } + + //value is a byte array + if value != nil { + microserv := Controller{} + err = db.DBconn.Unmarshal(value, µserv) + if err != nil { + return Controller{}, pkgerrors.Wrap(err, "Unmarshaling Value") + } + return microserv, nil + } + + return Controller{}, pkgerrors.New("Error getting Controller") +} + +// DeleteController the Controller from database +func (mc *ControllerClient) DeleteController(name string) error { + + //Construct the composite key to select the entry + key := ControllerKey{ + ControllerName: name, + } + err := db.DBconn.Delete(name, key, mc.tagMeta) + if err != nil { + return pkgerrors.Wrap(err, "Delete Controller Entry;") + } + return nil +} diff --git a/src/orchestrator/pkg/module/controller_test.go b/src/orchestrator/pkg/module/controller_test.go new file mode 100644 index 00000000..2e783c13 --- /dev/null +++ b/src/orchestrator/pkg/module/controller_test.go @@ -0,0 +1,181 @@ +/* + * 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 module + +import ( + "reflect" + "strings" + "testing" + + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" + + pkgerrors "github.com/pkg/errors" +) + +func TestCreateController(t *testing.T) { + testCases := []struct { + label string + inp Controller + expectedError string + mockdb *db.MockDB + expected Controller + }{ + { + label: "Create Controller", + inp: Controller{ + Name: "testController", + Host: "132.156.0.10", + Port: 8080, + }, + expected: Controller{ + Name: "testController", + Host: "132.156.0.10", + Port: 8080, + }, + expectedError: "", + mockdb: &db.MockDB{}, + }, + { + label: "Failed Create Controller", + expectedError: "Error Creating Controller", + mockdb: &db.MockDB{ + Err: pkgerrors.New("Error Creating Controller"), + }, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + db.DBconn = testCase.mockdb + impl := NewControllerClient() + got, err := impl.CreateController(testCase.inp) + if err != nil { + if testCase.expectedError == "" { + t.Fatalf("Create returned an unexpected error %s", err) + } + if strings.Contains(err.Error(), testCase.expectedError) == false { + t.Fatalf("Create returned an unexpected error %s", err) + } + } else { + if reflect.DeepEqual(testCase.expected, got) == false { + t.Errorf("Create returned unexpected body: got %v;"+ + " expected %v", got, testCase.expected) + } + } + }) + } +} + +func TestGetController(t *testing.T) { + + testCases := []struct { + label string + name string + expectedError string + mockdb *db.MockDB + inp string + expected Controller + }{ + { + label: "Get Controller", + name: "testController", + expected: Controller{ + Name: "testController", + Host: "132.156.0.10", + Port: 8080, + }, + expectedError: "", + mockdb: &db.MockDB{ + Items: map[string]map[string][]byte{ + ControllerKey{ControllerName: "testController"}.String(): { + "controllermetadata": []byte( + "{\"name\":\"testController\"," + + "\"host\":\"132.156.0.10\"," + + "\"port\":8080}"), + }, + }, + }, + }, + { + label: "Get Error", + expectedError: "DB Error", + mockdb: &db.MockDB{ + Err: pkgerrors.New("DB Error"), + }, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + db.DBconn = testCase.mockdb + impl := NewControllerClient() + got, err := impl.GetController(testCase.name) + if err != nil { + if testCase.expectedError == "" { + t.Fatalf("Get returned an unexpected error: %s", err) + } + if strings.Contains(err.Error(), testCase.expectedError) == false { + t.Fatalf("Get returned an unexpected error: %s", err) + } + } else { + if reflect.DeepEqual(testCase.expected, got) == false { + t.Errorf("Get returned unexpected body: got %v;"+ + " expected %v", got, testCase.expected) + } + } + }) + } +} + +func TestDeleteController(t *testing.T) { + + testCases := []struct { + label string + name string + expectedError string + mockdb *db.MockDB + }{ + { + label: "Delete Controller", + name: "testController", + mockdb: &db.MockDB{}, + }, + { + label: "Delete Error", + expectedError: "DB Error", + mockdb: &db.MockDB{ + Err: pkgerrors.New("DB Error"), + }, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + db.DBconn = testCase.mockdb + impl := NewControllerClient() + err := impl.DeleteController(testCase.name) + if err != nil { + if testCase.expectedError == "" { + t.Fatalf("Delete returned an unexpected error %s", err) + } + if strings.Contains(err.Error(), testCase.expectedError) == false { + t.Fatalf("Delete returned an unexpected error %s", err) + } + } + }) + } +} diff --git a/src/orchestrator/pkg/module/deployment_intent_groups.go b/src/orchestrator/pkg/module/deployment_intent_groups.go new file mode 100644 index 00000000..fe622771 --- /dev/null +++ b/src/orchestrator/pkg/module/deployment_intent_groups.go @@ -0,0 +1,177 @@ +/* + * 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 module + +import ( + "encoding/json" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" + "reflect" + + pkgerrors "github.com/pkg/errors" +) + +// DeploymentIntentGroup shall have 2 fields - MetaData and Spec +type DeploymentIntentGroup struct { + MetaData DepMetaData `json:"metadata"` + Spec DepSpecData `json:"spec"` +} + +// DepMetaData has Name, description, userdata1, userdata2 +type DepMetaData struct { + Name string `json:"name"` + Description string `json:"description"` + UserData1 string `json:"userData1"` + UserData2 string `json:"userData2"` +} + +// DepSpecData has profile, version, OverrideValuesObj +type DepSpecData struct { + Profile string `json:"profile"` + Version string `json:"version"` + OverrideValuesObj []OverrideValues `json:"override-values"` +} + +// OverrideValues has appName and ValuesObj +type OverrideValues struct { + AppName string `json:"app-name"` + ValuesObj map[string]string `json:"values"` +} + +// Values has ImageRepository +// type Values struct { +// ImageRepository string `json:"imageRepository"` +// } + +// DeploymentIntentGroupManager is an interface which exposes the DeploymentIntentGroupManager functionality +type DeploymentIntentGroupManager interface { + CreateDeploymentIntentGroup(d DeploymentIntentGroup, p string, ca string, v string) (DeploymentIntentGroup, error) + GetDeploymentIntentGroup(di string, p string, ca string, v string) (DeploymentIntentGroup, error) + DeleteDeploymentIntentGroup(di string, p string, ca string, v string) error +} + +// DeploymentIntentGroupKey consists of Name of the deployment group, project name, CompositeApp name, CompositeApp version +type DeploymentIntentGroupKey struct { + Name string `json:"name"` + Project string `json:"project"` + CompositeApp string `json:"compositeapp"` + Version string `json:"version"` +} + +// We will use json marshalling to convert to string to +// preserve the underlying structure. +func (dk DeploymentIntentGroupKey) String() string { + out, err := json.Marshal(dk) + if err != nil { + return "" + } + return string(out) +} + +// DeploymentIntentGroupClient implements the DeploymentIntentGroupManager interface +type DeploymentIntentGroupClient struct { + storeName string + tagMetaData string +} + +// NewDeploymentIntentGroupClient return an instance of DeploymentIntentGroupClient which implements DeploymentIntentGroupManager +func NewDeploymentIntentGroupClient() *DeploymentIntentGroupClient { + return &DeploymentIntentGroupClient{ + storeName: "orchestrator", + tagMetaData: "deploymentintentgroup", + } +} + +// CreateDeploymentIntentGroup creates an entry for a given DeploymentIntentGroup in the database. Other Input parameters for it - projectName, compositeAppName, version +func (c *DeploymentIntentGroupClient) CreateDeploymentIntentGroup(d DeploymentIntentGroup, p string, ca string, + v string) (DeploymentIntentGroup, error) { + + res, err := c.GetDeploymentIntentGroup(d.MetaData.Name, p, ca, v) + if !reflect.DeepEqual(res, DeploymentIntentGroup{}) { + return DeploymentIntentGroup{}, pkgerrors.New("AppIntent already exists") + } + + //Check if project exists + _, err = NewProjectClient().GetProject(p) + if err != nil { + return DeploymentIntentGroup{}, pkgerrors.New("Unable to find the project") + } + + //check if compositeApp exists + _, err = NewCompositeAppClient().GetCompositeApp(ca, v, p) + if err != nil { + return DeploymentIntentGroup{}, pkgerrors.New("Unable to find the composite-app") + } + + gkey := DeploymentIntentGroupKey{ + Name: d.MetaData.Name, + Project: p, + CompositeApp: ca, + Version: v, + } + + err = db.DBconn.Insert(c.storeName, gkey, nil, c.tagMetaData, d) + if err != nil { + return DeploymentIntentGroup{}, pkgerrors.Wrap(err, "Create DB entry error") + } + + return d, nil +} + +// GetDeploymentIntentGroup returns the DeploymentIntentGroup with a given name, project, compositeApp and version of compositeApp +func (c *DeploymentIntentGroupClient) GetDeploymentIntentGroup(di string, p string, ca string, v string) (DeploymentIntentGroup, error) { + + key := DeploymentIntentGroupKey{ + Name: di, + Project: p, + CompositeApp: ca, + Version: v, + } + + result, err := db.DBconn.Find(c.storeName, key, c.tagMetaData) + if err != nil { + return DeploymentIntentGroup{}, pkgerrors.Wrap(err, "Get DeploymentIntentGroup error") + } + + if result != nil { + d := DeploymentIntentGroup{} + err = db.DBconn.Unmarshal(result[0], &d) + if err != nil { + return DeploymentIntentGroup{}, pkgerrors.Wrap(err, "Unmarshalling DeploymentIntentGroup") + } + return d, nil + } + + return DeploymentIntentGroup{}, pkgerrors.New("Error getting DeploymentIntentGroup") + +} + +// DeleteDeploymentIntentGroup deletes a DeploymentIntentGroup +func (c *DeploymentIntentGroupClient) DeleteDeploymentIntentGroup(di string, p string, ca string, v string) error { + k := DeploymentIntentGroupKey{ + Name: di, + Project: p, + CompositeApp: ca, + Version: v, + } + + err := db.DBconn.Remove(c.storeName, k) + if err != nil { + return pkgerrors.Wrap(err, "Delete DeploymentIntentGroup entry;") + } + return nil + +} diff --git a/src/orchestrator/pkg/module/deployment_intent_groups_test.go b/src/orchestrator/pkg/module/deployment_intent_groups_test.go new file mode 100644 index 00000000..c0876ceb --- /dev/null +++ b/src/orchestrator/pkg/module/deployment_intent_groups_test.go @@ -0,0 +1,230 @@ +/* + * 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 module + +import ( + "reflect" + "strings" + "testing" + + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" +) + +func TestCreateDeploymentIntentGroup(t *testing.T) { + testCases := []struct { + label string + inputDeploymentIntentGrp DeploymentIntentGroup + inputProject string + inputCompositeApp string + inputCompositeAppVersion string + expectedError string + mockdb *db.MockDB + expected DeploymentIntentGroup + }{ + { + label: "Create DeploymentIntentGroup", + inputDeploymentIntentGrp: DeploymentIntentGroup{ + MetaData: DepMetaData{ + Name: "testDeploymentIntentGroup", + Description: "DescriptionTestDeploymentIntentGroup", + UserData1: "userData1", + UserData2: "userData2", + }, + Spec: DepSpecData{ + Profile: "Testprofile", + Version: "version of deployment", + OverrideValuesObj: []OverrideValues{ + {AppName: "TestAppName", + ValuesObj: map[string]string{ + "imageRepository": "registry.hub.docker.com", + }}, + {AppName: "TestAppName", + ValuesObj: map[string]string{ + "imageRepository": "registry.hub.docker.com", + }}, + }, + }, + }, + inputProject: "testProject", + inputCompositeApp: "testCompositeApp", + inputCompositeAppVersion: "testCompositeAppVersion", + expected: DeploymentIntentGroup{ + MetaData: DepMetaData{ + Name: "testDeploymentIntentGroup", + Description: "DescriptionTestDeploymentIntentGroup", + UserData1: "userData1", + UserData2: "userData2", + }, + Spec: DepSpecData{ + Profile: "Testprofile", + Version: "version of deployment", + OverrideValuesObj: []OverrideValues{ + {AppName: "TestAppName", + ValuesObj: map[string]string{ + "imageRepository": "registry.hub.docker.com", + }}, + {AppName: "TestAppName", + ValuesObj: map[string]string{ + "imageRepository": "registry.hub.docker.com", + }}, + }, + }, + }, + expectedError: "", + mockdb: &db.MockDB{ + Items: map[string]map[string][]byte{ + ProjectKey{ProjectName: "testProject"}.String(): { + "projectmetadata": []byte( + "{\"project-name\":\"testProject\"," + + "\"description\":\"Test project for unit testing\"}"), + }, + CompositeAppKey{CompositeAppName: "testCompositeApp", + Version: "testCompositeAppVersion", Project: "testProject"}.String(): { + "compositeAppmetadata": []byte( + "{\"metadata\":{" + + "\"name\":\"testCompositeApp\"," + + "\"description\":\"description\"," + + "\"userData1\":\"user data\"," + + "\"userData2\":\"user data\"" + + "}," + + "\"spec\":{" + + "\"version\":\"version of the composite app\"}}"), + }, + }, + }, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + db.DBconn = testCase.mockdb + depIntentCli := NewDeploymentIntentGroupClient() + got, err := depIntentCli.CreateDeploymentIntentGroup(testCase.inputDeploymentIntentGrp, testCase.inputProject, testCase.inputCompositeApp, testCase.inputCompositeAppVersion) + if err != nil { + if testCase.expectedError == "" { + t.Fatalf("CreateDeploymentIntentGroup returned an unexpected error %s, ", err) + } + if strings.Contains(err.Error(), testCase.expectedError) == false { + t.Fatalf("CreateDeploymentIntentGroup returned an unexpected error %s", err) + } + } else { + if reflect.DeepEqual(testCase.expected, got) == false { + t.Errorf("CreateDeploymentIntentGroup returned unexpected body: got %v; "+" expected %v", got, testCase.expected) + } + } + }) + } +} + +func TestGetDeploymentIntentGroup(t *testing.T) { + testCases := []struct { + label string + inputDeploymentIntentGrp string + inputProject string + inputCompositeApp string + inputCompositeAppVersion string + expected DeploymentIntentGroup + expectedError string + mockdb *db.MockDB + }{ + { + label: "Get DeploymentIntentGroup", + inputDeploymentIntentGrp: "testDeploymentIntentGroup", + inputProject: "testProject", + inputCompositeApp: "testCompositeApp", + inputCompositeAppVersion: "testCompositeAppVersion", + expected: DeploymentIntentGroup{ + MetaData: DepMetaData{ + Name: "testDeploymentIntentGroup", + Description: "DescriptionTestDeploymentIntentGroup", + UserData1: "userData1", + UserData2: "userData2", + }, + Spec: DepSpecData{ + Profile: "Testprofile", + Version: "version of deployment", + OverrideValuesObj: []OverrideValues{ + {AppName: "TestAppName", + ValuesObj: map[string]string{ + "imageRepository": "registry.hub.docker.com", + }}, + {AppName: "TestAppName", + ValuesObj: map[string]string{ + "imageRepository": "registry.hub.docker.com", + }}, + }, + }, + }, + expectedError: "", + mockdb: &db.MockDB{ + Items: map[string]map[string][]byte{ + DeploymentIntentGroupKey{ + Name: "testDeploymentIntentGroup", + Project: "testProject", + CompositeApp: "testCompositeApp", + Version: "testCompositeAppVersion", + }.String(): { + "deploymentintentgroup": []byte( + "{\"metadata\":{\"name\":\"testDeploymentIntentGroup\"," + + "\"description\":\"DescriptionTestDeploymentIntentGroup\"," + + "\"userData1\": \"userData1\"," + + "\"userData2\": \"userData2\"}," + + "\"spec\":{\"profile\": \"Testprofile\"," + + "\"version\": \"version of deployment\"," + + "\"override-values\":[" + + "{" + + "\"app-name\": \"TestAppName\"," + + "\"values\": " + + "{" + + "\"imageRepository\":\"registry.hub.docker.com\"" + + "}" + + "}," + + "{" + + "\"app-name\": \"TestAppName\"," + + "\"values\": " + + "{" + + "\"imageRepository\":\"registry.hub.docker.com\"" + + "}" + + "}" + + "]}}"), + }, + }, + }, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + db.DBconn = testCase.mockdb + depIntentCli := NewDeploymentIntentGroupClient() + got, err := depIntentCli.GetDeploymentIntentGroup(testCase.inputDeploymentIntentGrp, testCase.inputProject, testCase.inputCompositeApp, testCase.inputCompositeAppVersion) + if err != nil { + if testCase.expectedError == "" { + t.Fatalf("GetDeploymentIntentGroup returned an unexpected error: %s", err) + } + if strings.Contains(err.Error(), testCase.expectedError) == false { + t.Fatalf("GetDeploymentIntentGroup returned an unexpected error: %s", err) + } + } else { + if reflect.DeepEqual(testCase.expected, got) == false { + t.Errorf("GetDeploymentIntentGroup returned unexpected body: got %v;"+ + " expected %v", got, testCase.expected) + } + } + }) + } +} diff --git a/src/orchestrator/pkg/module/generic_placement_intent.go b/src/orchestrator/pkg/module/generic_placement_intent.go new file mode 100644 index 00000000..8e739fc8 --- /dev/null +++ b/src/orchestrator/pkg/module/generic_placement_intent.go @@ -0,0 +1,165 @@ +/* + * 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 module + +import ( + "encoding/json" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" + + pkgerrors "github.com/pkg/errors" +) + +// GenericPlacementIntent shall have 2 fields - metadata and spec +type GenericPlacementIntent struct { + MetaData GenIntentMetaData `json:"metadata"` + Spec GenIntentSpecData `json:"spec"` +} + +// GenIntentMetaData has name, description, userdata1, userdata2 +type GenIntentMetaData struct { + Name string `json:"name"` + Description string `json:"description"` + UserData1 string `json:"userData1"` + UserData2 string `json:"userData2"` +} + +// GenIntentSpecData has logical-cloud-name +type GenIntentSpecData struct { + LogicalCloud string `json:"logical-cloud"` +} + +// GenericPlacementIntentManager is an interface which exposes the GenericPlacementIntentManager functionality +type GenericPlacementIntentManager interface { + CreateGenericPlacementIntent(g GenericPlacementIntent, p string, ca string, + v string) (GenericPlacementIntent, error) + GetGenericPlacementIntent(intentName string, projectName string, + compositeAppName string, version string) (GenericPlacementIntent, error) + DeleteGenericPlacementIntent(intentName string, projectName string, + compositeAppName string, version string) error +} + +// GenericPlacementIntentKey is used as the primary key +type GenericPlacementIntentKey struct { + Name string `json:"name"` + Project string `json:"project"` + CompositeApp string `json:"compositeapp"` + Version string `json:"version"` +} + +// We will use json marshalling to convert to string to +// preserve the underlying structure. +func (gk GenericPlacementIntentKey) String() string { + out, err := json.Marshal(gk) + if err != nil { + return "" + } + return string(out) +} + +// GenericPlacementIntentClient implements the GenericPlacementIntentManager interface +type GenericPlacementIntentClient struct { + storeName string + tagMetaData string +} + +// NewGenericPlacementIntentClient return an instance of GenericPlacementIntentClient which implements GenericPlacementIntentManager +func NewGenericPlacementIntentClient() *GenericPlacementIntentClient { + return &GenericPlacementIntentClient{ + storeName: "orchestrator", + tagMetaData: "genericplacementintent", + } +} + +// CreateGenericPlacementIntent creates an entry for GenericPlacementIntent in the database. Other Input parameters for it - projectName, compositeAppName, version +func (c *GenericPlacementIntentClient) CreateGenericPlacementIntent(g GenericPlacementIntent, p string, ca string, + v string) (GenericPlacementIntent, error) { + + // check if the genericPlacement already exists. + res, err := c.GetGenericPlacementIntent(g.MetaData.Name, p, ca, v) + if res != (GenericPlacementIntent{}) { + return GenericPlacementIntent{}, pkgerrors.New("Intent already exists") + } + + //Check if project exists + _, err = NewProjectClient().GetProject(p) + if err != nil { + return GenericPlacementIntent{}, pkgerrors.New("Unable to find the project") + } + + // check if compositeApp exists + _, err = NewCompositeAppClient().GetCompositeApp(ca, v, p) + if err != nil { + return GenericPlacementIntent{}, pkgerrors.New("Unable to find the composite-app") + } + + gkey := GenericPlacementIntentKey{ + Name: g.MetaData.Name, + Project: p, + CompositeApp: ca, + Version: v, + } + + err = db.DBconn.Insert(c.storeName, gkey, nil, c.tagMetaData, g) + if err != nil { + return GenericPlacementIntent{}, pkgerrors.Wrap(err, "Create DB entry error") + } + + return g, nil +} + +// GetGenericPlacementIntent shall take arguments - name of the intent, name of the project, name of the composite app and version of the composite app. It shall return the genericPlacementIntent if its present. +func (c *GenericPlacementIntentClient) GetGenericPlacementIntent(i string, p string, ca string, v string) (GenericPlacementIntent, error) { + key := GenericPlacementIntentKey{ + Name: i, + Project: p, + CompositeApp: ca, + Version: v, + } + + result, err := db.DBconn.Find(c.storeName, key, c.tagMetaData) + if err != nil { + return GenericPlacementIntent{}, pkgerrors.Wrap(err, "Get Intent error") + } + + if result != nil { + g := GenericPlacementIntent{} + err = db.DBconn.Unmarshal(result[0], &g) + if err != nil { + return GenericPlacementIntent{}, pkgerrors.Wrap(err, "Unmarshalling GenericPlacement Intent") + } + return g, nil + } + + return GenericPlacementIntent{}, pkgerrors.New("Error getting GenericPlacementIntent") + +} + +// DeleteGenericPlacementIntent the intent from the database +func (c *GenericPlacementIntentClient) DeleteGenericPlacementIntent(i string, p string, ca string, v string) error { + key := GenericPlacementIntentKey{ + Name: i, + Project: p, + CompositeApp: ca, + Version: v, + } + + err := db.DBconn.Remove(c.storeName, key) + if err != nil { + return pkgerrors.Wrap(err, "Delete Project entry;") + } + return nil +} diff --git a/src/orchestrator/pkg/module/generic_placement_intent_test.go b/src/orchestrator/pkg/module/generic_placement_intent_test.go new file mode 100644 index 00000000..c87f9ddc --- /dev/null +++ b/src/orchestrator/pkg/module/generic_placement_intent_test.go @@ -0,0 +1,184 @@ +/* + * 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 module + +import ( + "reflect" + "strings" + "testing" + + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" +) + +func TestCreateGenericPlacementIntent(t *testing.T) { + testCases := []struct { + label string + inputIntent GenericPlacementIntent + inputProject string + inputCompositeApp string + inputCompositeAppVersion string + expectedError string + mockdb *db.MockDB + expected GenericPlacementIntent + }{ + { + label: "Create GenericPlacementIntent", + inputIntent: GenericPlacementIntent{ + MetaData: GenIntentMetaData{ + Name: "testGenericPlacement", + Description: " A sample intent for testing", + UserData1: "userData1", + UserData2: "userData2", + }, + Spec: GenIntentSpecData{ + LogicalCloud: "logicalCloud1", + }, + }, + inputProject: "testProject", + inputCompositeApp: "testCompositeApp", + inputCompositeAppVersion: "testCompositeAppVersion", + expected: GenericPlacementIntent{ + MetaData: GenIntentMetaData{ + Name: "testGenericPlacement", + Description: " A sample intent for testing", + UserData1: "userData1", + UserData2: "userData2", + }, + Spec: GenIntentSpecData{ + LogicalCloud: "logicalCloud1", + }, + }, + expectedError: "", + mockdb: &db.MockDB{ + Items: map[string]map[string][]byte{ + ProjectKey{ProjectName: "testProject"}.String(): { + "projectmetadata": []byte( + "{\"project-name\":\"testProject\"," + + "\"description\":\"Test project for unit testing\"}"), + }, + CompositeAppKey{CompositeAppName: "testCompositeApp", + Version: "testCompositeAppVersion", Project: "testProject"}.String(): { + "compositeAppmetadata": []byte( + "{\"metadata\":{" + + "\"name\":\"testCompositeApp\"," + + "\"description\":\"description\"," + + "\"userData1\":\"user data\"," + + "\"userData2\":\"user data\"" + + "}," + + "\"spec\":{" + + "\"version\":\"version of the composite app\"}}"), + }, + }, + }, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + db.DBconn = testCase.mockdb + intentCli := NewGenericPlacementIntentClient() + got, err := intentCli.CreateGenericPlacementIntent(testCase.inputIntent, testCase.inputProject, testCase.inputCompositeApp, testCase.inputCompositeAppVersion) + if err != nil { + if testCase.expectedError == "" { + t.Fatalf("CreateGenericPlacementIntent returned an unexpected error %s", err) + } + if strings.Contains(err.Error(), testCase.expectedError) == false { + t.Fatalf("CreateGenericPlacementIntent returned an unexpected error %s", err) + } + } else { + if reflect.DeepEqual(testCase.expected, got) == false { + t.Errorf("CreateGenericPlacementIntent returned unexpected body: got %v; "+" expected %v", got, testCase.expected) + } + } + }) + + } +} + +func TestGetGenericPlacementIntent(t *testing.T) { + + testCases := []struct { + label string + expectedError string + expected GenericPlacementIntent + mockdb *db.MockDB + intentName string + projectName string + compositeAppName string + compositeAppVersion string + }{ + { + label: "Get Intent", + intentName: "testIntent", + projectName: "testProject", + compositeAppName: "testCompositeApp", + compositeAppVersion: "testVersion", + expected: GenericPlacementIntent{ + MetaData: GenIntentMetaData{ + Name: "testIntent", + Description: "A sample intent for testing", + UserData1: "userData1", + UserData2: "userData2", + }, + Spec: GenIntentSpecData{ + LogicalCloud: "logicalCloud1", + }, + }, + expectedError: "", + mockdb: &db.MockDB{ + Items: map[string]map[string][]byte{ + GenericPlacementIntentKey{ + Name: "testIntent", + Project: "testProject", + CompositeApp: "testCompositeApp", + Version: "testVersion", + }.String(): { + "genericplacementintent": []byte( + "{\"metadata\":{\"Name\":\"testIntent\"," + + "\"Description\":\"A sample intent for testing\"," + + "\"UserData1\": \"userData1\"," + + "\"UserData2\": \"userData2\"}," + + "\"spec\":{\"Logical-Cloud\": \"logicalCloud1\"}}"), + }, + }, + }, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + db.DBconn = testCase.mockdb + intentCli := NewGenericPlacementIntentClient() + got, err := intentCli.GetGenericPlacementIntent(testCase.intentName, testCase.projectName, testCase.compositeAppName, testCase.compositeAppVersion) + if err != nil { + if testCase.expectedError == "" { + t.Fatalf("GetGenericPlacementIntent returned an unexpected error: %s", err) + } + if strings.Contains(err.Error(), testCase.expectedError) == false { + t.Fatalf("GetGenericPlacementIntent returned an unexpected error: %s", err) + } + } else { + if reflect.DeepEqual(testCase.expected, got) == false { + t.Errorf("GetGenericPlacementIntent returned unexpected body: got %v;"+ + " expected %v", got, testCase.expected) + } + } + + }) + } + +} diff --git a/src/orchestrator/pkg/module/module.go b/src/orchestrator/pkg/module/module.go index d03e5ffb..b46c6068 100644 --- a/src/orchestrator/pkg/module/module.go +++ b/src/orchestrator/pkg/module/module.go @@ -18,16 +18,29 @@ package module // Client for using the services in the orchestrator type Client struct { - Project *ProjectClient - CompositeApp *CompositeAppClient + Project *ProjectClient + CompositeApp *CompositeAppClient + Controller *ControllerClient + Cluster *ClusterClient + GenericPlacementIntent *GenericPlacementIntentClient + AppIntent *AppIntentClient // Add Clients for API's here + DeploymentIntentGroup *DeploymentIntentGroupClient + Intent *IntentClient } // NewClient creates a new client for using the services func NewClient() *Client { c := &Client{} + // Add Client API handlers here c.Project = NewProjectClient() c.CompositeApp = NewCompositeAppClient() + c.Controller = NewControllerClient() + c.Cluster = NewClusterClient() // Add Client API handlers here + c.GenericPlacementIntent = NewGenericPlacementIntentClient() + c.AppIntent = NewAppIntentClient() + c.DeploymentIntentGroup = NewDeploymentIntentGroupClient() + c.Intent = NewIntentClient() return c } |