Semaphore/api/projects/templates.go
2020-12-17 00:19:20 +05:00

160 lines
3.7 KiB
Go

package projects
import (
log "github.com/Sirupsen/logrus"
"github.com/ansible-semaphore/semaphore/api/helpers"
"github.com/ansible-semaphore/semaphore/db"
"net/http"
"strconv"
"github.com/gorilla/context"
)
// TemplatesMiddleware ensures a template exists and loads it to the context
func TemplatesMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
project := context.Get(r, "project").(db.Project)
templateID, err := helpers.GetIntParam("template_id", w, r)
if err != nil {
return
}
template, err := helpers.Store(r).GetTemplate(project.ID, templateID)
if err != nil {
helpers.WriteError(w, err)
return
}
context.Set(r, "template", template)
next.ServeHTTP(w, r)
})
}
// GetTemplate returns single template by ID
func GetTemplate(w http.ResponseWriter, r *http.Request) {
template := context.Get(r, "template").(db.Template)
helpers.WriteJSON(w, http.StatusOK, template)
}
// GetTemplates returns all templates for a project in a sort order
func GetTemplates(w http.ResponseWriter, r *http.Request) {
project := context.Get(r, "project").(db.Project)
params := db.RetrieveQueryParams{
SortBy: r.URL.Query().Get("sort"),
SortInverted: r.URL.Query().Get("order") == desc,
}
templates, err := helpers.Store(r).GetTemplates(project.ID, params)
if err != nil {
helpers.WriteError(w, err)
return
}
helpers.WriteJSON(w, http.StatusOK, templates)
}
// AddTemplate adds a template to the database
func AddTemplate(w http.ResponseWriter, r *http.Request) {
project := context.Get(r, "project").(db.Project)
var template db.Template
if !helpers.Bind(w, r, &template) {
return
}
template.ProjectID = project.ID
template, err := helpers.Store(r).CreateTemplate(template)
if err != nil {
helpers.WriteError(w, err)
return
}
objType := "template"
desc := "Template ID " + strconv.Itoa(template.ID) + " created"
_, err = helpers.Store(r).CreateEvent(db.Event{
ProjectID: &project.ID,
ObjectType: &objType,
ObjectID: &template.ID,
Description: &desc,
})
if err != nil {
log.Error(err)
}
helpers.WriteJSON(w, http.StatusCreated, template)
}
// UpdateTemplate writes a template to an existing key in the database
func UpdateTemplate(w http.ResponseWriter, r *http.Request) {
oldTemplate := context.Get(r, "template").(db.Template)
var template db.Template
if !helpers.Bind(w, r, &template) {
return
}
// project ID and template ID in the body and the path must be the same
if template.ID != oldTemplate.ID || template.ProjectID != oldTemplate.ProjectID {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": "You can not move ",
})
return
}
if template.Arguments != nil && *template.Arguments == "" {
template.Arguments = nil
}
err := helpers.Store(r).UpdateTemplate(template)
if err != nil {
helpers.WriteError(w, err)
return
}
desc := "Template ID " + strconv.Itoa(template.ID) + " updated"
objType := "template"
_, err = helpers.Store(r).CreateEvent(db.Event{
ProjectID: &template.ProjectID,
Description: &desc,
ObjectID: &template.ID,
ObjectType: &objType,
})
if err != nil {
log.Error(err)
}
w.WriteHeader(http.StatusNoContent)
}
// RemoveTemplate deletes a template from the database
func RemoveTemplate(w http.ResponseWriter, r *http.Request) {
tpl := context.Get(r, "template").(db.Template)
err := helpers.Store(r).DeleteTemplate(tpl.ProjectID, tpl.ID)
if err != nil {
helpers.WriteError(w, err)
return
}
desc := "Template ID " + strconv.Itoa(tpl.ID) + " deleted"
_, err = helpers.Store(r).CreateEvent(db.Event{
ProjectID: &tpl.ProjectID,
Description: &desc,
})
if err != nil {
log.Error(err)
}
w.WriteHeader(http.StatusNoContent)
}