feat(region,host): create or update server add machine option (#12607)

This commit is contained in:
Zexi Li
2021-11-04 22:27:12 +08:00
committed by GitHub
parent 515f057e58
commit 3a258e01b3
5 changed files with 37 additions and 2 deletions
+4
View File
@@ -396,6 +396,10 @@ type ServerCreateInput struct {
// emulate: BIOS, UEFI
Bios string `json:"bios"`
// Machine类型
// emulate: pc, q35
Machine string `json:"machine"`
// 启动顺序
// c: cdrome
// d: disk
+5
View File
@@ -176,6 +176,11 @@ const (
CPU_MODE_HOST = "host"
)
const (
VM_MACHINE_TYPE_PC = "pc"
VM_MACHINE_TYPE_Q35 = "q35"
)
var VM_RUNNING_STATUS = []string{VM_START_START, VM_STARTING, VM_RUNNING, VM_BLOCK_STREAM, VM_BLOCK_STREAM_FAIL}
var VM_CREATING_STATUS = []string{VM_CREATE_NETWORK, VM_CREATE_DISK, VM_START_DEPLOY, VM_DEPLOYING}
@@ -255,7 +255,29 @@ func (self *SVirtualizedGuestDriver) RequestStopGuestForDelete(ctx context.Conte
return nil
}
func (self *SVirtualizedGuestDriver) ValidateMachineType(machine string) error {
if !utils.IsInStringArray(machine, []string{api.VM_MACHINE_TYPE_PC, api.VM_MACHINE_TYPE_Q35}) {
return httperrors.NewBadRequestError("Invalid machine %q", machine)
}
return nil
}
func (self *SVirtualizedGuestDriver) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, input *api.ServerCreateInput) (*api.ServerCreateInput, error) {
if input.Machine != "" {
if err := self.ValidateMachineType(input.Machine); err != nil {
return nil, err
}
}
return input, nil
}
func (self *SVirtualizedGuestDriver) ValidateUpdateData(ctx context.Context, userCred mcclient.TokenCredential, input *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
machine, _ := input.GetString("machine")
if machine != "" {
if err := self.ValidateMachineType(machine); err != nil {
return nil, err
}
}
return input, nil
}
+3 -2
View File
@@ -29,6 +29,7 @@ import (
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/utils"
api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/hostman/options"
"yunion.io/x/onecloud/pkg/hostman/storageman"
"yunion.io/x/onecloud/pkg/util/fileutils2"
@@ -120,7 +121,7 @@ func (s *SKVMGuestInstance) isWindows10() bool {
func (s *SKVMGuestInstance) getMachine() string {
machine, err := s.Desc.GetString("machine")
if err != nil {
machine = "pc"
machine = api.VM_MACHINE_TYPE_PC
}
return machine
}
@@ -134,7 +135,7 @@ func (s *SKVMGuestInstance) getBios() string {
}
func (s *SKVMGuestInstance) isQ35() bool {
return s.getMachine() == "q35"
return s.getMachine() == api.VM_MACHINE_TYPE_Q35
}
func (s *SKVMGuestInstance) GetVdiProtocol() string {
+3
View File
@@ -365,6 +365,7 @@ type ServerCreateOptionalOptions struct {
Vga string `help:"VGA driver" choices:"std|vmware|cirrus|qxl"`
Vdi string `help:"VDI protocool" choices:"vnc|spice"`
Bios string `help:"BIOS" choices:"BIOS|UEFI"`
Machine string `help:"Machine type" choices:"pc|q35"`
Desc string `help:"Description" metavar:"<DESCRIPTION>" json:"description"`
Boot string `help:"Boot device" metavar:"<BOOT_DEVICE>" choices:"disk|cdrom" json:"-"`
EnableCloudInit bool `help:"Enable cloud-init service"`
@@ -468,6 +469,7 @@ func (opts *ServerCreateOptionalOptions) OptionalParams() (*computeapi.ServerCre
Vga: opts.Vga,
Vdi: opts.Vdi,
Bios: opts.Bios,
Machine: opts.Machine,
ShutdownBehavior: opts.ShutdownBehavior,
AutoStart: opts.AutoStart,
Duration: opts.Duration,
@@ -583,6 +585,7 @@ type ServerUpdateOptions struct {
Boot string `help:"Boot device" choices:"disk|cdrom"`
Delete string `help:"Lock server to prevent from deleting" choices:"enable|disable" json:"-"`
ShutdownBehavior string `help:"Behavior after VM server shutdown" choices:"stop|terminate"`
Machine string `help:"Machine type" choices:"q35|pc"`
}
func (opts *ServerUpdateOptions) Params() (jsonutils.JSONObject, error) {