Compare commits
	
		
			4 Commits 
		
	
	
		
			main
			...
			1.5.8-k8s-
		
	
	| Author | SHA1 | Date | 
|---|---|---|
|  | 1972956aeb | 2 years ago | 
|  | afcbc7e749 | 2 years ago | 
|  | 0b3de4df7f | 2 years ago | 
|  | c0608d08b9 | 2 years ago | 
| @ -1,3 +1,5 @@ | ||||
| cmd/ | ||||
| .idea/ | ||||
| .vscode/ | ||||
| .fleet/ | ||||
| .DS_Store | ||||
|  | ||||
| @ -1,6 +1,4 @@ | ||||
| ## Version 1.5.7 | ||||
| ## Version 1.6.0-beta | ||||
| 
 | ||||
| ### Bugfix | ||||
| - Remove the required tag of the start field in the CreateRequest model in cb/lb/create, since it is impossible to create an lb without starting it | ||||
| - Fix model the RecordGrid, add the ItemGridList model to cloudbroker/grid/models to correctly receive information on get and list requests | ||||
| - Fix tag json field GID in model RecordResourcesConsumption cb/grid/models | ||||
| - Refactored client, made it concurrent safe | ||||
| @ -1,40 +0,0 @@ | ||||
| package client | ||||
| 
 | ||||
| import ( | ||||
| 	"crypto/tls" | ||||
| 	"net/http" | ||||
| 	"time" | ||||
| 
 | ||||
| 	"repository.basistech.ru/BASIS/decort-golang-sdk/config" | ||||
| ) | ||||
| 
 | ||||
| func NewHttpClient(cfg config.Config) *http.Client { | ||||
| 
 | ||||
| 	transCfg := &http.Transport{ | ||||
| 		TLSClientConfig: &tls.Config{ | ||||
| 			//nolint:gosec
 | ||||
| 			InsecureSkipVerify: cfg.SSLSkipVerify, | ||||
| 		}, | ||||
| 	} | ||||
| 
 | ||||
| 	var expiredTime time.Time | ||||
| 
 | ||||
| 	if cfg.Token != "" { | ||||
| 		expiredTime = time.Now().AddDate(0, 0, 1) | ||||
| 	} | ||||
| 
 | ||||
| 	return &http.Client{ | ||||
| 		Transport: &transport{ | ||||
| 			base:         transCfg, | ||||
| 			retries:      cfg.Retries, | ||||
| 			clientID:     cfg.AppID, | ||||
| 			clientSecret: cfg.AppSecret, | ||||
| 			ssoURL:       cfg.SSOURL, | ||||
| 			token:        cfg.Token, | ||||
| 			expiryTime:   expiredTime, | ||||
| 			//TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
 | ||||
| 		}, | ||||
| 
 | ||||
| 		Timeout: cfg.Timeout.Get(), | ||||
| 	} | ||||
| } | ||||
| @ -1,40 +0,0 @@ | ||||
| package client | ||||
| 
 | ||||
| import ( | ||||
| 	"crypto/tls" | ||||
| 	"net/http" | ||||
| 	"net/url" | ||||
| 	"time" | ||||
| 
 | ||||
| 	"repository.basistech.ru/BASIS/decort-golang-sdk/config" | ||||
| ) | ||||
| 
 | ||||
| // NewLegacyHttpClient creates legacy HTTP Client
 | ||||
| func NewLegacyHttpClient(cfg config.LegacyConfig) *http.Client { | ||||
| 	transCfg := &http.Transport{ | ||||
| 		TLSClientConfig: &tls.Config{ | ||||
| 			//nolint:gosec
 | ||||
| 			InsecureSkipVerify: cfg.SSLSkipVerify, | ||||
| 		}, | ||||
| 	} | ||||
| 
 | ||||
| 	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, | ||||
| 			expiryTime: expiredTime, | ||||
| 		}, | ||||
| 
 | ||||
| 		Timeout: cfg.Timeout.Get(), | ||||
| 	} | ||||
| } | ||||
| @ -1,74 +0,0 @@ | ||||
| package client | ||||
| 
 | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"io" | ||||
| 	"net/http" | ||||
| 	"strings" | ||||
| 	"time" | ||||
| ) | ||||
| 
 | ||||
| type transportLegacy struct { | ||||
| 	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 == "" || time.Now().After(t.expiryTime) { | ||||
| 		body := fmt.Sprintf("username=%s&password=%s", t.username, t.password) | ||||
| 		bodyReader := strings.NewReader(body) | ||||
| 
 | ||||
| 		req, _ := http.NewRequestWithContext(request.Context(), "POST", t.decortURL+"/restmachine/cloudapi/user/authenticate", bodyReader) | ||||
| 		req.Header.Add("Content-Type", "application/x-www-form-urlencoded") | ||||
| 
 | ||||
| 		resp, err := t.base.RoundTrip(req) | ||||
| 		if err != nil { | ||||
| 			return nil, fmt.Errorf("unable to get token: %w", err) | ||||
| 		} | ||||
| 
 | ||||
| 		tokenBytes, _ := io.ReadAll(resp.Body) | ||||
| 		resp.Body.Close() | ||||
| 
 | ||||
| 		if resp.StatusCode != 200 { | ||||
| 			return nil, fmt.Errorf("unable to get token: %s", tokenBytes) | ||||
| 		} | ||||
| 
 | ||||
| 		token := string(tokenBytes) | ||||
| 		t.token = token | ||||
| 		t.expiryTime = time.Now().AddDate(0, 0, 1) | ||||
| 	} | ||||
| 
 | ||||
| 	tokenValue := fmt.Sprintf("&authkey=%s", t.token) | ||||
| 	tokenReader := strings.NewReader(tokenValue) | ||||
| 
 | ||||
| 	newBody := io.MultiReader(request.Body, tokenReader) | ||||
| 
 | ||||
| 	req, _ := http.NewRequestWithContext(request.Context(), request.Method, request.URL.String(), newBody) | ||||
| 
 | ||||
| 	req.Header.Add("Content-Type", "application/x-www-form-urlencoded") | ||||
| 	req.Header.Set("Accept", "application/json") | ||||
| 
 | ||||
| 	var resp *http.Response | ||||
| 	var err error | ||||
| 	for i := uint64(0); i < t.retries; i++ { | ||||
| 		resp, err = t.base.RoundTrip(req) | ||||
| 		if err == nil { | ||||
| 			if resp.StatusCode == 200 { | ||||
| 				return resp, nil | ||||
| 			} | ||||
| 			respBytes, _ := io.ReadAll(resp.Body) | ||||
| 			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) | ||||
| } | ||||
| @ -1,69 +0,0 @@ | ||||
| package client | ||||
| 
 | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"io" | ||||
| 	"net/http" | ||||
| 	"strings" | ||||
| 	"time" | ||||
| ) | ||||
| 
 | ||||
| type transport struct { | ||||
| 	base         http.RoundTripper | ||||
| 	retries      uint64 | ||||
| 	clientID     string | ||||
| 	clientSecret string | ||||
| 	token        string | ||||
| 	ssoURL       string | ||||
| 	expiryTime   time.Time | ||||
| } | ||||
| 
 | ||||
| func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) { | ||||
| 	if t.token == "" || time.Now().After(t.expiryTime) { | ||||
| 		body := fmt.Sprintf("grant_type=client_credentials&client_id=%s&client_secret=%s&response_type=id_token", t.clientID, t.clientSecret) | ||||
| 		bodyReader := strings.NewReader(body) | ||||
| 
 | ||||
| 		t.ssoURL = strings.TrimSuffix(t.ssoURL, "/") | ||||
| 
 | ||||
| 		req, _ := http.NewRequestWithContext(req.Context(), "POST", t.ssoURL+"/v1/oauth/access_token", bodyReader) | ||||
| 		req.Header.Add("Content-Type", "application/x-www-form-urlencoded") | ||||
| 
 | ||||
| 		resp, err := t.base.RoundTrip(req) | ||||
| 		if err != nil { | ||||
| 			return nil, fmt.Errorf("cannot get token: %w", err) | ||||
| 		} | ||||
| 
 | ||||
| 		tokenBytes, _ := io.ReadAll(resp.Body) | ||||
| 		resp.Body.Close() | ||||
| 
 | ||||
| 		if resp.StatusCode != 200 { | ||||
| 			return nil, fmt.Errorf("cannot get token: %s", tokenBytes) | ||||
| 		} | ||||
| 
 | ||||
| 		token := string(tokenBytes) | ||||
| 
 | ||||
| 		t.token = token | ||||
| 		t.expiryTime = time.Now().AddDate(0, 0, 1) | ||||
| 	} | ||||
| 
 | ||||
| 	req.Header.Add("Content-Type", "application/x-www-form-urlencoded") | ||||
| 	req.Header.Add("Authorization", "bearer "+t.token) | ||||
| 	req.Header.Set("Accept", "application/json") | ||||
| 
 | ||||
| 	var resp *http.Response | ||||
| 	var err error | ||||
| 	for i := uint64(0); i < t.retries; i++ { | ||||
| 		resp, err = t.base.RoundTrip(req) | ||||
| 		if err == nil { | ||||
| 			if resp.StatusCode == 200 { | ||||
| 				return resp, nil | ||||
| 			} | ||||
| 			respBytes, _ := io.ReadAll(resp.Body) | ||||
| 			err = fmt.Errorf("%s", respBytes) | ||||
| 			resp.Body.Close() | ||||
| 		} | ||||
| 		//logrus.Errorf("Could not execute request: %v. Retrying %d/%d", err, i+1, t.retries)
 | ||||
| 		time.Sleep(time.Second * 5) | ||||
| 	} | ||||
| 	return nil, fmt.Errorf("could not execute request: %w", err) | ||||
| } | ||||
					Loading…
					
					
				
		Reference in new issue