All checks were successful
continuous-integration/drone/push Build is passing
154 lines
4.7 KiB
Go
154 lines
4.7 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"net/http"
|
|
"time"
|
|
"vctp/db/queries"
|
|
"vctp/internal/vcenter"
|
|
)
|
|
|
|
// VmUpdateDetails refreshes inventory metadata from vCenter.
|
|
// @Summary Refresh VM details (deprecated)
|
|
// @Description Deprecated: Queries vCenter and updates inventory records with missing details.
|
|
// @Tags inventory
|
|
// @Deprecated
|
|
// @Produce json
|
|
// @Success 200 {object} models.StatusMessageResponse "Update completed"
|
|
// @Failure 500 {object} models.ErrorResponse "Server error"
|
|
// @Router /api/inventory/vm/update [post]
|
|
func (h *Handler) VmUpdateDetails(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
writeJSONError(w, http.StatusMethodNotAllowed, "method not allowed")
|
|
return
|
|
}
|
|
|
|
if h.denyLegacyAPI(w, "/api/inventory/vm/update") {
|
|
return
|
|
}
|
|
|
|
var matchFound bool
|
|
var inventoryId int64
|
|
var srmPlaceholder string
|
|
var vmUuid string
|
|
var dbUuid string
|
|
|
|
ctx, cancel := withRequestTimeout(r, longRunningRequestTimeout)
|
|
defer cancel()
|
|
|
|
// reload settings in case vcenter list has changed
|
|
if err := h.Settings.ReadYMLSettings(); err != nil {
|
|
h.Logger.Error("unable to reload settings", "error", err)
|
|
writeJSONError(w, http.StatusInternalServerError, "Unable to reload settings")
|
|
return
|
|
}
|
|
|
|
for _, url := range h.Settings.Values.Settings.VcenterAddresses {
|
|
h.Logger.Debug("connecting to vcenter", "url", url)
|
|
vc := vcenter.New(h.Logger, h.VcCreds)
|
|
if err := vc.Login(url); err != nil {
|
|
h.Logger.Error("unable to connect to vcenter", "url", url, "error", err)
|
|
writeJSONError(w, http.StatusInternalServerError, "Unable to connect to vcenter")
|
|
return
|
|
}
|
|
logout := func() {
|
|
logoutCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
|
|
defer cancel()
|
|
if err := vc.Logout(logoutCtx); err != nil {
|
|
h.Logger.Warn("vcenter logout failed", "url", url, "error", err)
|
|
}
|
|
}
|
|
|
|
// Get list of VMs from vcenter
|
|
vms, err := vc.GetAllVmReferences()
|
|
if err != nil {
|
|
logout()
|
|
h.Logger.Error("Unable to query vcenter VM references", "url", url, "error", err)
|
|
writeJSONError(w, http.StatusInternalServerError, "Unable to query vcenter VM references")
|
|
return
|
|
}
|
|
|
|
// Get list of VMs from inventory table
|
|
h.Logger.Debug("Querying inventory table")
|
|
results, err := h.Database.Queries().GetInventoryByVcenter(ctx, url)
|
|
if err != nil {
|
|
logout()
|
|
h.Logger.Error("Unable to query inventory table", "error", err)
|
|
writeJSONError(w, http.StatusInternalServerError, "Unable to query inventory table")
|
|
return
|
|
}
|
|
|
|
if len(results) == 0 {
|
|
h.Logger.Error("Empty inventory results")
|
|
}
|
|
|
|
// Iterate VMs from vcenter and see if they were in the database
|
|
for _, vm := range vms {
|
|
matchFound = false
|
|
inventoryId = 0
|
|
srmPlaceholder = "FALSE" // Default assumption
|
|
vmUuid = ""
|
|
|
|
for _, dbvm := range results {
|
|
if dbvm.VmId.String == vm.Reference().Value {
|
|
h.Logger.Debug("Found VM in database", "vm_name", dbvm.Name, "id", dbvm.VmId.String)
|
|
matchFound = true
|
|
inventoryId = dbvm.Iid
|
|
dbUuid = dbvm.VmUuid.String
|
|
break
|
|
}
|
|
}
|
|
|
|
if matchFound {
|
|
//h.Logger.Debug("Need to update VM in inventory table", "MoRef", vm.Reference())
|
|
vmObj, err := vc.ConvertObjToMoVM(vm)
|
|
if err != nil {
|
|
h.Logger.Error("Received error getting vm managedobject", "error", err)
|
|
continue
|
|
}
|
|
|
|
if vmObj.Config != nil {
|
|
vmUuid = vmObj.Config.Uuid
|
|
|
|
// Determine if the VM is a normal VM or an SRM placeholder
|
|
if vmObj.Config.ManagedBy != nil && vmObj.Config.ManagedBy.ExtensionKey == "com.vmware.vcDr" {
|
|
if vmObj.Config.ManagedBy.Type == "placeholderVm" {
|
|
h.Logger.Debug("VM is a placeholder")
|
|
srmPlaceholder = "TRUE"
|
|
} else {
|
|
h.Logger.Debug("VM is managed by SRM but not a placeholder", "details", vmObj.Config.ManagedBy)
|
|
}
|
|
}
|
|
|
|
if srmPlaceholder == "TRUE" || vmUuid != dbUuid {
|
|
h.Logger.Debug("Need to update vm", "name", vmObj.Name, "srm_placeholder", srmPlaceholder, "uuid", vmUuid)
|
|
params := queries.InventoryUpdateParams{
|
|
Iid: inventoryId,
|
|
SrmPlaceholder: srmPlaceholder,
|
|
Uuid: sql.NullString{String: vmUuid, Valid: vmUuid != ""},
|
|
}
|
|
|
|
h.Logger.Debug("database params", "params", params)
|
|
err := h.Database.Queries().InventoryUpdate(ctx, params)
|
|
|
|
if err != nil {
|
|
h.Logger.Error("Error received updating inventory for VM", "name", vmObj.Name, "error", err)
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
h.Logger.Warn("VM no longer present in vcenter or missing config values", "MoRef", vm.Reference())
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
logout()
|
|
}
|
|
|
|
h.Logger.Debug("Processed vm update successfully")
|
|
writeJSONOKMessage(w, "Processed vm update successfully")
|
|
}
|