Semaphore/api/projects/templates.go

129 lines
3.3 KiB
Go
Raw Normal View History

2016-04-04 15:44:34 +02:00
package projects
import (
"database/sql"
2016-04-17 12:41:36 +02:00
"strconv"
2016-05-24 11:55:48 +02:00
database "github.com/ansible-semaphore/semaphore/db"
"github.com/ansible-semaphore/semaphore/models"
"github.com/ansible-semaphore/semaphore/util"
"github.com/gin-gonic/gin"
"github.com/masterminds/squirrel"
)
2016-04-04 15:44:34 +02:00
func TemplatesMiddleware(c *gin.Context) {
project := c.MustGet("project").(models.Project)
templateID, err := util.GetIntParam("template_id", c)
if err != nil {
return
}
var template models.Template
if err := database.Mysql.SelectOne(&template, "select * from project__template where project_id=? and id=?", project.ID, templateID); err != nil {
if err == sql.ErrNoRows {
c.AbortWithStatus(404)
return
}
panic(err)
}
c.Set("template", template)
c.Next()
2016-04-04 15:44:34 +02:00
}
func GetTemplates(c *gin.Context) {
project := c.MustGet("project").(models.Project)
var templates []models.Template
q := squirrel.Select("*").
From("project__template").
Where("project_id=?", project.ID)
query, args, _ := q.ToSql()
if _, err := database.Mysql.Select(&templates, query, args...); err != nil {
panic(err)
}
c.JSON(200, templates)
2016-04-04 15:44:34 +02:00
}
func AddTemplate(c *gin.Context) {
project := c.MustGet("project").(models.Project)
var template models.Template
if err := c.Bind(&template); err != nil {
return
}
2016-11-15 16:44:41 +01:00
res, err := database.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)
2016-04-17 12:41:36 +02:00
objType := "template"
desc := "Template ID " + strconv.Itoa(template.ID) + " created"
if err := (models.Event{
ProjectID: &project.ID,
ObjectType: &objType,
ObjectID: &template.ID,
Description: &desc,
}.Insert()); err != nil {
panic(err)
}
c.JSON(201, template)
2016-04-04 15:44:34 +02:00
}
func UpdateTemplate(c *gin.Context) {
oldTemplate := c.MustGet("template").(models.Template)
var template models.Template
if err := c.Bind(&template); err != nil {
return
}
2016-11-15 16:44:41 +01:00
if _, err := database.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)
}
2016-04-17 12:41:36 +02:00
desc := "Template ID " + strconv.Itoa(template.ID) + " updated"
objType := "template"
if err := (models.Event{
ProjectID: &oldTemplate.ProjectID,
Description: &desc,
ObjectID: &oldTemplate.ID,
ObjectType: &objType,
}.Insert()); err != nil {
panic(err)
}
c.AbortWithStatus(204)
2016-04-04 15:44:34 +02:00
}
func RemoveTemplate(c *gin.Context) {
tpl := c.MustGet("template").(models.Template)
if _, err := database.Mysql.Exec("delete from project__template where id=?", tpl.ID); err != nil {
panic(err)
}
2016-04-17 12:41:36 +02:00
desc := "Template ID " + strconv.Itoa(tpl.ID) + " deleted"
if err := (models.Event{
ProjectID: &tpl.ProjectID,
Description: &desc,
}.Insert()); err != nil {
panic(err)
}
c.AbortWithStatus(204)
2016-04-04 15:44:34 +02:00
}