mirror of
https://github.com/semaphoreui/semaphore.git
synced 2025-01-20 15:29:28 +01:00
feat(inv): implement attach/detach to tpl
This commit is contained in:
parent
0a5e1c269c
commit
f75bbc2c5a
@ -230,13 +230,68 @@ func RemoveTemplate(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func SetTemplateInventory(w http.ResponseWriter, r *http.Request) {
|
func SetTemplateInventory(w http.ResponseWriter, r *http.Request) {
|
||||||
|
tpl := context.Get(r, "template").(db.Template)
|
||||||
|
inv := context.Get(r, "inventory").(db.Inventory)
|
||||||
|
|
||||||
|
if !tpl.App.HasInventoryType(inv.Type) {
|
||||||
|
helpers.WriteErrorStatus(w, "Inventory type is not supported for this template", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if tpl.App.IsTerraform() && (inv.TemplateID == nil || *inv.TemplateID != tpl.ID) {
|
||||||
|
helpers.WriteErrorStatus(w, "Inventory is not attached to this template", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
tpl.InventoryID = &inv.ID
|
||||||
|
err := helpers.Store(r).UpdateTemplate(tpl)
|
||||||
|
if err != nil {
|
||||||
|
helpers.WriteError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
}
|
}
|
||||||
|
|
||||||
func AttachInventory(w http.ResponseWriter, r *http.Request) {
|
func AttachInventory(w http.ResponseWriter, r *http.Request) {
|
||||||
|
tpl := context.Get(r, "template").(db.Template)
|
||||||
|
inv := context.Get(r, "inventory").(db.Inventory)
|
||||||
|
|
||||||
|
if inv.TemplateID != nil {
|
||||||
|
helpers.WriteErrorStatus(w, "Inventory is already attached to another template", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !tpl.App.HasInventoryType(inv.Type) {
|
||||||
|
helpers.WriteErrorStatus(w, "Inventory type is not supported for this template", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
inv.TemplateID = &tpl.ID
|
||||||
|
err := helpers.Store(r).UpdateInventory(inv)
|
||||||
|
if err != nil {
|
||||||
|
helpers.WriteError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
}
|
}
|
||||||
|
|
||||||
func DetachInventory(w http.ResponseWriter, r *http.Request) {
|
func DetachInventory(w http.ResponseWriter, r *http.Request) {
|
||||||
|
tpl := context.Get(r, "template").(db.Template)
|
||||||
|
inv := context.Get(r, "inventory").(db.Inventory)
|
||||||
|
|
||||||
|
if inv.TemplateID == nil || *inv.TemplateID != tpl.ID {
|
||||||
|
helpers.WriteErrorStatus(w, "Inventory is not attached to this template", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
inv.TemplateID = nil
|
||||||
|
err := helpers.Store(r).UpdateInventory(inv)
|
||||||
|
if err != nil {
|
||||||
|
helpers.WriteError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
}
|
}
|
||||||
|
@ -320,9 +320,12 @@ func Route() *mux.Router {
|
|||||||
projectTmplManagement.HandleFunc("/{template_id}/tasks/last", projects.GetLastTasks).Methods("GET")
|
projectTmplManagement.HandleFunc("/{template_id}/tasks/last", projects.GetLastTasks).Methods("GET")
|
||||||
projectTmplManagement.HandleFunc("/{template_id}/schedules", projects.GetTemplateSchedules).Methods("GET")
|
projectTmplManagement.HandleFunc("/{template_id}/schedules", projects.GetTemplateSchedules).Methods("GET")
|
||||||
projectTmplManagement.HandleFunc("/{template_id}/stats", projects.GetTaskStats).Methods("GET")
|
projectTmplManagement.HandleFunc("/{template_id}/stats", projects.GetTaskStats).Methods("GET")
|
||||||
projectTmplManagement.HandleFunc("/{template_id}/inventory", projects.AttachInventory).Methods("POST")
|
|
||||||
projectTmplManagement.HandleFunc("/{template_id}/inventory/{inventory_id}", projects.DetachInventory).Methods("DELETE")
|
projectTmplInvManagement := projectTmplManagement.PathPrefix("/{template_id}/inventory").Subrouter()
|
||||||
projectTmplManagement.HandleFunc("/{template_id}/default_inventory", projects.SetTemplateInventory).Methods("PUT")
|
projectTmplInvManagement.Use(projects.InventoryMiddleware)
|
||||||
|
projectTmplInvManagement.HandleFunc("/{inventory_id}/set_default", projects.SetTemplateInventory).Methods("POST")
|
||||||
|
projectTmplInvManagement.HandleFunc("/{inventory_id}/attach", projects.AttachInventory).Methods("POST")
|
||||||
|
projectTmplInvManagement.HandleFunc("/{inventory_id}/detach", projects.DetachInventory).Methods("POST")
|
||||||
|
|
||||||
projectTaskManagement := projectUserAPI.PathPrefix("/tasks").Subrouter()
|
projectTaskManagement := projectUserAPI.PathPrefix("/tasks").Subrouter()
|
||||||
projectTaskManagement.Use(projects.GetTaskMiddleware)
|
projectTaskManagement.Use(projects.GetTaskMiddleware)
|
||||||
|
@ -37,6 +37,19 @@ func (t TemplateApp) InventoryTypes() ([]InventoryType, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t TemplateApp) HasInventoryType(inventoryType InventoryType) bool {
|
||||||
|
types, err := t.InventoryTypes()
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, typ := range types {
|
||||||
|
if typ == inventoryType {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func (t TemplateApp) IsTerraform() bool {
|
func (t TemplateApp) IsTerraform() bool {
|
||||||
return t == AppTerraform || t == AppTofu
|
return t == AppTerraform || t == AppTofu
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user