Semaphore/api/projects/templates.go

179 lines
4.3 KiB
Go
Raw Normal View History

2016-04-04 15:44:34 +02:00
package projects
import (
log "github.com/sirupsen/logrus"
"github.com/ansible-semaphore/semaphore/api/helpers"
"github.com/ansible-semaphore/semaphore/db"
2021-09-06 13:05:10 +02:00
"github.com/gorilla/context"
2017-02-23 00:21:49 +01:00
"net/http"
2016-04-17 12:41:36 +02:00
"strconv"
)
2016-04-04 15:44:34 +02:00
// 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)
})
2016-04-04 15:44:34 +02:00
}
2020-10-05 00:29:02 +02:00
// 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)
2020-10-05 00:29:02 +02:00
}
func GetTemplateRefs(w http.ResponseWriter, r *http.Request) {
tpl := context.Get(r, "template").(db.Template)
refs, err := helpers.Store(r).GetTemplateRefs(tpl.ProjectID, tpl.ID)
if err != nil {
helpers.WriteError(w, err)
return
}
helpers.WriteJSON(w, http.StatusOK, refs)
}
// 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)
2022-01-19 20:35:59 +01:00
templates, err := helpers.Store(r).GetTemplates(project.ID, db.TemplateFilter{}, helpers.QueryParams(r.URL))
if err != nil {
helpers.WriteError(w, err)
return
}
helpers.WriteJSON(w, http.StatusOK, templates)
2016-04-04 15:44:34 +02:00
}
// 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
2022-06-29 18:25:28 +02:00
newTemplate, err := helpers.Store(r).CreateTemplate(template)
if err != nil {
helpers.WriteError(w, err)
return
}
2021-08-20 08:28:50 +02:00
user := context.Get(r, "user").(*db.User)
objType := db.EventTemplate
2022-06-29 18:25:28 +02:00
desc := "Template ID " + strconv.Itoa(newTemplate.ID) + " created"
_, err = helpers.Store(r).CreateEvent(db.Event{
2021-08-20 08:28:50 +02:00
UserID: &user.ID,
ProjectID: &project.ID,
ObjectType: &objType,
2022-06-29 18:25:28 +02:00
ObjectID: &newTemplate.ID,
Description: &desc,
})
if err != nil {
log.Error(err)
}
2022-06-29 18:25:28 +02:00
helpers.WriteJSON(w, http.StatusCreated, newTemplate)
2016-04-04 15:44:34 +02:00
}
// 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
2021-09-06 13:05:10 +02:00
if template.ID != oldTemplate.ID {
2020-12-04 23:22:05 +01:00
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
2021-09-06 13:05:10 +02:00
"error": "template id in URL and in body must be the same",
})
return
}
if template.ProjectID != oldTemplate.ProjectID {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": "You can not move template to other project",
2020-12-04 23:22:05 +01:00
})
return
}
if template.Arguments != nil && *template.Arguments == "" {
template.Arguments = nil
}
err := helpers.Store(r).UpdateTemplate(template)
if err != nil {
helpers.WriteError(w, err)
return
}
2021-08-20 08:28:50 +02:00
user := context.Get(r, "user").(*db.User)
desc := "Template ID " + strconv.Itoa(template.ID) + " updated"
objType := db.EventTemplate
_, err = helpers.Store(r).CreateEvent(db.Event{
2021-09-06 13:05:10 +02:00
UserID: &user.ID,
ProjectID: &template.ProjectID,
Description: &desc,
ObjectID: &template.ID,
ObjectType: &objType,
})
if err != nil {
log.Error(err)
}
w.WriteHeader(http.StatusNoContent)
2016-04-04 15:44:34 +02:00
}
// 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
}
2021-08-20 08:28:50 +02:00
user := context.Get(r, "user").(*db.User)
desc := "Template ID " + strconv.Itoa(tpl.ID) + " deleted"
_, err = helpers.Store(r).CreateEvent(db.Event{
2021-08-20 08:28:50 +02:00
UserID: &user.ID,
ProjectID: &tpl.ProjectID,
Description: &desc,
})
if err != nil {
log.Error(err)
}
w.WriteHeader(http.StatusNoContent)
2022-01-19 20:35:59 +01:00
}