diff --git a/db/migrations/20240926022714_updates_user.sql b/db/migrations/20240926022714_updates_user.sql new file mode 100644 index 0000000..2f0d43f --- /dev/null +++ b/db/migrations/20240926022714_updates_user.sql @@ -0,0 +1,9 @@ +-- +goose Up +-- +goose StatementBegin +ALTER TABLE "Updates" ADD COLUMN UserName TEXT; +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +ALTER TABLE "Updates" DROP COLUMN UserName; +-- +goose StatementEnd diff --git a/db/queries/models.go b/db/queries/models.go index 2ecc789..fd2913c 100644 --- a/db/queries/models.go +++ b/db/queries/models.go @@ -59,4 +59,5 @@ type Updates struct { EventKey sql.NullString EventId sql.NullString NewProvisionedDisk sql.NullFloat64 + UserName sql.NullString } diff --git a/db/queries/query.sql b/db/queries/query.sql index 192ada3..e670912 100644 --- a/db/queries/query.sql +++ b/db/queries/query.sql @@ -29,9 +29,9 @@ WHERE "VmId" = sqlc.arg('vmId') AND "Datacenter" = sqlc.arg('datacenterName'); -- name: CreateUpdate :one INSERT INTO "Updates" ( - "InventoryId", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool", "NewProvisionedDisk" + "InventoryId", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool", "NewProvisionedDisk", "UserName" ) VALUES( - ?, ?, ?, ?, ?, ?, ?, ?, ? + ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) RETURNING *; diff --git a/db/queries/query.sql.go b/db/queries/query.sql.go index 724540c..1f58c61 100644 --- a/db/queries/query.sql.go +++ b/db/queries/query.sql.go @@ -13,7 +13,7 @@ import ( 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 +RETURNING Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk, UserName ` type CleanupUpdatesParams struct { @@ -162,11 +162,11 @@ func (q *Queries) CreateInventory(ctx context.Context, arg CreateInventoryParams const createUpdate = `-- name: CreateUpdate :one INSERT INTO "Updates" ( - "InventoryId", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool", "NewProvisionedDisk" + "InventoryId", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool", "NewProvisionedDisk", "UserName" ) VALUES( - ?, ?, ?, ?, ?, ?, ?, ?, ? + ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) -RETURNING Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk +RETURNING Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk, UserName ` type CreateUpdateParams struct { @@ -179,6 +179,7 @@ type CreateUpdateParams struct { NewRam sql.NullInt64 NewResourcePool sql.NullString NewProvisionedDisk sql.NullFloat64 + UserName sql.NullString } func (q *Queries) CreateUpdate(ctx context.Context, arg CreateUpdateParams) (Updates, error) { @@ -192,6 +193,7 @@ func (q *Queries) CreateUpdate(ctx context.Context, arg CreateUpdateParams) (Upd arg.NewRam, arg.NewResourcePool, arg.NewProvisionedDisk, + arg.UserName, ) var i Updates err := row.Scan( @@ -205,6 +207,7 @@ func (q *Queries) CreateUpdate(ctx context.Context, arg CreateUpdateParams) (Upd &i.EventKey, &i.EventId, &i.NewProvisionedDisk, + &i.UserName, ) return i, err } diff --git a/server/handler/vmModify.go b/server/handler/vmModify.go index cc906ec..e5eefff 100644 --- a/server/handler/vmModify.go +++ b/server/handler/vmModify.go @@ -164,6 +164,7 @@ func (h *Handler) VmModify(w http.ResponseWriter, r *http.Request) { params.EventId = sql.NullString{String: event.CloudEvent.ID, Valid: event.CloudEvent.ID != ""} params.EventKey = sql.NullString{String: strconv.Itoa(event.CloudEvent.Data.Key), Valid: event.CloudEvent.Data.Key > 0} params.UpdateTime = sql.NullInt64{Int64: unixTimestamp, Valid: unixTimestamp > 0} + params.UserName = sql.NullString{String: event.CloudEvent.Data.UserName, Valid: event.CloudEvent.Data.UserName != ""} // Create the Update database record result, err := h.Database.Queries().CreateUpdate(context.Background(), params) diff --git a/server/router/router.go b/server/router/router.go index 43ea7ce..76672b9 100644 --- a/server/router/router.go +++ b/server/router/router.go @@ -27,7 +27,7 @@ func New(logger *slog.Logger, database db.Database, buildTime string, sha1ver st mux.HandleFunc("/api/event/vm/delete", h.VmDelete) mux.HandleFunc("/api/import/vm", h.VmImport) // temporary endpoint - mux.HandleFunc("/api/cleanup/updates", h.UpdateCleanup) + //mux.HandleFunc("/api/cleanup/updates", h.UpdateCleanup) return middleware.NewLoggingMiddleware(logger, mux) }