From b9c1f659715c85bbf7af3e014a99d105ce5517c6 Mon Sep 17 00:00:00 2001 From: Nathan Coad Date: Wed, 2 Oct 2024 08:29:49 +1000 Subject: [PATCH] minor adjustments --- .../20241001222729_add_placeholder.sql | 9 +++++++++ db/queries/models.go | 1 + db/queries/query.sql | 4 ++-- db/queries/query.sql.go | 14 ++++++++----- internal/report/create.go | 20 +++++++++++-------- server/handler/vmModifyEvent.go | 9 ++++++++- 6 files changed, 41 insertions(+), 16 deletions(-) create mode 100644 db/migrations/20241001222729_add_placeholder.sql diff --git a/db/migrations/20241001222729_add_placeholder.sql b/db/migrations/20241001222729_add_placeholder.sql new file mode 100644 index 0000000..f9e1a5d --- /dev/null +++ b/db/migrations/20241001222729_add_placeholder.sql @@ -0,0 +1,9 @@ +-- +goose Up +-- +goose StatementBegin +ALTER TABLE "Updates" ADD COLUMN PlaceholderChange TEXT; +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +ALTER TABLE "Updates" DROP COLUMN PlaceholderChange; +-- +goose StatementEnd diff --git a/db/queries/models.go b/db/queries/models.go index e6056bd..e8d3837 100644 --- a/db/queries/models.go +++ b/db/queries/models.go @@ -72,4 +72,5 @@ type Updates struct { EventId sql.NullString NewProvisionedDisk sql.NullFloat64 UserName sql.NullString + PlaceholderChange sql.NullString } diff --git a/db/queries/query.sql b/db/queries/query.sql index 21b0b87..5a246e2 100644 --- a/db/queries/query.sql +++ b/db/queries/query.sql @@ -60,9 +60,9 @@ RETURNING *; -- name: CreateUpdate :one INSERT INTO "Updates" ( - "InventoryId", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool", "NewProvisionedDisk", "UserName" + "InventoryId", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool", "NewProvisionedDisk", "UserName", "PlaceholderChange" ) VALUES( - ?, ?, ?, ?, ?, ?, ?, ?, ?, ? + ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) RETURNING *; diff --git a/db/queries/query.sql.go b/db/queries/query.sql.go index e59671e..2bc7577 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 +RETURNING Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk, UserName, PlaceholderChange ` type CleanupUpdatesParams struct { @@ -210,11 +210,11 @@ func (q *Queries) CreateInventoryHistory(ctx context.Context, arg CreateInventor const createUpdate = `-- name: CreateUpdate :one INSERT INTO "Updates" ( - "InventoryId", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool", "NewProvisionedDisk", "UserName" + "InventoryId", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool", "NewProvisionedDisk", "UserName", "PlaceholderChange" ) VALUES( - ?, ?, ?, ?, ?, ?, ?, ?, ?, ? + ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) -RETURNING Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk, UserName +RETURNING Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk, UserName, PlaceholderChange ` type CreateUpdateParams struct { @@ -228,6 +228,7 @@ type CreateUpdateParams struct { NewResourcePool sql.NullString NewProvisionedDisk sql.NullFloat64 UserName sql.NullString + PlaceholderChange sql.NullString } func (q *Queries) CreateUpdate(ctx context.Context, arg CreateUpdateParams) (Updates, error) { @@ -242,6 +243,7 @@ func (q *Queries) CreateUpdate(ctx context.Context, arg CreateUpdateParams) (Upd arg.NewResourcePool, arg.NewProvisionedDisk, arg.UserName, + arg.PlaceholderChange, ) var i Updates err := row.Scan( @@ -256,6 +258,7 @@ func (q *Queries) CreateUpdate(ctx context.Context, arg CreateUpdateParams) (Upd &i.EventId, &i.NewProvisionedDisk, &i.UserName, + &i.PlaceholderChange, ) return i, err } @@ -566,7 +569,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 FROM "Updates" +SELECT Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk, UserName, PlaceholderChange FROM "Updates" ORDER BY "UpdateTime" ` @@ -591,6 +594,7 @@ func (q *Queries) GetReportUpdates(ctx context.Context) ([]Updates, error) { &i.EventId, &i.NewProvisionedDisk, &i.UserName, + &i.PlaceholderChange, ); err != nil { return nil, err } diff --git a/internal/report/create.go b/internal/report/create.go index ccfe8f0..1971f85 100644 --- a/internal/report/create.go +++ b/internal/report/create.go @@ -113,10 +113,12 @@ func CreateInventoryReport(logger *slog.Logger, Database db.Database, ctx contex } // Set column autowidth - err = SetColAutoWidth(xlsx, sheetName) - if err != nil { - fmt.Printf("Error setting auto width : '%s'\n", err) - } + /* + err = SetColAutoWidth(xlsx, sheetName) + if err != nil { + fmt.Printf("Error setting auto width : '%s'\n", err) + } + */ // Save the Excel file into a byte buffer if err := xlsx.Write(&buffer); err != nil { @@ -224,10 +226,12 @@ func CreateUpdatesReport(logger *slog.Logger, Database db.Database, ctx context. } // Set column autowidth - err = SetColAutoWidth(xlsx, sheetName) - if err != nil { - fmt.Printf("Error setting auto width : '%s'\n", err) - } + /* + err = SetColAutoWidth(xlsx, sheetName) + if err != nil { + fmt.Printf("Error setting auto width : '%s'\n", err) + } + */ // Save the Excel file into a byte buffer if err := xlsx.Write(&buffer); err != nil { diff --git a/server/handler/vmModifyEvent.go b/server/handler/vmModifyEvent.go index 022916b..250eefc 100644 --- a/server/handler/vmModifyEvent.go +++ b/server/handler/vmModifyEvent.go @@ -164,7 +164,14 @@ func (h *Handler) VmModifyEvent(w http.ResponseWriter, r *http.Request) { if err != nil { h.Logger.Error("unable to find existing inventory record for this VM", "error", err) - // TODO - how to handle? + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusInternalServerError) + json.NewEncoder(w).Encode(map[string]string{ + "status": "ERROR", + "message": fmt.Sprintf("Valid request but could not locate vm id '%s' and datacenter name '%s' within inventory table : %s", + event.CloudEvent.Data.VM.VM.Value, event.CloudEvent.Data.Datacenter.Name, err), + }) + return } else { params.InventoryId = sql.NullInt64{Int64: invResult.Iid, Valid: invResult.Iid > 0} }