add vm cleanup endpoint
Some checks are pending
CI / Test (push) Waiting to run
CI / End-to-End (push) Waiting to run
CI / Publish Docker (push) Blocked by required conditions
CI / Lint (push) Waiting to run
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-09-26 13:29:02 +10:00
parent f712c7254f
commit 00d474b937
5 changed files with 114 additions and 1 deletions

View File

@@ -27,6 +27,11 @@ UPDATE "Inventory"
SET "DeletionTime" = sqlc.arg('deletionTime') SET "DeletionTime" = sqlc.arg('deletionTime')
WHERE "VmId" = sqlc.arg('vmId') AND "Datacenter" = sqlc.arg('datacenterName'); WHERE "VmId" = sqlc.arg('vmId') AND "Datacenter" = sqlc.arg('datacenterName');
-- name: InventoryCleanup :exec
DELETE FROM "Inventory"
WHERE "VmId" = sqlc.arg('vmId') AND "Datacenter" = sqlc.arg('datacenterName')
RETURNING *;
-- name: CreateUpdate :one -- name: CreateUpdate :one
INSERT INTO "Updates" ( INSERT INTO "Updates" (
"InventoryId", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool", "NewProvisionedDisk", "UserName" "InventoryId", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool", "NewProvisionedDisk", "UserName"

View File

@@ -329,6 +329,22 @@ func (q *Queries) GetInventoryVmId(ctx context.Context, arg GetInventoryVmIdPara
return i, err return i, err
} }
const inventoryCleanup = `-- name: InventoryCleanup :exec
DELETE FROM "Inventory"
WHERE "VmId" = ?1 AND "Datacenter" = ?2
RETURNING Iid, Name, Vcenter, VmId, EventKey, CloudId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, SrmPlaceholder, IsTemplate, PowerState
`
type InventoryCleanupParams struct {
VmId sql.NullString
DatacenterName sql.NullString
}
func (q *Queries) InventoryCleanup(ctx context.Context, arg InventoryCleanupParams) error {
_, err := q.db.ExecContext(ctx, inventoryCleanup, arg.VmId, arg.DatacenterName)
return err
}
const inventoryMarkDeleted = `-- name: InventoryMarkDeleted :exec const inventoryMarkDeleted = `-- name: InventoryMarkDeleted :exec
UPDATE "Inventory" UPDATE "Inventory"
SET "DeletionTime" = ?1 SET "DeletionTime" = ?1

View File

@@ -0,0 +1,90 @@
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
}
}

View File

@@ -39,7 +39,7 @@ func (h *Handler) VmImport(w http.ResponseWriter, r *http.Request) {
ctx := context.Background() ctx := context.Background()
// TODO - query Inventory table for this VM before adding it // Query Inventory table for this VM before adding it
h.Logger.Debug("Checking inventory table for VM record") h.Logger.Debug("Checking inventory table for VM record")
invParams := queries.GetInventoryVmIdParams{ invParams := queries.GetInventoryVmIdParams{
VmId: sql.NullString{String: inData.VmId, Valid: inData.VmId != ""}, VmId: sql.NullString{String: inData.VmId, Valid: inData.VmId != ""},

View File

@@ -26,6 +26,8 @@ func New(logger *slog.Logger, database db.Database, buildTime string, sha1ver st
mux.HandleFunc("/api/event/vm/modify", h.VmModify) mux.HandleFunc("/api/event/vm/modify", h.VmModify)
mux.HandleFunc("/api/event/vm/delete", h.VmDelete) mux.HandleFunc("/api/event/vm/delete", h.VmDelete)
mux.HandleFunc("/api/import/vm", h.VmImport) mux.HandleFunc("/api/import/vm", h.VmImport)
// Use this when we need to manually remove a VM from the database to clean up
mux.HandleFunc("/api/inventory/vm/delete", h.VmCleanup)
// temporary endpoint // temporary endpoint
//mux.HandleFunc("/api/cleanup/updates", h.UpdateCleanup) //mux.HandleFunc("/api/cleanup/updates", h.UpdateCleanup)