This commit is contained in:
2023-08-09 19:33:50 +03:00
parent 040235f92f
commit f1529c9aac
14 changed files with 80 additions and 24 deletions

View File

@@ -4,6 +4,7 @@ import (
"crypto/tls"
"net/http"
"net/url"
"time"
"repository.basistech.ru/BASIS/decort-golang-sdk/config"
)
@@ -17,14 +18,21 @@ func NewLegacyHttpClient(cfg config.LegacyConfig) *http.Client {
},
}
var expiredTime time.Time
if cfg.Token != "" {
expiredTime = time.Now().AddDate(0, 0, 1)
}
return &http.Client{
Transport: &transportLegacy{
base: transCfg,
username: url.QueryEscape(cfg.Username),
password: url.QueryEscape(cfg.Password),
retries: cfg.Retries,
token: cfg.Token,
decortURL: cfg.DecortURL,
base: transCfg,
username: url.QueryEscape(cfg.Username),
password: url.QueryEscape(cfg.Password),
retries: cfg.Retries,
token: cfg.Token,
decortURL: cfg.DecortURL,
expiryTime: expiredTime,
},
Timeout: cfg.Timeout.Get(),

View File

@@ -9,16 +9,17 @@ import (
)
type transportLegacy struct {
base http.RoundTripper
username string
password string
retries uint64
token string
decortURL string
base http.RoundTripper
username string
password string
retries uint64
token string
decortURL string
expiryTime time.Time
}
func (t *transportLegacy) RoundTrip(request *http.Request) (*http.Response, error) {
if t.token == "" {
if t.token == "" || time.Now().After(t.expiryTime) {
body := fmt.Sprintf("username=%s&password=%s", t.username, t.password)
bodyReader := strings.NewReader(body)
@@ -39,6 +40,7 @@ func (t *transportLegacy) RoundTrip(request *http.Request) (*http.Response, erro
token := string(tokenBytes)
t.token = token
t.expiryTime = time.Now().AddDate(0, 0, 1)
}
tokenValue := fmt.Sprintf("&authkey=%s", t.token)
@@ -63,7 +65,9 @@ func (t *transportLegacy) RoundTrip(request *http.Request) (*http.Response, erro
err = fmt.Errorf("%s", respBytes)
resp.Body.Close()
}
if err != nil {
return nil, fmt.Errorf("could not execute request: %w", err)
}
time.Sleep(time.Second * 5)
}
return nil, fmt.Errorf("could not execute request: %w", err)

View File

@@ -1,9 +1,10 @@
package validators
import (
"github.com/go-playground/validator/v10"
"regexp"
"strings"
"github.com/go-playground/validator/v10"
)
// computeDriverValidator is used to validate Driver field in kvmx86/kvmppc create.
@@ -263,3 +264,11 @@ func strictLooseValidator(fe validator.FieldLevel) bool {
return StringInSlice(fieldValue, strictLooseValues)
}
// name workerGroup must be more 3 symbol
func workerGroupNameValidator(fe validator.FieldLevel) bool {
fieldValue := fe.Field().String()
fieldValue = strings.Trim(fieldValue, " ")
return len(fieldValue) >= 3
}

View File

@@ -127,6 +127,12 @@ func errorMessage(fe validator.FieldError) string {
fe.Field(),
joinValues(flipgroupClientTypeValues))
// k8s Validators
case "workerGroupName":
return fmt.Sprintf("%s %s must be more 3 symbol",
prefix,
fe.Field())
// KVM_X86/KVM_PPC Validators
case "kvmNetType":
return fmt.Sprintf("%s %s must be one of the following: %s",

View File

@@ -180,5 +180,10 @@ func registerAllValidators(validate *validator.Validate) error {
return err
}
err = validate.RegisterValidation("workerGroupName", workerGroupNameValidator)
if err != nil {
return err
}
return nil
}