Files
decort-golang-sdk/pkg/cloudapi/lb/create.go

103 lines
2.6 KiB
Go
Raw Normal View History

2022-10-03 16:56:47 +03:00
package lb
import (
"context"
2023-09-24 12:11:31 +03:00
"encoding/json"
"errors"
2022-10-03 16:56:47 +03:00
"net/http"
2023-09-24 12:11:31 +03:00
"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-10-25 17:37:18 +03:00
// CreateRequest struct to create load balancer
2022-10-03 16:56:47 +03:00
type CreateRequest struct {
2022-12-22 17:56:47 +03:00
// ID of the resource group where this load balancer instance will be located
// Required: true
2023-03-24 17:09:30 +03:00
RGID uint64 `url:"rgId" json:"rgId" validate:"required"`
2022-12-22 17:56:47 +03:00
// Name of the load balancer.
// Must be unique among all load balancers in this Resource Group
// 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
// External network to connect this load balancer to
2024-11-22 12:09:50 +03:00
// Required: true, can be 0
ExtNetID uint64 `url:"extnetId" json:"extnetId"`
2022-12-22 17:56:47 +03:00
// Internal network (VINS) to connect this load balancer to
2024-11-22 12:09:50 +03:00
// Required: true, can be 0
VINSID uint64 `url:"vinsId" json:"vinsId"`
2023-09-24 12:11:31 +03:00
// Custom sysctl values for Load Balancer instance. Applied on boot
// Required: false
2024-04-16 14:26:06 +03:00
SysctlParams []map[string]interface{} `url:"-" json:"sysctlParams,omitempty" validate:"omitempty,dive"`
2023-09-24 12:11:31 +03:00
// Use Highly Available schema for LB deploy
// Required: false
HighlyAvailable bool `url:"highlyAvailable,omitempty" json:"highlyAvailable,omitempty"`
2022-12-22 17:56:47 +03:00
// Start now Load balancer
2025-07-15 17:39:18 +03:00
// Required: true
Start bool `url:"start" json:"start" validate:"required"`
2022-12-22 17:56:47 +03:00
// Text description of this load balancer
// Required: false
2023-03-01 19:05:53 +03:00
Description string `url:"desc,omitempty" json:"desc,omitempty"`
2025-07-15 17:39:18 +03:00
// Zone ID
// Required: false
ZoneID uint64 `url:"zoneId,omitempty" json:"zoneId,omitempty"`
2022-10-03 16:56:47 +03:00
}
2023-09-24 12:11:31 +03:00
type wrapperCreateRequest struct {
CreateRequest
Params []string `url:"sysctlParams,omitempty"`
}
2022-12-22 17:56:47 +03:00
// Create method will create a new load balancer instance
2023-09-24 12:11:31 +03:00
func (l LB) Create(ctx context.Context, req CreateRequest) (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
if req.ExtNetID == 0 && req.VINSID == 0 {
return 0, errors.New("vinsId and extNetId cannot be both in the value 0")
}
var params []string
if len(req.SysctlParams) != 0 {
params = make([]string, 0, len(req.SysctlParams))
2024-04-16 14:26:06 +03:00
for _, m := range req.SysctlParams {
encodeStr, err := json.Marshal(m)
2023-09-24 12:11:31 +03:00
if err != nil {
return 0, err
}
2024-04-16 14:26:06 +03:00
params = append(params, string(encodeStr))
2023-09-24 12:11:31 +03:00
}
} else {
params = []string{}
}
reqWrapped := wrapperCreateRequest{
CreateRequest: req,
Params: params,
}
2022-10-03 16:56:47 +03:00
url := "/cloudapi/lb/create"
2023-09-24 12:11:31 +03:00
res, err := l.client.DecortApiCall(ctx, http.MethodPost, url, reqWrapped)
2022-10-03 16:56:47 +03:00
if err != nil {
2023-09-24 12:11:31 +03:00
return 0, err
2022-10-03 16:56:47 +03:00
}
2023-09-24 12:11:31 +03:00
result, err := strconv.ParseUint(string(res), 10, 64)
if err != nil {
return 0, err
}
2022-12-22 17:56:47 +03:00
return result, nil
2022-10-03 16:56:47 +03:00
}