From 11dda8300d3872f9414e7fff0402fd6fcbf6d537 Mon Sep 17 00:00:00 2001 From: "Igor D.C" Date: Wed, 29 Jul 2020 01:37:15 +0000 Subject: DCM - fix code formatting via vscode (II) Part two of fixing code format/syntax with Visual Studio Code. Furthermore, also switched line endings from CRLF to LF (all test files seemed to be ending this way, unlike the other Go files). Issue-ID: MULTICLOUD-1143 Signed-off-by: Igor D.C Change-Id: Iaae868c780a42ea82ca1208e3cbdc3ec6ffd1f97 --- src/dcm/api/clusterHandler.go | 236 +++++++++++----------- src/dcm/api/keyValueHandler.go | 236 +++++++++++----------- src/dcm/api/quotaHandler.go | 236 +++++++++++----------- src/dcm/api/userPermissionsHandler.go | 236 +++++++++++----------- src/dcm/cmd/main.go | 98 ++++----- src/dcm/pkg/module/cluster.go | 274 +++++++++++++------------ src/dcm/pkg/module/cluster_test.go | 227 +++++++++++---------- src/dcm/pkg/module/keyvalue.go | 259 ++++++++++++------------ src/dcm/pkg/module/keyvalue_test.go | 227 +++++++++++---------- src/dcm/pkg/module/logicalcloud_test.go | 313 ++++++++++++++--------------- src/dcm/pkg/module/module.go | 28 +-- src/dcm/pkg/module/quota.go | 299 ++++++++++++++------------- src/dcm/pkg/module/quota_test.go | 227 +++++++++++---------- src/dcm/pkg/module/userpermissions.go | 261 ++++++++++++------------ src/dcm/pkg/module/userpermissions_test.go | 218 ++++++++++---------- 15 files changed, 1680 insertions(+), 1695 deletions(-) (limited to 'src') diff --git a/src/dcm/api/clusterHandler.go b/src/dcm/api/clusterHandler.go index 3e483f06..f4a3abdc 100644 --- a/src/dcm/api/clusterHandler.go +++ b/src/dcm/api/clusterHandler.go @@ -14,149 +14,149 @@ * See the License for the specific language governing permissions * and * limitations under the License. -*/ + */ package api import ( - "encoding/json" - "net/http" - "io" - "github.com/onap/multicloud-k8s/src/dcm/pkg/module" + "encoding/json" + "io" + "net/http" - "github.com/gorilla/mux" -) + "github.com/onap/multicloud-k8s/src/dcm/pkg/module" + "github.com/gorilla/mux" +) // clusterHandler is used to store backend implementations objects type clusterHandler struct { - client module.ClusterManager + client module.ClusterManager } // CreateHandler handles creation of the cluster reference entry in the database func (h clusterHandler) createHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - var v module.Cluster - - err := json.NewDecoder(r.Body).Decode(&v) - switch { - case err == io.EOF: - http.Error(w, "Empty body", http.StatusBadRequest) - return - case err != nil: - http.Error(w, err.Error(), http.StatusUnprocessableEntity) - return - } - - // Cluster Reference Name is required. - if v.MetaData.ClusterReference == "" { - http.Error(w, "Missing name in POST request", http.StatusBadRequest) - return - } - - ret, err := h.client.CreateCluster(project, logicalCloud, v) - 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 - } + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + var v module.Cluster + + err := json.NewDecoder(r.Body).Decode(&v) + switch { + case err == io.EOF: + http.Error(w, "Empty body", http.StatusBadRequest) + return + case err != nil: + http.Error(w, err.Error(), http.StatusUnprocessableEntity) + return + } + + // Cluster Reference Name is required. + if v.MetaData.ClusterReference == "" { + http.Error(w, "Missing name in POST request", http.StatusBadRequest) + return + } + + ret, err := h.client.CreateCluster(project, logicalCloud, v) + 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 + } } // getHandler handle GET operations on a particular name // Returns a Cluster Reference func (h clusterHandler) getHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - name := vars["cluster-reference"] - var ret interface{} - var err error - - if len(name) == 0 { - ret, err = h.client.GetAllClusters(project, logicalCloud) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - } else { - ret, err = h.client.GetCluster(project, logicalCloud, 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 - } + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + name := vars["cluster-reference"] + var ret interface{} + var err error + + if len(name) == 0 { + ret, err = h.client.GetAllClusters(project, logicalCloud) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } else { + ret, err = h.client.GetCluster(project, logicalCloud, 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 + } } // UpdateHandler handles Update operations on a particular cluster reference func (h clusterHandler) updateHandler(w http.ResponseWriter, r *http.Request) { - var v module.Cluster - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - name := vars["cluster-reference"] - - err := json.NewDecoder(r.Body).Decode(&v) - 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 v.MetaData.ClusterReference == "" { - http.Error(w, "Missing name in PUT request", http.StatusBadRequest) - return - } - - ret, err := h.client.UpdateCluster(project, logicalCloud, name, v) - 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 - } + var v module.Cluster + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + name := vars["cluster-reference"] + + err := json.NewDecoder(r.Body).Decode(&v) + 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 v.MetaData.ClusterReference == "" { + http.Error(w, "Missing name in PUT request", http.StatusBadRequest) + return + } + + ret, err := h.client.UpdateCluster(project, logicalCloud, name, v) + 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 + } } //deleteHandler handles DELETE operations on a particular record func (h clusterHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - name := vars["cluster-reference"] - - err := h.client.DeleteCluster(project, logicalCloud, name) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - w.WriteHeader(http.StatusNoContent) + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + name := vars["cluster-reference"] + + err := h.client.DeleteCluster(project, logicalCloud, name) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusNoContent) } diff --git a/src/dcm/api/keyValueHandler.go b/src/dcm/api/keyValueHandler.go index 57df6556..c67504f2 100644 --- a/src/dcm/api/keyValueHandler.go +++ b/src/dcm/api/keyValueHandler.go @@ -14,148 +14,148 @@ * See the License for the specific language governing permissions * and * limitations under the License. -*/ + */ package api import ( - "encoding/json" - "net/http" - "io" - "github.com/onap/multicloud-k8s/src/dcm/pkg/module" - "github.com/gorilla/mux" -) + "encoding/json" + "io" + "net/http" + "github.com/gorilla/mux" + "github.com/onap/multicloud-k8s/src/dcm/pkg/module" +) // keyValueHandler is used to store backend implementations objects type keyValueHandler struct { - client module.KeyValueManager + client module.KeyValueManager } // CreateHandler handles creation of the key value entry in the database func (h keyValueHandler) createHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - var v module.KeyValue - - err := json.NewDecoder(r.Body).Decode(&v) - switch { - case err == io.EOF: - http.Error(w, "Empty body", http.StatusBadRequest) - return - case err != nil: - http.Error(w, err.Error(), http.StatusUnprocessableEntity) - return - } - - // Key Value Name is required. - if v.MetaData.KeyValueName == "" { - http.Error(w, "Missing name in POST request", http.StatusBadRequest) - return - } - - ret, err := h.client.CreateKVPair(project, logicalCloud, v) - 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 - } + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + var v module.KeyValue + + err := json.NewDecoder(r.Body).Decode(&v) + switch { + case err == io.EOF: + http.Error(w, "Empty body", http.StatusBadRequest) + return + case err != nil: + http.Error(w, err.Error(), http.StatusUnprocessableEntity) + return + } + + // Key Value Name is required. + if v.MetaData.KeyValueName == "" { + http.Error(w, "Missing name in POST request", http.StatusBadRequest) + return + } + + ret, err := h.client.CreateKVPair(project, logicalCloud, v) + 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 + } } // getHandler handle GET operations on a particular name // Returns a Key Value func (h keyValueHandler) getHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - name := vars["kv-pair-name"] - var ret interface{} - var err error - - if len(name) == 0 { - ret, err = h.client.GetAllKVPairs(project, logicalCloud) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - } else { - ret, err = h.client.GetKVPair(project, logicalCloud, 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 - } + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + name := vars["kv-pair-name"] + var ret interface{} + var err error + + if len(name) == 0 { + ret, err = h.client.GetAllKVPairs(project, logicalCloud) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } else { + ret, err = h.client.GetKVPair(project, logicalCloud, 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 + } } // UpdateHandler handles Update operations on a particular Key Value func (h keyValueHandler) updateHandler(w http.ResponseWriter, r *http.Request) { - var v module.KeyValue - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - name := vars["kv-pair-name"] - - err := json.NewDecoder(r.Body).Decode(&v) - 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 v.MetaData.KeyValueName == "" { - http.Error(w, "Missing name in PUT request", http.StatusBadRequest) - return - } - - ret, err := h.client.UpdateKVPair(project, logicalCloud, name, v) - 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 - } + var v module.KeyValue + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + name := vars["kv-pair-name"] + + err := json.NewDecoder(r.Body).Decode(&v) + 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 v.MetaData.KeyValueName == "" { + http.Error(w, "Missing name in PUT request", http.StatusBadRequest) + return + } + + ret, err := h.client.UpdateKVPair(project, logicalCloud, name, v) + 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 + } } //deleteHandler handles DELETE operations on a particular record func (h keyValueHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - name := vars["kv-pair-name"] - - err := h.client.DeleteKVPair(project, logicalCloud, name) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - w.WriteHeader(http.StatusNoContent) + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + name := vars["kv-pair-name"] + + err := h.client.DeleteKVPair(project, logicalCloud, name) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusNoContent) } diff --git a/src/dcm/api/quotaHandler.go b/src/dcm/api/quotaHandler.go index bca5206a..deb18e18 100644 --- a/src/dcm/api/quotaHandler.go +++ b/src/dcm/api/quotaHandler.go @@ -14,149 +14,149 @@ * See the License for the specific language governing permissions * and * limitations under the License. -*/ + */ package api import ( - "encoding/json" - "net/http" - "io" - "github.com/onap/multicloud-k8s/src/dcm/pkg/module" + "encoding/json" + "io" + "net/http" - "github.com/gorilla/mux" -) + "github.com/onap/multicloud-k8s/src/dcm/pkg/module" + "github.com/gorilla/mux" +) // quotaHandler is used to store backend implementations objects type quotaHandler struct { - client module.QuotaManager + client module.QuotaManager } // CreateHandler handles creation of the quota entry in the database func (h quotaHandler) createHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - var v module.Quota - - err := json.NewDecoder(r.Body).Decode(&v) - switch { - case err == io.EOF: - http.Error(w, "Empty body", http.StatusBadRequest) - return - case err != nil: - http.Error(w, err.Error(), http.StatusUnprocessableEntity) - return - } - - // Quota Name is required. - if v.MetaData.QuotaName == "" { - http.Error(w, "Missing name in POST request", http.StatusBadRequest) - return - } - - ret, err := h.client.CreateQuota(project, logicalCloud, v) - 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 - } + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + var v module.Quota + + err := json.NewDecoder(r.Body).Decode(&v) + switch { + case err == io.EOF: + http.Error(w, "Empty body", http.StatusBadRequest) + return + case err != nil: + http.Error(w, err.Error(), http.StatusUnprocessableEntity) + return + } + + // Quota Name is required. + if v.MetaData.QuotaName == "" { + http.Error(w, "Missing name in POST request", http.StatusBadRequest) + return + } + + ret, err := h.client.CreateQuota(project, logicalCloud, v) + 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 + } } // getHandler handle GET operations on a particular name // Returns a quota func (h quotaHandler) getHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - name := vars["quota-name"] - var ret interface{} - var err error - - if len(name) == 0 { - ret, err = h.client.GetAllQuotas(project, logicalCloud) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - } else { - ret, err = h.client.GetQuota(project, logicalCloud, 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 - } + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + name := vars["quota-name"] + var ret interface{} + var err error + + if len(name) == 0 { + ret, err = h.client.GetAllQuotas(project, logicalCloud) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } else { + ret, err = h.client.GetQuota(project, logicalCloud, 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 + } } // UpdateHandler handles Update operations on a particular quota func (h quotaHandler) updateHandler(w http.ResponseWriter, r *http.Request) { - var v module.Quota - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - name := vars["quota-name"] - - err := json.NewDecoder(r.Body).Decode(&v) - 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 v.MetaData.QuotaName == "" { - http.Error(w, "Missing name in PUT request", http.StatusBadRequest) - return - } - - ret, err := h.client.UpdateQuota(project, logicalCloud, name, v) - 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 - } + var v module.Quota + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + name := vars["quota-name"] + + err := json.NewDecoder(r.Body).Decode(&v) + 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 v.MetaData.QuotaName == "" { + http.Error(w, "Missing name in PUT request", http.StatusBadRequest) + return + } + + ret, err := h.client.UpdateQuota(project, logicalCloud, name, v) + 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 + } } //deleteHandler handles DELETE operations on a particular record func (h quotaHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - name := vars["quota-name"] - - err := h.client.DeleteQuota(project, logicalCloud, name) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - w.WriteHeader(http.StatusNoContent) + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + name := vars["quota-name"] + + err := h.client.DeleteQuota(project, logicalCloud, name) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusNoContent) } diff --git a/src/dcm/api/userPermissionsHandler.go b/src/dcm/api/userPermissionsHandler.go index 48ab3d8e..156c390f 100644 --- a/src/dcm/api/userPermissionsHandler.go +++ b/src/dcm/api/userPermissionsHandler.go @@ -14,149 +14,149 @@ * See the License for the specific language governing permissions * and * limitations under the License. -*/ + */ package api import ( - "encoding/json" - "net/http" - "io" - "github.com/onap/multicloud-k8s/src/dcm/pkg/module" - "github.com/gorilla/mux" -) + "encoding/json" + "io" + "net/http" + "github.com/gorilla/mux" + "github.com/onap/multicloud-k8s/src/dcm/pkg/module" +) // userPermissionHandler is used to store backend implementations objects type userPermissionHandler struct { - client module.UserPermissionManager + client module.UserPermissionManager } // CreateHandler handles creation of the user permission entry in the database func (h userPermissionHandler) createHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - var v module.UserPermission - - err := json.NewDecoder(r.Body).Decode(&v) - switch { - case err == io.EOF: - http.Error(w, "Empty body", http.StatusBadRequest) - return - case err != nil: - http.Error(w, err.Error(), http.StatusUnprocessableEntity) - return - } - - // User-Permission Name is required. - if v.UserPermissionName == "" { - http.Error(w, "Missing name in POST request", http.StatusBadRequest) - return - } - - ret, err := h.client.CreateUserPerm(project, logicalCloud, v) - 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 - } + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + var v module.UserPermission + + err := json.NewDecoder(r.Body).Decode(&v) + switch { + case err == io.EOF: + http.Error(w, "Empty body", http.StatusBadRequest) + return + case err != nil: + http.Error(w, err.Error(), http.StatusUnprocessableEntity) + return + } + + // User-Permission Name is required. + if v.UserPermissionName == "" { + http.Error(w, "Missing name in POST request", http.StatusBadRequest) + return + } + + ret, err := h.client.CreateUserPerm(project, logicalCloud, v) + 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 + } } // getHandler handle GET operations on a particular name // Returns a User Permission func (h userPermissionHandler) getHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - name := vars["permission-name"] - var ret interface{} - var err error - - if len(name) == 0 { - ret, err = h.client.GetAllUserPerms(project, logicalCloud) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - } else { - ret, err = h.client.GetAllUserPerms(project, logicalCloud) - 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 - } + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + name := vars["permission-name"] + var ret interface{} + var err error + + if len(name) == 0 { + ret, err = h.client.GetAllUserPerms(project, logicalCloud) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } else { + ret, err = h.client.GetAllUserPerms(project, logicalCloud) + 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 + } } // UpdateHandler handles Update operations on a particular user permission func (h userPermissionHandler) updateHandler(w http.ResponseWriter, r *http.Request) { - var v module.UserPermission - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - name := vars["permission-name"] - - err := json.NewDecoder(r.Body).Decode(&v) - 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 v.UserPermissionName == "" { - http.Error(w, "Missing name in PUT request", http.StatusBadRequest) - return - } - - ret, err := h.client.UpdateUserPerm(project, logicalCloud, name, v) - 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 - } + var v module.UserPermission + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + name := vars["permission-name"] + + err := json.NewDecoder(r.Body).Decode(&v) + 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 v.UserPermissionName == "" { + http.Error(w, "Missing name in PUT request", http.StatusBadRequest) + return + } + + ret, err := h.client.UpdateUserPerm(project, logicalCloud, name, v) + 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 + } } //deleteHandler handles DELETE operations on a particular record func (h userPermissionHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - project := vars["project-name"] - logicalCloud := vars["logical-cloud-name"] - name := vars["permission-name"] - - err := h.client.DeleteUserPerm(project, logicalCloud, name) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - w.WriteHeader(http.StatusNoContent) + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + name := vars["permission-name"] + + err := h.client.DeleteUserPerm(project, logicalCloud, name) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusNoContent) } diff --git a/src/dcm/cmd/main.go b/src/dcm/cmd/main.go index 77d5348b..c08330fa 100644 --- a/src/dcm/cmd/main.go +++ b/src/dcm/cmd/main.go @@ -14,65 +14,65 @@ limitations under the License. package main import ( - "context" - "log" - "math/rand" - "net/http" - "os" - "os/signal" - "time" + "context" + "log" + "math/rand" + "net/http" + "os" + "os/signal" + "time" - "github.com/onap/multicloud-k8s/src/dcm/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/gorilla/handlers" + "github.com/onap/multicloud-k8s/src/dcm/api" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/auth" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/config" + contextDb "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/contextdb" + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" ) func main() { - rand.Seed(time.Now().UnixNano()) + rand.Seed(time.Now().UnixNano()) - err := db.InitializeDatabaseConnection("mco") - if err != nil { - log.Println("Unable to initialize database connection...") - log.Println(err) - log.Fatalln("Exiting...") - } + err := db.InitializeDatabaseConnection("mco") + if err != nil { + log.Println("Unable to initialize database connection...") + log.Println(err) + log.Fatalln("Exiting...") + } - err = contextDb.InitializeContextDatabase() - if err != nil { - log.Println("Unable to initialize database connection...") - log.Println(err) - log.Fatalln("Exiting...") - } + err = contextDb.InitializeContextDatabase() + if err != nil { + log.Println("Unable to initialize database connection...") + log.Println(err) + log.Fatalln("Exiting...") + } - httpRouter := api.NewRouter(nil, nil, nil, nil, nil) - loggedRouter := handlers.LoggingHandler(os.Stdout, httpRouter) - log.Println("Starting Distributed Cloud Manager API") + httpRouter := api.NewRouter(nil, nil, nil, nil, nil) + loggedRouter := handlers.LoggingHandler(os.Stdout, httpRouter) + log.Println("Starting Distributed Cloud Manager API") - httpServer := &http.Server{ - Handler: loggedRouter, - Addr: ":" + config.GetConfiguration().ServicePort, - } + httpServer := &http.Server{ + Handler: loggedRouter, + Addr: ":" + config.GetConfiguration().ServicePort, + } - connectionsClose := make(chan struct{}) - go func() { - c := make(chan os.Signal, 1) - signal.Notify(c, os.Interrupt) - <-c - httpServer.Shutdown(context.Background()) - close(connectionsClose) - }() + connectionsClose := make(chan struct{}) + go func() { + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + <-c + httpServer.Shutdown(context.Background()) + close(connectionsClose) + }() - tlsConfig, err := auth.GetTLSConfig("ca.cert", "server.cert", "server.key") - if err != nil { - log.Println("Error Getting TLS Configuration. Starting without TLS...") - log.Fatal(httpServer.ListenAndServe()) - } else { - httpServer.TLSConfig = tlsConfig + tlsConfig, err := auth.GetTLSConfig("ca.cert", "server.cert", "server.key") + if err != nil { + log.Println("Error Getting TLS Configuration. Starting without TLS...") + log.Fatal(httpServer.ListenAndServe()) + } else { + httpServer.TLSConfig = tlsConfig - err = httpServer.ListenAndServeTLS("", "") - } + err = httpServer.ListenAndServeTLS("", "") + } } diff --git a/src/dcm/pkg/module/cluster.go b/src/dcm/pkg/module/cluster.go index 38848990..206d79a6 100644 --- a/src/dcm/pkg/module/cluster.go +++ b/src/dcm/pkg/module/cluster.go @@ -12,194 +12,192 @@ * 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 ( - pkgerrors "github.com/pkg/errors" + pkgerrors "github.com/pkg/errors" ) // Cluster contains the parameters needed for a Cluster type Cluster struct { - MetaData ClusterMeta `json:"metadata"` - Specification ClusterSpec `json:"spec"` + MetaData ClusterMeta `json:"metadata"` + Specification ClusterSpec `json:"spec"` } type ClusterMeta struct { - ClusterReference string `json:"name"` - Description string `json:"description"` - UserData1 string `json:"userData1"` - UserData2 string `json:"userData2"` + ClusterReference string `json:"name"` + Description string `json:"description"` + UserData1 string `json:"userData1"` + UserData2 string `json:"userData2"` } type ClusterSpec struct { - ClusterProvider string `json:"cluster-provider"` - ClusterName string `json:"cluster-name"` - LoadBalancerIP string `json:"loadbalancer-ip"` + ClusterProvider string `json:"cluster-provider"` + ClusterName string `json:"cluster-name"` + LoadBalancerIP string `json:"loadbalancer-ip"` } - type ClusterKey struct { - Project string `json:"project"` - LogicalCloudName string `json:"logical-cloud-name"` - ClusterReference string `json:"clname"` + Project string `json:"project"` + LogicalCloudName string `json:"logical-cloud-name"` + ClusterReference string `json:"clname"` } // ClusterManager is an interface that exposes the connection // functionality type ClusterManager interface { - CreateCluster(project, logicalCloud string, c Cluster) (Cluster, error) - GetCluster(project, logicalCloud, name string) (Cluster, error) - GetAllClusters(project, logicalCloud string) ([]Cluster, error) - DeleteCluster(project, logicalCloud, name string) error - UpdateCluster(project, logicalCloud, name string, c Cluster) (Cluster, error) + CreateCluster(project, logicalCloud string, c Cluster) (Cluster, error) + GetCluster(project, logicalCloud, name string) (Cluster, error) + GetAllClusters(project, logicalCloud string) ([]Cluster, error) + DeleteCluster(project, logicalCloud, name string) error + UpdateCluster(project, logicalCloud, name string, c Cluster) (Cluster, error) } // ClusterClient implements the ClusterManager // It will also be used to maintain some localized state type ClusterClient struct { - storeName string - tagMeta string - util Utility + storeName string + tagMeta string + util Utility } // ClusterClient returns an instance of the ClusterClient // which implements the ClusterManager func NewClusterClient() *ClusterClient { - service := DBService{} - return &ClusterClient{ - storeName: "orchestrator", - tagMeta: "cluster", - util: service, - } + service := DBService{} + return &ClusterClient{ + storeName: "orchestrator", + tagMeta: "cluster", + util: service, + } } // Create entry for the cluster reference resource in the database func (v *ClusterClient) CreateCluster(project, logicalCloud string, c Cluster) (Cluster, error) { - //Construct key consisting of name - key := ClusterKey{ - Project: project, - LogicalCloudName: logicalCloud, - ClusterReference: c.MetaData.ClusterReference, - } - - //Check if project exists - err := v.util.CheckProject(project) - if err != nil { - return Cluster{}, pkgerrors.New("Unable to find the project") - } - //check if logical cloud exists - err = v.util.CheckLogicalCloud(project, logicalCloud) - if err != nil { - return Cluster{}, pkgerrors.New("Unable to find the logical cloud") - } - //Check if this Cluster reference already exists - _, err = v.GetCluster(project, logicalCloud, c.MetaData.ClusterReference) - if err == nil { - return Cluster{}, pkgerrors.New("Cluster reference already exists") - } - - err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) - if err != nil { - return Cluster{}, pkgerrors.Wrap(err, "Creating DB Entry") - } - - return c, nil + //Construct key consisting of name + key := ClusterKey{ + Project: project, + LogicalCloudName: logicalCloud, + ClusterReference: c.MetaData.ClusterReference, + } + + //Check if project exists + err := v.util.CheckProject(project) + if err != nil { + return Cluster{}, pkgerrors.New("Unable to find the project") + } + //check if logical cloud exists + err = v.util.CheckLogicalCloud(project, logicalCloud) + if err != nil { + return Cluster{}, pkgerrors.New("Unable to find the logical cloud") + } + //Check if this Cluster reference already exists + _, err = v.GetCluster(project, logicalCloud, c.MetaData.ClusterReference) + if err == nil { + return Cluster{}, pkgerrors.New("Cluster reference already exists") + } + + err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) + if err != nil { + return Cluster{}, pkgerrors.Wrap(err, "Creating DB Entry") + } + + return c, nil } // Get returns Cluster for corresponding cluster reference -func (v *ClusterClient) GetCluster(project, logicalCloud, clusterReference string)(Cluster, error) { - - //Construct the composite key to select the entry - key := ClusterKey{ - Project: project, - LogicalCloudName: logicalCloud, - ClusterReference: clusterReference, - } - - value, err := v.util.DBFind(v.storeName, key, v.tagMeta) - if err != nil { - return Cluster{}, pkgerrors.Wrap(err, "Get Cluster reference") - } - - //value is a byte array - if value != nil { - cl := Cluster{} - err = v.util.DBUnmarshal(value[0], &cl) - if err != nil { - return Cluster{}, pkgerrors.Wrap(err, "Unmarshaling value") - } - return cl, nil - } - - return Cluster{}, pkgerrors.New("Error getting Cluster") +func (v *ClusterClient) GetCluster(project, logicalCloud, clusterReference string) (Cluster, error) { + + //Construct the composite key to select the entry + key := ClusterKey{ + Project: project, + LogicalCloudName: logicalCloud, + ClusterReference: clusterReference, + } + + value, err := v.util.DBFind(v.storeName, key, v.tagMeta) + if err != nil { + return Cluster{}, pkgerrors.Wrap(err, "Get Cluster reference") + } + + //value is a byte array + if value != nil { + cl := Cluster{} + err = v.util.DBUnmarshal(value[0], &cl) + if err != nil { + return Cluster{}, pkgerrors.Wrap(err, "Unmarshaling value") + } + return cl, nil + } + + return Cluster{}, pkgerrors.New("Error getting Cluster") } - // GetAll returns all cluster references in the logical cloud -func (v *ClusterClient) GetAllClusters(project, logicalCloud string)([]Cluster, error) { - //Construct the composite key to select clusters - key := ClusterKey{ - Project: project, - LogicalCloudName: logicalCloud, - ClusterReference: "", - } - var resp []Cluster - values, err := v.util.DBFind(v.storeName, key, v.tagMeta) - if err != nil { - return []Cluster{}, pkgerrors.Wrap(err, "Get All Cluster references") - } - - for _, value := range values { - cl := Cluster{} - err = v.util.DBUnmarshal(value, &cl) - if err != nil { - return []Cluster{}, pkgerrors.Wrap(err, "Unmarshaling values") - } - resp = append(resp, cl) - } - - return resp, nil +func (v *ClusterClient) GetAllClusters(project, logicalCloud string) ([]Cluster, error) { + //Construct the composite key to select clusters + key := ClusterKey{ + Project: project, + LogicalCloudName: logicalCloud, + ClusterReference: "", + } + var resp []Cluster + values, err := v.util.DBFind(v.storeName, key, v.tagMeta) + if err != nil { + return []Cluster{}, pkgerrors.Wrap(err, "Get All Cluster references") + } + + for _, value := range values { + cl := Cluster{} + err = v.util.DBUnmarshal(value, &cl) + if err != nil { + return []Cluster{}, pkgerrors.Wrap(err, "Unmarshaling values") + } + resp = append(resp, cl) + } + + return resp, nil } // Delete the Cluster reference entry from database func (v *ClusterClient) DeleteCluster(project, logicalCloud, clusterReference string) error { - //Construct the composite key to select the entry - key := ClusterKey{ - Project: project, - LogicalCloudName: logicalCloud, - ClusterReference: clusterReference, - } - err := v.util.DBRemove(v.storeName, key) - if err != nil { - return pkgerrors.Wrap(err, "Delete Cluster Reference") - } - return nil + //Construct the composite key to select the entry + key := ClusterKey{ + Project: project, + LogicalCloudName: logicalCloud, + ClusterReference: clusterReference, + } + err := v.util.DBRemove(v.storeName, key) + if err != nil { + return pkgerrors.Wrap(err, "Delete Cluster Reference") + } + return nil } // Update an entry for the Cluster reference in the database func (v *ClusterClient) UpdateCluster(project, logicalCloud, clusterReference string, c Cluster) (Cluster, error) { - key := ClusterKey{ - Project: project, - LogicalCloudName: logicalCloud, - ClusterReference: clusterReference, - } - - //Check for name mismatch in cluster reference - if c.MetaData.ClusterReference != clusterReference { - return Cluster{}, pkgerrors.New("Update Error - Cluster reference mismatch") - } - //Check if this Cluster reference exists - _, err := v.GetCluster(project, logicalCloud, clusterReference) - if err != nil { - return Cluster{}, pkgerrors.New("Update Error - Cluster reference doesn't exist") - } - err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) - if err != nil { - return Cluster{}, pkgerrors.Wrap(err, "Updating DB Entry") - } - return c, nil + key := ClusterKey{ + Project: project, + LogicalCloudName: logicalCloud, + ClusterReference: clusterReference, + } + + //Check for name mismatch in cluster reference + if c.MetaData.ClusterReference != clusterReference { + return Cluster{}, pkgerrors.New("Update Error - Cluster reference mismatch") + } + //Check if this Cluster reference exists + _, err := v.GetCluster(project, logicalCloud, clusterReference) + if err != nil { + return Cluster{}, pkgerrors.New("Update Error - Cluster reference doesn't exist") + } + err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) + if err != nil { + return Cluster{}, pkgerrors.Wrap(err, "Updating DB Entry") + } + return c, nil } diff --git a/src/dcm/pkg/module/cluster_test.go b/src/dcm/pkg/module/cluster_test.go index d42935db..626adff7 100644 --- a/src/dcm/pkg/module/cluster_test.go +++ b/src/dcm/pkg/module/cluster_test.go @@ -1,115 +1,112 @@ -package module - -import ( - "testing" - - "github.com/pkg/errors" - -) - - -func TestCreateCluster(t *testing.T) { - - mData := ClusterMeta{ - ClusterReference: "test_cluster", - } - - cl := Cluster { - MetaData: mData, - } - data1 := [][]byte{} - - - key := ClusterKey{ - Project: "test_project", - LogicalCloudName: "test_asdf", - ClusterReference: "test_cluster", - } - myMocks := new(mockValues) - // just to get an error value - err1 := errors.New("math: square root of negative number") - - myMocks.On("CheckProject", "test_project").Return(nil) - myMocks.On("CheckLogicalCloud", "test_project", "test_asdf").Return(nil) - myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", cl).Return(nil) - myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, err1) - - clClient := ClusterClient{"test_dcm", "test_meta", myMocks} - _, err := clClient.CreateCluster("test_project", "test_asdf", cl) - if err != nil { - t.Errorf("Some error occured!") - } -} - -func TestGetCluster(t *testing.T) { - key := ClusterKey{ - Project: "test_project", - LogicalCloudName: "test_asdf", - ClusterReference: "test_cluster", - } - - data1 := [][]byte{ - []byte("abc"), - } - - data2 := []byte("abc") - - myMocks := new(mockValues) - - myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) - myMocks.On("DBUnmarshal", data2).Return(nil) - clClient := ClusterClient{"test_dcm", "test_meta", myMocks} - _, err := clClient.GetCluster("test_project", "test_asdf", "test_cluster") - if err != nil { - t.Errorf("Some error occured!") - } -} - -func TestDeleteCluster(t *testing.T) { - - key := ClusterKey{ - Project: "test_project", - LogicalCloudName: "test_asdf", - ClusterReference: "test_cluster", - } - - myMocks := new(mockValues) - - myMocks.On("DBRemove", "test_dcm", key).Return(nil) - - clClient := ClusterClient{"test_dcm", "test_meta", myMocks} - err := clClient.DeleteCluster("test_project", "test_asdf", "test_cluster") - if err != nil { - t.Errorf("Some error occured!") - } - -} - -func TestUpdateCluster(t *testing.T) { - key := ClusterKey{ - Project: "test_project", - LogicalCloudName: "test_asdf", - ClusterReference: "test_cluster", - } - mData := ClusterMeta{ - ClusterReference: "test_cluster", - } - cl := Cluster{ - MetaData: mData, - } - data1 := [][]byte{ - []byte("abc"), - } - data2 := []byte("abc") - - myMocks := new(mockValues) - - myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", cl).Return(nil) - myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) - myMocks.On("DBUnmarshal", data2).Return(nil) - clClient := ClusterClient{"test_dcm", "test_meta", myMocks} - _, err := clClient.UpdateCluster("test_project", "test_asdf", "test_cluster", cl) - if err != nil { - t.Errorf("Some error occured!") - } -} +package module + +import ( + "testing" + + "github.com/pkg/errors" +) + +func TestCreateCluster(t *testing.T) { + + mData := ClusterMeta{ + ClusterReference: "test_cluster", + } + + cl := Cluster{ + MetaData: mData, + } + data1 := [][]byte{} + + key := ClusterKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + ClusterReference: "test_cluster", + } + myMocks := new(mockValues) + // just to get an error value + err1 := errors.New("math: square root of negative number") + + myMocks.On("CheckProject", "test_project").Return(nil) + myMocks.On("CheckLogicalCloud", "test_project", "test_asdf").Return(nil) + myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", cl).Return(nil) + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, err1) + + clClient := ClusterClient{"test_dcm", "test_meta", myMocks} + _, err := clClient.CreateCluster("test_project", "test_asdf", cl) + if err != nil { + t.Errorf("Some error occured!") + } +} + +func TestGetCluster(t *testing.T) { + key := ClusterKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + ClusterReference: "test_cluster", + } + + data1 := [][]byte{ + []byte("abc"), + } + + data2 := []byte("abc") + + myMocks := new(mockValues) + + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) + myMocks.On("DBUnmarshal", data2).Return(nil) + clClient := ClusterClient{"test_dcm", "test_meta", myMocks} + _, err := clClient.GetCluster("test_project", "test_asdf", "test_cluster") + if err != nil { + t.Errorf("Some error occured!") + } +} + +func TestDeleteCluster(t *testing.T) { + + key := ClusterKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + ClusterReference: "test_cluster", + } + + myMocks := new(mockValues) + + myMocks.On("DBRemove", "test_dcm", key).Return(nil) + + clClient := ClusterClient{"test_dcm", "test_meta", myMocks} + err := clClient.DeleteCluster("test_project", "test_asdf", "test_cluster") + if err != nil { + t.Errorf("Some error occured!") + } + +} + +func TestUpdateCluster(t *testing.T) { + key := ClusterKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + ClusterReference: "test_cluster", + } + mData := ClusterMeta{ + ClusterReference: "test_cluster", + } + cl := Cluster{ + MetaData: mData, + } + data1 := [][]byte{ + []byte("abc"), + } + data2 := []byte("abc") + + myMocks := new(mockValues) + + myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", cl).Return(nil) + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) + myMocks.On("DBUnmarshal", data2).Return(nil) + clClient := ClusterClient{"test_dcm", "test_meta", myMocks} + _, err := clClient.UpdateCluster("test_project", "test_asdf", "test_cluster", cl) + if err != nil { + t.Errorf("Some error occured!") + } +} diff --git a/src/dcm/pkg/module/keyvalue.go b/src/dcm/pkg/module/keyvalue.go index 4e3e0fab..37c74a84 100644 --- a/src/dcm/pkg/module/keyvalue.go +++ b/src/dcm/pkg/module/keyvalue.go @@ -12,194 +12,193 @@ * 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 ( - pkgerrors "github.com/pkg/errors" + pkgerrors "github.com/pkg/errors" ) // KeyValue contains the parameters needed for a key value type KeyValue struct { - MetaData KVMetaDataList `json:"metadata"` - Specification KVSpec `json:"spec"` + MetaData KVMetaDataList `json:"metadata"` + Specification KVSpec `json:"spec"` } - // MetaData contains the parameters needed for metadata type KVMetaDataList struct { - KeyValueName string `json:"name"` - Description string `json:"description"` - UserData1 string `json:"userData1"` - UserData2 string `json:"userData2"` + KeyValueName string `json:"name"` + Description string `json:"description"` + UserData1 string `json:"userData1"` + UserData2 string `json:"userData2"` } // Spec contains the parameters needed for spec type KVSpec struct { - Kv []map[string]interface{} `json:"kv"` + Kv []map[string]interface{} `json:"kv"` } // KeyValueKey is the key structure that is used in the database type KeyValueKey struct { - Project string `json:"project"` - LogicalCloudName string `json:"logical-cloud-name"` - KeyValueName string `json:"kvname"` + Project string `json:"project"` + LogicalCloudName string `json:"logical-cloud-name"` + KeyValueName string `json:"kvname"` } // KeyValueManager is an interface that exposes the connection // functionality type KeyValueManager interface { - CreateKVPair(project, logicalCloud string, c KeyValue) (KeyValue, error) - GetKVPair(project, logicalCloud, name string) (KeyValue, error) - GetAllKVPairs(project, logicalCloud string) ([]KeyValue, error) - DeleteKVPair(project, logicalCloud, name string) error - UpdateKVPair(project, logicalCloud, name string, c KeyValue) (KeyValue, error) + CreateKVPair(project, logicalCloud string, c KeyValue) (KeyValue, error) + GetKVPair(project, logicalCloud, name string) (KeyValue, error) + GetAllKVPairs(project, logicalCloud string) ([]KeyValue, error) + DeleteKVPair(project, logicalCloud, name string) error + UpdateKVPair(project, logicalCloud, name string, c KeyValue) (KeyValue, error) } // KeyValueClient implements the KeyValueManager // It will also be used to maintain some localized state type KeyValueClient struct { - storeName string - tagMeta string - util Utility + storeName string + tagMeta string + util Utility } // KeyValueClient returns an instance of the KeyValueClient // which implements the KeyValueManager func NewKeyValueClient() *KeyValueClient { - service := DBService{} - return &KeyValueClient{ - storeName: "orchestrator", - tagMeta: "keyvalue", - util: service, - } + service := DBService{} + return &KeyValueClient{ + storeName: "orchestrator", + tagMeta: "keyvalue", + util: service, + } } // Create entry for the key value resource in the database func (v *KeyValueClient) CreateKVPair(project, logicalCloud string, c KeyValue) (KeyValue, error) { - //Construct key consisting of name - key := KeyValueKey{ - Project: project, - LogicalCloudName: logicalCloud, - KeyValueName: c.MetaData.KeyValueName, - } - - //Check if project exist - err := v.util.CheckProject(project) - if err != nil { - return KeyValue{}, pkgerrors.New("Unable to find the project") - } - //check if logical cloud exists - err = v.util.CheckLogicalCloud(project, logicalCloud) - if err != nil { - return KeyValue{}, pkgerrors.New("Unable to find the logical cloud") - } - //Check if this Key Value already exists - _, err = v.GetKVPair(project, logicalCloud, c.MetaData.KeyValueName) - if err == nil { - return KeyValue{}, pkgerrors.New("Key Value already exists") - } - - err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) - if err != nil { - return KeyValue{}, pkgerrors.Wrap(err, "Creating DB Entry") - } - - return c, nil + //Construct key consisting of name + key := KeyValueKey{ + Project: project, + LogicalCloudName: logicalCloud, + KeyValueName: c.MetaData.KeyValueName, + } + + //Check if project exist + err := v.util.CheckProject(project) + if err != nil { + return KeyValue{}, pkgerrors.New("Unable to find the project") + } + //check if logical cloud exists + err = v.util.CheckLogicalCloud(project, logicalCloud) + if err != nil { + return KeyValue{}, pkgerrors.New("Unable to find the logical cloud") + } + //Check if this Key Value already exists + _, err = v.GetKVPair(project, logicalCloud, c.MetaData.KeyValueName) + if err == nil { + return KeyValue{}, pkgerrors.New("Key Value already exists") + } + + err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) + if err != nil { + return KeyValue{}, pkgerrors.Wrap(err, "Creating DB Entry") + } + + return c, nil } // Get returns Key Value for correspondin name func (v *KeyValueClient) GetKVPair(project, logicalCloud, kvPairName string) (KeyValue, error) { - //Construct the composite key to select the entry - key := KeyValueKey{ - Project: project, - LogicalCloudName: logicalCloud, - KeyValueName: kvPairName, - } - value, err := v.util.DBFind(v.storeName, key, v.tagMeta) - if err != nil { - return KeyValue{}, pkgerrors.Wrap(err, "Get Key Value") - } - - //value is a byte array - if value != nil { - kv := KeyValue{} - err = v.util.DBUnmarshal(value[0], &kv) - if err != nil { - return KeyValue{}, pkgerrors.Wrap(err, "Unmarshaling value") - } - return kv, nil - } - - return KeyValue{}, pkgerrors.New("Error getting Key Value") + //Construct the composite key to select the entry + key := KeyValueKey{ + Project: project, + LogicalCloudName: logicalCloud, + KeyValueName: kvPairName, + } + value, err := v.util.DBFind(v.storeName, key, v.tagMeta) + if err != nil { + return KeyValue{}, pkgerrors.Wrap(err, "Get Key Value") + } + + //value is a byte array + if value != nil { + kv := KeyValue{} + err = v.util.DBUnmarshal(value[0], &kv) + if err != nil { + return KeyValue{}, pkgerrors.Wrap(err, "Unmarshaling value") + } + return kv, nil + } + + return KeyValue{}, pkgerrors.New("Error getting Key Value") } // Get All lists all key value pairs func (v *KeyValueClient) GetAllKVPairs(project, logicalCloud string) ([]KeyValue, error) { - //Construct the composite key to select the entry - key := KeyValueKey{ - Project: project, - LogicalCloudName: logicalCloud, - KeyValueName: "", - } - var resp []KeyValue - values, err := v.util.DBFind(v.storeName, key, v.tagMeta) - if err != nil { - return []KeyValue{}, pkgerrors.Wrap(err, "Get Key Value") - } - - for _, value := range values { - kv := KeyValue{} - err = v.util.DBUnmarshal(value, &kv) - if err != nil { - return []KeyValue{}, pkgerrors.Wrap(err, "Unmarshaling value") - } - resp = append(resp, kv) - } - - return resp, nil + //Construct the composite key to select the entry + key := KeyValueKey{ + Project: project, + LogicalCloudName: logicalCloud, + KeyValueName: "", + } + var resp []KeyValue + values, err := v.util.DBFind(v.storeName, key, v.tagMeta) + if err != nil { + return []KeyValue{}, pkgerrors.Wrap(err, "Get Key Value") + } + + for _, value := range values { + kv := KeyValue{} + err = v.util.DBUnmarshal(value, &kv) + if err != nil { + return []KeyValue{}, pkgerrors.Wrap(err, "Unmarshaling value") + } + resp = append(resp, kv) + } + + return resp, nil } // Delete the Key Value entry from database func (v *KeyValueClient) DeleteKVPair(project, logicalCloud, kvPairName string) error { - //Construct the composite key to select the entry - key := KeyValueKey{ - Project: project, - LogicalCloudName: logicalCloud, - KeyValueName: kvPairName, - } - err := v.util.DBRemove(v.storeName, key) - if err != nil { - return pkgerrors.Wrap(err, "Delete Key Value") - } - return nil + //Construct the composite key to select the entry + key := KeyValueKey{ + Project: project, + LogicalCloudName: logicalCloud, + KeyValueName: kvPairName, + } + err := v.util.DBRemove(v.storeName, key) + if err != nil { + return pkgerrors.Wrap(err, "Delete Key Value") + } + return nil } // Update an entry for the Key Value in the database func (v *KeyValueClient) UpdateKVPair(project, logicalCloud, kvPairName string, c KeyValue) (KeyValue, error) { - key := KeyValueKey{ - Project: project, - LogicalCloudName: logicalCloud, - KeyValueName: kvPairName, - } - //Check if KV pair URl name is the same name in json - if c.MetaData.KeyValueName != kvPairName { - return KeyValue{}, pkgerrors.New("Update Error - KV pair name mismatch") - } - //Check if this Key Value exists - _, err := v.GetKVPair(project, logicalCloud, kvPairName) - if err != nil { - return KeyValue{}, pkgerrors.New("Update Error - Key Value Pair doesn't exist") - } - err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) - if err != nil { - return KeyValue{}, pkgerrors.Wrap(err, "Updating DB Entry") - } - return c, nil + key := KeyValueKey{ + Project: project, + LogicalCloudName: logicalCloud, + KeyValueName: kvPairName, + } + //Check if KV pair URl name is the same name in json + if c.MetaData.KeyValueName != kvPairName { + return KeyValue{}, pkgerrors.New("Update Error - KV pair name mismatch") + } + //Check if this Key Value exists + _, err := v.GetKVPair(project, logicalCloud, kvPairName) + if err != nil { + return KeyValue{}, pkgerrors.New("Update Error - Key Value Pair doesn't exist") + } + err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) + if err != nil { + return KeyValue{}, pkgerrors.Wrap(err, "Updating DB Entry") + } + return c, nil } diff --git a/src/dcm/pkg/module/keyvalue_test.go b/src/dcm/pkg/module/keyvalue_test.go index 9faceda4..25fd55cb 100644 --- a/src/dcm/pkg/module/keyvalue_test.go +++ b/src/dcm/pkg/module/keyvalue_test.go @@ -1,115 +1,112 @@ -package module - -import ( - "testing" - - "github.com/pkg/errors" - -) - - -func TestCreateKVPair(t *testing.T) { - - mData := KVMetaDataList{ - KeyValueName: "test_kv_pair", - } - - kv := KeyValue { - MetaData: mData, - } - data1 := [][]byte{} - - - key := KeyValueKey{ - Project: "test_project", - LogicalCloudName: "test_asdf", - KeyValueName: "test_kv_pair", - } - myMocks := new(mockValues) - // just to get an error value - err1 := errors.New("math: square root of negative number") - - myMocks.On("CheckProject", "test_project").Return(nil) - myMocks.On("CheckLogicalCloud", "test_project", "test_asdf").Return(nil) - myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", kv).Return(nil) - myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, err1) - - kvClient := KeyValueClient{"test_dcm", "test_meta", myMocks} - _, err := kvClient.CreateKVPair("test_project", "test_asdf", kv) - if err != nil { - t.Errorf("Some error occured!") - } -} - -func TestGetKVPair(t *testing.T) { - key := KeyValueKey{ - Project: "test_project", - LogicalCloudName: "test_asdf", - KeyValueName: "test_kv_pair", - } - - data1 := [][]byte{ - []byte("abc"), - } - - data2 := []byte("abc") - - myMocks := new(mockValues) - - myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) - myMocks.On("DBUnmarshal", data2).Return(nil) - kvClient := KeyValueClient{"test_dcm", "test_meta", myMocks} - _, err := kvClient.GetKVPair("test_project", "test_asdf", "test_kv_pair") - if err != nil { - t.Errorf("Some error occured!") - } -} - -func TestDeleteKVPair(t *testing.T) { - - key := KeyValueKey{ - Project: "test_project", - LogicalCloudName: "test_asdf", - KeyValueName: "test_kv_pair", - } - - myMocks := new(mockValues) - - myMocks.On("DBRemove", "test_dcm", key).Return(nil) - - kvClient := KeyValueClient{"test_dcm", "test_meta", myMocks} - err := kvClient.DeleteKVPair("test_project", "test_asdf", "test_kv_pair") - if err != nil { - t.Errorf("Some error occured!") - } - -} - -func TestUpdateKVPair(t *testing.T) { - key := KeyValueKey{ - Project: "test_project", - LogicalCloudName: "test_asdf", - KeyValueName: "test_kv_pair", - } - mData := KVMetaDataList{ - KeyValueName: "test_kv_pair", - } - kv := KeyValue{ - MetaData: mData, - } - data1 := [][]byte{ - []byte("abc"), - } - data2 := []byte("abc") - - myMocks := new(mockValues) - - myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", kv).Return(nil) - myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) - myMocks.On("DBUnmarshal", data2).Return(nil) - kvClient := KeyValueClient{"test_dcm", "test_meta", myMocks} - _, err := kvClient.UpdateKVPair("test_project", "test_asdf", "test_kv_pair", kv) - if err != nil { - t.Errorf("Some error occured!") - } -} +package module + +import ( + "testing" + + "github.com/pkg/errors" +) + +func TestCreateKVPair(t *testing.T) { + + mData := KVMetaDataList{ + KeyValueName: "test_kv_pair", + } + + kv := KeyValue{ + MetaData: mData, + } + data1 := [][]byte{} + + key := KeyValueKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + KeyValueName: "test_kv_pair", + } + myMocks := new(mockValues) + // just to get an error value + err1 := errors.New("math: square root of negative number") + + myMocks.On("CheckProject", "test_project").Return(nil) + myMocks.On("CheckLogicalCloud", "test_project", "test_asdf").Return(nil) + myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", kv).Return(nil) + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, err1) + + kvClient := KeyValueClient{"test_dcm", "test_meta", myMocks} + _, err := kvClient.CreateKVPair("test_project", "test_asdf", kv) + if err != nil { + t.Errorf("Some error occured!") + } +} + +func TestGetKVPair(t *testing.T) { + key := KeyValueKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + KeyValueName: "test_kv_pair", + } + + data1 := [][]byte{ + []byte("abc"), + } + + data2 := []byte("abc") + + myMocks := new(mockValues) + + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) + myMocks.On("DBUnmarshal", data2).Return(nil) + kvClient := KeyValueClient{"test_dcm", "test_meta", myMocks} + _, err := kvClient.GetKVPair("test_project", "test_asdf", "test_kv_pair") + if err != nil { + t.Errorf("Some error occured!") + } +} + +func TestDeleteKVPair(t *testing.T) { + + key := KeyValueKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + KeyValueName: "test_kv_pair", + } + + myMocks := new(mockValues) + + myMocks.On("DBRemove", "test_dcm", key).Return(nil) + + kvClient := KeyValueClient{"test_dcm", "test_meta", myMocks} + err := kvClient.DeleteKVPair("test_project", "test_asdf", "test_kv_pair") + if err != nil { + t.Errorf("Some error occured!") + } + +} + +func TestUpdateKVPair(t *testing.T) { + key := KeyValueKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + KeyValueName: "test_kv_pair", + } + mData := KVMetaDataList{ + KeyValueName: "test_kv_pair", + } + kv := KeyValue{ + MetaData: mData, + } + data1 := [][]byte{ + []byte("abc"), + } + data2 := []byte("abc") + + myMocks := new(mockValues) + + myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", kv).Return(nil) + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) + myMocks.On("DBUnmarshal", data2).Return(nil) + kvClient := KeyValueClient{"test_dcm", "test_meta", myMocks} + _, err := kvClient.UpdateKVPair("test_project", "test_asdf", "test_kv_pair", kv) + if err != nil { + t.Errorf("Some error occured!") + } +} diff --git a/src/dcm/pkg/module/logicalcloud_test.go b/src/dcm/pkg/module/logicalcloud_test.go index 882cc292..fb205753 100644 --- a/src/dcm/pkg/module/logicalcloud_test.go +++ b/src/dcm/pkg/module/logicalcloud_test.go @@ -1,157 +1,156 @@ -package module - -import ( - "fmt" - "testing" - - "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" - "github.com/stretchr/testify/mock" - "github.com/pkg/errors" - -) - -type mockValues struct { - mock.Mock -} - -func (m *mockValues) DBInsert(name string, key db.Key, query interface {}, meta string, c interface {}) error{ - fmt.Println("Mocked Insert operation in Mongo") - args := m.Called(name, key, nil, meta, c) - - return args.Error(0) -} - -func (m *mockValues) DBFind(name string, key db.Key, meta string) ([][]byte, error) { - fmt.Println("Mocked Mongo DB Find Operation") - args := m.Called(name, key, meta) - - return args.Get(0).([][]byte), args.Error(1) -} - -func (m *mockValues) DBUnmarshal(value []byte, out interface{}) error { - fmt.Println("Mocked Mongo DB Unmarshal Operation") - args := m.Called(value) - - return args.Error(0) -} - -func (m *mockValues) DBRemove(name string, key db.Key) error { - fmt.Println("Mocked Mongo DB Remove operation") - args := m.Called(name, key) - - return args.Error(0) -} - -func (m *mockValues) CheckProject(project string) error { - fmt.Println("Mocked Check Project exists") - args := m.Called(project) - - return args.Error(0) -} - -func (m *mockValues) CheckLogicalCloud(project, logicalCloud string) error { - fmt.Println("Mocked Check Logical Cloud exists") - args := m.Called(project, logicalCloud) - - return args.Error(0) -} - -func TestCreateLogicalCloud(t *testing.T) { - - mData := MetaDataList{ - LogicalCloudName: "test_asdf", - } - - lc := LogicalCloud { - MetaData: mData, - } - data1 := [][]byte{} - - key := LogicalCloudKey{ - Project: "test_project", - LogicalCloudName: "test_asdf", - } - myMocks := new(mockValues) - // just to get an error value - err1 := errors.New("math: square root of negative number") - - myMocks.On("CheckProject", "test_project").Return(nil) - myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", lc).Return(nil) - myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, err1) - - lcClient := LogicalCloudClient{"test_dcm", "test_meta", myMocks} - _, err := lcClient.Create("test_project", lc) - if err != nil { - t.Errorf("Some error occured!") - } -} - -func TestGetLogicalCloud(t *testing.T) { - key := LogicalCloudKey{ - Project: "test_project", - LogicalCloudName: "test_asdf", - } - - data1 := [][]byte{ - []byte("abc"), - } - - data2 := []byte("abc") - - myMocks := new(mockValues) - - myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) - myMocks.On("DBUnmarshal", data2).Return(nil) - lcClient := LogicalCloudClient{"test_dcm", "test_meta", myMocks} - _, err := lcClient.Get("test_project", "test_asdf") - if err != nil { - t.Errorf("Some error occured!") - } -} - -func TestDeleteLogicalCloud(t *testing.T) { - - key := LogicalCloudKey{ - Project: "test_project", - LogicalCloudName: "test_asdf", - } - - myMocks := new(mockValues) - - myMocks.On("DBRemove", "test_dcm", key).Return(nil) - - lcClient := LogicalCloudClient{"test_dcm", "test_meta", myMocks} - err := lcClient.Delete("test_project", "test_asdf") - if err != nil { - t.Errorf("Some error occured!") - } - -} - -func TestUpdateLogicalCloud(t *testing.T) { - key := LogicalCloudKey{ - Project: "test_project", - LogicalCloudName: "test_asdf", - } - mData := MetaDataList{ - LogicalCloudName: "test_asdf", - } - lc := LogicalCloud{ - MetaData: mData, - } - data1 := [][]byte{ - []byte("abc"), - } - data2 := []byte("abc") - - myMocks := new(mockValues) - - myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", lc).Return(nil) - myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) - myMocks.On("DBUnmarshal", data2).Return(nil) - lcClient := LogicalCloudClient{"test_dcm", "test_meta", myMocks} - _, err := lcClient.Update("test_project", "test_asdf", lc) - if err != nil { - t.Errorf("Some error occured!") - } -} +package module + +import ( + "fmt" + "testing" + + "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db" + "github.com/pkg/errors" + "github.com/stretchr/testify/mock" +) + +type mockValues struct { + mock.Mock +} + +func (m *mockValues) DBInsert(name string, key db.Key, query interface{}, meta string, c interface{}) error { + fmt.Println("Mocked Insert operation in Mongo") + args := m.Called(name, key, nil, meta, c) + + return args.Error(0) +} + +func (m *mockValues) DBFind(name string, key db.Key, meta string) ([][]byte, error) { + fmt.Println("Mocked Mongo DB Find Operation") + args := m.Called(name, key, meta) + + return args.Get(0).([][]byte), args.Error(1) +} + +func (m *mockValues) DBUnmarshal(value []byte, out interface{}) error { + fmt.Println("Mocked Mongo DB Unmarshal Operation") + args := m.Called(value) + + return args.Error(0) +} + +func (m *mockValues) DBRemove(name string, key db.Key) error { + fmt.Println("Mocked Mongo DB Remove operation") + args := m.Called(name, key) + + return args.Error(0) +} + +func (m *mockValues) CheckProject(project string) error { + fmt.Println("Mocked Check Project exists") + args := m.Called(project) + + return args.Error(0) +} + +func (m *mockValues) CheckLogicalCloud(project, logicalCloud string) error { + fmt.Println("Mocked Check Logical Cloud exists") + args := m.Called(project, logicalCloud) + + return args.Error(0) +} + +func TestCreateLogicalCloud(t *testing.T) { + + mData := MetaDataList{ + LogicalCloudName: "test_asdf", + } + + lc := LogicalCloud{ + MetaData: mData, + } + data1 := [][]byte{} + + key := LogicalCloudKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + } + myMocks := new(mockValues) + // just to get an error value + err1 := errors.New("math: square root of negative number") + + myMocks.On("CheckProject", "test_project").Return(nil) + myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", lc).Return(nil) + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, err1) + + lcClient := LogicalCloudClient{"test_dcm", "test_meta", myMocks} + _, err := lcClient.Create("test_project", lc) + if err != nil { + t.Errorf("Some error occured!") + } +} + +func TestGetLogicalCloud(t *testing.T) { + key := LogicalCloudKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + } + + data1 := [][]byte{ + []byte("abc"), + } + + data2 := []byte("abc") + + myMocks := new(mockValues) + + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) + myMocks.On("DBUnmarshal", data2).Return(nil) + lcClient := LogicalCloudClient{"test_dcm", "test_meta", myMocks} + _, err := lcClient.Get("test_project", "test_asdf") + if err != nil { + t.Errorf("Some error occured!") + } +} + +func TestDeleteLogicalCloud(t *testing.T) { + + key := LogicalCloudKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + } + + myMocks := new(mockValues) + + myMocks.On("DBRemove", "test_dcm", key).Return(nil) + + lcClient := LogicalCloudClient{"test_dcm", "test_meta", myMocks} + err := lcClient.Delete("test_project", "test_asdf") + if err != nil { + t.Errorf("Some error occured!") + } + +} + +func TestUpdateLogicalCloud(t *testing.T) { + key := LogicalCloudKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + } + mData := MetaDataList{ + LogicalCloudName: "test_asdf", + } + lc := LogicalCloud{ + MetaData: mData, + } + data1 := [][]byte{ + []byte("abc"), + } + data2 := []byte("abc") + + myMocks := new(mockValues) + + myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", lc).Return(nil) + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) + myMocks.On("DBUnmarshal", data2).Return(nil) + lcClient := LogicalCloudClient{"test_dcm", "test_meta", myMocks} + _, err := lcClient.Update("test_project", "test_asdf", lc) + if err != nil { + t.Errorf("Some error occured!") + } +} diff --git a/src/dcm/pkg/module/module.go b/src/dcm/pkg/module/module.go index 293f6dd5..70e348e7 100644 --- a/src/dcm/pkg/module/module.go +++ b/src/dcm/pkg/module/module.go @@ -18,22 +18,22 @@ package module // Client for using the services in the orchestrator type Client struct { - LogicalCloud *LogicalCloudClient - Cluster *ClusterClient - Quota *QuotaClient - UserPermission *UserPermissionClient - KeyValue *KeyValueClient - // Add Clients for API's here + LogicalCloud *LogicalCloudClient + Cluster *ClusterClient + Quota *QuotaClient + UserPermission *UserPermissionClient + KeyValue *KeyValueClient + // Add Clients for API's here } // NewClient creates a new client for using the services func NewClient() *Client { - c := &Client{} - c.LogicalCloud = NewLogicalCloudClient() - c.Cluster = NewClusterClient() - c.Quota = NewQuotaClient() - c.UserPermission = NewUserPermissionClient() - c.KeyValue = NewKeyValueClient() - // Add Client API handlers here - return c + c := &Client{} + c.LogicalCloud = NewLogicalCloudClient() + c.Cluster = NewClusterClient() + c.Quota = NewQuotaClient() + c.UserPermission = NewUserPermissionClient() + c.KeyValue = NewKeyValueClient() + // Add Client API handlers here + return c } diff --git a/src/dcm/pkg/module/quota.go b/src/dcm/pkg/module/quota.go index 1a7012f6..cbd9c8b7 100644 --- a/src/dcm/pkg/module/quota.go +++ b/src/dcm/pkg/module/quota.go @@ -12,211 +12,210 @@ * 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 ( - pkgerrors "github.com/pkg/errors" + pkgerrors "github.com/pkg/errors" ) -// Quota contains the parameters needed for a Quota +// Quota contains the parameters needed for a Quota type Quota struct { - MetaData QMetaDataList `json:"metadata"` - Specification QSpec `json:"spec"` + MetaData QMetaDataList `json:"metadata"` + Specification QSpec `json:"spec"` } - // MetaData contains the parameters needed for metadata type QMetaDataList struct { - QuotaName string `json:"name"` - Description string `json:"description"` + QuotaName string `json:"name"` + Description string `json:"description"` } // Spec contains the parameters needed for spec type QSpec struct { - LimitsCPU string `json:"limits.cpu"` - LimitsMemory string `json:"limits.memory"` - RequestsCPU string `json:"requests.cpu"` - RequestsMemory string `json:"requests.memory"` - RequestsStorage string `json:"requests.storage"` - LimitsEphemeralStorage string `json:"limits.ephemeral.storage"` - PersistentVolumeClaims string `json:"persistentvolumeclaims"` - Pods string `json:"pods"` - ConfigMaps string `json:"configmaps"` - ReplicationControllers string `json:"replicationcontrollers"` - ResourceQuotas string `json:"resourcequotas"` - Services string `json:"services"` - ServicesLoadBalancers string `json:"services.loadbalancers"` - ServicesNodePorts string `json:"services.nodeports"` - Secrets string `json:"secrets"` - CountReplicationControllers string `json:"count/replicationcontrollers"` - CountDeploymentsApps string `json:"count/deployments.apps"` - CountReplicasetsApps string `json:"count/replicasets.apps"` - CountStatefulSets string `json:"count/statefulsets.apps"` - CountJobsBatch string `json:"count/jobs.batch"` - CountCronJobsBatch string `json:"count/cronjobs.batch"` - CountDeploymentsExtensions string `json:"count/deployments.extensions"` + LimitsCPU string `json:"limits.cpu"` + LimitsMemory string `json:"limits.memory"` + RequestsCPU string `json:"requests.cpu"` + RequestsMemory string `json:"requests.memory"` + RequestsStorage string `json:"requests.storage"` + LimitsEphemeralStorage string `json:"limits.ephemeral.storage"` + PersistentVolumeClaims string `json:"persistentvolumeclaims"` + Pods string `json:"pods"` + ConfigMaps string `json:"configmaps"` + ReplicationControllers string `json:"replicationcontrollers"` + ResourceQuotas string `json:"resourcequotas"` + Services string `json:"services"` + ServicesLoadBalancers string `json:"services.loadbalancers"` + ServicesNodePorts string `json:"services.nodeports"` + Secrets string `json:"secrets"` + CountReplicationControllers string `json:"count/replicationcontrollers"` + CountDeploymentsApps string `json:"count/deployments.apps"` + CountReplicasetsApps string `json:"count/replicasets.apps"` + CountStatefulSets string `json:"count/statefulsets.apps"` + CountJobsBatch string `json:"count/jobs.batch"` + CountCronJobsBatch string `json:"count/cronjobs.batch"` + CountDeploymentsExtensions string `json:"count/deployments.extensions"` } // QuotaKey is the key structure that is used in the database type QuotaKey struct { - Project string `json:"project"` - LogicalCloudName string `json:"logical-cloud-name"` - QuotaName string `json:"qname"` + Project string `json:"project"` + LogicalCloudName string `json:"logical-cloud-name"` + QuotaName string `json:"qname"` } // QuotaManager is an interface that exposes the connection // functionality type QuotaManager interface { - CreateQuota(project, logicalCloud string, c Quota) (Quota, error) - GetQuota(project, logicalCloud, name string) (Quota, error) - GetAllQuotas(project, logicalCloud string) ([]Quota, error) - DeleteQuota(project, logicalCloud, name string) error - UpdateQuota(project, logicalCloud, name string, c Quota) (Quota, error) + CreateQuota(project, logicalCloud string, c Quota) (Quota, error) + GetQuota(project, logicalCloud, name string) (Quota, error) + GetAllQuotas(project, logicalCloud string) ([]Quota, error) + DeleteQuota(project, logicalCloud, name string) error + UpdateQuota(project, logicalCloud, name string, c Quota) (Quota, error) } // QuotaClient implements the QuotaManager // It will also be used to maintain some localized state type QuotaClient struct { - storeName string - tagMeta string - util Utility + storeName string + tagMeta string + util Utility } // QuotaClient returns an instance of the QuotaClient // which implements the QuotaManager func NewQuotaClient() *QuotaClient { - service := DBService{} - return &QuotaClient{ - storeName: "orchestrator", - tagMeta: "quota", - util: service, - } + service := DBService{} + return &QuotaClient{ + storeName: "orchestrator", + tagMeta: "quota", + util: service, + } } // Create entry for the quota resource in the database func (v *QuotaClient) CreateQuota(project, logicalCloud string, c Quota) (Quota, error) { - //Construct key consisting of name - key := QuotaKey{ - Project: project, - LogicalCloudName: logicalCloud, - QuotaName: c.MetaData.QuotaName, - } - - //Check if project exists - err := v.util.CheckProject(project) - if err != nil { - return Quota{}, pkgerrors.New("Unable to find the project") - } - //check if logical cloud exists - err = v.util.CheckLogicalCloud(project, logicalCloud) - if err != nil { - return Quota{}, pkgerrors.New("Unable to find the logical cloud") - } - //Check if this Quota already exists - _, err = v.GetQuota(project, logicalCloud, c.MetaData.QuotaName) - if err == nil { - return Quota{}, pkgerrors.New("Quota already exists") - } - - err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) - if err != nil { - return Quota{}, pkgerrors.Wrap(err, "Creating DB Entry") - } - - return c, nil + //Construct key consisting of name + key := QuotaKey{ + Project: project, + LogicalCloudName: logicalCloud, + QuotaName: c.MetaData.QuotaName, + } + + //Check if project exists + err := v.util.CheckProject(project) + if err != nil { + return Quota{}, pkgerrors.New("Unable to find the project") + } + //check if logical cloud exists + err = v.util.CheckLogicalCloud(project, logicalCloud) + if err != nil { + return Quota{}, pkgerrors.New("Unable to find the logical cloud") + } + //Check if this Quota already exists + _, err = v.GetQuota(project, logicalCloud, c.MetaData.QuotaName) + if err == nil { + return Quota{}, pkgerrors.New("Quota already exists") + } + + err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) + if err != nil { + return Quota{}, pkgerrors.Wrap(err, "Creating DB Entry") + } + + return c, nil } // Get returns Quota for corresponding quota name func (v *QuotaClient) GetQuota(project, logicalCloud, quotaName string) (Quota, error) { - //Construct the composite key to select the entry - key := QuotaKey{ - Project: project, - LogicalCloudName: logicalCloud, - QuotaName: quotaName, - } - value, err := v.util.DBFind(v.storeName, key, v.tagMeta) - if err != nil { - return Quota{}, pkgerrors.Wrap(err, "Quota") - } - - //value is a byte array - if value != nil { - q := Quota{} - err = v.util.DBUnmarshal(value[0], &q) - if err != nil { - return Quota{}, pkgerrors.Wrap(err, "Unmarshaling value") - } - return q, nil - } - - return Quota{}, pkgerrors.New("Error getting Quota") + //Construct the composite key to select the entry + key := QuotaKey{ + Project: project, + LogicalCloudName: logicalCloud, + QuotaName: quotaName, + } + value, err := v.util.DBFind(v.storeName, key, v.tagMeta) + if err != nil { + return Quota{}, pkgerrors.Wrap(err, "Quota") + } + + //value is a byte array + if value != nil { + q := Quota{} + err = v.util.DBUnmarshal(value[0], &q) + if err != nil { + return Quota{}, pkgerrors.Wrap(err, "Unmarshaling value") + } + return q, nil + } + + return Quota{}, pkgerrors.New("Error getting Quota") } // GetAll returns all cluster quotas in the logical cloud func (v *QuotaClient) GetAllQuotas(project, logicalCloud string) ([]Quota, error) { - //Construct the composite key to select the entry - key := QuotaKey{ - Project: project, - LogicalCloudName: logicalCloud, - QuotaName: "", - } - var resp []Quota - values, err := v.util.DBFind(v.storeName, key, v.tagMeta) - if err != nil { - return []Quota{}, pkgerrors.Wrap(err, "Get All Quotas") - } - - for _, value := range values { - q := Quota{} - err = v.util.DBUnmarshal(value, &q) - if err != nil { - return []Quota{}, pkgerrors.Wrap(err, "Unmarshaling value") - } - resp = append(resp, q) - } - - return resp, nil + //Construct the composite key to select the entry + key := QuotaKey{ + Project: project, + LogicalCloudName: logicalCloud, + QuotaName: "", + } + var resp []Quota + values, err := v.util.DBFind(v.storeName, key, v.tagMeta) + if err != nil { + return []Quota{}, pkgerrors.Wrap(err, "Get All Quotas") + } + + for _, value := range values { + q := Quota{} + err = v.util.DBUnmarshal(value, &q) + if err != nil { + return []Quota{}, pkgerrors.Wrap(err, "Unmarshaling value") + } + resp = append(resp, q) + } + + return resp, nil } // Delete the Quota entry from database func (v *QuotaClient) DeleteQuota(project, logicalCloud, quotaName string) error { - //Construct the composite key to select the entry - key := QuotaKey{ - Project: project, - LogicalCloudName: logicalCloud, - QuotaName: quotaName, - } - err := v.util.DBRemove(v.storeName, key) - if err != nil { - return pkgerrors.Wrap(err, "Delete Quota") - } - return nil + //Construct the composite key to select the entry + key := QuotaKey{ + Project: project, + LogicalCloudName: logicalCloud, + QuotaName: quotaName, + } + err := v.util.DBRemove(v.storeName, key) + if err != nil { + return pkgerrors.Wrap(err, "Delete Quota") + } + return nil } // Update an entry for the Quota in the database func (v *QuotaClient) UpdateQuota(project, logicalCloud, quotaName string, c Quota) (Quota, error) { - key := QuotaKey{ - Project: project, - LogicalCloudName: logicalCloud, - QuotaName: quotaName, - } - //Check quota URL name against the quota json name - if c.MetaData.QuotaName != quotaName { - return Quota{}, pkgerrors.New("Update Error - Quota name mismatch") - } - //Check if this Quota exists - _, err := v.GetQuota(project, logicalCloud, quotaName) - if err != nil { - return Quota{}, pkgerrors.New("Update Error - Quota doesn't exist") - } - err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) - if err != nil { - return Quota{}, pkgerrors.Wrap(err, "Updating DB Entry") - } - return c, nil + key := QuotaKey{ + Project: project, + LogicalCloudName: logicalCloud, + QuotaName: quotaName, + } + //Check quota URL name against the quota json name + if c.MetaData.QuotaName != quotaName { + return Quota{}, pkgerrors.New("Update Error - Quota name mismatch") + } + //Check if this Quota exists + _, err := v.GetQuota(project, logicalCloud, quotaName) + if err != nil { + return Quota{}, pkgerrors.New("Update Error - Quota doesn't exist") + } + err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) + if err != nil { + return Quota{}, pkgerrors.Wrap(err, "Updating DB Entry") + } + return c, nil } diff --git a/src/dcm/pkg/module/quota_test.go b/src/dcm/pkg/module/quota_test.go index 87a60d71..5b70cf77 100644 --- a/src/dcm/pkg/module/quota_test.go +++ b/src/dcm/pkg/module/quota_test.go @@ -1,115 +1,112 @@ -package module - -import ( - "testing" - - "github.com/pkg/errors" - -) - - -func TestCreateQuota(t *testing.T) { - - mData := QMetaDataList{ - QuotaName: "test_quota", - } - - q := Quota { - MetaData: mData, - } - data1 := [][]byte{} - - - key := QuotaKey{ - Project: "test_project", - LogicalCloudName: "test_asdf", - QuotaName: "test_quota", - } - myMocks := new(mockValues) - // just to get an error value - err1 := errors.New("math: square root of negative number") - - myMocks.On("CheckProject", "test_project").Return(nil) - myMocks.On("CheckLogicalCloud", "test_project", "test_asdf").Return(nil) - myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", q).Return(nil) - myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, err1) - - qClient := QuotaClient{"test_dcm", "test_meta", myMocks} - _, err := qClient.CreateQuota("test_project", "test_asdf", q) - if err != nil { - t.Errorf("Some error occured!") - } -} - -func TestGetQuota(t *testing.T) { - key := QuotaKey{ - Project: "test_project", - LogicalCloudName: "test_asdf", - QuotaName: "test_quota", - } - - data1 := [][]byte{ - []byte("abc"), - } - - data2 := []byte("abc") - - myMocks := new(mockValues) - - myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) - myMocks.On("DBUnmarshal", data2).Return(nil) - qClient := QuotaClient{"test_dcm", "test_meta", myMocks} - _, err := qClient.GetQuota("test_project", "test_asdf", "test_quota") - if err != nil { - t.Errorf("Some error occured!") - } -} - -func TestDeleteQuota(t *testing.T) { - - key := QuotaKey{ - Project: "test_project", - LogicalCloudName: "test_asdf", - QuotaName: "test_quota", - } - - myMocks := new(mockValues) - - myMocks.On("DBRemove", "test_dcm", key).Return(nil) - - qClient := QuotaClient{"test_dcm", "test_meta", myMocks} - err := qClient.DeleteQuota("test_project", "test_asdf", "test_quota") - if err != nil { - t.Errorf("Some error occured!") - } - -} - -func TestUpdateQuota(t *testing.T) { - key := QuotaKey{ - Project: "test_project", - LogicalCloudName: "test_asdf", - QuotaName: "test_quota", - } - mData := QMetaDataList{ - QuotaName: "test_quota", - } - q := Quota{ - MetaData: mData, - } - data1 := [][]byte{ - []byte("abc"), - } - data2 := []byte("abc") - - myMocks := new(mockValues) - - myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", q).Return(nil) - myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) - myMocks.On("DBUnmarshal", data2).Return(nil) - qClient := QuotaClient{"test_dcm", "test_meta", myMocks} - _, err := qClient.UpdateQuota("test_project", "test_asdf", "test_quota", q) - if err != nil { - t.Errorf("Some error occured!") - } -} +package module + +import ( + "testing" + + "github.com/pkg/errors" +) + +func TestCreateQuota(t *testing.T) { + + mData := QMetaDataList{ + QuotaName: "test_quota", + } + + q := Quota{ + MetaData: mData, + } + data1 := [][]byte{} + + key := QuotaKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + QuotaName: "test_quota", + } + myMocks := new(mockValues) + // just to get an error value + err1 := errors.New("math: square root of negative number") + + myMocks.On("CheckProject", "test_project").Return(nil) + myMocks.On("CheckLogicalCloud", "test_project", "test_asdf").Return(nil) + myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", q).Return(nil) + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, err1) + + qClient := QuotaClient{"test_dcm", "test_meta", myMocks} + _, err := qClient.CreateQuota("test_project", "test_asdf", q) + if err != nil { + t.Errorf("Some error occured!") + } +} + +func TestGetQuota(t *testing.T) { + key := QuotaKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + QuotaName: "test_quota", + } + + data1 := [][]byte{ + []byte("abc"), + } + + data2 := []byte("abc") + + myMocks := new(mockValues) + + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) + myMocks.On("DBUnmarshal", data2).Return(nil) + qClient := QuotaClient{"test_dcm", "test_meta", myMocks} + _, err := qClient.GetQuota("test_project", "test_asdf", "test_quota") + if err != nil { + t.Errorf("Some error occured!") + } +} + +func TestDeleteQuota(t *testing.T) { + + key := QuotaKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + QuotaName: "test_quota", + } + + myMocks := new(mockValues) + + myMocks.On("DBRemove", "test_dcm", key).Return(nil) + + qClient := QuotaClient{"test_dcm", "test_meta", myMocks} + err := qClient.DeleteQuota("test_project", "test_asdf", "test_quota") + if err != nil { + t.Errorf("Some error occured!") + } + +} + +func TestUpdateQuota(t *testing.T) { + key := QuotaKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + QuotaName: "test_quota", + } + mData := QMetaDataList{ + QuotaName: "test_quota", + } + q := Quota{ + MetaData: mData, + } + data1 := [][]byte{ + []byte("abc"), + } + data2 := []byte("abc") + + myMocks := new(mockValues) + + myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", q).Return(nil) + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) + myMocks.On("DBUnmarshal", data2).Return(nil) + qClient := QuotaClient{"test_dcm", "test_meta", myMocks} + _, err := qClient.UpdateQuota("test_project", "test_asdf", "test_quota", q) + if err != nil { + t.Errorf("Some error occured!") + } +} diff --git a/src/dcm/pkg/module/userpermissions.go b/src/dcm/pkg/module/userpermissions.go index cf961a65..2cff712b 100644 --- a/src/dcm/pkg/module/userpermissions.go +++ b/src/dcm/pkg/module/userpermissions.go @@ -12,182 +12,183 @@ * 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 ( - pkgerrors "github.com/pkg/errors" + pkgerrors "github.com/pkg/errors" ) // UserPermission contains the parameters needed for a user permission type UserPermission struct { - UserPermissionName string `json:"name"` - APIGroups []string `json:"apiGroups"` - Resources []string `json:"resources"` - Verbs []string `json:"verbs"` + UserPermissionName string `json:"name"` + APIGroups []string `json:"apiGroups"` + Resources []string `json:"resources"` + Verbs []string `json:"verbs"` } // UserPermissionKey is the key structure that is used in the database type UserPermissionKey struct { - Project string `json:"project"` - LogicalCloudName string `json:"logical-cloud-name"` - UserPermissionName string `json:"upname"` + Project string `json:"project"` + LogicalCloudName string `json:"logical-cloud-name"` + UserPermissionName string `json:"upname"` } // UserPermissionManager is an interface that exposes the connection // functionality type UserPermissionManager interface { - CreateUserPerm(project, logicalCloud string, c UserPermission) (UserPermission, error) - GetUserPerm(project, logicalCloud, name string) (UserPermission, error) - GetAllUserPerms(project, logicalCloud string) ([]UserPermission, error) - DeleteUserPerm(project, logicalCloud, name string) error - UpdateUserPerm(project, logicalCloud, name string, c UserPermission) (UserPermission, error) + CreateUserPerm(project, logicalCloud string, c UserPermission) (UserPermission, error) + GetUserPerm(project, logicalCloud, name string) (UserPermission, error) + GetAllUserPerms(project, logicalCloud string) ([]UserPermission, error) + DeleteUserPerm(project, logicalCloud, name string) error + UpdateUserPerm(project, logicalCloud, name string, c UserPermission) (UserPermission, error) } // UserPermissionClient implements the UserPermissionManager // It will also be used to maintain some localized state type UserPermissionClient struct { - storeName string - tagMeta string - util Utility + storeName string + tagMeta string + util Utility } // UserPermissionClient returns an instance of the UserPermissionClient // which implements the UserPermissionManager func NewUserPermissionClient() *UserPermissionClient { - service := DBService{} - return &UserPermissionClient{ - storeName: "orchestrator", - tagMeta: "userpermission", - util: service, - } + service := DBService{} + return &UserPermissionClient{ + storeName: "orchestrator", + tagMeta: "userpermission", + util: service, + } } // Create entry for the User Permission resource in the database func (v *UserPermissionClient) CreateUserPerm(project, logicalCloud string, c UserPermission) (UserPermission, error) { - //Construct key consisting of name - key := UserPermissionKey { - Project: project, - LogicalCloudName: logicalCloud, - UserPermissionName: c.UserPermissionName, - } - - //Check if project exists - err := v.util.CheckProject(project) - if err != nil { - return UserPermission{}, pkgerrors.New("Unable to find the project") - } - //check if logical cloud exists - err = v.util.CheckLogicalCloud(project, logicalCloud) - if err != nil { - return UserPermission{}, pkgerrors.New("Unable to find the logical cloud") - } - - //Check if this User Permission already exists - _, err = v.GetUserPerm(project, logicalCloud, c.UserPermissionName) - if err == nil { - return UserPermission{}, pkgerrors.New("User Permission already exists") - } - - err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) - if err != nil { - return UserPermission{}, pkgerrors.Wrap(err, "Creating DB Entry") - } - - return c, nil + //Construct key consisting of name + key := UserPermissionKey{ + Project: project, + LogicalCloudName: logicalCloud, + UserPermissionName: c.UserPermissionName, + } + + //Check if project exists + err := v.util.CheckProject(project) + if err != nil { + return UserPermission{}, pkgerrors.New("Unable to find the project") + } + //check if logical cloud exists + err = v.util.CheckLogicalCloud(project, logicalCloud) + if err != nil { + return UserPermission{}, pkgerrors.New("Unable to find the logical cloud") + } + + //Check if this User Permission already exists + _, err = v.GetUserPerm(project, logicalCloud, c.UserPermissionName) + if err == nil { + return UserPermission{}, pkgerrors.New("User Permission already exists") + } + + err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) + if err != nil { + return UserPermission{}, pkgerrors.Wrap(err, "Creating DB Entry") + } + + return c, nil } // Get returns User Permission for corresponding name func (v *UserPermissionClient) GetUserPerm(project, logicalCloud, userPermName string) (UserPermission, error) { - //Construct the composite key to select the entry - key := UserPermissionKey{ - Project: project, - LogicalCloudName: logicalCloud, - UserPermissionName: userPermName, - } - - value, err := v.util.DBFind(v.storeName, key, v.tagMeta) - if err != nil { - return UserPermission{}, pkgerrors.Wrap(err, "Get User Permission") - } - - //value is a byte array - if value != nil { - up := UserPermission{} - err = v.util.DBUnmarshal(value[0], &up) - if err != nil { - return UserPermission{}, pkgerrors.Wrap(err, "Unmarshaling value") - } - return up, nil - } - - return UserPermission{}, pkgerrors.New("Error getting User Permission") + //Construct the composite key to select the entry + key := UserPermissionKey{ + Project: project, + LogicalCloudName: logicalCloud, + UserPermissionName: userPermName, + } + + value, err := v.util.DBFind(v.storeName, key, v.tagMeta) + if err != nil { + return UserPermission{}, pkgerrors.Wrap(err, "Get User Permission") + } + + //value is a byte array + if value != nil { + up := UserPermission{} + err = v.util.DBUnmarshal(value[0], &up) + if err != nil { + return UserPermission{}, pkgerrors.Wrap(err, "Unmarshaling value") + } + return up, nil + } + + return UserPermission{}, pkgerrors.New("Error getting User Permission") } // GetAll lists all user permissions func (v *UserPermissionClient) GetAllUserPerms(project, logicalCloud string) ([]UserPermission, error) { - //Construct the composite key to select the entry - key := UserPermissionKey { - Project: project, - LogicalCloudName: logicalCloud, - UserPermissionName: "", - } - var resp []UserPermission - values, err := v.util.DBFind(v.storeName, key, v.tagMeta) - if err != nil { - return []UserPermission{}, pkgerrors.Wrap(err, "Get All User Permissions") - } - - for _, value := range values { - up := UserPermission{} - err = v.util.DBUnmarshal(value, &up) - if err != nil { - return []UserPermission{}, pkgerrors.Wrap(err, "Unmarshaling value") - } - resp = append(resp, up) - } - return resp, nil + //Construct the composite key to select the entry + key := UserPermissionKey{ + Project: project, + LogicalCloudName: logicalCloud, + UserPermissionName: "", + } + var resp []UserPermission + values, err := v.util.DBFind(v.storeName, key, v.tagMeta) + if err != nil { + return []UserPermission{}, pkgerrors.Wrap(err, "Get All User Permissions") + } + + for _, value := range values { + up := UserPermission{} + err = v.util.DBUnmarshal(value, &up) + if err != nil { + return []UserPermission{}, pkgerrors.Wrap(err, "Unmarshaling value") + } + resp = append(resp, up) + } + return resp, nil } + // Delete the User Permission entry from database func (v *UserPermissionClient) DeleteUserPerm(project, logicalCloud, userPermName string) error { - //Construct the composite key to select the entry - key := UserPermissionKey{ - Project: project, - LogicalCloudName: logicalCloud, - UserPermissionName: userPermName, - } - err := v.util.DBRemove(v.storeName, key) - if err != nil { - return pkgerrors.Wrap(err, "Delete User Permission") - } - return nil + //Construct the composite key to select the entry + key := UserPermissionKey{ + Project: project, + LogicalCloudName: logicalCloud, + UserPermissionName: userPermName, + } + err := v.util.DBRemove(v.storeName, key) + if err != nil { + return pkgerrors.Wrap(err, "Delete User Permission") + } + return nil } // Update an entry for the User Permission in the database func (v *UserPermissionClient) UpdateUserPerm(project, logicalCloud, userPermName string, c UserPermission) ( - UserPermission, error) { - - key := UserPermissionKey{ - Project: project, - LogicalCloudName: logicalCloud, - UserPermissionName: userPermName, - } - //Check for URL name and json permission name mismatch - if c.UserPermissionName != userPermName { - return UserPermission{}, pkgerrors.New("Update Error - Permission name mismatch") - } - //Check if this User Permission exists - _, err := v.GetUserPerm(project, logicalCloud, userPermName) - if err != nil { - return UserPermission{}, pkgerrors.New( - "Update Error - User Permission doesn't exist") - } - err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) - if err != nil { - return UserPermission{}, pkgerrors.Wrap(err, "Updating DB Entry") - } - return c, nil + UserPermission, error) { + + key := UserPermissionKey{ + Project: project, + LogicalCloudName: logicalCloud, + UserPermissionName: userPermName, + } + //Check for URL name and json permission name mismatch + if c.UserPermissionName != userPermName { + return UserPermission{}, pkgerrors.New("Update Error - Permission name mismatch") + } + //Check if this User Permission exists + _, err := v.GetUserPerm(project, logicalCloud, userPermName) + if err != nil { + return UserPermission{}, pkgerrors.New( + "Update Error - User Permission doesn't exist") + } + err = v.util.DBInsert(v.storeName, key, nil, v.tagMeta, c) + if err != nil { + return UserPermission{}, pkgerrors.Wrap(err, "Updating DB Entry") + } + return c, nil } diff --git a/src/dcm/pkg/module/userpermissions_test.go b/src/dcm/pkg/module/userpermissions_test.go index f134aa0f..5ada2303 100644 --- a/src/dcm/pkg/module/userpermissions_test.go +++ b/src/dcm/pkg/module/userpermissions_test.go @@ -1,110 +1,108 @@ -package module - -import ( - "testing" - - "github.com/pkg/errors" - -) - - -func TestCreateUserPerm(t *testing.T) { - - up := UserPermission { - UserPermissionName: "test_user_perm", - } - data1 := [][]byte{} - - // data2 := []byte("abc") - - key := UserPermissionKey{ - Project: "test_project", - LogicalCloudName: "test_asdf", - UserPermissionName: "test_user_perm", - } - myMocks := new(mockValues) - // just to get an error value - err1 := errors.New("math: square root of negative number") - - myMocks.On("CheckProject", "test_project").Return(nil) - myMocks.On("CheckLogicalCloud", "test_project", "test_asdf").Return(nil) - myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", up).Return(nil) - myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, err1) - // myMocks.On("DBUnmarshal", data2).Return(nil) - - upClient := UserPermissionClient{"test_dcm", "test_meta", myMocks} - _, err := upClient.CreateUserPerm("test_project", "test_asdf", up) - if err != nil { - t.Errorf("Some error occured!") - } -} - -func TestGetUserPerm(t *testing.T) { - key := UserPermissionKey{ - Project: "test_project", - LogicalCloudName: "test_asdf", - UserPermissionName: "test_user_perm", - } - - data1 := [][]byte{ - []byte("abc"), - } - - data2 := []byte("abc") - - myMocks := new(mockValues) - - myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) - myMocks.On("DBUnmarshal", data2).Return(nil) - upClient := UserPermissionClient{"test_dcm", "test_meta", myMocks} - _, err := upClient.GetUserPerm("test_project", "test_asdf", "test_user_perm") - if err != nil { - t.Errorf("Some error occured!") - } -} - -func TestDeleteUserPerm(t *testing.T) { - - key := UserPermissionKey{ - Project: "test_project", - LogicalCloudName: "test_asdf", - UserPermissionName: "test_user_perm", - } - - myMocks := new(mockValues) - - myMocks.On("DBRemove", "test_dcm", key).Return(nil) - - upClient := UserPermissionClient{"test_dcm", "test_meta", myMocks} - err := upClient.DeleteUserPerm("test_project", "test_asdf", "test_user_perm") - if err != nil { - t.Errorf("Some error occured!") - } - -} - -func TestUpdateUserPerm(t *testing.T) { - key := UserPermissionKey{ - Project: "test_project", - LogicalCloudName: "test_asdf", - UserPermissionName: "test_user_perm", - } - up := UserPermission{ - UserPermissionName: "test_user_perm", - } - data1 := [][]byte{ - []byte("abc"), - } - data2 := []byte("abc") - - myMocks := new(mockValues) - - myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", up).Return(nil) - myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) - myMocks.On("DBUnmarshal", data2).Return(nil) - upClient := UserPermissionClient{"test_dcm", "test_meta", myMocks} - _, err := upClient.UpdateUserPerm("test_project", "test_asdf", "test_user_perm", up) - if err != nil { - t.Errorf("Some error occured!") - } -} +package module + +import ( + "testing" + + "github.com/pkg/errors" +) + +func TestCreateUserPerm(t *testing.T) { + + up := UserPermission{ + UserPermissionName: "test_user_perm", + } + data1 := [][]byte{} + + // data2 := []byte("abc") + + key := UserPermissionKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + UserPermissionName: "test_user_perm", + } + myMocks := new(mockValues) + // just to get an error value + err1 := errors.New("math: square root of negative number") + + myMocks.On("CheckProject", "test_project").Return(nil) + myMocks.On("CheckLogicalCloud", "test_project", "test_asdf").Return(nil) + myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", up).Return(nil) + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, err1) + // myMocks.On("DBUnmarshal", data2).Return(nil) + + upClient := UserPermissionClient{"test_dcm", "test_meta", myMocks} + _, err := upClient.CreateUserPerm("test_project", "test_asdf", up) + if err != nil { + t.Errorf("Some error occured!") + } +} + +func TestGetUserPerm(t *testing.T) { + key := UserPermissionKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + UserPermissionName: "test_user_perm", + } + + data1 := [][]byte{ + []byte("abc"), + } + + data2 := []byte("abc") + + myMocks := new(mockValues) + + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) + myMocks.On("DBUnmarshal", data2).Return(nil) + upClient := UserPermissionClient{"test_dcm", "test_meta", myMocks} + _, err := upClient.GetUserPerm("test_project", "test_asdf", "test_user_perm") + if err != nil { + t.Errorf("Some error occured!") + } +} + +func TestDeleteUserPerm(t *testing.T) { + + key := UserPermissionKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + UserPermissionName: "test_user_perm", + } + + myMocks := new(mockValues) + + myMocks.On("DBRemove", "test_dcm", key).Return(nil) + + upClient := UserPermissionClient{"test_dcm", "test_meta", myMocks} + err := upClient.DeleteUserPerm("test_project", "test_asdf", "test_user_perm") + if err != nil { + t.Errorf("Some error occured!") + } + +} + +func TestUpdateUserPerm(t *testing.T) { + key := UserPermissionKey{ + Project: "test_project", + LogicalCloudName: "test_asdf", + UserPermissionName: "test_user_perm", + } + up := UserPermission{ + UserPermissionName: "test_user_perm", + } + data1 := [][]byte{ + []byte("abc"), + } + data2 := []byte("abc") + + myMocks := new(mockValues) + + myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", up).Return(nil) + myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil) + myMocks.On("DBUnmarshal", data2).Return(nil) + upClient := UserPermissionClient{"test_dcm", "test_meta", myMocks} + _, err := upClient.UpdateUserPerm("test_project", "test_asdf", "test_user_perm", up) + if err != nil { + t.Errorf("Some error occured!") + } +} -- cgit 1.2.3-korg