2016-04-02 14:40:07 +02:00
|
|
|
package projects
|
|
|
|
|
2016-04-07 14:49:34 +02:00
|
|
|
import (
|
2016-04-13 18:09:44 +02:00
|
|
|
"database/sql"
|
2017-02-23 00:21:49 +01:00
|
|
|
"net/http"
|
2016-04-13 18:09:44 +02:00
|
|
|
|
2017-02-23 06:12:16 +01:00
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
2016-04-13 18:09:44 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/util"
|
2017-02-23 00:21:49 +01:00
|
|
|
"github.com/castawaylabs/mulekick"
|
|
|
|
"github.com/gorilla/context"
|
2016-04-07 14:49:34 +02:00
|
|
|
"github.com/masterminds/squirrel"
|
|
|
|
)
|
2016-04-02 14:40:07 +02:00
|
|
|
|
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)
|
2016-04-13 18:09:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
query, args, _ := squirrel.Select("*").
|
|
|
|
From("project__environment").
|
|
|
|
Where("project_id=?", project.ID).
|
|
|
|
Where("id=?", envID).
|
|
|
|
ToSql()
|
|
|
|
|
2017-02-23 06:12:16 +01:00
|
|
|
var env db.Environment
|
|
|
|
if err := db.Mysql.SelectOne(&env, query, args...); err != nil {
|
2016-04-13 18:09:44 +02:00
|
|
|
if err == sql.ErrNoRows {
|
2017-02-22 23:17:36 +01:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2016-04-13 18:09:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-02-23 00:21:49 +01:00
|
|
|
context.Set(r, "environment", env)
|
2016-04-02 14:40:07 +02:00
|
|
|
}
|
|
|
|
|
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
|
2016-04-07 14:49:34 +02:00
|
|
|
|
|
|
|
q := squirrel.Select("*").
|
|
|
|
From("project__environment").
|
2017-03-14 16:04:48 +01:00
|
|
|
Where("project_id=?", project.ID).
|
|
|
|
OrderBy("name asc")
|
2016-04-07 14:49:34 +02:00
|
|
|
|
|
|
|
query, args, _ := q.ToSql()
|
|
|
|
|
2017-02-23 06:12:16 +01:00
|
|
|
if _, err := db.Mysql.Select(&env, query, args...); err != nil {
|
2016-04-07 14:49:34 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-02-23 00:21:49 +01:00
|
|
|
mulekick.WriteJSON(w, http.StatusOK, env)
|
2016-04-02 14:40:07 +02:00
|
|
|
}
|
|
|
|
|
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
|
2017-02-22 23:21:52 +01:00
|
|
|
if err := mulekick.Bind(w, r, &env); err != nil {
|
2016-04-13 18:09:44 +02:00
|
|
|
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 {
|
2016-04-13 18:09:44 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-02-22 23:17:36 +01:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2016-04-13 18:09:44 +02:00
|
|
|
}
|
|
|
|
|
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
|
2016-04-13 18:09:44 +02:00
|
|
|
|
2017-02-22 23:21:52 +01:00
|
|
|
if err := mulekick.Bind(w, r, &env); err != nil {
|
2016-04-13 18:09:44 +02:00
|
|
|
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, _ := res.LastInsertId()
|
|
|
|
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 {
|
2016-04-13 18:09:44 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-02-22 23:17:36 +01:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2016-04-04 15:44:34 +02:00
|
|
|
}
|
|
|
|
|
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)
|
2016-04-13 18:09:44 +02:00
|
|
|
|
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)
|
2016-06-17 22:16:46 +02:00
|
|
|
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{}{
|
2016-06-17 22:16:46 +02:00
|
|
|
"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 {
|
2016-06-17 22:16:46 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-02-22 23:17:36 +01:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2016-06-17 22:16:46 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-23 06:12:16 +01:00
|
|
|
if _, err := db.Mysql.Exec("delete from project__environment where id=?", env.ID); err != nil {
|
2016-04-13 18:09:44 +02:00
|
|
|
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
|
|
|
}
|