2023-04-20 11:17:35 +03:00
|
|
|
package vgpu
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-10-09 16:23:35 +03:00
|
|
|
"encoding/json"
|
2023-04-20 11:17:35 +03:00
|
|
|
"net/http"
|
|
|
|
|
"strconv"
|
2026-08-21 17:52:28 +04:00
|
|
|
|
|
|
|
|
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
|
2023-04-20 11:17:35 +03:00
|
|
|
)
|
|
|
|
|
|
2023-10-25 17:37:18 +03:00
|
|
|
// CreateRequest struct for creating VGPU
|
2023-04-20 11:17:35 +03:00
|
|
|
type CreateRequest struct {
|
|
|
|
|
// ID of pGPU
|
|
|
|
|
// Required: true
|
2026-08-21 17:52:28 +04:00
|
|
|
PGPUID uint64 `url:"pgpu_id" json:"pgpu_id" validate:"required"`
|
2023-04-20 11:17:35 +03:00
|
|
|
|
2026-10-09 16:23:35 +03:00
|
|
|
// Name
|
|
|
|
|
// Required: true
|
|
|
|
|
Name string `url:"name" json:"name" validate:"required"`
|
|
|
|
|
|
2023-04-20 11:17:35 +03:00
|
|
|
// Virtual profile id
|
|
|
|
|
// Required: false
|
2026-08-21 17:52:28 +04:00
|
|
|
ProfileID string `url:"profile_id,omitempty" json:"profile_id,omitempty"`
|
|
|
|
|
|
|
|
|
|
// MDEV Type
|
|
|
|
|
// Required: false
|
|
|
|
|
MDEVType string `url:"mdev_type,omitempty" json:"mdev_type,omitempty"`
|
|
|
|
|
|
|
|
|
|
// PCI Address
|
|
|
|
|
// Required: false
|
|
|
|
|
PCIAddress string `url:"pci_address,omitempty" json:"pci_address,omitempty"`
|
2023-04-20 11:17:35 +03:00
|
|
|
|
2026-08-21 17:52:28 +04:00
|
|
|
// Account IDs
|
2023-04-20 11:17:35 +03:00
|
|
|
// Required: false
|
2026-08-21 17:52:28 +04:00
|
|
|
AccountIDs []uint64 `url:"account_ids,omitempty" json:"account_ids,omitempty"`
|
2023-04-20 11:17:35 +03:00
|
|
|
}
|
|
|
|
|
|
2026-10-09 16:23:35 +03:00
|
|
|
type asyncWrapperCreateRequest struct {
|
|
|
|
|
CreateRequest
|
|
|
|
|
AsyncMode bool `url:"async_mode"`
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-20 11:17:35 +03:00
|
|
|
// Create creates VGPU
|
|
|
|
|
func (v VGPU) Create(ctx context.Context, req CreateRequest) (uint64, error) {
|
|
|
|
|
err := validators.ValidateRequest(req)
|
|
|
|
|
if err != nil {
|
2023-10-25 17:37:18 +03:00
|
|
|
return 0, validators.ValidationErrors(validators.GetErrors(err))
|
2023-04-20 11:17:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
url := "/cloudbroker/vgpu/create"
|
|
|
|
|
|
2026-10-09 16:23:35 +03:00
|
|
|
aReq := asyncWrapperCreateRequest{
|
|
|
|
|
CreateRequest: req,
|
|
|
|
|
AsyncMode: false,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res, err := v.client.DecortApiCall(ctx, http.MethodPost, url, aReq)
|
2023-04-20 11:17:35 +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-10-09 16:23:35 +03:00
|
|
|
|
|
|
|
|
// CreateAsync creates VGPU in async mode
|
|
|
|
|
func (v VGPU) CreateAsync(ctx context.Context, req CreateRequest) (string, error) {
|
|
|
|
|
err := validators.ValidateRequest(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", validators.ValidationErrors(validators.GetErrors(err))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
url := "/cloudbroker/vgpu/create"
|
|
|
|
|
|
|
|
|
|
aReq := asyncWrapperCreateRequest{
|
|
|
|
|
CreateRequest: req,
|
|
|
|
|
AsyncMode: true,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res, err := v.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
|
|
|
|
|
|
|
|
|
|
}
|