diff --git a/db/migrations/20241002033323_add_change.sql b/db/migrations/20241002033323_add_change.sql new file mode 100644 index 0000000..2900ac6 --- /dev/null +++ b/db/migrations/20241002033323_add_change.sql @@ -0,0 +1,9 @@ +-- +goose Up +-- +goose StatementBegin +ALTER TABLE "Updates" ADD COLUMN RawChangeString BLOB; +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +ALTER TABLE "Updates" DROP COLUMN RawChangeString; +-- +goose StatementEnd diff --git a/db/queries/models.go b/db/queries/models.go index dde6217..22229ab 100644 --- a/db/queries/models.go +++ b/db/queries/models.go @@ -74,4 +74,5 @@ type Updates struct { UserName sql.NullString PlaceholderChange sql.NullString Name sql.NullString + RawChangeString []byte } diff --git a/db/queries/query.sql b/db/queries/query.sql index 563c76c..1dd2344 100644 --- a/db/queries/query.sql +++ b/db/queries/query.sql @@ -60,9 +60,9 @@ RETURNING *; -- name: CreateUpdate :one INSERT INTO "Updates" ( - "InventoryId", "Name", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool", "NewProvisionedDisk", "UserName", "PlaceholderChange" + "InventoryId", "Name", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool", "NewProvisionedDisk", "UserName", "PlaceholderChange", "RawChangeString" ) VALUES( - ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? + ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) RETURNING *; diff --git a/db/queries/query.sql.go b/db/queries/query.sql.go index 37f8d69..f8449ba 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, UserName, PlaceholderChange, Name +RETURNING Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk, UserName, PlaceholderChange, Name, RawChangeString ` type CleanupUpdatesParams struct { @@ -29,7 +29,7 @@ func (q *Queries) CleanupUpdates(ctx context.Context, arg CleanupUpdatesParams) const cleanupUpdatesNullVm = `-- name: CleanupUpdatesNullVm :exec DELETE FROM "Updates" WHERE "InventoryId" IS NULL -RETURNING Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk, UserName, PlaceholderChange, Name +RETURNING Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk, UserName, PlaceholderChange, Name, RawChangeString ` func (q *Queries) CleanupUpdatesNullVm(ctx context.Context) error { @@ -221,11 +221,11 @@ func (q *Queries) CreateInventoryHistory(ctx context.Context, arg CreateInventor const createUpdate = `-- name: CreateUpdate :one INSERT INTO "Updates" ( - "InventoryId", "Name", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool", "NewProvisionedDisk", "UserName", "PlaceholderChange" + "InventoryId", "Name", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool", "NewProvisionedDisk", "UserName", "PlaceholderChange", "RawChangeString" ) VALUES( - ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? + ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) -RETURNING Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk, UserName, PlaceholderChange, Name +RETURNING Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk, UserName, PlaceholderChange, Name, RawChangeString ` type CreateUpdateParams struct { @@ -241,6 +241,7 @@ type CreateUpdateParams struct { NewProvisionedDisk sql.NullFloat64 UserName sql.NullString PlaceholderChange sql.NullString + RawChangeString []byte } func (q *Queries) CreateUpdate(ctx context.Context, arg CreateUpdateParams) (Updates, error) { @@ -257,6 +258,7 @@ func (q *Queries) CreateUpdate(ctx context.Context, arg CreateUpdateParams) (Upd arg.NewProvisionedDisk, arg.UserName, arg.PlaceholderChange, + arg.RawChangeString, ) var i Updates err := row.Scan( @@ -273,6 +275,7 @@ func (q *Queries) CreateUpdate(ctx context.Context, arg CreateUpdateParams) (Upd &i.UserName, &i.PlaceholderChange, &i.Name, + &i.RawChangeString, ) return i, err } @@ -583,7 +586,7 @@ func (q *Queries) GetReportInventory(ctx context.Context) ([]Inventory, error) { } const getReportUpdates = `-- name: GetReportUpdates :many -SELECT Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk, UserName, PlaceholderChange, Name FROM "Updates" +SELECT Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk, UserName, PlaceholderChange, Name, RawChangeString FROM "Updates" ORDER BY "UpdateTime" ` @@ -610,6 +613,7 @@ func (q *Queries) GetReportUpdates(ctx context.Context) ([]Updates, error) { &i.UserName, &i.PlaceholderChange, &i.Name, + &i.RawChangeString, ); err != nil { return nil, err } diff --git a/server/handler/vmModifyEvent.go b/server/handler/vmModifyEvent.go index b196b50..6d21d8e 100644 --- a/server/handler/vmModifyEvent.go +++ b/server/handler/vmModifyEvent.go @@ -197,6 +197,7 @@ func (h *Handler) VmModifyEvent(w http.ResponseWriter, r *http.Request) { // populate other parameters for the Update database record params.Name = sql.NullString{String: event.CloudEvent.Data.VM.Name, Valid: event.CloudEvent.Data.VM.Name != ""} + params.RawChangeString = []byte(event.CloudEvent.Data.ConfigChanges.Modified) 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} diff --git a/server/router/router.go b/server/router/router.go index 8a2bfdc..83796c9 100644 --- a/server/router/router.go +++ b/server/router/router.go @@ -41,7 +41,7 @@ func New(logger *slog.Logger, database db.Database, buildTime string, sha1ver st //mux.HandleFunc("/api/inventory/vm/update", h.VmUpdateDetails) // temporary endpoint - mux.HandleFunc("/api/cleanup/updates", h.UpdateCleanup) + //mux.HandleFunc("/api/cleanup/updates", h.UpdateCleanup) //mux.HandleFunc("/api/cleanup/vcenter", h.VcCleanup) mux.HandleFunc("/api/report/inventory", h.InventoryReportDownload)