From a48ea8d4c139169b79e9b8bd4a2faead51d6fb69 Mon Sep 17 00:00:00 2001 From: medcl Date: Sat, 20 Feb 2021 16:02:35 +0800 Subject: [PATCH] update cluster api --- api/system/cluster_config.go | 46 +++++++++++++++++++++--------------- model/cluster_config.go | 20 +++++++++------- 2 files changed, 38 insertions(+), 28 deletions(-) diff --git a/api/system/cluster_config.go b/api/system/cluster_config.go index b37791b9..24044bd5 100644 --- a/api/system/cluster_config.go +++ b/api/system/cluster_config.go @@ -26,7 +26,7 @@ func (h *APIHandler) HandleCreateClusterAction(w http.ResponseWriter, req *http. err := h.DecodeJSON(req, conf) if err != nil { resBody["error"] = err - h.WriteJSON(w, resBody, http.StatusOK) + h.WriteJSON(w, resBody, http.StatusInternalServerError) return } // TODO validate data format @@ -34,18 +34,23 @@ func (h *APIHandler) HandleCreateClusterAction(w http.ResponseWriter, req *http. id := util.GetUUID() conf.Created = time.Now() conf.Updated = conf.Created - conf.ID = id + //conf.ID = id index:=orm.GetIndexName(model.ClusterConfig{}) - ir, err := esClient.Index(index, "", id, conf) + _, err = esClient.Index(index, "", id, conf) if err != nil { resBody["error"] = err - h.WriteJSON(w, resBody, http.StatusOK) + h.WriteJSON(w, resBody, http.StatusInternalServerError) return } - conf.ID = ir.ID - resBody["payload"] = conf - resBody["acknowledged"] = true - h.WriteJSON(w, resBody, http.StatusOK) + + //conf.ID = ir.ID + + resBody["_source"] = conf + resBody["_id"] = id + resBody["result"] = "created" + + h.WriteJSON(w, resBody,http.StatusOK) + } func (h *APIHandler) HandleUpdateClusterAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params){ @@ -55,7 +60,7 @@ func (h *APIHandler) HandleUpdateClusterAction(w http.ResponseWriter, req *http. err := h.DecodeJSON(req, &conf) if err != nil { resBody["error"] = err.Error() - h.WriteJSON(w, resBody, http.StatusOK) + h.WriteJSON(w, resBody, http.StatusInternalServerError) return } id := ps.ByName("id") @@ -64,7 +69,7 @@ func (h *APIHandler) HandleUpdateClusterAction(w http.ResponseWriter, req *http. originConf, err := esClient.Get(indexName, "", id) if err != nil { resBody["error"] = err.Error() - h.WriteJSON(w, resBody, http.StatusOK) + h.WriteJSON(w, resBody, http.StatusInternalServerError) return } source := originConf.Source @@ -78,28 +83,31 @@ func (h *APIHandler) HandleUpdateClusterAction(w http.ResponseWriter, req *http. _, err = esClient.Index(indexName, "", id, source) if err != nil { resBody["error"] = err.Error() - h.WriteJSON(w, resBody, http.StatusOK) + h.WriteJSON(w, resBody, http.StatusInternalServerError) return } - resBody["acknowledged"] = true - resBody["payload"] = conf - h.WriteJSON(w, resBody, http.StatusOK) -} + resBody["_source"] = conf + resBody["_id"] = id + resBody["result"] = "updated" + + h.WriteJSON(w, resBody,http.StatusOK)} func (h *APIHandler) HandleDeleteClusterAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params){ resBody := map[string] interface{}{ } id := ps.ByName("id") esClient := elastic.GetClient(h.Config.Elasticsearch) - _, err := esClient.Delete(orm.GetIndexName(model.ClusterConfig{}), "", id) + response, err := esClient.Delete(orm.GetIndexName(model.ClusterConfig{}), "", id) + if err != nil { resBody["error"] = err.Error() - h.WriteJSON(w, resBody, http.StatusOK) + h.WriteJSON(w, resBody, http.StatusInternalServerError) return } - resBody["acknowledged"] = true - h.WriteJSON(w, resBody, http.StatusOK) + resBody["_id"] = id + resBody["result"] = response.Result + h.WriteJSON(w, resBody, response.StatusCode) } func (h *APIHandler) HandleSearchClusterAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params){ diff --git a/model/cluster_config.go b/model/cluster_config.go index 7024eb70..07a7cd38 100644 --- a/model/cluster_config.go +++ b/model/cluster_config.go @@ -3,14 +3,16 @@ package model import "time" type ClusterConfig struct { - ID string `json:"id" elastic_meta:"_id"` - Name string `json:"name" elastic_mapping:"name:{type:text}"` - Endpoint string `json:"endpoint" elastic_mapping:"endpoint:{type:text}"` - UserName string `json:"username" elastic_mapping:"username:{type:keyword}"` - Password string `json:"password" elastic_mapping:"password:{type:keyword}" ` - Order int `json:"order" elastic_mapping:"order:{type:integer}"` - Description string `json:"description" elastic_mapping:"description:{type:text}"` + //ID string `json:"id" elastic_meta:"_id"` + Name string `json:"name" elastic_mapping:"name:{type:text}"` + Endpoint string `json:"endpoint" elastic_mapping:"endpoint:{type:text}"` + BasicAuth struct { + UserName string `json:"username,omitempty" elastic_mapping:"username:{type:keyword}"` + Password string `json:"password,omitempty" elastic_mapping:"password:{type:keyword}" ` + } `json:"basic_auth,omitempty" elastic_mapping:"basic_auth:{type:object}"` + Order int `json:"order,omitempty" elastic_mapping:"order:{type:integer}"` + Description string `json:"description,omitempty" elastic_mapping:"description:{type:text}"` Enabled bool `json:"enabled" elastic_mapping:"enabled:{type:boolean}"` - Created time.Time `json:"created" elastic_mapping:"created:{type:date}"` - Updated time.Time `json:"updated" elastic_mapping:"updated:{type:date}"` + Created time.Time `json:"created,omitempty" elastic_mapping:"created:{type:date}"` + Updated time.Time `json:"updated,omitempty" elastic_mapping:"updated:{type:date}"` }