update cluster api

This commit is contained in:
medcl 2021-02-20 16:02:35 +08:00
parent 2b330a56a7
commit a48ea8d4c1
2 changed files with 38 additions and 28 deletions

View File

@ -26,7 +26,7 @@ func (h *APIHandler) HandleCreateClusterAction(w http.ResponseWriter, req *http.
err := h.DecodeJSON(req, conf) err := h.DecodeJSON(req, conf)
if err != nil { if err != nil {
resBody["error"] = err resBody["error"] = err
h.WriteJSON(w, resBody, http.StatusOK) h.WriteJSON(w, resBody, http.StatusInternalServerError)
return return
} }
// TODO validate data format // TODO validate data format
@ -34,18 +34,23 @@ func (h *APIHandler) HandleCreateClusterAction(w http.ResponseWriter, req *http.
id := util.GetUUID() id := util.GetUUID()
conf.Created = time.Now() conf.Created = time.Now()
conf.Updated = conf.Created conf.Updated = conf.Created
conf.ID = id //conf.ID = id
index:=orm.GetIndexName(model.ClusterConfig{}) index:=orm.GetIndexName(model.ClusterConfig{})
ir, err := esClient.Index(index, "", id, conf) _, err = esClient.Index(index, "", id, conf)
if err != nil { if err != nil {
resBody["error"] = err resBody["error"] = err
h.WriteJSON(w, resBody, http.StatusOK) h.WriteJSON(w, resBody, http.StatusInternalServerError)
return return
} }
conf.ID = ir.ID
resBody["payload"] = conf //conf.ID = ir.ID
resBody["acknowledged"] = true
h.WriteJSON(w, resBody, http.StatusOK) 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){ 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) err := h.DecodeJSON(req, &conf)
if err != nil { if err != nil {
resBody["error"] = err.Error() resBody["error"] = err.Error()
h.WriteJSON(w, resBody, http.StatusOK) h.WriteJSON(w, resBody, http.StatusInternalServerError)
return return
} }
id := ps.ByName("id") id := ps.ByName("id")
@ -64,7 +69,7 @@ func (h *APIHandler) HandleUpdateClusterAction(w http.ResponseWriter, req *http.
originConf, err := esClient.Get(indexName, "", id) originConf, err := esClient.Get(indexName, "", id)
if err != nil { if err != nil {
resBody["error"] = err.Error() resBody["error"] = err.Error()
h.WriteJSON(w, resBody, http.StatusOK) h.WriteJSON(w, resBody, http.StatusInternalServerError)
return return
} }
source := originConf.Source source := originConf.Source
@ -78,28 +83,31 @@ func (h *APIHandler) HandleUpdateClusterAction(w http.ResponseWriter, req *http.
_, err = esClient.Index(indexName, "", id, source) _, err = esClient.Index(indexName, "", id, source)
if err != nil { if err != nil {
resBody["error"] = err.Error() resBody["error"] = err.Error()
h.WriteJSON(w, resBody, http.StatusOK) h.WriteJSON(w, resBody, http.StatusInternalServerError)
return return
} }
resBody["acknowledged"] = true resBody["_source"] = conf
resBody["payload"] = conf resBody["_id"] = id
h.WriteJSON(w, resBody, http.StatusOK) resBody["result"] = "updated"
}
h.WriteJSON(w, resBody,http.StatusOK)}
func (h *APIHandler) HandleDeleteClusterAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params){ func (h *APIHandler) HandleDeleteClusterAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params){
resBody := map[string] interface{}{ resBody := map[string] interface{}{
} }
id := ps.ByName("id") id := ps.ByName("id")
esClient := elastic.GetClient(h.Config.Elasticsearch) 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 { if err != nil {
resBody["error"] = err.Error() resBody["error"] = err.Error()
h.WriteJSON(w, resBody, http.StatusOK) h.WriteJSON(w, resBody, http.StatusInternalServerError)
return return
} }
resBody["acknowledged"] = true resBody["_id"] = id
h.WriteJSON(w, resBody, http.StatusOK) resBody["result"] = response.Result
h.WriteJSON(w, resBody, response.StatusCode)
} }
func (h *APIHandler) HandleSearchClusterAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params){ func (h *APIHandler) HandleSearchClusterAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params){

View File

@ -3,14 +3,16 @@ package model
import "time" import "time"
type ClusterConfig struct { type ClusterConfig struct {
ID string `json:"id" elastic_meta:"_id"` //ID string `json:"id" elastic_meta:"_id"`
Name string `json:"name" elastic_mapping:"name:{type:text}"` Name string `json:"name" elastic_mapping:"name:{type:text}"`
Endpoint string `json:"endpoint" elastic_mapping:"endpoint:{type:text}"` Endpoint string `json:"endpoint" elastic_mapping:"endpoint:{type:text}"`
UserName string `json:"username" elastic_mapping:"username:{type:keyword}"` BasicAuth struct {
Password string `json:"password" elastic_mapping:"password:{type:keyword}" ` UserName string `json:"username,omitempty" elastic_mapping:"username:{type:keyword}"`
Order int `json:"order" elastic_mapping:"order:{type:integer}"` Password string `json:"password,omitempty" elastic_mapping:"password:{type:keyword}" `
Description string `json:"description" elastic_mapping:"description:{type:text}"` } `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}"` Enabled bool `json:"enabled" elastic_mapping:"enabled:{type:boolean}"`
Created time.Time `json:"created" elastic_mapping:"created:{type:date}"` Created time.Time `json:"created,omitempty" elastic_mapping:"created:{type:date}"`
Updated time.Time `json:"updated" elastic_mapping:"updated:{type:date}"` Updated time.Time `json:"updated,omitempty" elastic_mapping:"updated:{type:date}"`
} }