cleanups and code fixes incl templ
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2026-03-20 13:21:15 +11:00
parent 4fbb2582e3
commit 9a561f3b07
24 changed files with 425 additions and 141 deletions

View File

@@ -6,7 +6,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"regexp"
"strconv"
@@ -32,6 +31,11 @@ import (
// @Failure 500 {object} models.ErrorResponse "Server error"
// @Router /api/event/vm/modify [post]
func (h *Handler) VmModifyEvent(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
writeJSONError(w, http.StatusMethodNotAllowed, "method not allowed")
return
}
if h.denyLegacyAPI(w, "/api/event/vm/modify") {
return
}
@@ -44,18 +48,10 @@ func (h *Handler) VmModifyEvent(w http.ResponseWriter, r *http.Request) {
ctx, cancel := withRequestTimeout(r, defaultRequestTimeout)
defer cancel()
reqBody, err := io.ReadAll(r.Body)
if err != nil {
h.Logger.Error("Invalid data received", "length", len(reqBody), "error", err)
writeJSONError(w, http.StatusInternalServerError, fmt.Sprintf("Invalid data received: '%s'", err))
return
}
// Decode the JSON body into CloudEventReceived struct
var event models.CloudEventReceived
if err := json.Unmarshal(reqBody, &event); err != nil {
h.Logger.Error("Unable to decode json request body", "length", len(reqBody), "error", err)
writeJSONError(w, http.StatusInternalServerError, fmt.Sprintf("Unable to decode json request body: '%s'", err))
if err := decodeJSONBody(w, r, &event); err != nil {
h.Logger.Error("Unable to decode json request body", "error", err)
writeJSONError(w, http.StatusBadRequest, fmt.Sprintf("Unable to decode json request body: '%s'", err))
return
} else {
//h.Logger.Debug("successfully decoded JSON")
@@ -339,7 +335,15 @@ func (h *Handler) calculateNewDiskSize(ctx context.Context, event models.CloudEv
var totalDiskBytes int64
h.Logger.Debug("connecting to vcenter")
vc := vcenter.New(h.Logger, h.VcCreds)
vc.Login(event.CloudEvent.Source)
if err := vc.Login(event.CloudEvent.Source); err != nil {
h.Logger.Error("unable to connect to vcenter while calculating disk size", "source", event.CloudEvent.Source, "error", err)
return 0
}
defer func() {
logoutCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
defer cancel()
_ = vc.Logout(logoutCtx)
}()
vmObject, err := vc.FindVMByIDWithDatacenter(event.CloudEvent.Data.VM.VM.Value, event.CloudEvent.Data.Datacenter.Datacenter.Value)
@@ -369,10 +373,6 @@ func (h *Handler) calculateNewDiskSize(ctx context.Context, event models.CloudEv
}
}
logoutCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
defer cancel()
_ = vc.Logout(logoutCtx)
h.Logger.Debug("Calculated new disk size", "value", diskSize)
return diskSize
@@ -394,7 +394,15 @@ func (h *Handler) AddVmToInventory(evt models.CloudEventReceived, ctx context.Co
)
//c.Logger.Debug("connecting to vcenter")
vc := vcenter.New(h.Logger, h.VcCreds)
vc.Login(evt.CloudEvent.Source)
if err := vc.Login(evt.CloudEvent.Source); err != nil {
h.Logger.Error("unable to connect to vcenter", "source", evt.CloudEvent.Source, "error", err)
return 0, err
}
defer func() {
logoutCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
defer cancel()
_ = vc.Logout(logoutCtx)
}()
//datacenter = evt.DatacenterName.String
vmObject, err := vc.FindVMByIDWithDatacenter(evt.CloudEvent.Data.VM.VM.Value, evt.CloudEvent.Data.Datacenter.Datacenter.Value)
@@ -410,7 +418,6 @@ func (h *Handler) AddVmToInventory(evt models.CloudEventReceived, ctx context.Co
if strings.HasPrefix(vmObject.Name, "vCLS-") {
h.Logger.Info("Skipping internal vCLS VM", "vm_name", vmObject.Name)
_ = vc.Logout(ctx)
return 0, nil
}
@@ -484,8 +491,6 @@ func (h *Handler) AddVmToInventory(evt models.CloudEventReceived, ctx context.Co
poweredOn = "TRUE"
}
_ = vc.Logout(ctx)
if foundVm {
e := evt.CloudEvent
h.Logger.Debug("Adding to Inventory table", "vm_name", e.Data.VM.Name, "vcpus", numVcpus, "ram", numRam, "dc", e.Data.Datacenter.Datacenter.Value)