2016-04-02 14:40:07 +02:00
|
|
|
package projects
|
|
|
|
|
2016-04-07 14:49:34 +02:00
|
|
|
import (
|
2024-02-27 12:06:05 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-12-03 14:51:15 +01:00
|
|
|
"github.com/ansible-semaphore/semaphore/api/helpers"
|
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
2020-12-01 20:06:49 +01:00
|
|
|
"net/http"
|
2019-07-09 19:45:27 +02:00
|
|
|
|
2017-02-23 00:21:49 +01:00
|
|
|
"github.com/gorilla/context"
|
2016-04-07 14:49:34 +02:00
|
|
|
)
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// EnvironmentMiddleware ensures an environment exists and loads it to the context
|
2019-07-09 18:14:06 +02:00
|
|
|
func EnvironmentMiddleware(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
project := context.Get(r, "project").(db.Project)
|
2020-12-03 14:51:15 +01:00
|
|
|
envID, err := helpers.GetIntParam("environment_id", w, r)
|
2019-07-09 18:14:06 +02:00
|
|
|
if err != nil {
|
2020-12-03 14:51:15 +01:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2016-04-13 18:09:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
env, err := helpers.Store(r).GetEnvironment(project.ID, envID)
|
2019-07-09 18:14:06 +02:00
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:14:06 +02:00
|
|
|
}
|
2016-04-13 18:09:44 +02:00
|
|
|
|
2019-07-09 18:14:06 +02:00
|
|
|
context.Set(r, "environment", env)
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
2016-04-02 14:40:07 +02:00
|
|
|
}
|
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
func GetEnvironmentRefs(w http.ResponseWriter, r *http.Request) {
|
|
|
|
env := context.Get(r, "environment").(db.Environment)
|
|
|
|
refs, err := helpers.Store(r).GetEnvironmentRefs(env.ProjectID, env.ID)
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helpers.WriteJSON(w, http.StatusOK, refs)
|
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// GetEnvironment retrieves sorted environments from the database
|
2019-07-09 18:11:01 +02:00
|
|
|
func GetEnvironment(w http.ResponseWriter, r *http.Request) {
|
2020-12-03 14:51:15 +01:00
|
|
|
|
|
|
|
// return single environment if request has environment ID
|
2020-11-03 20:32:24 +01:00
|
|
|
if environment := context.Get(r, "environment"); environment != nil {
|
2020-12-04 23:41:26 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusOK, environment.(db.Environment))
|
2020-11-03 20:32:24 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-04 23:41:26 +01:00
|
|
|
project := context.Get(r, "project").(db.Project)
|
2019-07-09 18:11:01 +02:00
|
|
|
|
2021-10-13 16:33:07 +02:00
|
|
|
env, err := helpers.Store(r).GetEnvironments(project.ID, helpers.QueryParams(r.URL))
|
2019-07-09 18:11:01 +02:00
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusOK, env)
|
2016-04-02 14:40:07 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// UpdateEnvironment updates an existing environment in the database
|
2019-07-09 18:11:01 +02:00
|
|
|
func UpdateEnvironment(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
oldEnv := context.Get(r, "environment").(db.Environment)
|
|
|
|
var env db.Environment
|
2020-12-03 14:51:15 +01:00
|
|
|
if !helpers.Bind(w, r, &env) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-07 20:48:52 +01:00
|
|
|
if env.ID != oldEnv.ID {
|
2022-02-03 08:05:13 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
|
|
|
|
"error": "Environment ID in body and URL must be the same",
|
|
|
|
})
|
2020-12-07 20:48:52 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if env.ProjectID != oldEnv.ProjectID {
|
|
|
|
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
|
|
|
|
"error": "Project ID in body and URL must be the same",
|
|
|
|
})
|
2019-07-09 18:11:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
if err := helpers.Store(r).UpdateEnvironment(env); err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2016-04-13 18:09:44 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// AddEnvironment creates an environment in the database
|
2019-07-09 18:11:01 +02:00
|
|
|
func AddEnvironment(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
project := context.Get(r, "project").(db.Project)
|
|
|
|
var env db.Environment
|
2019-07-09 18:11:01 +02:00
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
if !helpers.Bind(w, r, &env) {
|
2019-07-09 18:11:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-04 17:29:37 +01:00
|
|
|
if project.ID != env.ProjectID {
|
|
|
|
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
|
2020-12-07 20:48:52 +01:00
|
|
|
"error": "Project ID in body and URL must be the same",
|
2020-12-04 17:29:37 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
newEnv, err := helpers.Store(r).CreateEnvironment(env)
|
2019-07-09 18:11:01 +02:00
|
|
|
if err != nil {
|
2020-12-04 17:29:37 +01:00
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
2021-08-20 08:28:50 +02:00
|
|
|
user := context.Get(r, "user").(*db.User)
|
|
|
|
|
2021-10-13 16:07:22 +02:00
|
|
|
objType := db.EventEnvironment
|
2019-07-09 18:11:01 +02:00
|
|
|
|
2020-12-04 17:29:37 +01:00
|
|
|
desc := "Environment " + newEnv.Name + " created"
|
2020-12-04 23:41:26 +01:00
|
|
|
_, err = helpers.Store(r).CreateEvent(db.Event{
|
2022-02-03 08:05:13 +01:00
|
|
|
UserID: &user.ID,
|
2020-12-04 17:29:37 +01:00
|
|
|
ProjectID: &newEnv.ID,
|
2019-07-09 18:11:01 +02:00
|
|
|
ObjectType: &objType,
|
2020-12-04 17:29:37 +01:00
|
|
|
ObjectID: &newEnv.ID,
|
2019-07-09 18:11:01 +02:00
|
|
|
Description: &desc,
|
2020-12-01 20:06:49 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2016-04-04 15:44:34 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// RemoveEnvironment deletes an environment from the database
|
2019-07-09 18:11:01 +02:00
|
|
|
func RemoveEnvironment(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
env := context.Get(r, "environment").(db.Environment)
|
2019-07-09 18:11:01 +02:00
|
|
|
|
2020-12-04 17:29:37 +01:00
|
|
|
var err error
|
2020-12-03 14:51:15 +01:00
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
err = helpers.Store(r).DeleteEnvironment(env.ProjectID, env.ID)
|
|
|
|
if err == db.ErrInvalidOperation {
|
|
|
|
helpers.WriteJSON(w, http.StatusBadRequest, map[string]interface{}{
|
|
|
|
"error": "Environment is in use by one or more templates",
|
|
|
|
"inUse": true,
|
|
|
|
})
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 17:29:37 +01:00
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
2021-08-20 08:28:50 +02:00
|
|
|
user := context.Get(r, "user").(*db.User)
|
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
desc := "Environment " + env.Name + " deleted"
|
2020-12-04 23:41:26 +01:00
|
|
|
_, err = helpers.Store(r).CreateEvent(db.Event{
|
2021-08-20 08:28:50 +02:00
|
|
|
UserID: &user.ID,
|
2019-07-09 18:11:01 +02:00
|
|
|
ProjectID: &env.ProjectID,
|
|
|
|
Description: &desc,
|
2020-12-01 20:06:49 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2016-04-02 14:40:07 +02:00
|
|
|
}
|