Files
decort-golang-sdk/pkg/cloudapi/vins/create_in_account.go

110 lines
2.7 KiB
Go
Raw Normal View History

2022-10-03 16:56:47 +03:00
package vins
import (
"context"
2023-09-24 12:11:31 +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-09-24 12:11:31 +03:00
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"`
}
2023-10-25 17:37:18 +03:00
// CreateInAccountRequest struct to create VINS in account
2022-10-03 16:56:47 +03:00
type CreateInAccountRequest struct {
2022-12-22 17:56:47 +03:00
// VINS name
// 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
// ID of account
// Required: true
2023-03-24 17:09:30 +03:00
AccountID uint64 `url:"accountId" json:"accountId" validate:"required"`
2022-12-22 17:56:47 +03:00
// Grid ID
// Required: false
2023-03-01 19:05:53 +03:00
GID uint64 `url:"gid,omitempty" json:"gid,omitempty"`
2022-12-22 17:56:47 +03:00
// Private network IP CIDR
// Required: false
2023-03-01 19:05:53 +03:00
IPCIDR string `url:"ipcidr,omitempty" json:"ipcidr,omitempty"`
2022-12-22 17:56:47 +03:00
// 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
2024-04-16 14:26:06 +03:00
// List of DNS ip address
// Required: false
DNSList []string `url:"dnsList" json:"dnsList,omitempty"`
2022-12-22 17:56:47 +03:00
// Number of pre created reservations
// Required: false
2023-03-01 19:05:53 +03:00
PreReservationsNum uint64 `url:"preReservationsNum,omitempty" json:"preReservationsNum,omitempty"`
2023-09-24 12:11:31 +03:00
// List of static routes, each item must have destination, netmask, and gateway fields
// Required: false
Routes []Route `url:"-" json:"routes,omitempty" validate:"omitempty,dive"`
2025-07-15 17:39:18 +03:00
// Zone ID
// Required: false
ZoneID uint64 `url:"zoneId,omitempty" json:"zoneId,omitempty"`
2023-09-24 12:11:31 +03:00
}
type wrapperCreateRequestInAcc struct {
CreateInAccountRequest
Routes []string `url:"routes,omitempty"`
2022-10-03 16:56:47 +03:00
}
2022-12-22 17:56:47 +03:00
// CreateInAccount creates VINS in account level
2022-10-03 16:56:47 +03:00
func (v VINS) CreateInAccount(ctx context.Context, req CreateInAccountRequest) (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-09-24 12:11:31 +03:00
var routes []string
if len(req.Routes) != 0 {
routes = make([]string, 0, len(req.Routes))
for r := range req.Routes {
b, err := json.Marshal(req.Routes[r])
if err != nil {
return 0, err
}
routes = append(routes, string(b))
}
} else {
routes = []string{}
}
reqWrapped := wrapperCreateRequestInAcc{
CreateInAccountRequest: req,
Routes: routes,
}
2022-10-03 16:56:47 +03:00
url := "/cloudapi/vins/createInAccount"
2023-09-24 12:11:31 +03:00
res, err := v.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
}
return result, nil
}