91 lines
3.2 KiB
Go
91 lines
3.2 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"vctp/db/queries"
|
|
)
|
|
|
|
// Remove a specified VM from the inventory
|
|
func (h *Handler) VmCleanup(w http.ResponseWriter, r *http.Request) {
|
|
ctx := context.Background()
|
|
|
|
// Get the parameters
|
|
vmId := r.URL.Query().Get("vm_id")
|
|
datacenterName := r.URL.Query().Get("datacenter_name")
|
|
if vmId != "" && datacenterName != "" {
|
|
// check that the VM exists in the inventory
|
|
invParams := queries.GetInventoryVmIdParams{
|
|
VmId: sql.NullString{String: vmId, Valid: vmId != ""},
|
|
DatacenterName: sql.NullString{String: datacenterName, Valid: datacenterName != ""},
|
|
}
|
|
h.Logger.Debug("Checking inventory table for VM record", "params", invParams)
|
|
vm, err := h.Database.Queries().GetInventoryVmId(ctx, invParams)
|
|
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
h.Logger.Error("No VM found matching parameters", "vm_id", vmId, "datacenter_name", datacenterName)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
json.NewEncoder(w).Encode(map[string]string{
|
|
"status": "ERROR",
|
|
"message": fmt.Sprintf("No match to VM details specified. vm_id: '%s', datacenter_name: '%s'", vmId, datacenterName),
|
|
})
|
|
return
|
|
} else {
|
|
h.Logger.Error("Error checking for VM to cleanup", "error", err, "vm_id", vmId, "datacenter_name", datacenterName)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
json.NewEncoder(w).Encode(map[string]string{
|
|
"status": "ERROR",
|
|
"message": fmt.Sprintf("Error checking for VM to cleanup. error: '%s'", err),
|
|
})
|
|
return
|
|
}
|
|
} else {
|
|
// delete the VM
|
|
|
|
// create the database parameters
|
|
params := queries.InventoryCleanupParams{
|
|
VmId: sql.NullString{String: vmId, Valid: vmId != ""},
|
|
DatacenterName: sql.NullString{String: datacenterName, Valid: datacenterName != ""},
|
|
}
|
|
|
|
h.Logger.Debug("database params", "params", params)
|
|
err = h.Database.Queries().InventoryCleanup(ctx, params)
|
|
if err != nil {
|
|
h.Logger.Error("Error cleaning up VM from Inventory table", "error", err, "vm_id", vmId, "datacenter_name", datacenterName)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
json.NewEncoder(w).Encode(map[string]string{
|
|
"status": "ERROR",
|
|
"message": fmt.Sprintf("Error cleaning up VM from Inventory table. error: '%s'", err),
|
|
})
|
|
return
|
|
} else {
|
|
// Successful cleanup
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusOK)
|
|
json.NewEncoder(w).Encode(map[string]string{
|
|
"status": "OK",
|
|
"message": fmt.Sprintf("VM '%s' removed from Inventory table", vm.Name),
|
|
})
|
|
return
|
|
}
|
|
}
|
|
} else {
|
|
h.Logger.Error("Parameters not correctly specified", "vm_id", vmId, "datacenter_name", datacenterName)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
json.NewEncoder(w).Encode(map[string]string{
|
|
"status": "ERROR",
|
|
"message": fmt.Sprintf("Parameters not correctly specified. vm_id: '%s', datacenter_name: '%s'", vmId, datacenterName),
|
|
})
|
|
return
|
|
}
|
|
}
|