PLT-6595: API to purge Elasticsearch indexes. (#6971)

This commit is contained in:
George Goldberg
2017-07-19 09:43:05 +01:00
committed by GitHub
parent 97f34e483b
commit fe368a7456
6 changed files with 58 additions and 0 deletions

View File

@@ -16,6 +16,7 @@ func InitElasticsearch() {
l4g.Debug(utils.T("api.elasticsearch.init.debug"))
BaseRoutes.Elasticsearch.Handle("/test", ApiSessionRequired(testElasticsearch)).Methods("POST")
BaseRoutes.Elasticsearch.Handle("/purge_indexes", ApiSessionRequired(purgeElasticsearchIndexes)).Methods("POST")
}
func testElasticsearch(c *Context, w http.ResponseWriter, r *http.Request) {
@@ -36,3 +37,17 @@ func testElasticsearch(c *Context, w http.ResponseWriter, r *http.Request) {
ReturnStatusOK(w)
}
func purgeElasticsearchIndexes(c *Context, w http.ResponseWriter, r *http.Request) {
if !app.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
return
}
if err := app.PurgeElasticsearchIndexes(); err != nil {
c.Err = err
return
}
ReturnStatusOK(w)
}

View File

@@ -17,3 +17,14 @@ func TestElasticsearchTest(t *testing.T) {
_, resp = th.SystemAdminClient.TestElasticsearch()
CheckNotImplementedStatus(t, resp)
}
func TestElasticsearchPurgeIndexes(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer TearDown()
_, resp := th.Client.PurgeElasticsearchIndexes()
CheckForbiddenStatus(t, resp)
_, resp = th.SystemAdminClient.PurgeElasticsearchIndexes()
CheckNotImplementedStatus(t, resp)
}

View File

@@ -31,3 +31,16 @@ func TestElasticsearch(cfg *model.Config) *model.AppError {
return nil
}
func PurgeElasticsearchIndexes() *model.AppError {
if esI := einterfaces.GetElasticsearchInterface(); esI != nil {
if err := esI.PurgeIndexes(); err != nil {
return err
}
} else {
err := model.NewAppError("PurgeElasticsearchIndexes", "ent.elasticsearch.test_config.license.error", nil, "", http.StatusNotImplemented)
return err
}
return nil
}

View File

@@ -11,6 +11,7 @@ type ElasticsearchInterface interface {
SearchPosts(channels *model.ChannelList, searchParams []*model.SearchParams) ([]string, *model.AppError)
DeletePost(postId string) *model.AppError
TestConfig(cfg *model.Config) *model.AppError
PurgeIndexes() *model.AppError
}
var theElasticsearchInterface ElasticsearchInterface

View File

@@ -3511,6 +3511,14 @@
"id": "ent.elasticsearch.search_posts.disabled",
"translation": "ElasticSearch searching is disabled on this server"
},
{
"id": "ent.elasticsearch.generic.disabled",
"translation": "ElasticSearch search is not enabled on this server"
},
{
"id": "ent.elasticsearch.purge_indexes.delete_failed",
"translation": "Failed to delete Elasticsearch index"
},
{
"id": "ent.elasticsearch.search_posts.search_failed",
"translation": "Search failed to complete"

View File

@@ -2551,6 +2551,16 @@ func (c *Client4) TestElasticsearch() (bool, *Response) {
}
}
// PurgeElasticsearchIndexes immediately deletes all Elasticsearch indexes.
func (c *Client4) PurgeElasticsearchIndexes() (bool, *Response) {
if r, err := c.DoApiPost(c.GetElasticsearchRoute()+"/test", ""); err != nil {
return false, BuildErrorResponse(r, err)
} else {
defer closeBody(r)
return CheckStatusOK(r), BuildResponse(r)
}
}
// Commands Section
// CreateCommand will create a new command if the user have the right permissions.