PLT-7705: API to get data retention policy. (#7539)

* PLT-7705: API to get data retention policy.

* Fix review comments.
This commit is contained in:
George Goldberg
2017-10-02 12:43:21 +01:00
committed by GitHub
parent 9bc7af0c57
commit 76bd1bb212
13 changed files with 157 additions and 12 deletions

View File

@@ -80,6 +80,8 @@ type Routes struct {
Elasticsearch *mux.Router // 'api/v4/elasticsearch'
DataRetention *mux.Router // 'api/v4/data_retention'
Brand *mux.Router // 'api/v4/brand'
System *mux.Router // 'api/v4/system'
@@ -185,6 +187,7 @@ func Init(a *app.App, root *mux.Router, full bool) *API {
api.BaseRoutes.Reactions = api.BaseRoutes.ApiRoot.PathPrefix("/reactions").Subrouter()
api.BaseRoutes.Jobs = api.BaseRoutes.ApiRoot.PathPrefix("/jobs").Subrouter()
api.BaseRoutes.Elasticsearch = api.BaseRoutes.ApiRoot.PathPrefix("/elasticsearch").Subrouter()
api.BaseRoutes.DataRetention = api.BaseRoutes.ApiRoot.PathPrefix("/data_retention").Subrouter()
api.BaseRoutes.Emojis = api.BaseRoutes.ApiRoot.PathPrefix("/emoji").Subrouter()
api.BaseRoutes.Emoji = api.BaseRoutes.Emojis.PathPrefix("/{emoji_id:[A-Za-z0-9]+}").Subrouter()
@@ -208,6 +211,7 @@ func Init(a *app.App, root *mux.Router, full bool) *API {
api.InitCluster()
api.InitLdap()
api.InitElasticsearch()
api.InitDataRetention()
api.InitBrand()
api.InitJob()
api.InitCommand()

30
api4/data_retention.go Normal file
View File

@@ -0,0 +1,30 @@
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package api4
import (
"net/http"
l4g "github.com/alecthomas/log4go"
"github.com/mattermost/mattermost-server/utils"
)
func (api *API) InitDataRetention() {
l4g.Debug(utils.T("api.data_retention.init.debug"))
api.BaseRoutes.DataRetention.Handle("/policy", api.ApiSessionRequired(getPolicy)).Methods("GET")
}
func getPolicy(c *Context, w http.ResponseWriter, r *http.Request) {
// No permission check required.
if policy, err := c.App.GetDataRetentionPolicy(); err != nil {
c.Err = err
return
} else {
w.Write([]byte(policy.ToJson()))
}
}

View File

@@ -0,0 +1,16 @@
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package api4
import (
"testing"
)
func TestDataRetentionGetPolicy(t *testing.T) {
th := Setup().InitBasic()
defer TearDown()
_, resp := th.Client.GetDataRetentionPolicy()
CheckNotImplementedStatus(t, resp)
}

View File

@@ -34,6 +34,7 @@ type App struct {
Brand einterfaces.BrandInterface
Cluster einterfaces.ClusterInterface
Compliance einterfaces.ComplianceInterface
DataRetention einterfaces.DataRetentionInterface
Elasticsearch einterfaces.ElasticsearchInterface
Ldap einterfaces.LdapInterface
Metrics einterfaces.MetricsInterface
@@ -103,10 +104,16 @@ func RegisterComplianceInterface(f func(*App) einterfaces.ComplianceInterface) {
complianceInterface = f
}
var jobsDataRetentionInterface func(*App) ejobs.DataRetentionInterface
var dataRetentionInterface func(*App) einterfaces.DataRetentionInterface
func RegisterJobsDataRetentionInterface(f func(*App) ejobs.DataRetentionInterface) {
jobsDataRetentionInterface = f
func RegisterDataRetentionInterface(f func(*App) einterfaces.DataRetentionInterface) {
dataRetentionInterface = f
}
var jobsDataRetentionJobInterface func(*App) ejobs.DataRetentionJobInterface
func RegisterJobsDataRetentionJobInterface(f func(*App) ejobs.DataRetentionJobInterface) {
jobsDataRetentionJobInterface = f
}
var jobsElasticsearchAggregatorInterface func(*App) ejobs.ElasticsearchAggregatorInterface
@@ -183,9 +190,11 @@ func (a *App) initEnterprise() {
a.Saml.ConfigureSP()
})
}
if jobsDataRetentionInterface != nil {
a.Jobs.DataRetention = jobsDataRetentionInterface(a)
if dataRetentionInterface != nil {
a.DataRetention = dataRetentionInterface(a)
}
if jobsDataRetentionJobInterface != nil {
a.Jobs.DataRetentionJob = jobsDataRetentionJobInterface(a)
}
if jobsElasticsearchAggregatorInterface != nil {
a.Jobs.ElasticsearchAggregator = jobsElasticsearchAggregatorInterface(a)

18
app/data_retention.go Normal file
View File

@@ -0,0 +1,18 @@
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package app
import (
"net/http"
"github.com/mattermost/mattermost-server/model"
)
func (a *App) GetDataRetentionPolicy() (*model.DataRetentionPolicy, *model.AppError) {
if a.DataRetention == nil {
return nil, model.NewAppError("App.GetDataRetentionPolicy", "ent.data_retention.generic.license.error", nil, "", http.StatusNotImplemented)
}
return a.DataRetention.GetPolicy()
}

View File

@@ -0,0 +1,12 @@
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package einterfaces
import (
"github.com/mattermost/mattermost-server/model"
)
type DataRetentionInterface interface {
GetPolicy() (*model.DataRetentionPolicy, *model.AppError)
}

View File

@@ -7,7 +7,7 @@ import (
"github.com/mattermost/mattermost-server/model"
)
type DataRetentionInterface interface {
type DataRetentionJobInterface interface {
MakeWorker() model.Worker
MakeScheduler() model.Scheduler
}

View File

@@ -3687,6 +3687,10 @@
"id": "ent.compliance.run_started.info",
"translation": "Compliance export started for job '{{.JobName}}' at '{{.FilePath}}'"
},
{
"id": "ent.data_retention.generic.license.error",
"translation": "License does not support Data Retention."
},
{
"id": "ent.elasticsearch.aggregator_worker.create_index_job.error",
"translation": "Elasticsearch aggregator worker failed to create the indexing job"

View File

@@ -35,8 +35,8 @@ func (srv *JobServer) InitSchedulers() *Schedulers {
jobs: srv,
}
if dataRetentionInterface := srv.DataRetention; dataRetentionInterface != nil {
schedulers.schedulers = append(schedulers.schedulers, dataRetentionInterface.MakeScheduler())
if srv.DataRetentionJob != nil {
schedulers.schedulers = append(schedulers.schedulers, srv.DataRetentionJob.MakeScheduler())
}
if elasticsearchAggregatorInterface := srv.ElasticsearchAggregator; elasticsearchAggregatorInterface != nil {

View File

@@ -17,7 +17,7 @@ type JobServer struct {
Workers *Workers
Schedulers *Schedulers
DataRetention ejobs.DataRetentionInterface
DataRetentionJob ejobs.DataRetentionJobInterface
ElasticsearchAggregator ejobs.ElasticsearchAggregatorInterface
ElasticsearchIndexer ejobs.ElasticsearchIndexerInterface
LdapSync ejobs.LdapSyncInterface

View File

@@ -27,8 +27,8 @@ func (srv *JobServer) InitWorkers() *Workers {
workers := &Workers{}
workers.Watcher = srv.MakeWatcher(workers, DEFAULT_WATCHER_POLLING_INTERVAL)
if dataRetentionInterface := srv.DataRetention; dataRetentionInterface != nil {
workers.DataRetention = dataRetentionInterface.MakeWorker()
if srv.DataRetentionJob != nil {
workers.DataRetention = srv.DataRetentionJob.MakeWorker()
}
if elasticsearchIndexerInterface := srv.ElasticsearchIndexer; elasticsearchIndexerInterface != nil {

View File

@@ -254,6 +254,10 @@ func (c *Client4) GetBrandRoute() string {
return fmt.Sprintf("/brand")
}
func (c *Client4) GetDataRetentionRoute() string {
return fmt.Sprintf("/data_retention")
}
func (c *Client4) GetElasticsearchRoute() string {
return fmt.Sprintf("/elasticsearch")
}
@@ -2747,6 +2751,18 @@ func (c *Client4) PurgeElasticsearchIndexes() (bool, *Response) {
}
}
// Data Retention Section
// GetDataRetentionPolicy will get the current server data retention policy details.
func (c *Client4) GetDataRetentionPolicy() (*DataRetentionPolicy, *Response) {
if r, err := c.DoApiGet(c.GetDataRetentionRoute()+"/policy", ""); err != nil {
return nil, BuildErrorResponse(r, err)
} else {
defer closeBody(r)
return DataRetentionPolicyFromJson(r.Body), BuildResponse(r)
}
}
// Commands Section
// CreateCommand will create a new command if the user have the right permissions.

View File

@@ -0,0 +1,36 @@
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package model
import (
"encoding/json"
"io"
)
type DataRetentionPolicy struct {
MessageDeletionEnabled bool `json:"message_deletion_enabled"`
FileDeletionEnabled bool `json:"file_deletion_enabled"`
MessageRetentionCutoff int64 `json:"message_retention_cutoff"`
FileRetentionCutoff int64 `json:"file_retention_cutoff"`
}
func (me *DataRetentionPolicy) ToJson() string {
b, err := json.Marshal(me)
if err != nil {
return ""
} else {
return string(b)
}
}
func DataRetentionPolicyFromJson(data io.Reader) *DataRetentionPolicy {
decoder := json.NewDecoder(data)
var me DataRetentionPolicy
err := decoder.Decode(&me)
if err == nil {
return &me
} else {
return nil
}
}