Skip to content

Commit

Permalink
Use kedautil for handling http
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Felipe Dominguite <[email protected]>
  • Loading branch information
pdominguite committed Jan 6, 2021
1 parent 4416698 commit 9d9469e
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions pkg/scalers/openstack/keystone_authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import (
"net/url"
"path"
"time"

kedautil "github.com/kedacore/keda/v2/pkg/util"
)

const tokensEndpoint = "/auth/tokens"

var httpClient = &http.Client{}

// KeystoneAuthMetadata contains all the necessary metadata for Keystone authentication
type KeystoneAuthMetadata struct {
AuthURL string `json:"-"`
AuthToken string `json:"-"`
HTTPClient http.Client `json:"-"`
Properties *authProps `json:"auth"`
AuthURL string `json:"-"`
AuthToken string `json:"-"`
HTTPClient *http.Client `json:"-"`
Properties *authProps `json:"auth"`
}

type authProps struct {
Expand Down Expand Up @@ -74,11 +74,20 @@ func (authProps *KeystoneAuthMetadata) GetToken() (string, error) {

tokenURL.Path = path.Join(tokenURL.Path, tokensEndpoint)

resp, requestError := http.Post(tokenURL.String(), "application/json", body)
getTokenRequest, getTokenRequestError := http.NewRequest("POST", tokenURL.String(), body)

getTokenRequest.Header.Set("Content-Type", "application/json")

if getTokenRequestError != nil {
return "", getTokenRequestError
}

resp, requestError := authProps.HTTPClient.Do(getTokenRequest)

if requestError != nil {
return "", requestError
}

defer resp.Body.Close()

if resp.StatusCode >= 200 && resp.StatusCode < 300 {
Expand Down Expand Up @@ -151,9 +160,7 @@ func NewPasswordAuth(authURL string, userID string, userPassword string, project

passAuth.AuthURL = url.String()

passAuth.HTTPClient = *httpClient

passAuth.HTTPClient.Timeout = time.Duration(httpTimeout) * time.Second
passAuth.HTTPClient = kedautil.CreateHTTPClient(time.Duration(httpTimeout) * time.Second)

passAuth.Properties.Identity.Methods = []string{"password"}
passAuth.Properties.Identity.Password.User.ID = userID
Expand Down Expand Up @@ -186,9 +193,7 @@ func NewAppCredentialsAuth(authURL string, id string, secret string, httpTimeout

appAuth.AuthURL = url.String()

appAuth.HTTPClient = *httpClient

appAuth.HTTPClient.Timeout = time.Duration(httpTimeout) * time.Second
appAuth.HTTPClient = kedautil.CreateHTTPClient(time.Duration(httpTimeout) * time.Second)

appAuth.Properties.Identity.AppCredential = new(appCredentialProps)
appAuth.Properties.Identity.Methods = []string{"application_credential"}
Expand Down

0 comments on commit 9d9469e

Please sign in to comment.