modify some file
This commit is contained in:
@@ -84,6 +84,10 @@ func handleCreateVM(cfg config.Config, svc Services) http.HandlerFunc {
|
|||||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if err := validators.CheckStoragePoolsVM(spec.Disks, cfg); err != nil {
|
||||||
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
if err := svc.Store.SaveVM(spec); err != nil {
|
if err := svc.Store.SaveVM(spec); err != nil {
|
||||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||||
return
|
return
|
||||||
@@ -203,6 +207,10 @@ func handleCreateCT(cfg config.Config, svc Services) http.HandlerFunc {
|
|||||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if err := validators.CheckStoragePoolsCT(spec, cfg); err != nil {
|
||||||
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
if err := svc.Store.SaveCT(spec); err != nil {
|
if err := svc.Store.SaveCT(spec); err != nil {
|
||||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||||
return
|
return
|
||||||
@@ -410,3 +418,14 @@ func authMiddleware(token string) func(http.Handler) http.Handler {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func validateVM(spec libvirt.VMSpec, cfg config.Config) error {
|
||||||
|
if spec.CPU <= 0 {
|
||||||
|
return errors.New("cpu must be > 0")
|
||||||
|
}
|
||||||
|
if spec.MemoryMB <= 0 {
|
||||||
|
return errors.New("memory_mb must be > 0")
|
||||||
|
}
|
||||||
|
// storage pools validated elsewhere
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,6 +3,10 @@ package validators
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
|
||||||
|
"jagacloud/node-agent/pkg/compute/libvirt"
|
||||||
|
"jagacloud/node-agent/pkg/config"
|
||||||
|
"jagacloud/node-agent/pkg/containers/lxc"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CheckBridge returns nil if the bridge exists on the host.
|
// CheckBridge returns nil if the bridge exists on the host.
|
||||||
@@ -44,3 +48,33 @@ func CheckStoragePool(name string) error {
|
|||||||
// TODO: query configured pools
|
// TODO: query configured pools
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CheckStoragePoolsVM ensures all disks reference known pools from config.
|
||||||
|
func CheckStoragePoolsVM(disks []libvirt.DiskSpec, cfg config.Config) error {
|
||||||
|
for _, d := range disks {
|
||||||
|
if d.Pool == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !poolExists(cfg, d.Pool) {
|
||||||
|
return fmt.Errorf("storage pool %s not configured", d.Pool)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CheckStoragePoolsCT ensures rootfs pool exists.
|
||||||
|
func CheckStoragePoolsCT(spec lxc.Spec, cfg config.Config) error {
|
||||||
|
if spec.RootfsPool != "" && !poolExists(cfg, spec.RootfsPool) {
|
||||||
|
return fmt.Errorf("storage pool %s not configured", spec.RootfsPool)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func poolExists(cfg config.Config, name string) bool {
|
||||||
|
for _, p := range cfg.StoragePools {
|
||||||
|
if p.Name == name {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user