From f80dfe902744f25ce2618a2fc2fe51b6e86a64a4 Mon Sep 17 00:00:00 2001 From: Nathan Coad Date: Wed, 2 Oct 2024 13:14:09 +1000 Subject: [PATCH] add vm name and placeholder change to updates table --- db/migrations/20241001222729_add_placeholder.sql | 2 ++ db/queries/models.go | 1 + db/queries/query.sql | 4 ++-- db/queries/query.sql.go | 14 +++++++++----- main.go | 2 +- server/handler/vmModifyEvent.go | 15 +++++++++++++-- 6 files changed, 28 insertions(+), 10 deletions(-) diff --git a/db/migrations/20241001222729_add_placeholder.sql b/db/migrations/20241001222729_add_placeholder.sql index f9e1a5d..bd45ead 100644 --- a/db/migrations/20241001222729_add_placeholder.sql +++ b/db/migrations/20241001222729_add_placeholder.sql @@ -1,9 +1,11 @@ -- +goose Up -- +goose StatementBegin ALTER TABLE "Updates" ADD COLUMN PlaceholderChange TEXT; +ALTER TABLE "Updates" ADD COLUMN Name TEXT; -- +goose StatementEnd -- +goose Down -- +goose StatementBegin +ALTER TABLE "Updates" DROP COLUMN Name; ALTER TABLE "Updates" DROP COLUMN PlaceholderChange; -- +goose StatementEnd diff --git a/db/queries/models.go b/db/queries/models.go index e8d3837..dde6217 100644 --- a/db/queries/models.go +++ b/db/queries/models.go @@ -73,4 +73,5 @@ type Updates struct { NewProvisionedDisk sql.NullFloat64 UserName sql.NullString PlaceholderChange sql.NullString + Name sql.NullString } diff --git a/db/queries/query.sql b/db/queries/query.sql index 5a246e2..f9fdb50 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", "PlaceholderChange" + "InventoryId", "Name", "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 2bc7577..8f0f1d7 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 +RETURNING Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk, UserName, PlaceholderChange, Name ` type CleanupUpdatesParams struct { @@ -210,15 +210,16 @@ 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", "PlaceholderChange" + "InventoryId", "Name", "EventKey", "EventId", "UpdateTime", "UpdateType", "NewVcpus", "NewRam", "NewResourcePool", "NewProvisionedDisk", "UserName", "PlaceholderChange" ) VALUES( - ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? + ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) -RETURNING Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk, UserName, PlaceholderChange +RETURNING Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk, UserName, PlaceholderChange, Name ` type CreateUpdateParams struct { InventoryId sql.NullInt64 + Name sql.NullString EventKey sql.NullString EventId sql.NullString UpdateTime sql.NullInt64 @@ -234,6 +235,7 @@ type CreateUpdateParams struct { func (q *Queries) CreateUpdate(ctx context.Context, arg CreateUpdateParams) (Updates, error) { row := q.db.QueryRowContext(ctx, createUpdate, arg.InventoryId, + arg.Name, arg.EventKey, arg.EventId, arg.UpdateTime, @@ -259,6 +261,7 @@ func (q *Queries) CreateUpdate(ctx context.Context, arg CreateUpdateParams) (Upd &i.NewProvisionedDisk, &i.UserName, &i.PlaceholderChange, + &i.Name, ) return i, err } @@ -569,7 +572,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 FROM "Updates" +SELECT Uid, InventoryId, UpdateTime, UpdateType, NewVcpus, NewRam, NewResourcePool, EventKey, EventId, NewProvisionedDisk, UserName, PlaceholderChange, Name FROM "Updates" ORDER BY "UpdateTime" ` @@ -595,6 +598,7 @@ func (q *Queries) GetReportUpdates(ctx context.Context) ([]Updates, error) { &i.NewProvisionedDisk, &i.UserName, &i.PlaceholderChange, + &i.Name, ); err != nil { return nil, err } diff --git a/main.go b/main.go index c9fb670..474e543 100644 --- a/main.go +++ b/main.go @@ -191,7 +191,7 @@ func main() { logger.Debug("Created event processing cron job", "job", job.ID()) // start background checks of vcenter inventory - startsAt2 := time.Now().Add(time.Second * 30) + startsAt2 := time.Now().Add(time.Second * 300) job2, err := c.NewJob( gocron.DurationJob(cronInvFrequency), gocron.NewTask(func() { diff --git a/server/handler/vmModifyEvent.go b/server/handler/vmModifyEvent.go index 250eefc..b196b50 100644 --- a/server/handler/vmModifyEvent.go +++ b/server/handler/vmModifyEvent.go @@ -104,8 +104,17 @@ func (h *Handler) VmModifyEvent(w http.ResponseWriter, r *http.Request) { params.NewRam = sql.NullInt64{Int64: i, Valid: i > 0} params.UpdateType = "reconfigure" } - case "config.managedBy": - // This changes when a VM becomes a placeholder or vice versa + case "config.managedBy": // This changes when a VM becomes a placeholder or vice versa + found = true + params.UpdateType = "srm" + + if change["newValue"] == "(extensionKey = \"com.vmware.vcDr\", type = \"placeholderVm\")" { + params.PlaceholderChange = sql.NullString{String: "placeholderVm", Valid: true} + } else if change["newValue"] == "" { + params.PlaceholderChange = sql.NullString{String: "Vm", Valid: true} + } else { + h.Logger.Error("Unexpected value for managedBy configuration", "new_value", change["newValue"]) + } // map[newValue:(extensionKey = \"com.vmware.vcDr\", type = \"placeholderVm\") type:config.managedBy] // map[newValue:\"testVm\" type:config.managedBy.type] @@ -187,12 +196,14 @@ 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.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 + h.Logger.Debug("Adding Update record", "params", params) result, err := h.Database.Queries().CreateUpdate(ctx, params) if err != nil { h.Logger.Error("unable to perform database insert", "error", err)