Refactor code to use 'any' type and improve context handling
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -41,7 +41,8 @@ func (h *Handler) VmModifyEvent(w http.ResponseWriter, r *http.Request) {
|
||||
var unixTimestamp int64
|
||||
|
||||
re := regexp.MustCompile(`/([^/]+)/[^/]+\.vmdk$`)
|
||||
ctx := context.Background()
|
||||
ctx, cancel := withRequestTimeout(r, defaultRequestTimeout)
|
||||
defer cancel()
|
||||
|
||||
reqBody, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
@@ -189,7 +190,7 @@ func (h *Handler) VmModifyEvent(w http.ResponseWriter, r *http.Request) {
|
||||
// If we found a disk change belonging to this VM then recalculate the disk size
|
||||
if diskChangeFound {
|
||||
params.UpdateType = "diskchange"
|
||||
diskSize := h.calculateNewDiskSize(event)
|
||||
diskSize := h.calculateNewDiskSize(ctx, event)
|
||||
params.NewProvisionedDisk = sql.NullFloat64{Float64: diskSize, Valid: diskSize > 0}
|
||||
}
|
||||
}
|
||||
@@ -333,7 +334,7 @@ func (h *Handler) processConfigChanges(configChanges string) []map[string]string
|
||||
return result
|
||||
}
|
||||
|
||||
func (h *Handler) calculateNewDiskSize(event models.CloudEventReceived) float64 {
|
||||
func (h *Handler) calculateNewDiskSize(ctx context.Context, event models.CloudEventReceived) float64 {
|
||||
var diskSize float64
|
||||
var totalDiskBytes int64
|
||||
h.Logger.Debug("connecting to vcenter")
|
||||
@@ -368,7 +369,9 @@ func (h *Handler) calculateNewDiskSize(event models.CloudEventReceived) float64
|
||||
}
|
||||
}
|
||||
|
||||
_ = vc.Logout(context.Background())
|
||||
logoutCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
|
||||
defer cancel()
|
||||
_ = vc.Logout(logoutCtx)
|
||||
|
||||
h.Logger.Debug("Calculated new disk size", "value", diskSize)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user