[MM-56074] mmctl job commands (#26855)
* add job list and update job status command to mmctl
Этот коммит содержится в:
@@ -23,6 +23,7 @@ func (api *API) InitJob() {
|
||||
api.BaseRoutes.Jobs.Handle("/{job_id:[A-Za-z0-9]+}/download", api.APISessionRequiredTrustRequester(downloadJob)).Methods("GET")
|
||||
api.BaseRoutes.Jobs.Handle("/{job_id:[A-Za-z0-9]+}/cancel", api.APISessionRequired(cancelJob)).Methods("POST")
|
||||
api.BaseRoutes.Jobs.Handle("/type/{job_type:[A-Za-z0-9_-]+}", api.APISessionRequired(getJobsByType)).Methods("GET")
|
||||
api.BaseRoutes.Jobs.Handle("/{job_id:[A-Za-z0-9]+}/status", api.APISessionRequired(updateJobStatus)).Methods("PATCH")
|
||||
}
|
||||
|
||||
func getJob(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
@@ -147,23 +148,58 @@ func getJobs(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
jobType := r.URL.Query().Get("job_type")
|
||||
var validJobTypes []string
|
||||
for _, jobType := range model.AllJobTypes {
|
||||
|
||||
if jobType != "" {
|
||||
isValidJobType := model.IsValidJobType(jobType)
|
||||
if !isValidJobType {
|
||||
c.SetInvalidURLParam("job_type")
|
||||
return
|
||||
}
|
||||
hasPermission, permissionRequired := c.App.SessionHasPermissionToReadJob(*c.AppContext.Session(), jobType)
|
||||
if permissionRequired == nil {
|
||||
c.Logger.Warn("The job types of a job you are trying to retrieve does not contain permissions", mlog.String("jobType", jobType))
|
||||
continue
|
||||
c.Err = model.NewAppError("getJobsByType", "api.job.retrieve.nopermissions", nil, "", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if hasPermission {
|
||||
validJobTypes = append(validJobTypes, jobType)
|
||||
if !hasPermission {
|
||||
c.SetPermissionError(permissionRequired)
|
||||
return
|
||||
}
|
||||
validJobTypes = append(validJobTypes, jobType)
|
||||
} else {
|
||||
for _, jType := range model.AllJobTypes {
|
||||
hasPermission, permissionRequired := c.App.SessionHasPermissionToReadJob(*c.AppContext.Session(), jType)
|
||||
if permissionRequired == nil {
|
||||
c.Logger.Warn("The job types of a job you are trying to retrieve does not contain permissions", mlog.String("jobType", jType))
|
||||
continue
|
||||
}
|
||||
if hasPermission {
|
||||
validJobTypes = append(validJobTypes, jType)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(validJobTypes) == 0 {
|
||||
c.SetPermissionError()
|
||||
return
|
||||
}
|
||||
|
||||
jobs, appErr := c.App.GetJobsByTypesPage(c.AppContext, validJobTypes, c.Params.Page, c.Params.PerPage)
|
||||
status := r.URL.Query().Get("status")
|
||||
isValidStatus := model.IsValidJobStatus(status)
|
||||
if status != "" && !isValidStatus {
|
||||
c.Err = model.NewAppError("getJobs", "api.job.status.invalid", nil, "", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
var jobs []*model.Job
|
||||
var appErr *model.AppError
|
||||
|
||||
if status == "" {
|
||||
jobs, appErr = c.App.GetJobsByTypesPage(c.AppContext, validJobTypes, c.Params.Page, c.Params.PerPage)
|
||||
} else {
|
||||
jobs, appErr = c.App.GetJobsByTypeAndStatus(c.AppContext, validJobTypes, status, c.Params.Page, c.Params.PerPage)
|
||||
}
|
||||
|
||||
if appErr != nil {
|
||||
c.Err = appErr
|
||||
return
|
||||
@@ -248,3 +284,60 @@ func cancelJob(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
ReturnStatusOK(w)
|
||||
}
|
||||
|
||||
func updateJobStatus(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
c.RequireJobId()
|
||||
if c.Err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
auditRec := c.MakeAuditRecord("updateJobStatus", audit.Fail)
|
||||
defer c.LogAuditRec(auditRec)
|
||||
audit.AddEventParameter(auditRec, "job_id", c.Params.JobId)
|
||||
|
||||
props := model.StringInterfaceFromJSON(r.Body)
|
||||
status, ok := props["status"].(string)
|
||||
if !ok {
|
||||
c.SetInvalidParam("status")
|
||||
return
|
||||
}
|
||||
|
||||
force, ok := props["force"].(bool)
|
||||
if !ok {
|
||||
force = false
|
||||
}
|
||||
|
||||
job, err := c.App.GetJob(c.AppContext, c.Params.JobId)
|
||||
if err != nil {
|
||||
c.Err = err
|
||||
return
|
||||
}
|
||||
|
||||
auditRec.AddEventPriorState(job)
|
||||
auditRec.AddEventObjectType("job")
|
||||
|
||||
hasPermission, permissionRequired := c.App.SessionHasPermissionToManageJob(*c.AppContext.Session(), job)
|
||||
if permissionRequired == nil {
|
||||
c.Err = model.NewAppError("updateJobStatus", "api.job.unable_to_manage_job.incorrect_job_type", nil, "", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if !hasPermission {
|
||||
c.SetPermissionError(permissionRequired)
|
||||
return
|
||||
}
|
||||
|
||||
if !force && !job.IsValidStatusChange(status) {
|
||||
c.Err = model.NewAppError("updateJobStatus", "api.job.status.invalid", nil, "", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if err := c.App.UpdateJobStatus(c.AppContext, job, status); err != nil {
|
||||
c.Err = err
|
||||
return
|
||||
}
|
||||
|
||||
auditRec.Success()
|
||||
|
||||
ReturnStatusOK(w)
|
||||
}
|
||||
|
||||
@@ -9,4 +9,5 @@ func (api *API) InitJobLocal() {
|
||||
api.BaseRoutes.Jobs.Handle("/{job_id:[A-Za-z0-9]+}", api.APILocal(getJob)).Methods("GET")
|
||||
api.BaseRoutes.Jobs.Handle("/{job_id:[A-Za-z0-9]+}/cancel", api.APILocal(cancelJob)).Methods("POST")
|
||||
api.BaseRoutes.Jobs.Handle("/type/{job_type:[A-Za-z0-9_-]+}", api.APILocal(getJobsByType)).Methods("GET")
|
||||
api.BaseRoutes.Jobs.Handle("/{job_id:[A-Za-z0-9]+}/status", api.APILocal(updateJobStatus)).Methods("PATCH")
|
||||
}
|
||||
|
||||
@@ -102,6 +102,12 @@ func TestGetJobs(t *testing.T) {
|
||||
Type: jobType,
|
||||
CreateAt: t0 + 2,
|
||||
},
|
||||
{
|
||||
Id: model.NewId(),
|
||||
Type: model.JobTypeLdapSync,
|
||||
CreateAt: t0 + 3,
|
||||
Status: model.JobStatusPending,
|
||||
},
|
||||
}
|
||||
|
||||
for _, job := range jobs {
|
||||
@@ -110,21 +116,47 @@ func TestGetJobs(t *testing.T) {
|
||||
defer th.App.Srv().Store().Job().Delete(job.Id)
|
||||
}
|
||||
|
||||
received, _, err := th.SystemAdminClient.GetJobs(context.Background(), 0, 2)
|
||||
require.NoError(t, err)
|
||||
t.Run("Get 2 jobs", func(t *testing.T) {
|
||||
received, _, err := th.SystemAdminClient.GetJobs(context.Background(), "", "", 0, 2)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Len(t, received, 2, "received wrong number of jobs")
|
||||
require.Equal(t, jobs[2].Id, received[0].Id, "should've received newest job first")
|
||||
require.Equal(t, jobs[0].Id, received[1].Id, "should've received second newest job second")
|
||||
require.Len(t, received, 2, "received wrong number of jobs")
|
||||
require.Equal(t, jobs[3].Id, received[0].Id, "should've received newest job first")
|
||||
require.Equal(t, jobs[2].Id, received[1].Id, "should've received second newest job second")
|
||||
})
|
||||
|
||||
received, _, err = th.SystemAdminClient.GetJobs(context.Background(), 1, 2)
|
||||
require.NoError(t, err)
|
||||
t.Run("Get oldest job using paging", func(t *testing.T) {
|
||||
received, _, err := th.SystemAdminClient.GetJobs(context.Background(), "", "", 1, 3)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, jobs[1].Id, received[0].Id, "should've received oldest job last")
|
||||
})
|
||||
|
||||
require.Equal(t, jobs[1].Id, received[0].Id, "should've received oldest job last")
|
||||
t.Run("Return error fetching job without permissions", func(t *testing.T) {
|
||||
_, resp, err := th.Client.GetJobs(context.Background(), "", "", 0, 60)
|
||||
require.Error(t, err)
|
||||
CheckForbiddenStatus(t, resp)
|
||||
})
|
||||
|
||||
_, resp, err := th.Client.GetJobs(context.Background(), 0, 60)
|
||||
require.Error(t, err)
|
||||
CheckForbiddenStatus(t, resp)
|
||||
t.Run("Get job by type", func(t *testing.T) {
|
||||
received, _, err := th.SystemAdminClient.GetJobs(context.Background(), model.JobTypeLdapSync, "", 0, 3)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, received, 1, "received wrong number of jobs")
|
||||
require.Equal(t, jobs[3].Id, received[0].Id, "should've received the ldap sync job")
|
||||
})
|
||||
|
||||
t.Run("Get job by status", func(t *testing.T) {
|
||||
received, _, err := th.SystemAdminClient.GetJobs(context.Background(), "", model.JobStatusPending, 0, 3)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, received, 1, "received wrong number of jobs")
|
||||
require.Equal(t, jobs[3].Id, received[0].Id, "should've received the ldap sync job")
|
||||
})
|
||||
|
||||
t.Run("Get job by type and status", func(t *testing.T) {
|
||||
received, _, err := th.SystemAdminClient.GetJobs(context.Background(), model.JobTypeLdapSync, model.JobStatusPending, 0, 3)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, received, 1, "received wrong number of jobs")
|
||||
require.Equal(t, jobs[3].Id, received[0].Id, "should've received the ldap sync job")
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetJobsByType(t *testing.T) {
|
||||
@@ -336,3 +368,69 @@ func TestCancelJob(t *testing.T) {
|
||||
require.Error(t, err)
|
||||
CheckNotFoundStatus(t, resp)
|
||||
}
|
||||
|
||||
func TestUpdateJobStatus(t *testing.T) {
|
||||
th := Setup(t)
|
||||
defer th.TearDown()
|
||||
|
||||
jobType := model.JobTypeDataRetention
|
||||
jobs := []*model.Job{
|
||||
{
|
||||
Id: model.NewId(),
|
||||
Type: jobType,
|
||||
Status: model.JobStatusPending,
|
||||
},
|
||||
{
|
||||
Id: model.NewId(),
|
||||
Type: jobType,
|
||||
Status: model.JobStatusInProgress,
|
||||
},
|
||||
{
|
||||
Id: model.NewId(),
|
||||
Type: jobType,
|
||||
Status: model.JobStatusSuccess,
|
||||
},
|
||||
{
|
||||
Id: model.NewId(),
|
||||
Type: jobType,
|
||||
Status: model.JobStatusPending,
|
||||
},
|
||||
}
|
||||
|
||||
for _, job := range jobs {
|
||||
_, err := th.App.Srv().Store().Job().Save(job)
|
||||
require.NoError(t, err)
|
||||
defer th.App.Srv().Store().Job().Delete(job.Id)
|
||||
}
|
||||
|
||||
t.Run("Fail to update job status without permission", func(t *testing.T) {
|
||||
resp, err := th.Client.UpdateJobStatus(context.Background(), jobs[0].Id, model.JobStatusCancelRequested, false)
|
||||
require.Error(t, err)
|
||||
CheckForbiddenStatus(t, resp)
|
||||
})
|
||||
|
||||
t.Run("Change a pending job to cancel requested without force with sysadmin client", func(t *testing.T) {
|
||||
_, err := th.SystemAdminClient.UpdateJobStatus(context.Background(), jobs[0].Id, model.JobStatusCancelRequested, false)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("Change a pending job to cancel requested without force with local client", func(t *testing.T) {
|
||||
_, err := th.LocalClient.UpdateJobStatus(context.Background(), jobs[3].Id, model.JobStatusCancelRequested, false)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("Fail to change a pending job to canceled without force", func(t *testing.T) {
|
||||
th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
|
||||
resp, err := client.UpdateJobStatus(context.Background(), jobs[0].Id, model.JobStatusCanceled, false)
|
||||
require.Error(t, err)
|
||||
CheckBadRequestStatus(t, resp)
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Change a pending job to canceled with force", func(t *testing.T) {
|
||||
th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
|
||||
_, err := client.UpdateJobStatus(context.Background(), jobs[0].Id, model.JobStatusCanceled, true)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user