dont query vm folder path unless we need to
Some checks are pending
continuous-integration/drone/push Build is passing
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

This commit is contained in:
2024-09-16 13:52:56 +10:00
parent 4efdf50433
commit e47718cd7f
3 changed files with 23 additions and 17 deletions

View File

@@ -28,6 +28,7 @@ func (c *CronTask) RunVmCheck(ctx context.Context, logger *slog.Logger) error {
foundVm bool foundVm bool
isTemplate int isTemplate int
poweredOn int poweredOn int
folderPath string
) )
logger.Debug("Started Events processing", "time", time.Now()) logger.Debug("Started Events processing", "time", time.Now())
@@ -60,6 +61,7 @@ func (c *CronTask) RunVmCheck(ctx context.Context, logger *slog.Logger) error {
numVcpus = 0 numVcpus = 0
totalDiskGB = 0 totalDiskGB = 0
isTemplate = 0 isTemplate = 0
folderPath = ""
} else { } else {
c.Logger.Debug("found VM") c.Logger.Debug("found VM")
srmPlaceholder = 0 // Default assumption srmPlaceholder = 0 // Default assumption
@@ -89,6 +91,15 @@ func (c *CronTask) RunVmCheck(ctx context.Context, logger *slog.Logger) error {
isTemplate = 0 isTemplate = 0
} }
// Retrieve the full folder path of the VM
folderPath, err = vc.GetVMFolderPath(vmObject.Vm)
if err != nil {
c.Logger.Error("failed to get vm folder path", "error", err)
folderPath = ""
} else {
c.Logger.Debug("Found vm folder path", "folder_path", folderPath)
}
foundVm = true foundVm = true
} else { } else {
c.Logger.Error("Empty VM config") c.Logger.Error("Empty VM config")
@@ -125,7 +136,7 @@ func (c *CronTask) RunVmCheck(ctx context.Context, logger *slog.Logger) error {
InitialVcpus: sql.NullInt64{Int64: int64(numVcpus), Valid: numVcpus > 0}, InitialVcpus: sql.NullInt64{Int64: int64(numVcpus), Valid: numVcpus > 0},
InitialRam: sql.NullInt64{Int64: int64(numRam), Valid: numRam > 0}, InitialRam: sql.NullInt64{Int64: int64(numRam), Valid: numRam > 0},
ProvisionedDisk: sql.NullFloat64{Float64: totalDiskGB, Valid: totalDiskGB > 0}, ProvisionedDisk: sql.NullFloat64{Float64: totalDiskGB, Valid: totalDiskGB > 0},
Folder: sql.NullString{String: vmObject.FolderPath, Valid: vmObject.FolderPath != ""}, Folder: sql.NullString{String: folderPath, Valid: folderPath != ""},
ResourcePool: sql.NullString{String: vmObject.ResourcePool, Valid: vmObject.ResourcePool != ""}, ResourcePool: sql.NullString{String: vmObject.ResourcePool, Valid: vmObject.ResourcePool != ""},
SrmPlaceholder: sql.NullInt64{Int64: int64(srmPlaceholder), Valid: true}, SrmPlaceholder: sql.NullInt64{Int64: int64(srmPlaceholder), Valid: true},
IsTemplate: sql.NullInt64{Int64: int64(isTemplate), Valid: true}, IsTemplate: sql.NullInt64{Int64: int64(isTemplate), Valid: true},

View File

@@ -27,8 +27,6 @@ type Vcenter struct {
type VmProperties struct { type VmProperties struct {
Vm mo.VirtualMachine Vm mo.VirtualMachine
ResourcePool string ResourcePool string
FolderPath string
//Datacenter string
} }
// New creates a new Vcenter with the given logger // New creates a new Vcenter with the given logger
@@ -181,7 +179,6 @@ func (v *Vcenter) FindVMByIDWithDatacenter(vmID string, dcID string) (*VmPropert
//var dcName string //var dcName string
var err error var err error
resourcePool := "" resourcePool := ""
vmFolderPath := ""
v.Logger.Debug("searching for vm id", "vm_id", vmID, "datacenter_id", dcID) v.Logger.Debug("searching for vm id", "vm_id", vmID, "datacenter_id", dcID)
finder := find.NewFinder(v.client.Client, true) finder := find.NewFinder(v.client.Client, true)
@@ -226,20 +223,10 @@ func (v *Vcenter) FindVMByIDWithDatacenter(vmID string, dcID string) (*VmPropert
} }
// Retrieve the full folder path of the VM
folderPath, err := v.getVMFolderPath(vm)
if err != nil {
v.Logger.Error("failed to get vm folder path", "error", err)
} else {
v.Logger.Debug("Found vm folder path", "folder_path", folderPath)
vmFolderPath = folderPath
}
return &VmProperties{ return &VmProperties{
//Datacenter: dcName, //Datacenter: dcName,
Vm: vm, Vm: vm,
ResourcePool: resourcePool, ResourcePool: resourcePool,
FolderPath: vmFolderPath,
}, nil }, nil
} else if _, ok := err.(*find.NotFoundError); !ok { } else if _, ok := err.(*find.NotFoundError); !ok {
// If the error is not a NotFoundError, return it // If the error is not a NotFoundError, return it
@@ -255,7 +242,7 @@ func (v *Vcenter) FindVMByIDWithDatacenter(vmID string, dcID string) (*VmPropert
} }
// Helper function to retrieve the full folder path for the VM // Helper function to retrieve the full folder path for the VM
func (v *Vcenter) getVMFolderPath(vm mo.VirtualMachine) (string, error) { func (v *Vcenter) GetVMFolderPath(vm mo.VirtualMachine) (string, error) {
//finder := find.NewFinder(v.client.Client, true) //finder := find.NewFinder(v.client.Client, true)
v.Logger.Debug("Commencing vm folder path search") v.Logger.Debug("Commencing vm folder path search")

View File

@@ -74,6 +74,14 @@ func (h *Handler) VmModify(w http.ResponseWriter, r *http.Request) {
params.NewRam = sql.NullInt64{Int64: i, Valid: i > 0} params.NewRam = sql.NullInt64{Int64: i, Valid: i > 0}
} }
} }
// Check if a disk was added (or maybe removed?)
if strings.Contains(change["type"], "config.hardware.device") &&
strings.Contains(event.CloudEvent.Data.FullFormattedMessage, ".vmdk") {
// TODO - recalculate total disk size
h.Logger.Debug("Detected config change for VM disk")
}
} }
// Only create a database record if we found one of the config changes we were interested in // Only create a database record if we found one of the config changes we were interested in
@@ -143,7 +151,7 @@ func (h *Handler) processConfigChanges(configChanges string) []map[string]string
for _, change := range changes { for _, change := range changes {
// Trim any extra spaces and skip empty lines // Trim any extra spaces and skip empty lines
change = strings.TrimSpace(change) change = strings.TrimSpace(change)
h.Logger.Debug("Processing config change element", "substring", change) //h.Logger.Debug("Processing config change element", "substring", change)
if change == "" { if change == "" {
continue continue
} }
@@ -156,7 +164,7 @@ func (h *Handler) processConfigChanges(configChanges string) []map[string]string
"type": match[1], // config type "type": match[1], // config type
"newValue": match[2], // new value after -> or <- "newValue": match[2], // new value after -> or <-
} }
h.Logger.Debug("Adding new entry to output", "map", changeMap) //h.Logger.Debug("Adding new entry to output", "map", changeMap)
result = append(result, changeMap) result = append(result, changeMap)
} else { } else {
h.Logger.Warn("No regex matches for string", "input", change) h.Logger.Warn("No regex matches for string", "input", change)