Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed SetHardwareOptions to fetch the hardware version from QueryConfigOption instead of the default properties #1159

Merged
merged 2 commits into from
Aug 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 45 additions & 8 deletions vsphere/internal/helper/virtualmachine/virtual_machine_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/view"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
)
Expand Down Expand Up @@ -213,6 +214,32 @@ func Properties(vm *object.VirtualMachine) (*mo.VirtualMachine, error) {
return &props, nil
}

// ConfigOptions is a convenience method that wraps fetching the VirtualMachine ConfigOptions
// as returned by QueryConfigOption.
func ConfigOptions(vm *object.VirtualMachine) (*types.VirtualMachineConfigOption, error) {

// First grab the properties so that we can sneak the EnvironmentBrowser out of it
props, err := Properties(vm)
if err != nil {
return nil, err
}

// Make a context so we can timeout according to the provider configuration
ctx, cancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout)
defer cancel()

// Build a request for the config option, and then query for configuration options
log.Printf("[DEBUG] Fetching configuration options for VM %q", vm.InventoryPath)
request := types.QueryConfigOption{This: props.EnvironmentBrowser}

response, err := methods.QueryConfigOption(ctx, vm.Client(), &request)
if err != nil {
return nil, err
}

return response.Returnval, nil
}

// WaitForGuestIP waits for a virtual machine to have an IP address.
//
// The timeout is specified in minutes. If zero or a negative value is passed,
Expand Down Expand Up @@ -889,25 +916,35 @@ func GetHardwareVersionNumber(vstring string) int {
// SetHardwareVersion sets the virtual machine's hardware version. The virtual
// machine must be powered off, and the version can only be increased.
func SetHardwareVersion(vm *object.VirtualMachine, target int) error {
// First get current and target versions and validate
tv := GetHardwareVersionID(target)
vprops, err := Properties(vm)

// First query for the configuration options of the vm
copts, err := ConfigOptions(vm)
if err != nil {
return err
}
cv := vprops.Config.Version
// Skip the rest if there is no version change.
if cv == tv || tv == "" {

// Now we can grab its version to compare against the target
current := int(copts.HardwareOptions.HwVersion)
log.Printf("[DEBUG] Found current hardware version: %d", current)

// If the hardware version matches, then we're done here and can leave.
if current == target || target == 0 {
return nil
}
if err := ValidateHardwareVersion(GetHardwareVersionNumber(cv), target); err != nil {

// Otherwise we need to validate it to ensure we're not downgrading
// the hardware version.
log.Printf("[DEBUG] Validating the target hardware version: %d", target)
if err := ValidateHardwareVersion(current, target); err != nil {
return err
}

// We can now proceed to upgrade the hardware version on the vm
ctx, cancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout)
defer cancel()

task, err := vm.UpgradeVM(ctx, tv)
log.Printf("[DEBUG] Upgrading VM from hw version %d to hw version %d", current, target)
task, err := vm.UpgradeVM(ctx, GetHardwareVersionID(target))
_, err = task.WaitForResult(ctx, nil)
return err
}
Expand Down