Files
vctp2/server/handler/vcCleanup.go
Nathan Coad 9a561f3b07
All checks were successful
continuous-integration/drone/push Build is passing
cleanups and code fixes incl templ
2026-03-20 13:21:15 +11:00

69 lines
2.3 KiB
Go

package handler
import (
"database/sql"
"errors"
"fmt"
"net/http"
)
// VcCleanup removes inventory entries for a vCenter instance.
// @Summary Cleanup vCenter inventory (deprecated)
// @Description Deprecated: Removes all inventory entries associated with a vCenter URL.
// @Tags maintenance
// @Deprecated
// @Produce json
// @Param vc_url query string true "vCenter URL"
// @Success 200 {object} models.StatusMessageResponse "Cleanup completed"
// @Failure 400 {object} models.ErrorResponse "Invalid request"
// @Router /api/cleanup/vcenter [delete]
func (h *Handler) VcCleanup(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodDelete {
writeJSONError(w, http.StatusMethodNotAllowed, "method not allowed")
return
}
if h.denyLegacyAPI(w, "/api/cleanup/vcenter") {
return
}
ctx, cancel := withRequestTimeout(r, defaultRequestTimeout)
defer cancel()
// Get the parameters
vcUrl := r.URL.Query().Get("vc_url")
if vcUrl != "" {
h.Logger.Debug("Checking inventory table for vCenter", "url", vcUrl)
_, err := h.Database.Queries().GetInventoryVcUrl(ctx, vcUrl)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
h.Logger.Error("No VMs found for vcenter", "url", vcUrl)
writeJSONError(w, http.StatusBadRequest, fmt.Sprintf("No match to vcenter details specified. vc_url: '%s'", vcUrl))
return
} else {
h.Logger.Error("Error checking for vcenter to cleanup", "error", err, "url", vcUrl)
writeJSONError(w, http.StatusBadRequest, fmt.Sprintf("Error checking for vcenter to cleanup. error: '%s'", err))
return
}
} else {
// delete the VMs
err = h.Database.Queries().InventoryCleanupVcenter(ctx, vcUrl)
if err != nil {
h.Logger.Error("Error cleaning up VMs from Inventory table", "error", err, "url", vcUrl)
writeJSONError(w, http.StatusBadRequest, fmt.Sprintf("Error cleaning up VMs from Inventory table. error: '%s'", err))
return
} else {
// Successful cleanup
h.Logger.Debug("VMs successfully removed from inventory for vcenter", "url", vcUrl)
writeJSONOKMessage(w, fmt.Sprintf("Removed VMs from Inventory table for vcenter '%s'", vcUrl))
return
}
}
} else {
h.Logger.Error("Parameters not correctly specified", "url", vcUrl)
writeJSONError(w, http.StatusBadRequest, fmt.Sprintf("Parameters not correctly specified. vc_url: '%s'", vcUrl))
return
}
}