Skip to content

Instantly share code, notes, and snippets.

@codegold79
Last active October 2, 2020 02:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codegold79/a07f922029f9dc9ad52e6f7f4fcc1e5a to your computer and use it in GitHub Desktop.
Save codegold79/a07f922029f9dc9ad52e6f7f4fcc1e5a to your computer and use it in GitHub Desktop.
func (c *vsClient) applyCfgs(ctx context.Context, moVM mo.VirtualMachine, mor types.ManagedObjectReference, cfgs []vmConfig) (*object.Task, error) {
vm := object.NewVirtualMachine(c.govmomi.Client, mor)
desiredSpec := generateDesiredSpec(cfgs, moVM)
task, err := vm.Reconfigure(ctx, desiredSpec)
if err != nil {
return nil, err
}
if debug {
log.Println("VM reconfigured with", desiredSpec)
}
return task, nil
}
func generateDesiredSpec(cfgs []vmConfig, moVM mo.VirtualMachine) types.VirtualMachineConfigSpec {
var spec types.VirtualMachineConfigSpec
for _, c := range cfgs {
switch c.name {
case "numCPU":
spec.NumCPUs = int32(c.valInt)
case "memoryMB":
spec.MemoryMB = int64(c.valInt)
case "numCoresPerSocket":
spec.NumCoresPerSocket = int32(c.valInt)
case "memoryHotAddEnabled":
spec.MemoryHotAddEnabled = &c.valBool
case "cpuHotRemoveEnabled":
spec.CpuHotRemoveEnabled = &c.valBool
case "cpuHotAddEnabled":
spec.CpuHotAddEnabled = &c.valBool
}
}
// Set ChangeVersion to guard against updates that have happened between when
// configInfo is read and when it is applied.
spec.ChangeVersion = moVM.Config.ChangeVersion
return spec
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment