From fe368a74565e736ad1fb1dfc20ab364379d83ae9 Mon Sep 17 00:00:00 2001 From: George Goldberg Date: Wed, 19 Jul 2017 09:43:05 +0100 Subject: PLT-6595: API to purge Elasticsearch indexes. (#6971) --- api4/elasticsearch.go | 15 +++++++++++++++ api4/elasticsearch_test.go | 11 +++++++++++ app/elasticsearch.go | 13 +++++++++++++ einterfaces/elasticsearch.go | 1 + i18n/en.json | 8 ++++++++ model/client4.go | 10 ++++++++++ 6 files changed, 58 insertions(+) diff --git a/api4/elasticsearch.go b/api4/elasticsearch.go index 9eafec48b..c8af8089b 100644 --- a/api4/elasticsearch.go +++ b/api4/elasticsearch.go @@ -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) +} diff --git a/api4/elasticsearch_test.go b/api4/elasticsearch_test.go index 1478f052f..768a73ad7 100644 --- a/api4/elasticsearch_test.go +++ b/api4/elasticsearch_test.go @@ -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) +} diff --git a/app/elasticsearch.go b/app/elasticsearch.go index ef34a6074..857ed570a 100644 --- a/app/elasticsearch.go +++ b/app/elasticsearch.go @@ -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 +} diff --git a/einterfaces/elasticsearch.go b/einterfaces/elasticsearch.go index f5babffe0..b82482146 100644 --- a/einterfaces/elasticsearch.go +++ b/einterfaces/elasticsearch.go @@ -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 diff --git a/i18n/en.json b/i18n/en.json index 0ba3194fa..27e65c6ba 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -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" diff --git a/model/client4.go b/model/client4.go index a19a17d3a..feff9f8de 100644 --- a/model/client4.go +++ b/model/client4.go @@ -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. -- cgit v1.2.3-1-g7c22