Files
decort-golang-sdk/pkg/cloudbroker/compute/disk_add.go

122 lines
3.1 KiB
Go
Raw Normal View History

2022-12-22 17:56:47 +03:00
package compute
import (
"context"
"net/http"
"strconv"
2023-03-24 17:09:30 +03:00
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
2022-12-22 17:56:47 +03:00
)
2023-10-25 17:37:18 +03:00
// DiskAddRequest struct to create and attach disk to compute
2022-12-22 17:56:47 +03:00
type DiskAddRequest struct {
// ID of compute instance
// Required: true
2023-04-28 11:46:58 +03:00
ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"`
2022-12-22 17:56:47 +03:00
// Name for disk
// Required: true
2023-04-28 11:46:58 +03:00
DiskName string `url:"diskName" json:"diskName" validate:"required"`
2022-12-22 17:56:47 +03:00
// Disk size in GB
// Required: true
2023-04-28 11:46:58 +03:00
Size uint64 `url:"size" json:"size" validate:"required"`
// Storage endpoint provider ID
// By default the same with boot disk
// Required: false
SepID uint64 `url:"sepId,omitempty" json:"sepId,omitempty"`
2022-12-22 17:56:47 +03:00
2025-08-29 12:51:25 +03:00
// Storage policy id of compute. The rules of the specified storage policy will be used.
// Required: true
StoragePolicyID uint64 `url:"storage_policy_id" json:"storage_policy_id" validate:"required"`
2022-12-22 17:56:47 +03:00
// Pool name
// By default will be chosen automatically
// 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
// Optional description
// 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
// Specify image id for create disk from template
// Required: false
2023-03-01 19:05:53 +03:00
ImageID uint64 `url:"imageId,omitempty" json:"imageId,omitempty"`
2025-09-19 13:45:58 +03:00
// Desired PCI slot (hex string, e.g. "0x1A")
// Required: false
PCISlot string `url:"pci_slot,omitempty" json:"pci_slot,omitempty"`
// Desired bus number (hex string, e.g. "0x03")
// Required: false
BusNumber string `url:"bus_number,omitempty" json:"bus_number,omitempty"`
2026-01-16 16:50:40 +03:00
// Disk cache mode
// Required: false
Cache string `url:"cache,omitempty" json:"cache,omitempty"`
2026-02-06 17:14:17 +03:00
2026-06-05 17:14:39 +03:00
// Discard
2026-02-06 17:14:17 +03:00
// Required: false
2026-06-05 17:14:39 +03:00
Discard string `url:"discard,omitempty" json:"discard,omitempty"`
// Mount disk in read-only mode
// Required: false
ReadOnly bool `url:"read_only,omitempty" json:"read_only,omitempty"`
}
type wrapperDiskAddRequest struct {
DiskAddRequest
AsyncMode bool `url:"asyncMode"`
2022-12-22 17:56:47 +03:00
}
// DiskAdd creates new disk and attach to compute
func (c Compute) DiskAdd(ctx context.Context, req DiskAddRequest) (uint64, error) {
2023-03-24 17:09:30 +03:00
err := validators.ValidateRequest(req)
2022-12-22 17:56:47 +03:00
if err != nil {
2023-10-25 17:37:18 +03:00
return 0, validators.ValidationErrors(validators.GetErrors(err))
2022-12-22 17:56:47 +03:00
}
2026-06-05 17:14:39 +03:00
reqWrapped := wrapperDiskAddRequest{
DiskAddRequest: req,
AsyncMode: false,
}
2022-12-22 17:56:47 +03:00
url := "/cloudbroker/compute/diskAdd"
2026-06-05 17:14:39 +03:00
res, err := c.client.DecortApiCall(ctx, http.MethodPost, url, reqWrapped)
2022-12-22 17:56:47 +03:00
if err != nil {
return 0, err
}
result, err := strconv.ParseUint(string(res), 10, 64)
if err != nil {
return 0, err
}
return result, nil
}
2026-06-05 17:14:39 +03:00
// DiskAddAsync creates new disk and attach to compute with AsyncMode
func (c Compute) DiskAddAsync(ctx context.Context, req DiskAddRequest) (string, error) {
err := validators.ValidateRequest(req)
if err != nil {
return "", validators.ValidationErrors(validators.GetErrors(err))
}
reqWrapped := wrapperDiskAddRequest{
DiskAddRequest: req,
AsyncMode: true,
}
url := "/cloudbroker/compute/diskAdd"
res, err := c.client.DecortApiCall(ctx, http.MethodPost, url, reqWrapped)
if err != nil {
return "", err
}
return string(res), nil
}