improve logic for temporary VM renames
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-10-15 08:51:35 +11:00
parent 81271873f3
commit c9375f3099

View File

@@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"log/slog"
"strings"
"time"
"vctp/db/queries"
"vctp/internal/vcenter"
@@ -54,16 +55,35 @@ func (c *CronTask) RunVmCheck(ctx context.Context, logger *slog.Logger) error {
if err != nil {
c.Logger.Error("Can't locate vm in vCenter", "vmID", evt.VmId.String, "error", err)
continue
} else if vmObject == nil {
c.Logger.Debug("didn't find VM", "vm_id", evt.VmId.String)
// TODO - if VM name ends with -tmp or -phVm then we mark this record as processed and stop trying to find a VM that doesnt exist anymore
if strings.HasSuffix(evt.VmName.String, "-phVm") || strings.HasSuffix(evt.VmName.String, "-tmp") {
c.Logger.Info("VM name indicates temporary VM, marking as processed", "vm_name", evt.VmName.String)
err = c.Database.Queries().UpdateEventsProcessed(ctx, evt.Eid)
if err != nil {
c.Logger.Error("Unable to mark this event as processed", "event_id", evt.Eid, "error", err)
} else {
//c.Logger.Debug("Marked event as processed", "event_id", evt.Eid)
}
}
/*
numRam = 0
numVcpus = 0
totalDiskGB = 0
isTemplate = "FALSE"
folderPath = ""
vmUuid = ""
} else {
c.Logger.Debug("found VM")
*/
continue
}
//c.Logger.Debug("found VM")
srmPlaceholder = "FALSE" // Default assumption
//prettyPrint(vmObject)
@@ -133,7 +153,6 @@ func (c *CronTask) RunVmCheck(ctx context.Context, logger *slog.Logger) error {
poweredOn = "TRUE"
}
}
err = vc.Logout()
if err != nil {
c.Logger.Error("unable to logout of vcenter", "error", err)
@@ -162,7 +181,7 @@ func (c *CronTask) RunVmCheck(ctx context.Context, logger *slog.Logger) error {
PoweredOn: poweredOn,
}
c.Logger.Debug("database params", "params", params)
//c.Logger.Debug("database params", "params", params)
// Insert the new inventory record into the database
_, err := c.Database.Queries().CreateInventory(ctx, params)
@@ -176,7 +195,7 @@ func (c *CronTask) RunVmCheck(ctx context.Context, logger *slog.Logger) error {
if err != nil {
c.Logger.Error("Unable to mark this event as processed", "event_id", evt.Eid, "error", err)
} else {
c.Logger.Debug("Marked event as processed", "event_id", evt.Eid)
//c.Logger.Debug("Marked event as processed", "event_id", evt.Eid)
}
}
} else {