capture new disk size
This commit is contained in:
@@ -32,11 +32,11 @@ func (h *Handler) VmDelete(w http.ResponseWriter, r *http.Request) {
|
||||
var event models.CloudEventReceived
|
||||
if err := json.Unmarshal(reqBody, &event); err != nil {
|
||||
h.Logger.Error("unable to decode json", "error", err)
|
||||
prettyPrint(event)
|
||||
http.Error(w, "Invalid JSON body", http.StatusBadRequest)
|
||||
return
|
||||
} else {
|
||||
h.Logger.Debug("successfully decoded JSON")
|
||||
prettyPrint(event)
|
||||
}
|
||||
|
||||
// Use the event CreatedTime to update the DeletionTime column in the VM inventory table
|
||||
|
@@ -12,7 +12,10 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
"vctp/db/queries"
|
||||
"vctp/internal/vcenter"
|
||||
models "vctp/server/models"
|
||||
|
||||
"github.com/vmware/govmomi/vim25/types"
|
||||
)
|
||||
|
||||
// VmModify receives the CloudEvent for a VM modification or move
|
||||
@@ -65,6 +68,7 @@ func (h *Handler) VmModify(w http.ResponseWriter, r *http.Request) {
|
||||
} else {
|
||||
found = true
|
||||
params.NewVcpus = sql.NullInt64{Int64: i, Valid: i > 0}
|
||||
params.UpdateType = "reconfigure"
|
||||
}
|
||||
case "config.hardware.memoryMB":
|
||||
i, err := strconv.ParseInt(change["newValue"], 10, 64)
|
||||
@@ -73,6 +77,7 @@ func (h *Handler) VmModify(w http.ResponseWriter, r *http.Request) {
|
||||
} else {
|
||||
found = true
|
||||
params.NewRam = sql.NullInt64{Int64: i, Valid: i > 0}
|
||||
params.UpdateType = "reconfigure"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,6 +87,10 @@ func (h *Handler) VmModify(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// TODO - recalculate total disk size
|
||||
h.Logger.Debug("Detected config change for VM disk")
|
||||
found = true
|
||||
params.UpdateType = "diskchange"
|
||||
diskSize := h.calculateNewDiskSize(event)
|
||||
params.NewProvisionedDisk = sql.NullFloat64{Float64: diskSize, Valid: diskSize > 0}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,7 +126,6 @@ func (h *Handler) VmModify(w http.ResponseWriter, r *http.Request) {
|
||||
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)
|
||||
@@ -135,7 +143,7 @@ func (h *Handler) VmModify(w http.ResponseWriter, r *http.Request) {
|
||||
} else {
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
fmt.Fprintf(w, "Processed update event but no config changes were of interest\n")
|
||||
prettyPrint(configChanges)
|
||||
prettyPrint(event.CloudEvent.Data.ConfigSpec)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -179,3 +187,35 @@ func (h *Handler) processConfigChanges(configChanges string) []map[string]string
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (h *Handler) calculateNewDiskSize(event models.CloudEventReceived) float64 {
|
||||
var diskSize float64
|
||||
h.Logger.Debug("connecting to vcenter")
|
||||
vc := vcenter.New(h.Logger)
|
||||
vc.Login(event.CloudEvent.Source)
|
||||
|
||||
vmObject, err := vc.FindVMByIDWithDatacenter(event.CloudEvent.Data.VM.VM.Value, event.CloudEvent.Data.Datacenter.Datacenter.Value)
|
||||
|
||||
if err != nil {
|
||||
h.Logger.Error("Can't locate vm in vCenter", "vmID", event.CloudEvent.Data.VM.VM.Value, "error", err)
|
||||
} else {
|
||||
if vmObject.Vm.Config != nil {
|
||||
h.Logger.Debug("Found VM with config, calculating new total disk size", "vmID", event.CloudEvent.Data.VM.VM.Value)
|
||||
// Calculate the total disk allocated in GB
|
||||
for _, device := range vmObject.Vm.Config.Hardware.Device {
|
||||
if disk, ok := device.(*types.VirtualDisk); ok {
|
||||
diskSize += float64(disk.CapacityInBytes / 1024 / 1024 / 1024) // Convert from bytes to GB
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err = vc.Logout()
|
||||
if err != nil {
|
||||
h.Logger.Error("unable to logout of vcenter", "error", err)
|
||||
}
|
||||
|
||||
h.Logger.Debug("Calculated new disk size", "value", diskSize)
|
||||
|
||||
return diskSize
|
||||
}
|
||||
|
Reference in New Issue
Block a user