add more fields to the Update database record
Some checks are pending
CI / Lint (push) Waiting to run
CI / Test (push) Waiting to run
CI / End-to-End (push) Waiting to run
CI / Publish Docker (push) Blocked by required conditions
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-09-16 12:55:34 +10:00
parent d2aac0c6d4
commit 4efdf50433
3 changed files with 44 additions and 4 deletions

View File

@@ -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"

View File

@@ -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,

View File

@@ -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)