github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/api4/job.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package api4
     5  
     6  import (
     7  	"net/http"
     8  
     9  	"github.com/mattermost/mattermost-server/model"
    10  )
    11  
    12  func (api *API) InitJob() {
    13  	api.BaseRoutes.Jobs.Handle("", api.ApiSessionRequired(getJobs)).Methods("GET")
    14  	api.BaseRoutes.Jobs.Handle("", api.ApiSessionRequired(createJob)).Methods("POST")
    15  	api.BaseRoutes.Jobs.Handle("/{job_id:[A-Za-z0-9]+}", api.ApiSessionRequired(getJob)).Methods("GET")
    16  	api.BaseRoutes.Jobs.Handle("/{job_id:[A-Za-z0-9]+}/cancel", api.ApiSessionRequired(cancelJob)).Methods("POST")
    17  	api.BaseRoutes.Jobs.Handle("/type/{job_type:[A-Za-z0-9_-]+}", api.ApiSessionRequired(getJobsByType)).Methods("GET")
    18  }
    19  
    20  func getJob(c *Context, w http.ResponseWriter, r *http.Request) {
    21  	c.RequireJobId()
    22  	if c.Err != nil {
    23  		return
    24  	}
    25  
    26  	if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_JOBS) {
    27  		c.SetPermissionError(model.PERMISSION_MANAGE_JOBS)
    28  		return
    29  	}
    30  
    31  	if job, err := c.App.GetJob(c.Params.JobId); err != nil {
    32  		c.Err = err
    33  		return
    34  	} else {
    35  		w.Write([]byte(job.ToJson()))
    36  	}
    37  }
    38  
    39  func createJob(c *Context, w http.ResponseWriter, r *http.Request) {
    40  	job := model.JobFromJson(r.Body)
    41  	if job == nil {
    42  		c.SetInvalidParam("job")
    43  		return
    44  	}
    45  
    46  	if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_JOBS) {
    47  		c.SetPermissionError(model.PERMISSION_MANAGE_JOBS)
    48  		return
    49  	}
    50  
    51  	if job, err := c.App.CreateJob(job); err != nil {
    52  		c.Err = err
    53  		return
    54  	} else {
    55  		w.WriteHeader(http.StatusCreated)
    56  		w.Write([]byte(job.ToJson()))
    57  	}
    58  }
    59  
    60  func getJobs(c *Context, w http.ResponseWriter, r *http.Request) {
    61  	if c.Err != nil {
    62  		return
    63  	}
    64  
    65  	if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_JOBS) {
    66  		c.SetPermissionError(model.PERMISSION_MANAGE_JOBS)
    67  		return
    68  	}
    69  
    70  	if jobs, err := c.App.GetJobsPage(c.Params.Page, c.Params.PerPage); err != nil {
    71  		c.Err = err
    72  		return
    73  	} else {
    74  		w.Write([]byte(model.JobsToJson(jobs)))
    75  	}
    76  }
    77  
    78  func getJobsByType(c *Context, w http.ResponseWriter, r *http.Request) {
    79  	c.RequireJobType()
    80  	if c.Err != nil {
    81  		return
    82  	}
    83  
    84  	if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_JOBS) {
    85  		c.SetPermissionError(model.PERMISSION_MANAGE_JOBS)
    86  		return
    87  	}
    88  
    89  	if jobs, err := c.App.GetJobsByTypePage(c.Params.JobType, c.Params.Page, c.Params.PerPage); err != nil {
    90  		c.Err = err
    91  		return
    92  	} else {
    93  		w.Write([]byte(model.JobsToJson(jobs)))
    94  	}
    95  }
    96  
    97  func cancelJob(c *Context, w http.ResponseWriter, r *http.Request) {
    98  	c.RequireJobId()
    99  	if c.Err != nil {
   100  		return
   101  	}
   102  
   103  	if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_JOBS) {
   104  		c.SetPermissionError(model.PERMISSION_MANAGE_JOBS)
   105  		return
   106  	}
   107  
   108  	if err := c.App.CancelJob(c.Params.JobId); err != nil {
   109  		c.Err = err
   110  		return
   111  	}
   112  
   113  	ReturnStatusOK(w)
   114  }