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

feat(rest): add can* params in GET /vm/state #63

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
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
8 changes: 5 additions & 3 deletions doc/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,17 +351,19 @@ None
Used to obtain the state of the virtual machine that is being run by VFKit.

GET `/vm/state`
Response: {"state": "string"}
Response: { "state": string, "canStart": bool, "canPause": bool, "canResume": bool, "canStop": bool, "canHardStop": bool }

> `canHardStop` is only supported on macOS 12 and newer, false will always be returned on older versions.

### Change VM State

Change the state of the virtual machine. Valid states are:
* Hardstop
* HardStop
* Pause
* Resume
* Stop

POST `/vm/state` {"new_state": "new value"}
POST `/vm/state` { "state": "new value"}

Response: http 200

Expand Down
20 changes: 20 additions & 0 deletions pkg/rest/vf/state_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,23 @@ func (vm *VzVirtualMachine) HardStop() error {
logrus.Debug("force stopping machine")
return vm.VzVM.Stop()
}

func (vm *VzVirtualMachine) CanStart() bool {
return vm.VzVM.CanStart()
}

func (vm *VzVirtualMachine) CanPause() bool {
return vm.VzVM.CanPause()
}

func (vm *VzVirtualMachine) CanResume() bool {
return vm.VzVM.CanResume()
}

func (vm *VzVirtualMachine) CanStop() bool {
return vm.VzVM.CanRequestStop()
}

func (vm *VzVirtualMachine) CanHardStop() bool {
return vm.VzVM.CanStop()
}
9 changes: 8 additions & 1 deletion pkg/rest/vf/vm_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ func (vm *VzVirtualMachine) Inspect(c *gin.Context) {
// getVMState retrieves the current vm state
func (vm *VzVirtualMachine) GetVMState(c *gin.Context) {
current := vm.GetState()
c.JSON(http.StatusOK, gin.H{"state": current.String()})
c.JSON(http.StatusOK, gin.H{
"state": current.String(),
"canStart": vm.CanStart(),
"canPause": vm.CanPause(),
"canResume": vm.CanResume(),
"canStop": vm.CanStop(),
"canHardStop": vm.CanHardStop(),
BlackHole1 marked this conversation as resolved.
Show resolved Hide resolved
})
}

// setVMState requests a state change on a virtual machine. At this time only
Expand Down
Loading