diff --git a/db/queries/query.sql b/db/queries/query.sql index a86bb08..192ada3 100644 --- a/db/queries/query.sql +++ b/db/queries/query.sql @@ -35,6 +35,11 @@ INSERT INTO "Updates" ( ) RETURNING *; +-- name: CleanupUpdates :exec +DELETE FROM "Updates" +WHERE "UpdateType" = sqlc.arg('updateType') AND "UpdateTime" <= sqlc.arg('updateTime') +RETURNING *; + -- name: CreateEvent :one INSERT INTO "Events" ( "CloudId", "Source", "EventTime", "ChainId", "VmId", "VmName", "EventKey", "DatacenterId", "DatacenterName", "ComputeResourceId", "ComputeResourceName", "UserName" diff --git a/db/queries/query.sql.go b/db/queries/query.sql.go index f874752..724540c 100644 --- a/db/queries/query.sql.go +++ b/db/queries/query.sql.go @@ -10,6 +10,22 @@ import ( "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 INSERT INTO "Events" ( "CloudId", "Source", "EventTime", "ChainId", "VmId", "VmName", "EventKey", "DatacenterId", "DatacenterName", "ComputeResourceId", "ComputeResourceName", "UserName" diff --git a/server/handler/updateCleanup.go b/server/handler/updateCleanup.go new file mode 100644 index 0000000..d6baa6b --- /dev/null +++ b/server/handler/updateCleanup.go @@ -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") + } +} diff --git a/server/models/models.go b/server/models/models.go index 80bb956..4eb6507 100644 --- a/server/models/models.go +++ b/server/models/models.go @@ -85,19 +85,19 @@ type ConfigChangesReceived struct { // This probably needs more fields added so not in use yet type ConfigSpec struct { - AlternateGuestName string `json:"AlternateGuestName"` - Annotation string `json:"Annotation"` - BootOptions any `json:"BootOptions"` - ChangeTrackingEnabled any `json:"ChangeTrackingEnabled"` - ChangeVersion string `json:"ChangeVersion"` - ConsolePreferences any `json:"ConsolePreferences"` - CPUAffinity any `json:"CpuAffinity"` - CPUAllocation any `json:"CpuAllocation"` - CPUFeatureMask any `json:"CpuFeatureMask"` - CPUHotAddEnabled any `json:"CpuHotAddEnabled"` - CPUHotRemoveEnabled any `json:"CpuHotRemoveEnabled"` - CreateDate time.Time `json:"CreateDate"` - Crypto any `json:"Crypto"` + AlternateGuestName string `json:"AlternateGuestName"` + Annotation string `json:"Annotation"` + BootOptions any `json:"BootOptions"` + ChangeTrackingEnabled any `json:"ChangeTrackingEnabled"` + ChangeVersion string `json:"ChangeVersion"` + ConsolePreferences any `json:"ConsolePreferences"` + CPUAffinity any `json:"CpuAffinity"` + CPUAllocation any `json:"CpuAllocation"` + CPUFeatureMask any `json:"CpuFeatureMask"` + CPUHotAddEnabled any `json:"CpuHotAddEnabled"` + CPUHotRemoveEnabled any `json:"CpuHotRemoveEnabled"` + CreateDate string `json:"CreateDate"` // Modified from time.Time + Crypto any `json:"Crypto"` DeviceChange []struct { Backing any `json:"Backing"` Device struct { @@ -151,7 +151,7 @@ type ConfigSpec struct { Profile []struct { ProfileData struct { ExtensionKey string `json:"ExtensionKey"` - ObjectData time.Time `json:"ObjectData"` + ObjectData time.Time `json:"ObjectData"` // Modified from time.Time } `json:"ProfileData"` ProfileID string `json:"ProfileId"` ProfileParams any `json:"ProfileParams"` diff --git a/server/router/router.go b/server/router/router.go index 0d1debd..43ea7ce 100644 --- a/server/router/router.go +++ b/server/router/router.go @@ -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/delete", h.VmDelete) mux.HandleFunc("/api/import/vm", h.VmImport) + // temporary endpoint + mux.HandleFunc("/api/cleanup/updates", h.UpdateCleanup) return middleware.NewLoggingMiddleware(logger, mux) }