Semaphore/routes/projects/environment.go

118 lines
2.6 KiB
Go
Raw Normal View History

2016-04-02 14:40:07 +02:00
package projects
import (
"database/sql"
"github.com/ansible-semaphore/semaphore/database"
"github.com/ansible-semaphore/semaphore/models"
"github.com/ansible-semaphore/semaphore/util"
"github.com/gin-gonic/gin"
"github.com/masterminds/squirrel"
)
2016-04-02 14:40:07 +02:00
2016-04-04 15:44:34 +02:00
func EnvironmentMiddleware(c *gin.Context) {
project := c.MustGet("project").(models.Project)
envID, err := util.GetIntParam("environment_id", c)
if err != nil {
return
}
query, args, _ := squirrel.Select("*").
From("project__environment").
Where("project_id=?", project.ID).
Where("id=?", envID).
ToSql()
var env models.Environment
if err := database.Mysql.SelectOne(&env, query, args...); err != nil {
if err == sql.ErrNoRows {
c.AbortWithStatus(404)
return
}
panic(err)
}
c.Set("environment", env)
c.Next()
2016-04-02 14:40:07 +02:00
}
2016-04-04 15:44:34 +02:00
func GetEnvironment(c *gin.Context) {
project := c.MustGet("project").(models.Project)
var env []models.Environment
q := squirrel.Select("*").
From("project__environment").
Where("project_id=?", project.ID)
query, args, _ := q.ToSql()
if _, err := database.Mysql.Select(&env, query, args...); err != nil {
panic(err)
}
c.JSON(200, env)
2016-04-02 14:40:07 +02:00
}
func UpdateEnvironment(c *gin.Context) {
oldEnv := c.MustGet("environment").(models.Environment)
var env models.Environment
if err := c.Bind(&env); err != nil {
return
}
if _, err := database.Mysql.Exec("update project__environment set name=?, json=? where id=?", env.Name, env.JSON, oldEnv.ID); err != nil {
panic(err)
}
c.AbortWithStatus(204)
}
2016-04-04 15:44:34 +02:00
func AddEnvironment(c *gin.Context) {
project := c.MustGet("project").(models.Project)
var env models.Environment
if err := c.Bind(&env); err != nil {
return
}
2016-04-17 02:20:23 +02:00
res, err := database.Mysql.Exec("insert into project__environment set project_id=?, name=?, json=?, password=?", project.ID, env.Name, env.JSON, env.Password)
if err != nil {
panic(err)
}
insertID, _ := res.LastInsertId()
insertIDInt := int(insertID)
objType := "environment"
desc := "Environment " + env.Name + " created"
if err := (models.Event{
ProjectID: &project.ID,
ObjectType: &objType,
ObjectID: &insertIDInt,
Description: &desc,
}.Insert()); err != nil {
panic(err)
}
c.AbortWithStatus(204)
2016-04-04 15:44:34 +02:00
}
func RemoveEnvironment(c *gin.Context) {
env := c.MustGet("environment").(models.Environment)
if _, err := database.Mysql.Exec("delete from project__environment where id=?", env.ID); err != nil {
panic(err)
}
2016-04-17 02:20:23 +02:00
desc := "Environment " + env.Name + " deleted"
if err := (models.Event{
ProjectID: &env.ProjectID,
Description: &desc,
}.Insert()); err != nil {
panic(err)
}
c.AbortWithStatus(204)
2016-04-02 14:40:07 +02:00
}