You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.0 KiB
85 lines
2.0 KiB
4 months ago
|
package vm
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"encoding/json"
|
||
|
"net/http"
|
||
|
|
||
|
"repository.basistech.ru/BASIS/dynamix-standart-go-sdk/internal/constants"
|
||
|
"repository.basistech.ru/BASIS/dynamix-standart-go-sdk/internal/validators"
|
||
|
"repository.basistech.ru/BASIS/dynamix-standart-go-sdk/pkg/vm/models"
|
||
|
"repository.basistech.ru/BASIS/dynamix-standart-go-sdk/pkg/vm/requests"
|
||
|
)
|
||
|
|
||
|
type wrapperCreateVMRequest struct {
|
||
|
requests.CreateVMRequest
|
||
|
BootDevices []string `url:"boot_device,omitempty"`
|
||
|
Device []string `url:"device,omitempty"`
|
||
|
}
|
||
|
|
||
|
// Create new VM
|
||
|
func (vm VM) Create(ctx context.Context, req requests.CreateVMRequest) (*models.CreateVMResponse, error) {
|
||
|
if len(req.Names) == 0 {
|
||
|
req.Names = []string{req.Name}
|
||
|
}
|
||
|
|
||
|
res, err := vm.CreateRaw(ctx, req)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
item := models.CreateVMResponse{}
|
||
|
|
||
|
err = json.Unmarshal(res, &item)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return &item, nil
|
||
|
}
|
||
|
|
||
|
// CreateRaw return information about created VMas an array of bytes
|
||
|
func (vm VM) CreateRaw(ctx context.Context, req requests.CreateVMRequest) ([]byte, error) {
|
||
|
if err := validators.ValidateRequest(req); err != nil {
|
||
|
return nil, validators.ValidationErrors(validators.GetErrors(err))
|
||
|
}
|
||
|
|
||
|
var bootDevices []string
|
||
|
|
||
|
if req.BootDeviceList != nil && len(req.BootDeviceList) != 0 {
|
||
|
bootDevices = make([]string, 0, len(req.BootDeviceList))
|
||
|
|
||
|
for i := range req.BootDeviceList {
|
||
|
d, err := json.Marshal(req.BootDeviceList[i])
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
bootDevices = append(bootDevices, string(d))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var devices []string
|
||
|
|
||
|
if req.Devices != nil && len(req.Devices) != 0 {
|
||
|
devices = make([]string, 0, len(req.Devices))
|
||
|
|
||
|
for i := range req.Devices {
|
||
|
d, err := json.Marshal(req.Devices[i])
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
devices = append(devices, string(d))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
reqWrapped := wrapperCreateVMRequest{
|
||
|
CreateVMRequest: req,
|
||
|
BootDevices: bootDevices,
|
||
|
Device: devices,
|
||
|
}
|
||
|
|
||
|
url := constants.APIv0 + "/vms"
|
||
|
|
||
|
res, err := vm.client.ApiCall(ctx, http.MethodPost, url, reqWrapped)
|
||
|
return res, err
|
||
|
}
|