Files
vctp2/server/handler/vmCleanup.go
Nathan Coad ceadf42048
All checks were successful
continuous-integration/drone/push Build is passing
update godoc
2026-01-22 12:52:28 +11:00

101 lines
3.8 KiB
Go

package handler
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"net/http"
"vctp/db/queries"
)
// VmCleanup removes a VM from inventory by ID and datacenter.
// @Summary Cleanup VM inventory entry
// @Description Removes a VM inventory entry by VM ID and datacenter name.
// @Tags inventory
// @Produce json
// @Param vm_id query string true "VM ID"
// @Param datacenter_name query string true "Datacenter name"
// @Success 200 {object} models.StatusMessageResponse "Cleanup completed"
// @Failure 400 {object} models.ErrorResponse "Invalid request"
// @Router /api/inventory/vm/delete [delete]
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
h.Logger.Debug("VM successfully removed from inventory", "vm_name", vm.Name, "iid", vm.Iid, "vm_id", vmId, "datacenter_name", datacenterName)
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
}
}