Semaphore/api/projects/environment.go

182 lines
4.5 KiB
Go
Raw Normal View History

2016-04-02 14:40:07 +02:00
package projects
import (
"database/sql"
2017-04-18 15:58:48 +02:00
"encoding/json"
2017-02-23 00:21:49 +01:00
"net/http"
2017-02-23 06:12:16 +01:00
"github.com/ansible-semaphore/semaphore/db"
"github.com/ansible-semaphore/semaphore/util"
"github.com/ansible-semaphore/semaphore/mulekick"
2017-02-23 00:21:49 +01:00
"github.com/gorilla/context"
"github.com/masterminds/squirrel"
)
2016-04-02 14:40:07 +02:00
// EnvironmentMiddleware ensures an environment exists and loads it to the context
2017-02-22 23:17:36 +01:00
func EnvironmentMiddleware(w http.ResponseWriter, r *http.Request) {
2017-02-23 06:12:16 +01:00
project := context.Get(r, "project").(db.Project)
2017-02-23 00:21:49 +01:00
envID, err := util.GetIntParam("environment_id", w, r)
if err != nil {
return
}
query, args, err := squirrel.Select("*").
From("project__environment").
Where("project_id=?", project.ID).
Where("id=?", envID).
ToSql()
util.LogWarning(err)
2017-02-23 06:12:16 +01:00
var env db.Environment
if err := db.Mysql.SelectOne(&env, query, args...); err != nil {
if err == sql.ErrNoRows {
2017-02-22 23:17:36 +01:00
w.WriteHeader(http.StatusNotFound)
return
}
panic(err)
}
2017-02-23 00:21:49 +01:00
context.Set(r, "environment", env)
2016-04-02 14:40:07 +02:00
}
// GetEnvironment retrieves sorted environments from the database
2017-02-22 23:17:36 +01:00
func GetEnvironment(w http.ResponseWriter, r *http.Request) {
2017-02-23 06:12:16 +01:00
project := context.Get(r, "project").(db.Project)
var env []db.Environment
sort := r.URL.Query().Get("sort")
order := r.URL.Query().Get("order")
if order != "asc" && order != "desc" {
order = "asc"
}
q := squirrel.Select("*").
From("project__environment pe").
Where("project_id=?", project.ID)
switch sort {
case "name":
q = q.Where("pe.project_id=?", project.ID).
OrderBy("pe." + sort + " " + order)
default:
q = q.Where("pe.project_id=?", project.ID).
OrderBy("pe.name " + order)
}
query, args, err := q.ToSql()
util.LogWarning(err)
2017-02-23 06:12:16 +01:00
if _, err := db.Mysql.Select(&env, query, args...); err != nil {
panic(err)
}
2017-02-23 00:21:49 +01:00
mulekick.WriteJSON(w, http.StatusOK, env)
2016-04-02 14:40:07 +02:00
}
// UpdateEnvironment updates an existing environment in the database
2017-02-22 23:17:36 +01:00
func UpdateEnvironment(w http.ResponseWriter, r *http.Request) {
2017-02-23 06:12:16 +01:00
oldEnv := context.Get(r, "environment").(db.Environment)
var env db.Environment
if err := mulekick.Bind(w, r, &env); err != nil {
return
}
var js map[string]interface{}
if json.Unmarshal([]byte(env.JSON), &js) != nil {
2017-04-18 15:58:48 +02:00
mulekick.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": "JSON is not valid",
})
return
}
2017-02-23 06:12:16 +01:00
if _, err := db.Mysql.Exec("update project__environment set name=?, json=? where id=?", env.Name, env.JSON, oldEnv.ID); err != nil {
panic(err)
}
2017-02-22 23:17:36 +01:00
w.WriteHeader(http.StatusNoContent)
}
// AddEnvironment creates an environment in the database
2017-02-22 23:17:36 +01:00
func AddEnvironment(w http.ResponseWriter, r *http.Request) {
2017-02-23 06:12:16 +01:00
project := context.Get(r, "project").(db.Project)
var env db.Environment
2017-04-18 15:58:48 +02:00
if err := mulekick.Bind(w, r, &env); err != nil {
return
}
var js map[string]interface{}
if json.Unmarshal([]byte(env.JSON), &js) != nil {
2017-04-18 15:58:48 +02:00
mulekick.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": "JSON is not valid",
})
return
}
2017-02-23 06:12:16 +01:00
res, err := db.Mysql.Exec("insert into project__environment set project_id=?, name=?, json=?, password=?", project.ID, env.Name, env.JSON, env.Password)
2016-04-17 02:20:23 +02:00
if err != nil {
panic(err)
}
insertID, err := res.LastInsertId()
util.LogWarning(err)
2016-04-17 02:20:23 +02:00
insertIDInt := int(insertID)
objType := "environment"
desc := "Environment " + env.Name + " created"
2017-02-23 06:12:16 +01:00
if err := (db.Event{
2016-04-17 02:20:23 +02:00
ProjectID: &project.ID,
ObjectType: &objType,
ObjectID: &insertIDInt,
Description: &desc,
}.Insert()); err != nil {
panic(err)
}
2017-02-22 23:17:36 +01:00
w.WriteHeader(http.StatusNoContent)
2016-04-04 15:44:34 +02:00
}
// RemoveEnvironment deletes an environment from the database
2017-02-22 23:17:36 +01:00
func RemoveEnvironment(w http.ResponseWriter, r *http.Request) {
2017-02-23 06:12:16 +01:00
env := context.Get(r, "environment").(db.Environment)
2017-02-23 06:12:16 +01:00
templatesC, err := db.Mysql.SelectInt("select count(1) from project__template where project_id=? and environment_id=?", env.ProjectID, env.ID)
if err != nil {
panic(err)
}
if templatesC > 0 {
2017-02-23 00:21:49 +01:00
if len(r.URL.Query().Get("setRemoved")) == 0 {
mulekick.WriteJSON(w, http.StatusBadRequest, map[string]interface{}{
"error": "Environment is in use by one or more templates",
"inUse": true,
})
return
}
2017-02-23 06:12:16 +01:00
if _, err := db.Mysql.Exec("update project__environment set removed=1 where id=?", env.ID); err != nil {
panic(err)
}
2017-02-22 23:17:36 +01:00
w.WriteHeader(http.StatusNoContent)
return
}
2017-02-23 06:12:16 +01:00
if _, err := db.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"
2017-02-23 06:12:16 +01:00
if err := (db.Event{
2016-04-17 02:20:23 +02:00
ProjectID: &env.ProjectID,
Description: &desc,
}.Insert()); err != nil {
panic(err)
}
2017-02-22 23:17:36 +01:00
w.WriteHeader(http.StatusNoContent)
2016-04-02 14:40:07 +02:00
}