2016-04-04 15:44:34 +02:00
|
|
|
package projects
|
|
|
|
|
2016-04-07 14:49:34 +02:00
|
|
|
import (
|
2016-04-16 21:42:57 +02:00
|
|
|
"database/sql"
|
2017-02-23 00:21:49 +01:00
|
|
|
"net/http"
|
2016-04-17 12:41:36 +02:00
|
|
|
"strconv"
|
2016-04-16 21:42:57 +02:00
|
|
|
|
2017-02-23 06:12:16 +01:00
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
2019-07-09 19:45:27 +02:00
|
|
|
|
2019-07-09 18:14:06 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/util"
|
2017-02-23 00:21:49 +01:00
|
|
|
"github.com/gorilla/context"
|
2016-04-07 14:49:34 +02:00
|
|
|
"github.com/masterminds/squirrel"
|
|
|
|
)
|
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) {
|
|
|
|
project := context.Get(r, "project").(db.Project)
|
|
|
|
templateID, err := util.GetIntParam("template_id", w, r)
|
|
|
|
if err != nil {
|
2016-04-16 21:42:57 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-09 18:14:06 +02:00
|
|
|
var template db.Template
|
|
|
|
if err := db.Mysql.SelectOne(&template, "select * from project__template where project_id=? and id=?", project.ID, templateID); err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
panic(err)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
project := context.Get(r, "project").(db.Project)
|
|
|
|
var templates []db.Template
|
2016-04-07 14:49:34 +02:00
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
sort := r.URL.Query().Get("sort")
|
|
|
|
order := r.URL.Query().Get("order")
|
2017-03-16 15:30:41 +01:00
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
if order != asc && order != desc {
|
|
|
|
order = asc
|
|
|
|
}
|
2017-03-16 15:30:41 +01:00
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
q := squirrel.Select("pt.id",
|
2019-07-09 18:14:06 +02:00
|
|
|
"pt.ssh_key_id",
|
|
|
|
"pt.project_id",
|
|
|
|
"pt.inventory_id",
|
|
|
|
"pt.repository_id",
|
|
|
|
"pt.environment_id",
|
|
|
|
"pt.alias",
|
|
|
|
"pt.playbook",
|
|
|
|
"pt.arguments",
|
|
|
|
"pt.override_args").
|
|
|
|
From("project__template pt")
|
2017-03-16 15:30:41 +01:00
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
switch sort {
|
|
|
|
case "alias", "playbook":
|
|
|
|
q = q.Where("pt.project_id=?", project.ID).
|
2019-07-09 18:14:06 +02:00
|
|
|
OrderBy("pt." + sort + " " + order)
|
2019-07-09 18:11:01 +02:00
|
|
|
case "ssh_key":
|
|
|
|
q = q.LeftJoin("access_key ak ON (pt.ssh_key_id = ak.id)").
|
|
|
|
Where("pt.project_id=?", project.ID).
|
|
|
|
OrderBy("ak.name " + order)
|
|
|
|
case "inventory":
|
|
|
|
q = q.LeftJoin("project__inventory pi ON (pt.inventory_id = pi.id)").
|
|
|
|
Where("pt.project_id=?", project.ID).
|
|
|
|
OrderBy("pi.name " + order)
|
|
|
|
case "environment":
|
|
|
|
q = q.LeftJoin("project__environment pe ON (pt.environment_id = pe.id)").
|
|
|
|
Where("pt.project_id=?", project.ID).
|
|
|
|
OrderBy("pe.name " + order)
|
|
|
|
case "repository":
|
|
|
|
q = q.LeftJoin("project__repository pr ON (pt.repository_id = pr.id)").
|
|
|
|
Where("pt.project_id=?", project.ID).
|
|
|
|
OrderBy("pr.name " + order)
|
|
|
|
default:
|
|
|
|
q = q.Where("pt.project_id=?", project.ID).
|
2019-07-09 18:14:06 +02:00
|
|
|
OrderBy("pt.alias " + order)
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
query, args, err := q.ToSql()
|
|
|
|
util.LogWarning(err)
|
|
|
|
|
|
|
|
if _, err := db.Mysql.Select(&templates, query, args...); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-07-09 19:45:27 +02:00
|
|
|
util.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) {
|
|
|
|
project := context.Get(r, "project").(db.Project)
|
|
|
|
|
|
|
|
var template db.Template
|
2019-07-09 19:45:27 +02:00
|
|
|
if err := util.Bind(w, r, &template); err != nil {
|
2019-07-09 18:11:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := db.Mysql.Exec("insert into project__template set ssh_key_id=?, project_id=?, inventory_id=?, repository_id=?, environment_id=?, alias=?, playbook=?, arguments=?, override_args=?", template.SSHKeyID, project.ID, template.InventoryID, template.RepositoryID, template.EnvironmentID, template.Alias, template.Playbook, template.Arguments, template.OverrideArguments)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
insertID, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
template.ID = int(insertID)
|
|
|
|
|
|
|
|
objType := "template"
|
|
|
|
desc := "Template ID " + strconv.Itoa(template.ID) + " created"
|
|
|
|
if err := (db.Event{
|
|
|
|
ProjectID: &project.ID,
|
|
|
|
ObjectType: &objType,
|
|
|
|
ObjectID: &template.ID,
|
|
|
|
Description: &desc,
|
|
|
|
}.Insert()); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-07-09 19:45:27 +02:00
|
|
|
util.WriteJSON(w, http.StatusCreated, template)
|
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) {
|
|
|
|
oldTemplate := context.Get(r, "template").(db.Template)
|
|
|
|
|
|
|
|
var template db.Template
|
2019-07-09 19:45:27 +02:00
|
|
|
if err := util.Bind(w, r, &template); err != nil {
|
2019-07-09 18:11:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if template.Arguments != nil && *template.Arguments == "" {
|
|
|
|
template.Arguments = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := db.Mysql.Exec("update project__template set ssh_key_id=?, inventory_id=?, repository_id=?, environment_id=?, alias=?, playbook=?, arguments=?, override_args=? where id=?", template.SSHKeyID, template.InventoryID, template.RepositoryID, template.EnvironmentID, template.Alias, template.Playbook, template.Arguments, template.OverrideArguments, oldTemplate.ID); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
desc := "Template ID " + strconv.Itoa(template.ID) + " updated"
|
|
|
|
objType := "template"
|
|
|
|
if err := (db.Event{
|
|
|
|
ProjectID: &oldTemplate.ProjectID,
|
|
|
|
Description: &desc,
|
|
|
|
ObjectID: &oldTemplate.ID,
|
|
|
|
ObjectType: &objType,
|
|
|
|
}.Insert()); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
tpl := context.Get(r, "template").(db.Template)
|
|
|
|
|
|
|
|
if _, err := db.Mysql.Exec("delete from project__template where id=?", tpl.ID); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
desc := "Template ID " + strconv.Itoa(tpl.ID) + " deleted"
|
|
|
|
if err := (db.Event{
|
|
|
|
ProjectID: &tpl.ProjectID,
|
|
|
|
Description: &desc,
|
|
|
|
}.Insert()); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2016-04-04 15:44:34 +02:00
|
|
|
}
|