140 lines
3.9 KiB
Go
140 lines
3.9 KiB
Go
package image
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
|
|
)
|
|
|
|
// CreateFromDiskRequest to create image from existing disk
|
|
type CreateFromDiskRequest struct {
|
|
// Name of the new image
|
|
// Required: true
|
|
Name string `url:"name" json:"name" validate:"required"`
|
|
|
|
// ID of the source disk to create image from
|
|
// Required: true
|
|
DiskID uint64 `url:"disk_id" json:"disk_id" validate:"required"`
|
|
|
|
// Boot type of the image (bios or uefi)
|
|
// Required: true
|
|
// Options:
|
|
// - bios
|
|
// - uefi
|
|
BootType string `url:"boot_type" json:"boot_type" validate:"required,imageBootType"`
|
|
|
|
// Type of the operating system
|
|
// Required: true
|
|
// Options:
|
|
// - linux
|
|
// - windows
|
|
// - unknown
|
|
ImageType string `url:"image_type" json:"image_type" validate:"required,imageType"`
|
|
|
|
// ID of the storage policy to apply
|
|
// Required: true
|
|
StoragePolicyID uint64 `url:"storage_policy_id" json:"storage_policy_id" validate:"required"`
|
|
|
|
// Network interface naming pattern for Linux machines. eth - onboard naming, ens - PCI slot naming.
|
|
// Required: false
|
|
// Default: ens
|
|
// Options:
|
|
// - eth
|
|
// - ens
|
|
NetworkInterfaceNaming string `url:"network_interface_naming,omitempty" json:"network_interface_naming,omitempty" validate:"omitempty,networkInterfaceNaming"`
|
|
|
|
// Whether the image supports hot resize (online resizing)
|
|
// Required: false
|
|
// Default: false
|
|
HotResize interface{} `url:"hot_resize,omitempty" json:"hot_resize,omitempty" validate:"omitempty,isBool"`
|
|
|
|
// Default username for VMs created from this image
|
|
// Required: false
|
|
UserName string `url:"user_name,omitempty" json:"user_name,omitempty"`
|
|
|
|
// Default password for the default user
|
|
// Required: false
|
|
Password string `url:"password,omitempty" json:"password,omitempty"`
|
|
|
|
// Account ID to grant exclusive access to the image
|
|
// Required: false
|
|
AccountID uint64 `url:"account_id,omitempty" json:"account_id,omitempty"`
|
|
|
|
// Target SEP ID where the image will be created. If not specified, the best SEP will be selected based on storage policy.
|
|
// Required: false
|
|
SEPID uint64 `url:"sep_id,omitempty" json:"sep_id,omitempty"`
|
|
|
|
// Target pool name in the SEP. If not specified, the best pool will be selected automatically.
|
|
// Required: false
|
|
PoolName string `url:"pool_name,omitempty" json:"pool_name,omitempty"`
|
|
|
|
// Whether the image is bootable
|
|
// Required: false
|
|
// Default: true
|
|
Bootable interface{} `url:"bootable,omitempty" json:"bootable,omitempty" validate:"omitempty,isBool"`
|
|
}
|
|
|
|
type wrapperCreateFromDiskRequest struct {
|
|
CreateFromDiskRequest
|
|
AsyncMode bool `url:"async_mode" json:"async_mode" validate:"omitempty,isBool"`
|
|
}
|
|
|
|
// CreateFromDisk creates image from existing disk
|
|
func (i Image) CreateFromDisk(ctx context.Context, req CreateFromDiskRequest) (uint64, error) {
|
|
err := validators.ValidateRequest(req)
|
|
if err != nil {
|
|
return 0, validators.ValidationErrors(validators.GetErrors(err))
|
|
}
|
|
|
|
url := "/cloudbroker/image/create_from_disk"
|
|
|
|
aReq := wrapperCreateFromDiskRequest{
|
|
CreateFromDiskRequest: req,
|
|
AsyncMode: false,
|
|
}
|
|
|
|
res, err := i.client.DecortApiCall(ctx, http.MethodPost, url, aReq)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
result, err := strconv.ParseUint(string(res), 10, 64)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
// CreateFromDiskAsync creates image from existing disk in async mode
|
|
func (i Image) CreateFromDiskAsync(ctx context.Context, req CreateFromDiskRequest) (string, error) {
|
|
err := validators.ValidateRequest(req)
|
|
if err != nil {
|
|
return "", validators.ValidationErrors(validators.GetErrors(err))
|
|
}
|
|
|
|
url := "/cloudbroker/image/create_from_disk"
|
|
|
|
aReq := wrapperCreateFromDiskRequest{
|
|
CreateFromDiskRequest: req,
|
|
AsyncMode: true,
|
|
}
|
|
|
|
res, err := i.client.DecortApiCall(ctx, http.MethodPost, url, aReq)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
var resp string
|
|
|
|
err = json.Unmarshal(res, &resp)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return resp, nil
|
|
}
|