You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
406 lines
11 KiB
406 lines
11 KiB
package decortsdk
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/tls"
|
|
"fmt"
|
|
"io"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/google/go-querystring/query"
|
|
"repository.basistech.ru/BASIS/decort-golang-sdk/config"
|
|
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi"
|
|
k8s_ca "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/k8s"
|
|
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker"
|
|
k8s_cb "repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/k8s"
|
|
)
|
|
|
|
// Legacy HTTP-client for platform
|
|
type LegacyDecortClient struct {
|
|
decortURL string
|
|
client *http.Client
|
|
cfg config.LegacyConfig
|
|
expiryTime time.Time
|
|
mutex *sync.Mutex
|
|
}
|
|
|
|
// Legacy client builder
|
|
func NewLegacy(cfg config.LegacyConfig) *LegacyDecortClient {
|
|
if cfg.Retries == 0 {
|
|
cfg.Retries = 5
|
|
}
|
|
|
|
var expiryTime time.Time
|
|
|
|
if cfg.Token != "" {
|
|
expiryTime = time.Now().AddDate(0, 0, 1)
|
|
}
|
|
|
|
return &LegacyDecortClient{
|
|
decortURL: cfg.DecortURL,
|
|
client: &http.Client{
|
|
Transport: &http.Transport{
|
|
TLSClientConfig: &tls.Config{
|
|
//nolint:gosec
|
|
InsecureSkipVerify: cfg.SSLSkipVerify,
|
|
},
|
|
},
|
|
},
|
|
cfg: cfg,
|
|
expiryTime: expiryTime,
|
|
mutex: &sync.Mutex{},
|
|
}
|
|
}
|
|
|
|
// CloudAPI builder
|
|
func (ldc *LegacyDecortClient) CloudAPI() *cloudapi.CloudAPI {
|
|
return cloudapi.New(ldc)
|
|
}
|
|
|
|
// CloudBroker builder
|
|
func (ldc *LegacyDecortClient) CloudBroker() *cloudbroker.CloudBroker {
|
|
return cloudbroker.New(ldc)
|
|
}
|
|
|
|
// DecortApiCall method for sending requests to the platform
|
|
func (ldc *LegacyDecortClient) DecortApiCall(ctx context.Context, method, url string, params interface{}) ([]byte, error) {
|
|
if err := ldc.getToken(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
k8sCaCreateReq, okCa := params.(k8s_ca.CreateRequest)
|
|
k8sCbCreateReq, okCb := params.(k8s_cb.CreateRequest)
|
|
|
|
var body *bytes.Buffer
|
|
var ctype string
|
|
|
|
if okCa {
|
|
body, ctype = createK8sCloudApiLegacy(k8sCaCreateReq, ldc.cfg.Token)
|
|
} else if okCb {
|
|
body, ctype = createK8sCloudBrokerLegacy(k8sCbCreateReq, ldc.cfg.Token)
|
|
} else {
|
|
values, err := query.Values(params)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
body = bytes.NewBufferString(values.Encode() + fmt.Sprintf("&authkey=%s", ldc.cfg.Token))
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, method, ldc.decortURL+"/restmachine"+url, body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// perform request
|
|
var respBytes []byte
|
|
respBytes, err = ldc.do(req, ctype)
|
|
|
|
return respBytes, err
|
|
}
|
|
|
|
func (ldc *LegacyDecortClient) getToken(ctx context.Context) error {
|
|
ldc.mutex.Lock()
|
|
defer ldc.mutex.Unlock()
|
|
|
|
if ldc.cfg.Token == "" || time.Now().After(ldc.expiryTime) {
|
|
body := fmt.Sprintf("username=%s&password=%s", url.QueryEscape(ldc.cfg.Username), url.QueryEscape(ldc.cfg.Password))
|
|
bodyReader := strings.NewReader(body)
|
|
|
|
req, _ := http.NewRequestWithContext(ctx, "POST", ldc.cfg.DecortURL+"/restmachine/cloudapi/user/authenticate", bodyReader)
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
resp, err := ldc.client.Do(req)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to get token: %w", err)
|
|
}
|
|
|
|
tokenBytes, _ := io.ReadAll(resp.Body)
|
|
resp.Body.Close()
|
|
|
|
if resp.StatusCode != 200 {
|
|
return fmt.Errorf("unable to get token: %s", tokenBytes)
|
|
}
|
|
|
|
token := string(tokenBytes)
|
|
ldc.cfg.Token = token
|
|
ldc.expiryTime = time.Now().AddDate(0, 0, 1)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (ldc *LegacyDecortClient) do(req *http.Request, ctype string) ([]byte, error) {
|
|
if ctype != "" {
|
|
req.Header.Add("Content-Type", ctype)
|
|
} else {
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
}
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
buf, err := io.ReadAll(req.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Body.Close()
|
|
req.Body = io.NopCloser(bytes.NewBuffer(buf))
|
|
|
|
resp, err := ldc.client.Do(req)
|
|
if err != nil || resp == nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
|
|
// handle successful request
|
|
respBytes, err := io.ReadAll(resp.Body)
|
|
if err!= nil {
|
|
return nil, err
|
|
}
|
|
if resp.StatusCode == 200 {
|
|
return respBytes, nil
|
|
}
|
|
|
|
// handle errors with status code other than 200
|
|
err = fmt.Errorf("%s", respBytes)
|
|
return nil, fmt.Errorf("could not execute request: %w", err)
|
|
}
|
|
|
|
func createK8sCloudApiLegacy(req k8s_ca.CreateRequest, token string) (*bytes.Buffer, string) {
|
|
reqBody := &bytes.Buffer{}
|
|
writer := multipart.NewWriter(reqBody)
|
|
if req.OidcCertificate != "" {
|
|
part, _ := writer.CreateFormFile("oidcCertificate", "ca.crt")
|
|
_, _ = io.Copy(part, strings.NewReader(req.OidcCertificate))
|
|
}
|
|
|
|
_ = writer.WriteField("name", req.Name)
|
|
_ = writer.WriteField("rgId", strconv.FormatUint(req.RGID, 10))
|
|
_ = writer.WriteField("k8ciId", strconv.FormatUint(req.K8SCIID, 10))
|
|
_ = writer.WriteField("workerGroupName", req.WorkerGroupName)
|
|
_ = writer.WriteField("networkPlugin", req.NetworkPlugin)
|
|
|
|
if req.MasterSEPID != 0 {
|
|
_ = writer.WriteField("masterSepId", strconv.FormatUint(req.MasterSEPID, 10))
|
|
}
|
|
if req.MasterSEPPool != "" {
|
|
_ = writer.WriteField("masterSepPool", req.MasterSEPPool)
|
|
}
|
|
if req.WorkerSEPID != 0 {
|
|
_ = writer.WriteField("workerSepId", strconv.FormatUint(req.WorkerSEPID, 10))
|
|
}
|
|
if req.WorkerSEPPool != "" {
|
|
_ = writer.WriteField("workerSepPool", req.WorkerSEPPool)
|
|
}
|
|
|
|
if req.Labels != nil {
|
|
for _, v := range req.Labels {
|
|
_ = writer.WriteField("labels", v)
|
|
}
|
|
}
|
|
if req.Taints != nil {
|
|
for _, v := range req.Taints {
|
|
_ = writer.WriteField("taints", v)
|
|
}
|
|
}
|
|
if req.Annotations != nil {
|
|
for _, v := range req.Annotations {
|
|
_ = writer.WriteField("annotations", v)
|
|
}
|
|
}
|
|
|
|
if req.MasterCPU != 0 {
|
|
_ = writer.WriteField("masterCpu", strconv.FormatUint(uint64(req.MasterCPU), 10))
|
|
}
|
|
if req.MasterNum != 0 {
|
|
_ = writer.WriteField("masterNum", strconv.FormatUint(uint64(req.MasterNum), 10))
|
|
}
|
|
if req.MasterRAM != 0 {
|
|
_ = writer.WriteField("masterRam", strconv.FormatUint(uint64(req.MasterRAM), 10))
|
|
}
|
|
if req.MasterDisk != 0 {
|
|
_ = writer.WriteField("masterDisk", strconv.FormatUint(uint64(req.MasterDisk), 10))
|
|
}
|
|
if req.WorkerCPU != 0 {
|
|
_ = writer.WriteField("workerCpu", strconv.FormatUint(uint64(req.WorkerCPU), 10))
|
|
}
|
|
if req.WorkerNum != 0 {
|
|
_ = writer.WriteField("workerNum", strconv.FormatUint(uint64(req.WorkerNum), 10))
|
|
}
|
|
if req.WorkerRAM != 0 {
|
|
_ = writer.WriteField("workerRam", strconv.FormatUint(uint64(req.WorkerRAM), 10))
|
|
}
|
|
if req.WorkerDisk != 0 {
|
|
_ = writer.WriteField("workerDisk", strconv.FormatUint(uint64(req.WorkerDisk), 10))
|
|
}
|
|
if req.ExtNetID != 0 {
|
|
_ = writer.WriteField("extnetId", strconv.FormatUint(req.ExtNetID, 10))
|
|
}
|
|
if req.VinsId != 0 {
|
|
_ = writer.WriteField("vinsId", strconv.FormatUint(req.VinsId, 10))
|
|
}
|
|
if !req.WithLB {
|
|
_ = writer.WriteField("withLB", strconv.FormatBool(req.WithLB))
|
|
}
|
|
|
|
_ = writer.WriteField("highlyAvailableLB", strconv.FormatBool(req.HighlyAvailable))
|
|
|
|
if req.AdditionalSANs != nil {
|
|
for _, v := range req.AdditionalSANs {
|
|
_ = writer.WriteField("additionalSANs", v)
|
|
}
|
|
}
|
|
if req.InitConfiguration != "" {
|
|
_ = writer.WriteField("initConfiguration", req.InitConfiguration)
|
|
}
|
|
if req.ClusterConfiguration != "" {
|
|
_ = writer.WriteField("clusterConfiguration", req.ClusterConfiguration)
|
|
}
|
|
if req.KubeletConfiguration != "" {
|
|
_ = writer.WriteField("kubeletConfiguration", req.KubeletConfiguration)
|
|
}
|
|
if req.KubeProxyConfiguration != "" {
|
|
_ = writer.WriteField("kubeProxyConfiguration", req.KubeProxyConfiguration)
|
|
}
|
|
if req.JoinConfiguration != "" {
|
|
_ = writer.WriteField("joinConfiguration", req.JoinConfiguration)
|
|
}
|
|
if req.Description != "" {
|
|
_ = writer.WriteField("desc", req.Description)
|
|
}
|
|
if req.UserData != "" {
|
|
_ = writer.WriteField("userData", req.UserData)
|
|
}
|
|
|
|
_ = writer.WriteField("extnetOnly", strconv.FormatBool(req.ExtNetOnly))
|
|
|
|
_ = writer.WriteField("authkey", token)
|
|
|
|
ct := writer.FormDataContentType()
|
|
writer.Close()
|
|
|
|
return reqBody, ct
|
|
}
|
|
|
|
func createK8sCloudBrokerLegacy(req k8s_cb.CreateRequest, token string) (*bytes.Buffer, string) {
|
|
reqBody := &bytes.Buffer{}
|
|
writer := multipart.NewWriter(reqBody)
|
|
if req.OidcCertificate != "" {
|
|
part, _ := writer.CreateFormFile("oidcCertificate", "ca.crt")
|
|
_, _ = io.Copy(part, strings.NewReader(req.OidcCertificate))
|
|
}
|
|
|
|
_ = writer.WriteField("name", req.Name)
|
|
_ = writer.WriteField("rgId", strconv.FormatUint(req.RGID, 10))
|
|
_ = writer.WriteField("k8ciId", strconv.FormatUint(req.K8CIID, 10))
|
|
_ = writer.WriteField("workerGroupName", req.WorkerGroupName)
|
|
_ = writer.WriteField("networkPlugin", req.NetworkPlugin)
|
|
|
|
if req.MasterSEPID != 0 {
|
|
_ = writer.WriteField("masterSepId", strconv.FormatUint(req.MasterSEPID, 10))
|
|
}
|
|
if req.MasterSEPPool != "" {
|
|
_ = writer.WriteField("masterSepPool", req.MasterSEPPool)
|
|
}
|
|
if req.WorkerSEPID != 0 {
|
|
_ = writer.WriteField("workerSepId", strconv.FormatUint(req.WorkerSEPID, 10))
|
|
}
|
|
if req.WorkerSEPPool != "" {
|
|
_ = writer.WriteField("workerSepPool", req.WorkerSEPPool)
|
|
}
|
|
|
|
if req.Labels != nil {
|
|
for _, v := range req.Labels {
|
|
_ = writer.WriteField("labels", v)
|
|
}
|
|
}
|
|
if req.Taints != nil {
|
|
for _, v := range req.Taints {
|
|
_ = writer.WriteField("taints", v)
|
|
}
|
|
}
|
|
if req.Annotations != nil {
|
|
for _, v := range req.Annotations {
|
|
_ = writer.WriteField("annotations", v)
|
|
}
|
|
}
|
|
|
|
if req.MasterCPU != 0 {
|
|
_ = writer.WriteField("masterCpu", strconv.FormatUint(req.MasterCPU, 10))
|
|
}
|
|
if req.MasterNum != 0 {
|
|
_ = writer.WriteField("masterNum", strconv.FormatUint(req.MasterNum, 10))
|
|
}
|
|
if req.MasterRAM != 0 {
|
|
_ = writer.WriteField("masterRam", strconv.FormatUint(req.MasterRAM, 10))
|
|
}
|
|
if req.MasterDisk != 0 {
|
|
_ = writer.WriteField("masterDisk", strconv.FormatUint(req.MasterDisk, 10))
|
|
}
|
|
if req.WorkerCPU != 0 {
|
|
_ = writer.WriteField("workerCpu", strconv.FormatUint(req.WorkerCPU, 10))
|
|
}
|
|
if req.WorkerNum != 0 {
|
|
_ = writer.WriteField("workerNum", strconv.FormatUint(req.WorkerNum, 10))
|
|
}
|
|
if req.WorkerRAM != 0 {
|
|
_ = writer.WriteField("workerRam", strconv.FormatUint(req.WorkerRAM, 10))
|
|
}
|
|
if req.WorkerDisk != 0 {
|
|
_ = writer.WriteField("workerDisk", strconv.FormatUint(req.WorkerDisk, 10))
|
|
}
|
|
if req.ExtNetID != 0 {
|
|
_ = writer.WriteField("extnetId", strconv.FormatUint(req.ExtNetID, 10))
|
|
}
|
|
if req.VinsId != 0 {
|
|
_ = writer.WriteField("vinsId", strconv.FormatUint(req.VinsId, 10))
|
|
}
|
|
if !req.WithLB {
|
|
_ = writer.WriteField("withLB", strconv.FormatBool(req.WithLB))
|
|
}
|
|
|
|
_ = writer.WriteField("highlyAvailableLB", strconv.FormatBool(req.HighlyAvailable))
|
|
|
|
if req.AdditionalSANs != nil {
|
|
for _, v := range req.AdditionalSANs {
|
|
_ = writer.WriteField("additionalSANs", v)
|
|
}
|
|
}
|
|
if req.InitConfiguration != "" {
|
|
_ = writer.WriteField("initConfiguration", req.InitConfiguration)
|
|
}
|
|
if req.ClusterConfiguration != "" {
|
|
_ = writer.WriteField("clusterConfiguration", req.ClusterConfiguration)
|
|
}
|
|
if req.KubeletConfiguration != "" {
|
|
_ = writer.WriteField("kubeletConfiguration", req.KubeletConfiguration)
|
|
}
|
|
if req.KubeProxyConfiguration != "" {
|
|
_ = writer.WriteField("kubeProxyConfiguration", req.KubeProxyConfiguration)
|
|
}
|
|
if req.JoinConfiguration != "" {
|
|
_ = writer.WriteField("joinConfiguration", req.JoinConfiguration)
|
|
}
|
|
if req.Description != "" {
|
|
_ = writer.WriteField("desc", req.Description)
|
|
}
|
|
if req.UserData != "" {
|
|
_ = writer.WriteField("userData", req.UserData)
|
|
}
|
|
|
|
_ = writer.WriteField("extnetOnly", strconv.FormatBool(req.ExtNetOnly))
|
|
|
|
_ = writer.WriteField("authkey", token)
|
|
|
|
ct := writer.FormDataContentType()
|
|
|
|
writer.Close()
|
|
return reqBody, ct
|
|
}
|