2016-04-02 14:40:07 +02:00
|
|
|
package projects
|
|
|
|
|
2016-04-04 15:44:34 +02:00
|
|
|
import (
|
2020-12-01 20:06:49 +01:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2020-12-03 14:51:15 +01:00
|
|
|
"github.com/ansible-semaphore/semaphore/api/helpers"
|
2020-12-04 23:41:26 +01:00
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
2017-02-23 00:21:49 +01:00
|
|
|
"net/http"
|
2016-04-13 18:09:44 +02:00
|
|
|
|
2019-07-09 18:14:06 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2017-02-23 00:21:49 +01:00
|
|
|
"github.com/gorilla/context"
|
2016-04-04 15:44:34 +02:00
|
|
|
)
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
const (
|
2019-07-09 18:14:06 +02:00
|
|
|
asc = "asc"
|
2018-03-27 22:12:47 +02:00
|
|
|
desc = "desc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// InventoryMiddleware ensures an inventory exists and loads it to the context
|
2019-07-09 18:14:06 +02:00
|
|
|
func InventoryMiddleware(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
|
|
|
inventoryID, err := helpers.GetIntParam("inventory_id", w, r)
|
2019-07-09 18:14:06 +02:00
|
|
|
if err != nil {
|
2019-07-09 18:11:01 +02:00
|
|
|
return
|
2019-07-09 14:56:03 +02:00
|
|
|
}
|
|
|
|
|
2020-12-07 13:13:59 +01:00
|
|
|
inventory, err := helpers.Store(r).GetInventory(project.ID, inventoryID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
2020-12-08 08:23:33 +01:00
|
|
|
return
|
2019-07-09 18:14:06 +02:00
|
|
|
}
|
2019-07-09 14:56:03 +02:00
|
|
|
|
2019-07-09 18:14:06 +02:00
|
|
|
context.Set(r, "inventory", inventory)
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
2016-04-02 14:40:07 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// GetInventory returns an inventory from the database
|
2019-07-09 18:11:01 +02:00
|
|
|
func GetInventory(w http.ResponseWriter, r *http.Request) {
|
2020-11-03 19:09:09 +01:00
|
|
|
if inventory := context.Get(r, "inventory"); inventory != nil {
|
2020-12-04 23:41:26 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusOK, inventory.(db.Inventory))
|
2020-11-03 19:09:09 +01:00
|
|
|
return
|
|
|
|
}
|
2020-11-22 01:32:49 +01:00
|
|
|
|
2020-12-04 23:41:26 +01:00
|
|
|
project := context.Get(r, "project").(db.Project)
|
2020-11-03 19:09:09 +01:00
|
|
|
|
2020-12-07 13:13:59 +01:00
|
|
|
params := db.RetrieveQueryParams{
|
|
|
|
SortBy: r.URL.Query().Get("sort"),
|
|
|
|
SortInverted: r.URL.Query().Get("order") == desc,
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2017-03-16 15:55:50 +01:00
|
|
|
|
2020-12-07 13:13:59 +01:00
|
|
|
inventories, err := helpers.Store(r).GetInventories(project.ID, params)
|
2017-03-16 15:55:50 +01:00
|
|
|
|
2020-12-07 13:13:59 +01:00
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2016-04-04 15:44:34 +02:00
|
|
|
|
2020-12-07 13:13:59 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusOK, inventories)
|
2016-04-04 15:44:34 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// AddInventory creates an inventory in the database
|
2019-07-09 18:11:01 +02:00
|
|
|
func AddInventory(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
project := context.Get(r, "project").(db.Project)
|
2020-12-07 13:13:59 +01:00
|
|
|
|
|
|
|
var inventory db.Inventory
|
2016-04-04 15:44:34 +02:00
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
if !helpers.Bind(w, r, &inventory) {
|
2019-07-09 18:11:01 +02:00
|
|
|
return
|
|
|
|
}
|
2016-04-04 15:44:34 +02:00
|
|
|
|
2020-12-07 13:13:59 +01:00
|
|
|
if inventory.ProjectID != project.ID {
|
|
|
|
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
|
|
|
|
"error": "Project ID in body and URL must be the same",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
switch inventory.Type {
|
|
|
|
case "static", "file":
|
|
|
|
break
|
|
|
|
default:
|
2020-12-07 13:13:59 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
|
|
|
|
"error": "Not supported inventory type",
|
|
|
|
})
|
2019-07-09 18:11:01 +02:00
|
|
|
return
|
|
|
|
}
|
2016-04-17 02:20:23 +02:00
|
|
|
|
2020-12-07 13:13:59 +01:00
|
|
|
newInventory, err := helpers.Store(r).CreateInventory(inventory)
|
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
if err != nil {
|
2020-12-07 13:13:59 +01:00
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2016-04-04 15:44:34 +02:00
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
objType := "inventory"
|
|
|
|
desc := "Inventory " + inventory.Name + " created"
|
2020-12-04 23:41:26 +01:00
|
|
|
_, err = helpers.Store(r).CreateEvent(db.Event{
|
2019-07-09 18:11:01 +02:00
|
|
|
ProjectID: &project.ID,
|
|
|
|
ObjectType: &objType,
|
2020-12-07 13:13:59 +01:00
|
|
|
ObjectID: &newInventory.ID,
|
2019-07-09 18:11:01 +02:00
|
|
|
Description: &desc,
|
2020-12-01 20:06:49 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2020-12-07 13:13:59 +01:00
|
|
|
// Write error to log but return ok to user, because inventory created
|
2020-12-01 20:06:49 +01:00
|
|
|
log.Error(err)
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2019-07-09 14:56:03 +02:00
|
|
|
|
2020-12-07 13:13:59 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusCreated, newInventory)
|
2016-04-04 15:44:34 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// IsValidInventoryPath tests a path to ensure it is below the cwd
|
2018-03-08 12:37:38 +01:00
|
|
|
func IsValidInventoryPath(path string) bool {
|
2018-03-27 22:12:47 +02:00
|
|
|
|
|
|
|
currentPath, err := os.Getwd()
|
|
|
|
if err != nil {
|
2018-03-08 10:04:34 +01:00
|
|
|
return false
|
2018-03-27 22:12:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
absPath, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
2018-03-08 10:04:34 +01:00
|
|
|
return false
|
2018-03-27 22:12:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
relPath, err := filepath.Rel(currentPath, absPath)
|
|
|
|
if err != nil {
|
2018-03-08 10:04:34 +01:00
|
|
|
return false
|
|
|
|
}
|
2018-03-27 22:12:47 +02:00
|
|
|
|
|
|
|
return !strings.HasPrefix(relPath, "..")
|
2018-03-08 10:04:34 +01:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// UpdateInventory writes updated values to an existing inventory item in the database
|
2019-07-09 18:11:01 +02:00
|
|
|
func UpdateInventory(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
oldInventory := context.Get(r, "inventory").(db.Inventory)
|
2019-07-09 18:11:01 +02:00
|
|
|
|
2020-12-07 13:13:59 +01:00
|
|
|
var inventory db.Inventory
|
2016-04-16 21:42:57 +02:00
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
if !helpers.Bind(w, r, &inventory) {
|
2019-07-09 18:11:01 +02:00
|
|
|
return
|
|
|
|
}
|
2016-04-16 21:42:57 +02:00
|
|
|
|
2020-12-07 13:13:59 +01:00
|
|
|
if inventory.ID != oldInventory.ID {
|
|
|
|
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
|
|
|
|
"error": "Inventory ID in body and URL must be the same",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if inventory.ProjectID != oldInventory.ProjectID {
|
|
|
|
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
|
|
|
|
"error": "Project ID in body and URL must be the same",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
switch inventory.Type {
|
|
|
|
case "static":
|
|
|
|
break
|
|
|
|
case "file":
|
|
|
|
if !IsValidInventoryPath(inventory.Inventory) {
|
2020-11-22 01:32:49 +01:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
2019-07-09 14:56:03 +02:00
|
|
|
}
|
2019-07-09 18:11:01 +02:00
|
|
|
default:
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2016-04-16 21:42:57 +02:00
|
|
|
|
2020-12-07 13:13:59 +01:00
|
|
|
err := helpers.Store(r).UpdateInventory(inventory)
|
2020-12-01 20:06:49 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-12-07 13:13:59 +01:00
|
|
|
helpers.WriteError(w, err)
|
2020-12-01 20:06:49 +01:00
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2019-07-09 14:56:03 +02:00
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2016-04-02 14:40:07 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// RemoveInventory deletes an inventory from the database
|
2019-07-09 18:11:01 +02:00
|
|
|
func RemoveInventory(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
inventory := context.Get(r, "inventory").(db.Inventory)
|
2020-12-07 19:50:41 +01:00
|
|
|
var err error
|
2016-04-13 18:09:44 +02:00
|
|
|
|
2020-12-07 19:50:41 +01:00
|
|
|
softDeletion := len(r.URL.Query().Get("setRemoved")) == 0
|
2016-06-17 22:16:46 +02:00
|
|
|
|
2020-12-07 19:50:41 +01:00
|
|
|
if softDeletion {
|
|
|
|
err = helpers.Store(r).DeleteInventorySoft(inventory.ProjectID, inventory.ID)
|
|
|
|
} else {
|
|
|
|
err = helpers.Store(r).DeleteInventory(inventory.ProjectID, inventory.ID)
|
|
|
|
if err == db.ErrInvalidOperation {
|
2020-12-03 14:51:15 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusBadRequest, map[string]interface{}{
|
2019-07-09 18:11:01 +02:00
|
|
|
"error": "Inventory is in use by one or more templates",
|
|
|
|
"inUse": true,
|
|
|
|
})
|
2016-06-17 22:16:46 +02:00
|
|
|
return
|
|
|
|
}
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
2020-12-07 19:50:41 +01:00
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
desc := "Inventory " + inventory.Name + " deleted"
|
2020-12-01 20:06:49 +01:00
|
|
|
|
2020-12-04 23:41:26 +01:00
|
|
|
_, err = helpers.Store(r).CreateEvent(db.Event{
|
2019-07-09 18:11:01 +02:00
|
|
|
ProjectID: &inventory.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
|
|
|
}
|
2016-04-17 02:20:23 +02:00
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2016-04-02 14:40:07 +02:00
|
|
|
}
|