From 80107dc294c86a452c6108ffca6f281b4dbb9192 Mon Sep 17 00:00:00 2001 From: "Igor D.C" Date: Wed, 30 Sep 2020 21:30:06 +0000 Subject: Subtle refactoring in a few functions In DCM. Essentially refactored two different areas: - rename apply/terminate functions in module to clarify what they do - split gets from getAlls in API code of the 5 DCM resource types And cleaned up here and there. Issue-ID: MULTICLOUD-1143 Change-Id: I9b72c77ba34a1febd5df4a339e87968ddc4a7891 Signed-off-by: Igor D.C --- src/dcm/api/clusterHandler.go | 52 +++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 19 deletions(-) (limited to 'src/dcm/api/clusterHandler.go') diff --git a/src/dcm/api/clusterHandler.go b/src/dcm/api/clusterHandler.go index db110399..427a4262 100644 --- a/src/dcm/api/clusterHandler.go +++ b/src/dcm/api/clusterHandler.go @@ -23,9 +23,8 @@ import ( "io" "net/http" - "github.com/onap/multicloud-k8s/src/dcm/pkg/module" - "github.com/gorilla/mux" + "github.com/onap/multicloud-k8s/src/dcm/pkg/module" ) // clusterHandler is used to store backend implementations objects @@ -33,8 +32,7 @@ type clusterHandler struct { client module.ClusterManager } -// CreateHandler handles creation of the cluster reference entry in the database - +// 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"] @@ -72,7 +70,31 @@ func (h clusterHandler) createHandler(w http.ResponseWriter, r *http.Request) { } } -// getHandler handle GET operations on a particular name +// getAllHandler handles GET operations over cluster references +// Returns a list of Cluster References +func (h clusterHandler) getAllHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + project := vars["project-name"] + logicalCloud := vars["logical-cloud-name"] + var ret interface{} + var err error + + ret, err = h.client.GetAllClusters(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 + } +} + +// getHandler handles GET operations on a particular name // Returns a Cluster Reference func (h clusterHandler) getHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) @@ -82,22 +104,14 @@ func (h clusterHandler) getHandler(w http.ResponseWriter, r *http.Request) { 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 { - if err.Error() == "Cluster Reference does not exist" { - http.Error(w, err.Error(), http.StatusNotFound) - return - } - http.Error(w, err.Error(), http.StatusInternalServerError) + ret, err = h.client.GetCluster(project, logicalCloud, name) + if err != nil { + if err.Error() == "Cluster Reference does not exist" { + http.Error(w, err.Error(), http.StatusNotFound) return } + http.Error(w, err.Error(), http.StatusInternalServerError) + return } w.Header().Set("Content-Type", "application/json") -- cgit 1.2.3-korg