Files
decort-golang-sdk/pkg/cloudapi/kvmx86/create.go

289 lines
9.0 KiB
Go
Raw Normal View History

2022-10-03 16:56:47 +03:00
package kvmx86
import (
"context"
2023-07-13 18:32:21 +03:00
"encoding/json"
2022-10-03 16:56:47 +03:00
"net/http"
"strconv"
2023-03-24 17:09:30 +03:00
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
2022-10-03 16:56:47 +03:00
)
2023-06-30 11:21:47 +03:00
type Interface struct {
// Network type
// Should be one of:
// - VINS
// - EXTNET
2024-05-31 13:35:39 +03:00
// - VFNIC
2024-11-12 12:51:21 +03:00
// - DPDK
2025-07-15 17:39:18 +03:00
// - EMPTY
// - SDN
// - TRUNK
2024-05-31 13:35:39 +03:00
NetType string `url:"netType" json:"netType" validate:"required,kvmx86NetType"`
2023-06-30 11:21:47 +03:00
// Network ID for connect to,
// for EXTNET - external network ID,
// for VINS - VINS ID,
NetID uint64 `url:"netId" json:"netId" validate:"required"`
// IP address to assign to this VM when connecting to the specified network
// Required: false
IPAddr string `url:"ipAddr,omitempty" json:"ipAddr,omitempty"`
2024-12-04 11:50:22 +03:00
// Maximum transmission unit, must be 1-9216
// Used only to DPDK net type
// Required: false
MTU uint64 `url:"mtu,omitempty" json:"mtu,omitempty" validate:"omitempty,mtu"`
2025-04-09 11:21:07 +03:00
2025-12-23 17:39:58 +03:00
// Net mask
// Used only to DPDK or VFNIC net type
// Required: false
NetMask uint64 `url:"netMask,omitempty" json:"netMask,omitempty"`
2025-04-09 11:21:07 +03:00
// MAC address to assign to this VM when connecting to the specified network
// Required: false
MAC string `url:"mac,omitempty" json:"mac,omitempty" validate:"omitempty"`
2025-07-15 17:39:18 +03:00
// SDN interface id
// Required: false
SDNInterfaceID string `url:"sdn_interface_id,omitempty" json:"sdn_interface_id,omitempty"`
2025-08-29 12:51:25 +03:00
// List of security group IDs to assign to this interface
// Required: false
SecGroups []uint64 `url:"security_groups,omitempty" json:"security_groups,omitempty"`
// Flag indicating whether security groups are enabled for this interface
// Required: false
EnableSecGroups bool `url:"enable_secgroups,omitempty" json:"enable_secgroups,omitempty"`
// Flag indicating whether this interface is enabled (only for VINS, EXTNET, DPDK, SDN, TRUNK)
// Required: false
Enabled interface{} `url:"enabled,omitempty" json:"enabled,omitempty" validate:"omitempty,isBool"`
2023-06-30 11:21:47 +03:00
}
2024-04-16 14:26:06 +03:00
// DataDisk detailed struct for DataDisks field in CreateRequest and CreateBlankRequest
type DataDisk struct {
// Name for disk
// Required: true
DiskName string `url:"diskName" json:"diskName" validate:"required"`
// Disk size in GB
// Required: true
Size uint64 `url:"size" json:"size" validate:"required"`
2025-08-29 12:51:25 +03:00
// Storage policy id of disk. The rules of the specified storage policy will be used.
// Required: true
StoragePolicyID uint64 `url:"storage_policy_id" json:"storage_policy_id" validate:"required"`
2024-04-16 14:26:06 +03:00
// Storage endpoint provider ID
// By default the same with boot disk
// Required: false
SepID uint64 `url:"sepId,omitempty" json:"sepId,omitempty"`
// Pool name
// By default will be chosen automatically
// Required: false
Pool string `url:"pool,omitempty" json:"pool,omitempty"`
// Optional description
// Required: false
Description string `url:"desc,omitempty" json:"desc,omitempty"`
// Specify image id for create disk from template
// Required: false
ImageID uint64 `url:"imageId,omitempty" json:"imageId,omitempty"`
}
2023-10-25 17:37:18 +03:00
// CreateRequest struct to create KVM x86 VM
2022-10-03 16:56:47 +03:00
type CreateRequest struct {
2022-12-22 17:56:47 +03:00
// ID of the resource group, which will own this VM
// Required: true
2023-03-24 17:09:30 +03:00
RGID uint64 `url:"rgId" json:"rgId" validate:"required"`
2022-12-22 17:56:47 +03:00
// Name of this VM.
// Must be unique among all VMs (including those in DELETED state) in target resource group
// Required: true
2023-03-24 17:09:30 +03:00
Name string `url:"name" json:"name" validate:"required"`
2022-12-22 17:56:47 +03:00
// Number CPUs to allocate to this VM
// Required: true
2023-03-24 17:09:30 +03:00
CPU uint64 `url:"cpu" json:"cpu" validate:"required"`
2022-12-22 17:56:47 +03:00
// Volume of RAM in MB to allocate to this VM
// Required: true
2023-03-24 17:09:30 +03:00
RAM uint64 `url:"ram" json:"ram" validate:"required"`
2022-12-22 17:56:47 +03:00
2025-08-29 12:51:25 +03:00
// Storage policy id of сompute. The rules of the specified storage policy will be used.
// Required: true
StoragePolicyID uint64 `url:"storage_policy_id" json:"storage_policy_id" validate:"required"`
2024-05-31 13:35:39 +03:00
// If True, the imageId, bootDisk, sepId, pool parameters are ignored and the compute is created without a boot disk in the stopped state
// Required: false
WithoutBootDisk bool `url:"withoutBootDisk" json:"withoutBootDisk"`
2022-12-22 17:56:47 +03:00
// ID of the OS image to base this VM on;
// Could be boot disk image or CD-ROM image
2024-05-31 13:35:39 +03:00
// Required: false
ImageID uint64 `url:"imageId,omitempty" json:"imageId,omitempty"`
2022-12-22 17:56:47 +03:00
2025-09-11 15:56:44 +03:00
// The OS version that will be installed on the virtual machine
// Required: false
OSVersion string `url:"os_version,omitempty" json:"os_version,omitempty"`
2022-12-22 17:56:47 +03:00
// Size of the boot disk in GB
// Required: false
2023-03-01 19:05:53 +03:00
BootDisk uint64 `url:"bootDisk,omitempty" json:"bootDisk,omitempty"`
2022-12-22 17:56:47 +03:00
// ID of SEP to create boot disk on.
// Uses images SEP ID if not set
// Required: false
2023-03-01 19:05:53 +03:00
SepID uint64 `url:"sepId,omitempty" json:"sepId,omitempty"`
2022-12-22 17:56:47 +03:00
// Pool to use if sepId is set, can be also empty if needed to be chosen by system
// Required: false
2023-03-01 19:05:53 +03:00
Pool string `url:"pool,omitempty" json:"pool,omitempty"`
2022-12-22 17:56:47 +03:00
2024-04-16 14:26:06 +03:00
// Slice of structs with data disk description. Each disk has parameters: required - diskName, size; optional - sepId, pool, desc and imageId.
// If not specified, compute will be created without disks.
// To create compute without disks, pass initialized empty slice .
// Required: false
DataDisks []DataDisk `url:"-" json:"dataDisks,omitempty" validate:"omitempty,dive"`
2023-06-30 11:21:47 +03:00
// Slice of structs with net interface description.
2023-07-24 15:13:04 +03:00
// If not specified, compute will be created with default interface from RG.
// To create compute without interfaces, pass initialized empty slice .
2023-07-13 15:28:07 +03:00
// Required: false
2023-07-21 15:14:10 +03:00
Interfaces []Interface `url:"-" json:"interfaces,omitempty" validate:"omitempty,dive"`
2022-12-22 17:56:47 +03:00
// Input data for cloud-init facility
// Required: false
2023-03-01 19:05:53 +03:00
Userdata string `url:"userdata,omitempty" json:"userdata,omitempty"`
2022-12-22 17:56:47 +03:00
// Text description of this VM
// Required: false
2023-03-01 19:05:53 +03:00
Description string `url:"desc,omitempty" json:"desc,omitempty"`
2022-12-22 17:56:47 +03:00
// Start VM upon success
// Required: false
2023-09-25 19:11:33 +03:00
Start bool `url:"start" json:"start"`
2022-12-22 17:56:47 +03:00
// System name
// Required: false
2023-03-01 19:05:53 +03:00
IS string `url:"IS,omitempty" json:"IS,omitempty"`
2022-12-22 17:56:47 +03:00
// Compute purpose
// Required: false
2023-03-01 19:05:53 +03:00
IPAType string `url:"ipaType,omitempty" json:"ipaType,omitempty"`
2023-07-21 15:14:10 +03:00
// Custom fields for compute. Must be a dict
// Required: false
CustomFields string `url:"customFields,omitempty" json:"customFields,omitempty"`
2024-05-31 13:35:39 +03:00
// Rule for VM placement with NUMA affinity.
// Possible values - none (placement without NUMA affinity),
// strict (strictly with NUMA affinity, if not possible - do not start VM),
// loose (use NUMA affinity if possible)
// Required: false
// Default: none
NumaAffinity string `url:"numaAffinity,omitempty" json:"numaAffinity,omitempty" validate:"omitempty,numaAffinity"`
// Run VM on dedicated CPUs. To use this feature, the system must be pre-configured by allocating CPUs on the physical node
// Required: false
// Default: false
CPUPin bool `url:"cpupin" json:"cpupin"`
2024-11-12 12:51:21 +03:00
// Type of the emulated system, Q35 or i440fx
// Required: false
2025-12-08 16:16:35 +03:00
// Default: Q35
2024-11-22 12:09:50 +03:00
Chipset string `url:"chipset,omitempty" json:"chipset,omitempty" validate:"omitempty,chipset"`
2024-11-12 12:51:21 +03:00
2024-05-31 13:35:39 +03:00
// Use Huge Pages to allocate RAM of the virtual machine. The system must be pre-configured by allocating Huge Pages on the physical node
// Required: false
// Default: false
HPBacked bool `url:"hpBacked" json:"hpBacked"`
2025-02-07 11:11:43 +03:00
// Recommended isolated CPUs. Field is ignored if compute.cpupin=False or compute.pinned=False
// Required: false
PreferredCPU []int64 `url:"preferredCpu,omitempty" json:"preferredCpu,omitempty" validate:"omitempty,preferredCPU"`
2025-07-15 17:39:18 +03:00
// Zone ID
// Required: false
ZoneID uint64 `url:"zoneId,omitempty" json:"zoneId,omitempty"`
2022-10-03 16:56:47 +03:00
}
2024-04-16 14:26:06 +03:00
// GetRAM returns RAM field values
func (r CreateRequest) GetRAM() map[string]uint64 {
res := make(map[string]uint64, 1)
res["RAM"] = r.RAM
return res
}
2023-07-13 18:32:21 +03:00
type wrapperCreateRequest struct {
CreateRequest
Interfaces []string `url:"interfaces,omitempty"`
2024-04-16 14:26:06 +03:00
DataDisks []string `url:"dataDisks,omitempty"`
2023-07-13 18:32:21 +03:00
}
2022-12-22 17:56:47 +03:00
// Create creates KVM x86 VM based on specified OS image
2022-10-03 16:56:47 +03:00
func (k KVMX86) Create(ctx context.Context, req CreateRequest) (uint64, error) {
2023-03-24 17:09:30 +03:00
err := validators.ValidateRequest(req)
2022-10-03 16:56:47 +03:00
if err != nil {
2023-10-25 17:37:18 +03:00
return 0, validators.ValidationErrors(validators.GetErrors(err))
2022-10-03 16:56:47 +03:00
}
2023-07-24 15:13:04 +03:00
var interfaces []string
2023-07-13 18:32:21 +03:00
2023-07-24 15:13:04 +03:00
if req.Interfaces != nil && len(req.Interfaces) != 0 {
interfaces = make([]string, 0, len(req.Interfaces))
for i := range req.Interfaces {
b, err := json.Marshal(req.Interfaces[i])
if err != nil {
return 0, err
}
2023-07-13 18:32:21 +03:00
2023-07-24 15:13:04 +03:00
interfaces = append(interfaces, string(b))
}
} else if req.Interfaces != nil && len(req.Interfaces) == 0 {
interfaces = []string{"[]"}
2023-07-13 18:32:21 +03:00
}
2024-04-16 14:26:06 +03:00
var dataDisks []string
if req.DataDisks != nil && len(req.DataDisks) != 0 {
dataDisks = make([]string, 0, len(req.DataDisks))
for i := range req.DataDisks {
b, err := json.Marshal(req.DataDisks[i])
if err != nil {
return 0, err
}
dataDisks = append(dataDisks, string(b))
}
}
2023-07-13 18:32:21 +03:00
reqWrapped := wrapperCreateRequest{
CreateRequest: req,
Interfaces: interfaces,
2024-04-16 14:26:06 +03:00
DataDisks: dataDisks,
2023-07-13 18:32:21 +03:00
}
2022-12-22 17:56:47 +03:00
url := "/cloudapi/kvmx86/create"
2022-10-03 16:56:47 +03:00
2023-07-13 18:32:21 +03:00
res, err := k.client.DecortApiCall(ctx, http.MethodPost, url, reqWrapped)
2022-10-03 16:56:47 +03:00
if err != nil {
return 0, err
}
result, err := strconv.ParseUint(string(res), 10, 64)
if err != nil {
return 0, err
}
2022-12-22 17:56:47 +03:00
return result, nil
2022-10-03 16:56:47 +03:00
}