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

Add nowait kvp removal form #110

Merged
merged 1 commit into from
Mar 21, 2024
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
32 changes: 24 additions & 8 deletions pkg/hypervctl/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ func (vm *VirtualMachine) SplitAndAddIgnition(keyPrefix string, ignRdr *bytes.Re
}

func (vm *VirtualMachine) AddKeyValuePair(key string, value string) error {
return vm.kvpOperation("AddKvpItems", key, value, "key already exists?")
return vm.kvpOperation("AddKvpItems", key, value, false, "key already exists?")
}

func (vm *VirtualMachine) ModifyKeyValuePair(key string, value string) error {
return vm.kvpOperation("ModifyKvpItems", key, value, "key invalid?")
return vm.kvpOperation("ModifyKvpItems", key, value, false, "key invalid?")
}

func (vm *VirtualMachine) PutKeyValuePair(key string, value string) error {
Expand All @@ -107,7 +107,11 @@ func (vm *VirtualMachine) PutKeyValuePair(key string, value string) error {
}

func (vm *VirtualMachine) RemoveKeyValuePair(key string) error {
return vm.kvpOperation("RemoveKvpItems", key, "", "key invalid?")
return vm.kvpOperation("RemoveKvpItems", key, "", false, "key invalid?")
}

func (vm *VirtualMachine) RemoveKeyValuePairNoWait(key string) error {
return vm.kvpOperation("RemoveKvpItems", key, "", true, "key invalid?")
}

func (vm *VirtualMachine) GetKeyValuePairs() (map[string]string, error) {
Expand Down Expand Up @@ -148,9 +152,10 @@ func (vm *VirtualMachine) GetKeyValuePairs() (map[string]string, error) {
return parseKvpMapXml(s)
}

func (vm *VirtualMachine) kvpOperation(op string, key string, value string, illegalSuggestion string) error {
func (vm *VirtualMachine) kvpOperation(op string, key string, value string, nowait bool, illegalSuggestion string) error {
var service *wmiext.Service
var vsms, job *wmiext.Instance
var ret int32
var err error

if service, err = NewLocalHyperVService(); err != nil {
Expand All @@ -172,15 +177,26 @@ func (vm *VirtualMachine) kvpOperation(op string, key string, value string, ille
execution := vsms.BeginInvoke(op).
In("TargetSystem", vm.Path()).
In("DataItems", []string{itemStr}).
Execute()
Execute().
Out("ReturnValue", &ret).
Out("Job", &job)

if err := execution.Out("Job", &job).End(); err != nil {
if err := execution.End(); err != nil {
return fmt.Errorf("%s execution failed: %w", op, err)
}

err = translateKvpError(wmiext.WaitJob(service, job), illegalSuggestion)
defer job.Close()
return err
if ret == 0 || (nowait && ret == 4096) {
return nil
}

if ret == 4096 {
err = wmiext.WaitJob(service, job)
} else {
err = &wmiext.JobError{ErrorCode: int(ret)}
}

return translateKvpError(err, illegalSuggestion)
}

func waitVMResult(res int32, service *wmiext.Service, job *wmiext.Instance, errorMsg string, translate func(int) error) error {
Expand Down