2016-04-04 15:44:34 +02:00
|
|
|
package projects
|
|
|
|
|
2016-04-07 14:49:34 +02:00
|
|
|
import (
|
2024-04-12 09:23:13 +02:00
|
|
|
"fmt"
|
2024-07-10 10:12:01 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/util"
|
2024-04-12 09:23:13 +02:00
|
|
|
"net/http"
|
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
"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"
|
2024-04-02 19:43:49 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2016-04-07 14:49:34 +02:00
|
|
|
)
|
2016-04-04 15:44:34 +02:00
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// TemplatesMiddleware ensures a template exists and loads it to the context
|
2019-07-09 18:14:06 +02:00
|
|
|
func TemplatesMiddleware(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
project := context.Get(r, "project").(db.Project)
|
2020-12-03 14:51:15 +01:00
|
|
|
templateID, err := helpers.GetIntParam("template_id", w, r)
|
2019-07-09 18:14:06 +02:00
|
|
|
if err != nil {
|
2016-04-16 21:42:57 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
template, err := helpers.Store(r).GetTemplate(project.ID, templateID)
|
2019-07-09 18:14:06 +02:00
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:14:06 +02:00
|
|
|
}
|
2016-04-16 21:42:57 +02:00
|
|
|
|
2019-07-09 18:14:06 +02:00
|
|
|
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) {
|
2020-12-04 23:41:26 +01:00
|
|
|
template := context.Get(r, "template").(db.Template)
|
2020-12-03 14:51:15 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusOK, template)
|
2020-10-05 00:29:02 +02:00
|
|
|
}
|
|
|
|
|
2022-02-04 18:35:08 +01: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)
|
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// GetTemplates returns all templates for a project in a sort order
|
2019-07-09 18:11:01 +02:00
|
|
|
func GetTemplates(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
project := context.Get(r, "project").(db.Project)
|
2017-03-16 15:30:41 +01:00
|
|
|
|
2022-01-19 20:35:59 +01:00
|
|
|
templates, err := helpers.Store(r).GetTemplates(project.ID, db.TemplateFilter{}, helpers.QueryParams(r.URL))
|
2019-07-09 18:11:01 +02:00
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusOK, templates)
|
2016-04-04 15:44:34 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// AddTemplate adds a template to the database
|
2019-07-09 18:11:01 +02:00
|
|
|
func AddTemplate(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
project := context.Get(r, "project").(db.Project)
|
2019-07-09 18:11:01 +02:00
|
|
|
|
2020-12-04 23:41:26 +01:00
|
|
|
var template db.Template
|
2020-12-03 14:51:15 +01:00
|
|
|
if !helpers.Bind(w, r, &template) {
|
2019-07-09 18:11:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-12 21:53:00 +02:00
|
|
|
var err error
|
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
template.ProjectID = project.ID
|
2022-06-29 18:25:28 +02:00
|
|
|
newTemplate, err := helpers.Store(r).CreateTemplate(template)
|
2019-07-09 18:11:01 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-12-03 14:51:15 +01:00
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
2024-07-10 10:12:01 +02:00
|
|
|
if _, ok := util.Config.Apps[string(newTemplate.App)]; !ok {
|
2024-07-10 14:25:38 +02:00
|
|
|
helpers.WriteErrorStatus(w, "Invalid app id: "+string(newTemplate.App), http.StatusBadRequest)
|
2024-07-10 10:12:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check workspace and create it if required.
|
2024-06-12 21:53:00 +02:00
|
|
|
if newTemplate.App.IsTerraform() {
|
|
|
|
var inv db.Inventory
|
|
|
|
|
|
|
|
if newTemplate.InventoryID == nil {
|
|
|
|
inv, err = helpers.Store(r).CreateInventory(db.Inventory{
|
|
|
|
Name: newTemplate.Name + " - default",
|
|
|
|
ProjectID: project.ID,
|
|
|
|
HolderID: &newTemplate.ID,
|
|
|
|
Type: db.InventoryTerraformWorkspace,
|
|
|
|
Inventory: "default",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
newTemplate.InventoryID = &inv.ID
|
|
|
|
err = helpers.Store(r).UpdateTemplate(newTemplate)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
inv, err = helpers.Store(r).GetInventory(project.ID, *newTemplate.InventoryID)
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
inv.HolderID = &newTemplate.ID
|
|
|
|
err = helpers.Store(r).UpdateInventory(inv)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-12 09:23:13 +02:00
|
|
|
helpers.EventLog(r, helpers.EventLogCreate, helpers.EventLogItem{
|
|
|
|
UserID: helpers.UserFromContext(r).ID,
|
|
|
|
ProjectID: project.ID,
|
|
|
|
ObjectType: db.EventSchedule,
|
|
|
|
ObjectID: newTemplate.ID,
|
|
|
|
Description: fmt.Sprintf("Template ID %d created", newTemplate.ID),
|
2020-12-01 20:06:49 +01:00
|
|
|
})
|
|
|
|
|
2022-06-29 18:25:28 +02:00
|
|
|
helpers.WriteJSON(w, http.StatusCreated, newTemplate)
|
2016-04-04 15:44:34 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// UpdateTemplate writes a template to an existing key in the database
|
2019-07-09 18:11:01 +02:00
|
|
|
func UpdateTemplate(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
oldTemplate := context.Get(r, "template").(db.Template)
|
2019-07-09 18:11:01 +02:00
|
|
|
|
2020-12-04 23:41:26 +01:00
|
|
|
var template db.Template
|
2020-12-03 14:51:15 +01:00
|
|
|
if !helpers.Bind(w, r, &template) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-10 10:12:01 +02:00
|
|
|
if _, ok := util.Config.Apps[string(template.App)]; !ok {
|
2024-07-10 14:25:38 +02:00
|
|
|
helpers.WriteErrorStatus(w, "Invalid app id: "+string(template.App), http.StatusBadRequest)
|
2024-07-10 10:12:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
// 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
|
|
|
})
|
2019-07-09 18:11:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if template.Arguments != nil && *template.Arguments == "" {
|
|
|
|
template.Arguments = nil
|
|
|
|
}
|
|
|
|
|
2024-04-02 19:43:49 +02:00
|
|
|
if template.Type != db.TemplateDeploy {
|
|
|
|
template.BuildTemplateID = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if template.Type != db.TemplateBuild {
|
|
|
|
template.StartVersion = nil
|
|
|
|
}
|
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
err := helpers.Store(r).UpdateTemplate(template)
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
2024-04-12 09:23:13 +02:00
|
|
|
helpers.EventLog(r, helpers.EventLogUpdate, helpers.EventLogItem{
|
|
|
|
UserID: helpers.UserFromContext(r).ID,
|
|
|
|
ProjectID: oldTemplate.ProjectID,
|
|
|
|
ObjectType: db.EventTemplate,
|
|
|
|
ObjectID: oldTemplate.ID,
|
|
|
|
Description: fmt.Sprintf("Template ID %d updated", template.ID),
|
2020-12-01 20:06:49 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2016-04-04 15:44:34 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// RemoveTemplate deletes a template from the database
|
2019-07-09 18:11:01 +02:00
|
|
|
func RemoveTemplate(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
tpl := context.Get(r, "template").(db.Template)
|
2019-07-09 18:11:01 +02:00
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
err := helpers.Store(r).DeleteTemplate(tpl.ProjectID, tpl.ID)
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
2024-04-12 09:23:13 +02:00
|
|
|
helpers.EventLog(r, helpers.EventLogDelete, helpers.EventLogItem{
|
|
|
|
UserID: helpers.UserFromContext(r).ID,
|
|
|
|
ProjectID: tpl.ProjectID,
|
|
|
|
ObjectType: db.EventTemplate,
|
|
|
|
ObjectID: tpl.ID,
|
|
|
|
Description: fmt.Sprintf("Template ID %d deleted", tpl.ID),
|
2020-12-01 20:06:49 +01:00
|
|
|
})
|
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2022-01-19 20:35:59 +01:00
|
|
|
}
|