From 4efdf50433bd489cad35f96149b87effe35af090 Mon Sep 17 00:00:00 2001 From: Nathan Coad Date: Mon, 16 Sep 2024 12:55:34 +1000 Subject: [PATCH] add more fields to the Update database record --- db/queries/query.sql | 2 +- db/queries/query.sql.go | 11 ++++++++--- server/handler/vmModify.go | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/db/queries/query.sql b/db/queries/query.sql index e47356f..b42dd94 100644 --- a/db/queries/query.sql +++ b/db/queries/query.sql @@ -8,7 +8,7 @@ WHERE "Name" = ?; -- name: GetInventoryVmId :one SELECT * FROM "Inventory" -WHERE "VmId" = ? LIMIT 1; +WHERE "VmId" = sqlc.arg('vmId') AND "Datacenter" = sqlc.arg('datacenterName'); -- name: GetInventoryEventId :one SELECT * FROM "Inventory" diff --git a/db/queries/query.sql.go b/db/queries/query.sql.go index 0c76766..d860edc 100644 --- a/db/queries/query.sql.go +++ b/db/queries/query.sql.go @@ -272,11 +272,16 @@ func (q *Queries) GetInventoryEventId(ctx context.Context, eventid sql.NullStrin const getInventoryVmId = `-- name: GetInventoryVmId :one SELECT Iid, Name, Vcenter, VmId, EventKey, EventId, CreationTime, DeletionTime, ResourcePool, VmType, Datacenter, Cluster, Folder, ProvisionedDisk, InitialVcpus, InitialRam, SrmPlaceholder, IsTemplate, PowerState FROM "Inventory" -WHERE "VmId" = ? LIMIT 1 +WHERE "VmId" = ?1 AND "Datacenter" = ?2 ` -func (q *Queries) GetInventoryVmId(ctx context.Context, vmid sql.NullString) (Inventory, error) { - row := q.db.QueryRowContext(ctx, getInventoryVmId, vmid) +type GetInventoryVmIdParams struct { + VmId sql.NullString + DatacenterName sql.NullString +} + +func (q *Queries) GetInventoryVmId(ctx context.Context, arg GetInventoryVmIdParams) (Inventory, error) { + row := q.db.QueryRowContext(ctx, getInventoryVmId, arg.VmId, arg.DatacenterName) var i Inventory err := row.Scan( &i.Iid, diff --git a/server/handler/vmModify.go b/server/handler/vmModify.go index cf8fab7..fc52fa2 100644 --- a/server/handler/vmModify.go +++ b/server/handler/vmModify.go @@ -10,6 +10,7 @@ import ( "regexp" "strconv" "strings" + "time" "vctp/db/queries" models "vctp/server/models" ) @@ -18,6 +19,7 @@ import ( func (h *Handler) VmModify(w http.ResponseWriter, r *http.Request) { var configChanges []map[string]string params := queries.CreateUpdateParams{} + var unixTimestamp int64 reqBody, err := io.ReadAll(r.Body) if err != nil { @@ -76,6 +78,39 @@ func (h *Handler) VmModify(w http.ResponseWriter, r *http.Request) { // Only create a database record if we found one of the config changes we were interested in if found { + // lookup Iid from Inventory table for this VM + // also figure out what to do if we didn't find an entry for this VM in the Inventory table. Create one? + h.Logger.Debug("Checking inventory table for VM record") + invParams := queries.GetInventoryVmIdParams{ + VmId: sql.NullString{String: event.CloudEvent.Data.VM.VM.Value, Valid: event.CloudEvent.Data.VM.VM.Value != ""}, + DatacenterName: sql.NullString{String: event.CloudEvent.Data.Datacenter.Name, Valid: event.CloudEvent.Data.Datacenter.Name != ""}, + } + invResult, err := h.Database.Queries().GetInventoryVmId(context.Background(), invParams) + + if err != nil { + h.Logger.Error("unable to find existing inventory record for this VM", "error", err) + // TODO - how to handle? + } else { + params.InventoryId = sql.NullInt64{Int64: invResult.Iid, Valid: invResult.Iid > 0} + } + + // Parse the datetime string to a time.Time object + eventTime, err := time.Parse(time.RFC3339, event.CloudEvent.Data.CreatedTime) + if err != nil { + h.Logger.Warn("unable to convert cloud event time to timestamp", "error", err) + unixTimestamp = time.Now().Unix() + } else { + // Convert to Unix timestamp + unixTimestamp = eventTime.Unix() + } + + // populate other parameters for the Update database record + 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.UpdateType = "reconfigure" + + // Create the Update database record result, err := h.Database.Queries().CreateUpdate(context.Background(), params) if err != nil { h.Logger.Error("unable to perform database insert", "error", err)