Files
decort-golang-sdk/pkg/cloudbroker/vins/create_in_account.go
2026-09-15 17:35:01 +03:00

133 lines
3.6 KiB
Go

package vins
import (
"context"
"encoding/json"
"net/http"
"strconv"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/constants"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators"
)
type Route struct {
// Destination network
Destination string `url:"destination" json:"destination" validate:"required"`
//Destination network mask in 255.255.255.255 format
Netmask string `url:"netmask" json:"netmask" validate:"required"`
//Next hop host, IP address from ViNS ID free IP pool
Gateway string `url:"gateway" json:"gateway" validate:"required"`
}
// CreateInAccountRequest struct to create VINS in account
type CreateInAccountRequest struct {
// VINS name
// Required: true
Name string `url:"name" json:"name" validate:"required"`
// ID of account
// Required: true
AccountID uint64 `url:"accountId" json:"accountId" validate:"required"`
// Grid ID
// Required: false
GID uint64 `url:"gid,omitempty" json:"gid,omitempty"`
// Private network IP CIDR
// Required: false
IPCIDR string `url:"ipcidr,omitempty" json:"ipcidr,omitempty"`
// Description
// Required: false
Description string `url:"desc,omitempty" json:"desc,omitempty"`
// Number of pre created reservations
// Required: false
PreReservationsNum uint64 `url:"preReservationsNum,omitempty" json:"preReservationsNum,omitempty"`
// List of DNS ip address
// Required: false
DNSList []string `url:"dnsList" json:"dnsList,omitempty"`
// List of static routes, each item must have destination, netmask, and gateway fields
// Required: false
Routes []Route `url:"-" json:"routes,omitempty" validate:"omitempty,dive"`
// Zone ID
// Required: false
ZoneID uint64 `url:"zoneId,omitempty" json:"zoneId,omitempty"`
// Enable security groups for VINS
// Required: false
// Default: false
EnableSecGroups interface{} `url:"enable_secgroups,omitempty" json:"enable_secgroups,omitempty" validate:"omitempty,isBool"`
// Flag indicating whether default gateway is enabled for this VINS
// Required: false
// Default: true
EnableDefaultGateway interface{} `url:"enable_default_gateway,omitempty" json:"enable_default_gateway,omitempty" validate:"omitempty,isBool"`
}
type asyncWrapperCreateInAccountRequest struct {
CreateInAccountRequest
AsyncMode bool `url:"asyncMode" json:"asyncMode"`
}
// CreateInAccount creates VINS in account level
func (v VINS) CreateInAccount(ctx context.Context, req CreateInAccountRequest) (uint64, error) {
err := validators.ValidateRequest(req)
if err != nil {
return 0, validators.ValidationErrors(validators.GetErrors(err))
}
url := "/cloudbroker/vins/createInAccount"
syncReq := asyncWrapperCreateInAccountRequest{
CreateInAccountRequest: req,
AsyncMode: false,
}
res, err := v.client.DecortApiCallCtype(ctx, http.MethodPost, url, constants.MIMEJSON, syncReq)
if err != nil {
return 0, err
}
result, err := strconv.ParseUint(string(res), 10, 64)
if err != nil {
return 0, err
}
return result, nil
}
// CreateInAccount creates VINS in account level in async mode
func (v VINS) AsyncCreateInAccount(ctx context.Context, req CreateInAccountRequest) (string, error) {
err := validators.ValidateRequest(req)
if err != nil {
return "", validators.ValidationErrors(validators.GetErrors(err))
}
asyncReq := asyncWrapperCreateInAccountRequest{
CreateInAccountRequest: req,
AsyncMode: true,
}
url := "/cloudbroker/vins/createInAccount"
result, err := v.client.DecortApiCallCtype(ctx, http.MethodPost, url, constants.MIMEJSON, asyncReq)
if err != nil {
return "", err
}
var resp string
err = json.Unmarshal(result, &resp)
if err != nil {
return "", err
}
return resp, nil
}