github.com/turgay/mattermost-server@v5.3.2-0.20181002173352-2945e8a2b0ce+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  	job, err := c.App.GetJob(c.Params.JobId)
    32  	if err != nil {
    33  		c.Err = err
    34  		return
    35  	}
    36  
    37  	w.Write([]byte(job.ToJson()))
    38  }
    39  
    40  func createJob(c *Context, w http.ResponseWriter, r *http.Request) {
    41  	job := model.JobFromJson(r.Body)
    42  	if job == nil {
    43  		c.SetInvalidParam("job")
    44  		return
    45  	}
    46  
    47  	if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_JOBS) {
    48  		c.SetPermissionError(model.PERMISSION_MANAGE_JOBS)
    49  		return
    50  	}
    51  
    52  	job, err := c.App.CreateJob(job)
    53  	if err != nil {
    54  		c.Err = err
    55  		return
    56  	}
    57  
    58  	w.WriteHeader(http.StatusCreated)
    59  	w.Write([]byte(job.ToJson()))
    60  }
    61  
    62  func getJobs(c *Context, w http.ResponseWriter, r *http.Request) {
    63  	if c.Err != nil {
    64  		return
    65  	}
    66  
    67  	if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_JOBS) {
    68  		c.SetPermissionError(model.PERMISSION_MANAGE_JOBS)
    69  		return
    70  	}
    71  
    72  	jobs, err := c.App.GetJobsPage(c.Params.Page, c.Params.PerPage)
    73  	if err != nil {
    74  		c.Err = err
    75  		return
    76  	}
    77  
    78  	w.Write([]byte(model.JobsToJson(jobs)))
    79  }
    80  
    81  func getJobsByType(c *Context, w http.ResponseWriter, r *http.Request) {
    82  	c.RequireJobType()
    83  	if c.Err != nil {
    84  		return
    85  	}
    86  
    87  	if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_JOBS) {
    88  		c.SetPermissionError(model.PERMISSION_MANAGE_JOBS)
    89  		return
    90  	}
    91  
    92  	jobs, err := c.App.GetJobsByTypePage(c.Params.JobType, c.Params.Page, c.Params.PerPage)
    93  	if err != nil {
    94  		c.Err = err
    95  		return
    96  	}
    97  
    98  	w.Write([]byte(model.JobsToJson(jobs)))
    99  }
   100  
   101  func cancelJob(c *Context, w http.ResponseWriter, r *http.Request) {
   102  	c.RequireJobId()
   103  	if c.Err != nil {
   104  		return
   105  	}
   106  
   107  	if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_JOBS) {
   108  		c.SetPermissionError(model.PERMISSION_MANAGE_JOBS)
   109  		return
   110  	}
   111  
   112  	if err := c.App.CancelJob(c.Params.JobId); err != nil {
   113  		c.Err = err
   114  		return
   115  	}
   116  
   117  	ReturnStatusOK(w)
   118  }