summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGeorge Goldberg <george@gberg.me>2017-07-19 09:43:05 +0100
committerGitHub <noreply@github.com>2017-07-19 09:43:05 +0100
commitfe368a74565e736ad1fb1dfc20ab364379d83ae9 (patch)
tree56186c11129a4aadc068a6387af39288cc9802b7
parent97f34e483b0fa8b2a8cfe75b72168cfa38cc9d80 (diff)
downloadchat-fe368a74565e736ad1fb1dfc20ab364379d83ae9.tar.gz
chat-fe368a74565e736ad1fb1dfc20ab364379d83ae9.tar.bz2
chat-fe368a74565e736ad1fb1dfc20ab364379d83ae9.zip
PLT-6595: API to purge Elasticsearch indexes. (#6971)
-rw-r--r--api4/elasticsearch.go15
-rw-r--r--api4/elasticsearch_test.go11
-rw-r--r--app/elasticsearch.go13
-rw-r--r--einterfaces/elasticsearch.go1
-rw-r--r--i18n/en.json8
-rw-r--r--model/client4.go10
6 files changed, 58 insertions, 0 deletions
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
@@ -3512,6 +3512,14 @@
"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.