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

rest: Isolate Code-Hex/vz code from the rest code #52

Merged
merged 1 commit into from
Jul 10, 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
4 changes: 3 additions & 1 deletion cmd/vfkit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/crc-org/vfkit/pkg/cmdline"
"github.com/crc-org/vfkit/pkg/config"
"github.com/crc-org/vfkit/pkg/rest"
restvf "github.com/crc-org/vfkit/pkg/rest/vf"
"github.com/crc-org/vfkit/pkg/vf"
"github.com/docker/go-units"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -133,7 +134,8 @@ func runVFKit(vmConfig *config.VirtualMachine, opts *cmdline.Options) error {

// Do not enable the rests server if user sets scheme to None
if opts.RestfulURI != cmdline.DefaultRestfulURI {
srv, err := rest.NewServer(rest.NewVzVirtualMachine(vm, vzVMConfig), opts.RestfulURI)
restVM := restvf.NewVzVirtualMachine(vm, vzVMConfig)
srv, err := rest.NewServer(restVM, restVM, opts.RestfulURI)
if err != nil {
return err
}
Expand Down
54 changes: 9 additions & 45 deletions pkg/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package rest
import (
"errors"
"fmt"
"net/http"
"net/url"
"strings"

"github.com/crc-org/vfkit/pkg/cmdline"
"github.com/crc-org/vfkit/pkg/rest/define"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -73,7 +71,7 @@ func (v *VFKitService) Start() {
}

// NewServer creates a new restful service
func NewServer(vm *VzVirtualMachine, endpoint string) (*VFKitService, error) {
func NewServer(inspector VirtualMachineInspector, stateHandler VirtualMachineStateHandler, endpoint string) (*VFKitService, error) {
r := gin.Default()
ep, err := NewEndpoint(endpoint)
if err != nil {
Expand All @@ -85,53 +83,19 @@ func NewServer(vm *VzVirtualMachine, endpoint string) (*VFKitService, error) {
}

// Handlers for the restful service. This is where endpoints are defined.
r.GET("/vm/state", vm.getVMState)
r.POST("/vm/state", vm.setVMState)
r.GET("/vm/inspect", vm.inspect)
r.GET("/vm/state", stateHandler.GetVMState)
r.POST("/vm/state", stateHandler.SetVMState)
r.GET("/vm/inspect", inspector.Inspect)
return &s, nil
}

// inspect returns information about the virtual machine like hw resources
// and devices
func (vm *VzVirtualMachine) inspect(c *gin.Context) {
ii := define.InspectResponse{
// TODO complete me
CPUs: 1,
Memory: 2048,
//Devices: vm.Devices,
}
c.JSON(http.StatusOK, ii)
}

// 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()})
type VirtualMachineInspector interface {
Inspect(c *gin.Context)
}

// setVMState requests a state change on a virtual machine. At this time only
// the following states are valid:
// Pause - pause a running machine
// Resume - resume a paused machine
// Stop - stops a running machine
// HardStop - forceably stops a running machine
func (vm *VzVirtualMachine) setVMState(c *gin.Context) {
var (
s define.VMState
)

if err := c.ShouldBindJSON(&s); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

response := vm.ChangeState(define.StateChange(s.State))
if response != nil {
logrus.Errorf("failed action %s: %q", s.State, response)
c.JSON(http.StatusInternalServerError, gin.H{"error": response.Error()})
return
}
c.Status(http.StatusAccepted)
type VirtualMachineStateHandler interface {
GetVMState(c *gin.Context)
SetVMState(c *gin.Context)
}

// parseRestfulURI validates the input URI and returns an URL object
Expand Down
File renamed without changes.
62 changes: 62 additions & 0 deletions pkg/rest/vf/vm_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package rest

import (
"net/http"

"github.com/Code-Hex/vz/v3"
"github.com/crc-org/vfkit/pkg/rest/define"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)

type VzVirtualMachine struct {
VzVM *vz.VirtualMachine
config *vz.VirtualMachineConfiguration
}

func NewVzVirtualMachine(vm *vz.VirtualMachine, config *vz.VirtualMachineConfiguration) *VzVirtualMachine {
return &VzVirtualMachine{config: config, VzVM: vm}
}

// inspect returns information about the virtual machine like hw resources
// and devices
func (vm *VzVirtualMachine) Inspect(c *gin.Context) {
ii := define.InspectResponse{
// TODO complete me
CPUs: 1,
Memory: 2048,
//Devices: vm.Devices,
}
c.JSON(http.StatusOK, ii)
}

// 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()})
}

// setVMState requests a state change on a virtual machine. At this time only
// the following states are valid:
// Pause - pause a running machine
// Resume - resume a paused machine
// Stop - stops a running machine
// HardStop - forceably stops a running machine
func (vm *VzVirtualMachine) SetVMState(c *gin.Context) {
var (
s define.VMState
)

if err := c.ShouldBindJSON(&s); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

response := vm.ChangeState(define.StateChange(s.State))
if response != nil {
logrus.Errorf("failed action %s: %q", s.State, response)
c.JSON(http.StatusInternalServerError, gin.H{"error": response.Error()})
return
}
c.Status(http.StatusAccepted)
}
14 changes: 0 additions & 14 deletions pkg/rest/vm_config.go

This file was deleted.

Loading