adds some code

This commit is contained in:
2025-11-23 13:37:10 +07:00
parent 66591c1b71
commit b506a64ed1
6 changed files with 127 additions and 2 deletions

View File

@@ -140,6 +140,29 @@ func handleDeleteVM(cfg config.Config, svc Services) http.HandlerFunc {
func lifecycleVM(cfg config.Config, svc Services, action string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
// Ensure VM exists either in store or runtime
spec, specErr := svc.Store.LoadVM(id)
runtimeExists := false
if specErr != nil {
if vms, err := svc.Libvirt.ListVMs(); err == nil {
for _, vm := range vms {
if vm.ID == id || vm.Name == id {
runtimeExists = true
break
}
}
}
}
if specErr != nil && !runtimeExists {
writeJSON(w, http.StatusNotFound, map[string]string{"error": "vm not found"})
return
}
if specErr == nil {
if err := validators.CheckStoragePoolsVM(spec.Disks, cfg); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
return
}
}
taskID := enqueueWork(svc.Tasks, "vm."+action, func(ctx context.Context) (interface{}, error) {
unlock := svc.StoreLock(id)
defer unlock()
@@ -259,6 +282,28 @@ func handleDeleteCT(cfg config.Config, svc Services) http.HandlerFunc {
func lifecycleCT(cfg config.Config, svc Services, action string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
spec, specErr := svc.Store.LoadCT(id)
runtimeExists := false
if specErr != nil {
if cts, err := svc.LXC.List(); err == nil {
for _, ct := range cts {
if ct.ID == id || ct.Name == id {
runtimeExists = true
break
}
}
}
}
if specErr != nil && !runtimeExists {
writeJSON(w, http.StatusNotFound, map[string]string{"error": "ct not found"})
return
}
if specErr == nil {
if err := validators.CheckStoragePoolsCT(spec, cfg); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
return
}
}
taskID := enqueueWork(svc.Tasks, "ct."+action, func(ctx context.Context) (interface{}, error) {
unlock := svc.StoreLock(id)
defer unlock()