diff --git a/plugin/api/index_management/indices.go b/plugin/api/index_management/indices.go index 7ab92ccd..51aec6d9 100644 --- a/plugin/api/index_management/indices.go +++ b/plugin/api/index_management/indices.go @@ -37,7 +37,7 @@ func (handler APIHandler) HandleGetMappingsAction(w http.ResponseWriter, req *ht handler.WriteJSON(w, idxs, http.StatusOK) } -func (handler APIHandler) HandleGetIndicesAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { +func (handler APIHandler) HandleCatIndicesAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { targetClusterID := ps.ByName("id") client := elastic.GetClient(targetClusterID) //filter indices @@ -146,3 +146,17 @@ func (handler APIHandler) HandleCreateIndexAction(w http.ResponseWriter, req *ht resBody["result"] = "created" handler.WriteJSON(w, resBody, http.StatusCreated) } + +func (handler APIHandler) HandleGetIndexAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { + targetClusterID := ps.ByName("id") + client := elastic.GetClient(targetClusterID) + indexName := ps.ByName("index") + indexRes, err := client.GetIndex(indexName) + if err != nil { + log.Error(err) + handler.WriteError(w, err.Error(), http.StatusInternalServerError) + return + } + handler.WriteJSON(w, indexRes, http.StatusOK) + +} \ No newline at end of file diff --git a/plugin/api/init.go b/plugin/api/init.go index 0125013b..8b020da4 100644 --- a/plugin/api/init.go +++ b/plugin/api/init.go @@ -40,12 +40,13 @@ func Init(cfg *config.AppConfig) { api.HandleAPIMethod(api.GET, path.Join(pathPrefix, "rebuild/_search"), handler.HandleGetRebuildListAction) api.HandleAPIMethod(api.DELETE, path.Join(pathPrefix, "rebuild/:id"), handler.HandleDeleteRebuildAction) - api.HandleAPIMethod(api.GET, path.Join(esPrefix, "_cat/indices"), handler.RequireLogin(handler.HandleGetIndicesAction)) + api.HandleAPIMethod(api.GET, path.Join(esPrefix, "_cat/indices"), handler.RequireLogin(handler.HandleCatIndicesAction)) api.HandleAPIMethod(api.GET, path.Join(esPrefix, "index/:index/_mappings"), handler.IndexRequired(handler.HandleGetMappingsAction, "indices.get_mapping")) api.HandleAPIMethod(api.GET, path.Join(esPrefix, "index/:index/_settings"), handler.IndexRequired(handler.HandleGetSettingsAction, "indices.get_settings")) api.HandleAPIMethod(api.PUT, path.Join(esPrefix, "index/:index/_settings"), handler.IndexRequired(handler.HandleUpdateSettingsAction, "indices.put_settings")) api.HandleAPIMethod(api.DELETE, path.Join(esPrefix, "index/:index"), handler.IndexRequired(handler.HandleDeleteIndexAction, "indices.delete")) api.HandleAPIMethod(api.POST, path.Join(esPrefix, "index/:index"), handler.IndexRequired(handler.HandleCreateIndexAction, "indices.create")) + api.HandleAPIMethod(api.GET, path.Join(esPrefix, "index/:index"), handler.IndexRequired(handler.HandleGetIndexAction, "indices.get")) api.HandleAPIMethod(api.POST, path.Join(pathPrefix, "elasticsearch/command"), handler.RequirePermission(handler.HandleAddCommonCommandAction, enum.PermissionCommandWrite)) api.HandleAPIMethod(api.PUT, path.Join(pathPrefix, "elasticsearch/command/:cid"), handler.RequirePermission(handler.HandleSaveCommonCommandAction, enum.PermissionCommandWrite)) diff --git a/plugin/migration/api.go b/plugin/migration/api.go index 45ff1057..6dfffb23 100644 --- a/plugin/migration/api.go +++ b/plugin/migration/api.go @@ -40,6 +40,7 @@ func InitAPI() { api.HandleAPIMethod(api.GET, "/migration/data/:task_id/info", handler.RequirePermission(handler.getDataMigrationTaskInfo, enum.PermissionTaskRead)) api.HandleAPIMethod(api.GET, "/migration/data/:task_id/info/:index", handler.RequirePermission(handler.getDataMigrationTaskOfIndex, enum.PermissionTaskRead)) api.HandleAPIMethod(api.PUT, "/migration/data/:task_id/status", handler.RequirePermission(handler.updateDataMigrationTaskStatus, enum.PermissionTaskRead)) + api.HandleAPIMethod(api.POST, "/elasticsearch/:id/index/:index/_init", handler.initIndex) } @@ -945,6 +946,61 @@ func (h *APIHandler) validateMultiType(w http.ResponseWriter, req *http.Request, }, http.StatusOK) } +func (h *APIHandler) initIndex(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { + targetClusterID := ps.MustGetParameter("id") + indexName := ps.MustGetParameter("index") + reqBody := &InitIndexRequest{} + err := h.DecodeJSON(req, reqBody) + if err != nil { + log.Error(err) + h.WriteError(w, err.Error(), http.StatusInternalServerError) + return + } + client := elastic.GetClient(targetClusterID) + exists, err := client.Exists(indexName) + if err != nil { + log.Error(err) + h.WriteError(w, err.Error(), http.StatusInternalServerError) + return + } + if exists { + if len(reqBody.Settings) > 0 { + err = client.UpdateIndexSettings(indexName, reqBody.Settings) + if err != nil { + log.Error(err) + h.WriteError(w, err.Error(), http.StatusInternalServerError) + return + } + } + if len(reqBody.Mappings) > 0 { + mappingBytes := util.MustToJSONBytes(reqBody.Mappings) + _, err = client.UpdateMapping(indexName, mappingBytes) + if err != nil { + log.Error(err) + h.WriteError(w, err.Error(), http.StatusInternalServerError) + return + } + } + }else{ + indexSettings := map[string]interface{}{} + if len(reqBody.Settings) > 0 { + indexSettings["settings"] = reqBody.Settings + } + if len(reqBody.Mappings) > 0 { + indexSettings["mappings"] = reqBody.Mappings + } + err = client.CreateIndex(indexName, indexSettings) + if err != nil { + log.Error(err) + h.WriteError(w, err.Error(), http.StatusInternalServerError) + return + } + } + h.WriteJSON(w, util.MapStr{ + "success": true, + }, http.StatusOK) +} + func getMajorTaskStatsFromInstances(majorTaskID string) (taskStats MajorTaskState, err error) { taskQuery := util.MapStr{ "size": 500, diff --git a/plugin/migration/model.go b/plugin/migration/model.go index 695bb798..131c72eb 100644 --- a/plugin/migration/model.go +++ b/plugin/migration/model.go @@ -109,3 +109,8 @@ type IndexStateInfo struct { ErrorPartitions int IndexDocs float64 } + +type InitIndexRequest struct { + Mappings map[string]interface{} `json:"mappings"` + Settings map[string]interface{} `json:"settings"` +} \ No newline at end of file