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

This commit is contained in:
2024-09-26 12:15:39 +10:00
parent 3b53455343
commit dd13fd6759
5 changed files with 77 additions and 14 deletions

View File

@@ -35,6 +35,11 @@ INSERT INTO "Updates" (
) )
RETURNING *; RETURNING *;
-- name: CleanupUpdates :exec
DELETE FROM "Updates"
WHERE "UpdateType" = sqlc.arg('updateType') AND "UpdateTime" <= sqlc.arg('updateTime')
RETURNING *;
-- name: CreateEvent :one -- name: CreateEvent :one
INSERT INTO "Events" ( INSERT INTO "Events" (
"CloudId", "Source", "EventTime", "ChainId", "VmId", "VmName", "EventKey", "DatacenterId", "DatacenterName", "ComputeResourceId", "ComputeResourceName", "UserName" "CloudId", "Source", "EventTime", "ChainId", "VmId", "VmName", "EventKey", "DatacenterId", "DatacenterName", "ComputeResourceId", "ComputeResourceName", "UserName"

View File

@@ -10,6 +10,22 @@ import (
"database/sql" "database/sql"
) )
const cleanupUpdates = `-- name: CleanupUpdates :exec
DELETE FROM "Updates"
WHERE "UpdateType" = ?1 AND "UpdateTime" <= ?2
RETURNING Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk
`
type CleanupUpdatesParams struct {
UpdateType string
UpdateTime sql.NullInt64
}
func (q *Queries) CleanupUpdates(ctx context.Context, arg CleanupUpdatesParams) error {
_, err := q.db.ExecContext(ctx, cleanupUpdates, arg.UpdateType, arg.UpdateTime)
return err
}
const createEvent = `-- name: CreateEvent :one const createEvent = `-- name: CreateEvent :one
INSERT INTO "Events" ( INSERT INTO "Events" (
"CloudId", "Source", "EventTime", "ChainId", "VmId", "VmName", "EventKey", "DatacenterId", "DatacenterName", "ComputeResourceId", "ComputeResourceName", "UserName" "CloudId", "Source", "EventTime", "ChainId", "VmId", "VmName", "EventKey", "DatacenterId", "DatacenterName", "ComputeResourceId", "ComputeResourceName", "UserName"

View File

@@ -0,0 +1,40 @@
package handler
import (
"context"
"database/sql"
"fmt"
"net/http"
"time"
"vctp/db/queries"
)
// VmUpdate receives the CloudEvent for a VM modification or move
func (h *Handler) UpdateCleanup(w http.ResponseWriter, r *http.Request) {
// Get the current time
now := time.Now()
// Get the start of the current day (midnight today)
midnightToday := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
// Convert to Unix time
unixTime := midnightToday.Unix()
// create the database parameters
params := queries.CleanupUpdatesParams{
UpdateType: "diskchange",
UpdateTime: sql.NullInt64{Int64: unixTime, Valid: unixTime > 0},
}
h.Logger.Debug("database params", "params", params)
err := h.Database.Queries().CleanupUpdates(context.Background(), params)
if err != nil {
h.Logger.Error("Error received cleaning updates table", "error", err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Delete Request unsuccessful %s\n", err)
} else {
h.Logger.Debug("Processed update cleanup successfully")
w.WriteHeader(http.StatusOK)
// TODO - return some JSON
fmt.Fprintf(w, "Processed update cleanup successfully")
}
}

View File

@@ -85,19 +85,19 @@ type ConfigChangesReceived struct {
// This probably needs more fields added so not in use yet // This probably needs more fields added so not in use yet
type ConfigSpec struct { type ConfigSpec struct {
AlternateGuestName string `json:"AlternateGuestName"` AlternateGuestName string `json:"AlternateGuestName"`
Annotation string `json:"Annotation"` Annotation string `json:"Annotation"`
BootOptions any `json:"BootOptions"` BootOptions any `json:"BootOptions"`
ChangeTrackingEnabled any `json:"ChangeTrackingEnabled"` ChangeTrackingEnabled any `json:"ChangeTrackingEnabled"`
ChangeVersion string `json:"ChangeVersion"` ChangeVersion string `json:"ChangeVersion"`
ConsolePreferences any `json:"ConsolePreferences"` ConsolePreferences any `json:"ConsolePreferences"`
CPUAffinity any `json:"CpuAffinity"` CPUAffinity any `json:"CpuAffinity"`
CPUAllocation any `json:"CpuAllocation"` CPUAllocation any `json:"CpuAllocation"`
CPUFeatureMask any `json:"CpuFeatureMask"` CPUFeatureMask any `json:"CpuFeatureMask"`
CPUHotAddEnabled any `json:"CpuHotAddEnabled"` CPUHotAddEnabled any `json:"CpuHotAddEnabled"`
CPUHotRemoveEnabled any `json:"CpuHotRemoveEnabled"` CPUHotRemoveEnabled any `json:"CpuHotRemoveEnabled"`
CreateDate time.Time `json:"CreateDate"` CreateDate string `json:"CreateDate"` // Modified from time.Time
Crypto any `json:"Crypto"` Crypto any `json:"Crypto"`
DeviceChange []struct { DeviceChange []struct {
Backing any `json:"Backing"` Backing any `json:"Backing"`
Device struct { Device struct {
@@ -151,7 +151,7 @@ type ConfigSpec struct {
Profile []struct { Profile []struct {
ProfileData struct { ProfileData struct {
ExtensionKey string `json:"ExtensionKey"` ExtensionKey string `json:"ExtensionKey"`
ObjectData time.Time `json:"ObjectData"` ObjectData time.Time `json:"ObjectData"` // Modified from time.Time
} `json:"ProfileData"` } `json:"ProfileData"`
ProfileID string `json:"ProfileId"` ProfileID string `json:"ProfileId"`
ProfileParams any `json:"ProfileParams"` ProfileParams any `json:"ProfileParams"`

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)
// temporary endpoint
mux.HandleFunc("/api/cleanup/updates", h.UpdateCleanup)
return middleware.NewLoggingMiddleware(logger, mux) return middleware.NewLoggingMiddleware(logger, mux)
} }