diff --git a/src/router/routerVMs.go b/src/router/routerVMs.go index 909325c7eeb7549a87ac65ba31966ca505694bec..b989a0df814cca9353a63987b12e20650beebc29 100644 --- a/src/router/routerVMs.go +++ b/src/router/routerVMs.go @@ -165,7 +165,7 @@ func (r *RouterVMs)GetFilesImportableVMs(c echo.Context) error { // Creates a VM. // Requires this capability: // User cap: CAP_VM_CREATE. -// curl --cacert ca.pem -X POST https://localhost:1077/vms -H 'Content-Type: application/json' -d '{"name":"Systems Programming 2022","cpus":2,"ram":4096,"nic":"none","template":"Xubuntu_22.04"}' -H "Authorization: Bearer <AccessToken>" +// curl --cacert ca.pem -X POST https://localhost:1077/vms -H 'Content-Type: application/json' -d '{"name":"Systems Programming 2022","cpus":2,"ram":4096,"nic":"none","usbDevs":["1307:0165","1234:abcd"],"template":"Xubuntu_22.04"}' -H "Authorization: Bearer <AccessToken>" func (r *RouterVMs)CreateVM(c echo.Context) error { // Retrieves logged user from context. user, err := getLoggedUser(r.users, c) @@ -279,23 +279,24 @@ func (r *RouterVMs)RebootVM(c echo.Context) error { // Requires to be the VM's owner, or either capability: // User cap: CAP_VM_EDIT_ANY: any VM can be edited. // VM access cap: CAP_VM_EDIT: any of the VMs with this cap for the logged user can be edited. -// curl --cacert ca.pem -X PUT https://localhost:1077/vms/e41f3556-ca24-4658-bd79-8c85bd6bff59 -H 'Content-Type: application/json' -d '{"name":"Edited VM","cpus":1,"ram":2048,"nic":"user"}' -H "Authorization: Bearer <AccessToken>" +// curl --cacert ca.pem -X PUT https://localhost:1077/vms/e41f3556-ca24-4658-bd79-8c85bd6bff59 -H 'Content-Type: application/json' -d '{"name":"Edited VM","cpus":1,"ram":2048,"nic":"user","usbDevs":["1307:0165","1234:abcd"]}' -H "Authorization: Bearer <AccessToken>" func (r *RouterVMs)EditVMByID(c echo.Context) error { return r.performVMAction(c, caps.CAP_VM_EDIT_ANY, caps.CAP_VM_EDIT, func(c echo.Context, vm *vms.VM) error { // Deserializes and validates the client's parameters. // Given these parameters are optional, we can't use a validator on them. // Validation is performed in vm.EditVM() instead. type Parameters struct { - Name string `json:"name"` - Cpus int `json:"cpus"` - Ram int `json:"ram"` // in MB - Nic vms.NicType `json:"nic"` + Name string `json:"name" validate:"required,min=4,max=256"` + Cpus int `json:"cpus" validate:"required,gte=1,lte=16"` + Ram int `json:"ram" validate:"required,gte=512,lte=32768"` + Nic vms.NicType `json:"nic" validate:"required` + UsbDevs []string `json:"usbDevs" validate:"required` } p := new(Parameters) if err := decodeJson(c, &p); err != nil { return echo.NewHTTPError(http.StatusBadRequest, err.Error()) } - if err := r.vms.EditVM(vm.ID, p.Name, p.Cpus, p.Ram, p.Nic); err != nil { + if err := r.vms.EditVM(vm.ID, p.Name, p.Cpus, p.Ram, p.Nic, p.UsbDevs); err != nil { return echo.NewHTTPError(http.StatusBadRequest, err.Error()) } return c.JSONPretty(http.StatusOK, jsonMsg("OK"), " ") diff --git a/src/vms/vms.go b/src/vms/vms.go index f16b70b36997aabc079dd037c38d444ac8cd8e31..68938686eee2516f6a34412692804c6f697a35e8 100644 --- a/src/vms/vms.go +++ b/src/vms/vms.go @@ -284,7 +284,7 @@ func (vms *VMs)IsTemplateUsed(templateID string) bool { } // Edit a VM' specs: name, cpus, ram, nic -func (vms *VMs)EditVM(vmID uuid.UUID, name string, cpus, ram int, nic NicType) error { +func (vms *VMs)EditVM(vmID uuid.UUID, name string, cpus, ram int, nic NicType, usbDevs []string) error { vm, err := vms.getVMUnsafe(vmID) if err != nil { return err @@ -303,6 +303,9 @@ func (vms *VMs)EditVM(vmID uuid.UUID, name string, cpus, ram int, nic NicType) e if nic != "" { vm.Nic = nic } + if usbDevs != nil { + vm.UsbDevs = usbDevs + } if err = vm.validate(); err != nil { return err