Skip to content

Commit

Permalink
Merge pull request #9 from IBM/prgavali-sl-timeout-fix
Browse files Browse the repository at this point in the history
Fixed timeout for volume provisioning
  • Loading branch information
pramodgavali authored Oct 29, 2018
2 parents 21ce857 + 808bf65 commit 2218e46
Show file tree
Hide file tree
Showing 22 changed files with 155 additions and 117 deletions.
27 changes: 15 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ type Config struct {
Gen2 *Gen2Config
}

func ReadConfig(confPath string, logger zap.Logger) (*Config, error) {
//ReadConfig loads the config from file
func ReadConfig(confPath string, logger *zap.Logger) (*Config, error) {
// load the default config, if confPath not provided
if confPath == "" {
confPath = GetDefaultConfPath()
Expand All @@ -64,9 +65,9 @@ func GetDefaultConfPath() string {
}

// ParseConfig ...
func ParseConfig(filePath string, conf interface{}, logger zap.Logger) error {
func ParseConfig(filePath string, conf interface{}, logger *zap.Logger) error {
_, err := toml.DecodeFile(filePath, conf)
if err != nil {
if err != nil {
logger.Error("Failed to parse config file", zap.Error(err))
}
return err
Expand All @@ -83,15 +84,17 @@ type BluemixConfig struct {

// SoftlayerConfig ...
type SoftlayerConfig struct {
SoftlayerBlockEnabled bool `toml:"softlayer_block_enabled" envconfig:"SOFTLAYER_BLOCK_ENABLED"`
SoftlayerBlockProviderName string `toml:"softlayer_block_provider_name" envconfig:"SOFTLAYER_BLOCK_PROVIDER_NAME"`
SoftlayerFileEnabled bool `toml:"softlayer_file_enabled" envconfig:"SOFTLAYER_FILE_ENABLED"`
SoftlayerFileProviderName string `toml:"softlayer_file_provider_name" envconfig:"SOFTLAYER_FILE_PROVIDER_NAME"`
SoftlayerUsername string `toml:"softlayer_username" json:"-"`
SoftlayerAPIKey string `toml:"softlayer_api_key" json:"-"`
SoftlayerEndpointURL string `toml:"softlayer_endpoint_url"`
SoftlayerDataCenter string `toml:"softlayer_datacenter"`
SoftlayerTimeout string `toml:"softlayer_api_timeout"`
SoftlayerBlockEnabled bool `toml:"softlayer_block_enabled" envconfig:"SOFTLAYER_BLOCK_ENABLED"`
SoftlayerBlockProviderName string `toml:"softlayer_block_provider_name" envconfig:"SOFTLAYER_BLOCK_PROVIDER_NAME"`
SoftlayerFileEnabled bool `toml:"softlayer_file_enabled" envconfig:"SOFTLAYER_FILE_ENABLED"`
SoftlayerFileProviderName string `toml:"softlayer_file_provider_name" envconfig:"SOFTLAYER_FILE_PROVIDER_NAME"`
SoftlayerUsername string `toml:"softlayer_username" json:"-"`
SoftlayerAPIKey string `toml:"softlayer_api_key" json:"-"`
SoftlayerEndpointURL string `toml:"softlayer_endpoint_url"`
SoftlayerDataCenter string `toml:"softlayer_datacenter"`
SoftlayerTimeout string `toml:"softlayer_api_timeout" envconfig:"SOFTLAYER_API_TIMEOUT"`
SoftlayerVolProvisionTimeout string `toml:"softlayer_vol_provision_timeout" envconfig:"SOFTLAYER_VOL_PROVISION_TIMEOUT"`
SoftlayerRetryInterval string `toml:"softlayer_api_retry_interval" envconfig:"SOFTLAYER_API_RETRY_INTERVAL"`
//Configuration values for JWT tokens
SoftlayerJWTKID string `toml:"softlayer_jwt_kid"`
SoftlayerJWTTTL int `toml:"softlayer_jwt_ttl"`
Expand Down
8 changes: 5 additions & 3 deletions etc/libconfig.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[bluemix]
iam_url = "https://iam.stage1.ng.bluemix.net"
iam_url = "https://iam.bluemix.net"
iam_client_id = "bx"
iam_client_secret = "bx"
iam_api_key = "free"
iam_api_key = ""
refresh_token = ""


Expand All @@ -16,7 +16,9 @@ softlayer_api_key = ""
softlayer_endpoint_url = "https://api.softlayer.com/rest/v3"
softlayer_iam_endpoint_url = "https://api.softlayer.com/mobile/v3"
softlayer_datacenter = "mex01"
softlayer_api_timeout = "20s"
softlayer_api_timeout = "30s"
softlayer_vol_provision_timeout = "30m"
softlayer_api_retry_interval = "5s"

[genesis]
genesis_provider_enabled = false
Expand Down
18 changes: 13 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,24 @@ func main() {
*/

// Load config file
conf := config.ReadConfig("", *logger)
if conf == nil {
conf, err := config.ReadConfig("", logger)
if err != nil {
logger.Fatal("Error loading configuration")
}

// Prepare provider registry
providerRegistry, err := provider_util.InitProviders(conf, *logger)
providerRegistry, err := provider_util.InitProviders(conf, logger)
if err != nil {
logger.Fatal("Error configuring providers", local.ZapError(err))
}

//dc_name := "mex01"
providerName := conf.Softlayer.SoftlayerBlockProviderName
if conf.Softlayer.SoftlayerFileEnabled {
providerName = conf.Softlayer.SoftlayerFileProviderName
}
logger.Info("In main before openProviderSession call", zap.Reflect("providerRegistry", providerRegistry))
sess, _, err := provider_util.OpenProviderSession(conf, providerRegistry, conf.Softlayer.SoftlayerBlockProviderName, *logger)
sess, _, err := provider_util.OpenProviderSession(conf, providerRegistry, providerName, logger)
if err != nil {
logger.Error("Failed to get session", zap.Reflect("Error", err))
return
Expand Down Expand Up @@ -136,12 +140,15 @@ func main() {
fmt.Println("You selected choice to Create volume\n")
volume := &provider.Volume{}
volume.VolumeType = "block"
if conf.Softlayer.SoftlayerFileEnabled {
volume.VolumeType = "file"
}
dcName := ""
volSize := 0
Iops := "0"
tier := ""
providerType := ""
//volume.SnapshotSpace = 0

var choice int
fmt.Printf("\nPlease enter storage type choice 1- for endurance 2- for performance: ")
_, er11 = fmt.Scanf("%d", &choice)
Expand Down Expand Up @@ -171,6 +178,7 @@ func main() {
_, er11 = fmt.Scanf("%s", &tier)
volume.Tier = &tier
}
volume.SnapshotSpace = &volSize
_, errr := sess.VolumeCreate(*volume)
if errr == nil {
logger.Info("Successfully ordered volume ================>", zap.Reflect("StorageType", volume.ProviderType))
Expand Down
8 changes: 4 additions & 4 deletions provider/auth/token_exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ type TokenExchangeService interface {

// ExchangeRefreshTokenForAccessToken ...
// TODO Deprecate when no longer reliant on refresh token authentication
ExchangeRefreshTokenForAccessToken(refreshToken string, logger zap.Logger) (*AccessToken, error)
ExchangeRefreshTokenForAccessToken(refreshToken string, logger *zap.Logger) (*AccessToken, error)

// ExchangeAccessTokenForIMSToken ...
ExchangeAccessTokenForIMSToken(accessToken AccessToken, logger zap.Logger) (*IMSToken, error)
ExchangeAccessTokenForIMSToken(accessToken AccessToken, logger *zap.Logger) (*IMSToken, error)

// ExchangeIAMAPIKeyForIMSToken ...
ExchangeIAMAPIKeyForIMSToken(iamAPIKey string, logger zap.Logger) (*IMSToken, error)
ExchangeIAMAPIKeyForIMSToken(iamAPIKey string, logger *zap.Logger) (*IMSToken, error)

// ExchangeIAMAPIKeyForAccessToken ...
ExchangeIAMAPIKeyForAccessToken(iamAPIKey string, logger zap.Logger) (*AccessToken, error)
ExchangeIAMAPIKeyForAccessToken(iamAPIKey string, logger *zap.Logger) (*AccessToken, error)
}
6 changes: 3 additions & 3 deletions provider/local/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Provider interface {
// The implementation can choose to verify the credentials and return an error if they are invalid.
// Alternatively, the implementation can choose to defer credential verification until individual
// methods of the context are called.
OpenSession(context.Context, provider.ContextCredentials, zap.Logger) (provider.Session, error)
OpenSession(context.Context, provider.ContextCredentials, *zap.Logger) (provider.Session, error)

// Returns a configured ContextCredentialsFactory for this provider
ContextCredentialsFactory(datacenter *string) (ContextCredentialsFactory, error)
Expand All @@ -35,8 +35,8 @@ type Provider interface {
//go:generate counterfeiter -o fakes/context_credentials_factory.go --fake-name ContextCredentialsFactory . ContextCredentialsFactory
type ContextCredentialsFactory interface {
// ForIaaSAPIKey returns a config using an explicit API key for an IaaS user account
ForIaaSAPIKey(iamAccountID, iaasUserID, iaasAPIKey string, logger zap.Logger) (provider.ContextCredentials, error)
ForIaaSAPIKey(iamAccountID, iaasUserID, iaasAPIKey string, logger *zap.Logger) (provider.ContextCredentials, error)

// ForIAMAPIKey returns a config derived from an IAM API key (if applicable)
ForIAMAPIKey(iamAccountID, iamAPIKey string, logger zap.Logger) (provider.ContextCredentials, error)
ForIAMAPIKey(iamAccountID, iamAPIKey string, logger *zap.Logger) (provider.ContextCredentials, error)
}
9 changes: 5 additions & 4 deletions provider/utils/init_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"go.uber.org/zap"

softlayer_block "github.com/IBM/ibmcloud-storage-volume-lib/volume-providers/softlayer/block"
softlayer_file "github.com/IBM/ibmcloud-storage-volume-lib/volume-providers/softlayer/file"

"github.com/IBM/ibmcloud-storage-volume-lib/config"
"github.com/IBM/ibmcloud-storage-volume-lib/lib/provider"
Expand All @@ -24,7 +25,7 @@ import (
"github.com/IBM/ibmcloud-storage-volume-lib/provider/registry"
)

func InitProviders(conf *config.Config, logger zap.Logger) (registry.Providers, error) {
func InitProviders(conf *config.Config, logger *zap.Logger) (registry.Providers, error) {
var haveProviders bool
providerRegistry := &registry.ProviderRegistry{}
// BLOCK volume registration
Expand All @@ -41,7 +42,7 @@ func InitProviders(conf *config.Config, logger zap.Logger) (registry.Providers,

// FILE volume registration
if conf.Softlayer != nil && conf.Softlayer.SoftlayerFileEnabled {
prov, err := softlayer_block.NewProvider(conf, logger)
prov, err := softlayer_file.NewProvider(conf, logger)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -70,7 +71,7 @@ func isEmptyStringValue(value *string) bool {
return value == nil || *value == ""
}

func OpenProviderSession(conf *config.Config, providers registry.Providers, providerID string, logger zap.Logger) (session provider.Session, fatal bool, err1 error) {
func OpenProviderSession(conf *config.Config, providers registry.Providers, providerID string, logger *zap.Logger) (session provider.Session, fatal bool, err1 error) {
logger.Info("In OpenProviderSession methods")
prov, err := providers.Get(providerID)
if err != nil {
Expand All @@ -97,7 +98,7 @@ func OpenProviderSession(conf *config.Config, providers registry.Providers, prov
return
}

func GenerateContextCredentials(conf *config.Config, providerID string, contextCredentialsFactory local.ContextCredentialsFactory, logger zap.Logger) (provider.ContextCredentials, error) {
func GenerateContextCredentials(conf *config.Config, providerID string, contextCredentialsFactory local.ContextCredentialsFactory, logger *zap.Logger) (provider.ContextCredentials, error) {
logger.Info("Generating generateContextCredentials for ", zap.String("Provider ID", providerID))

AccountID := conf.Bluemix.IamClientID
Expand Down
14 changes: 7 additions & 7 deletions volume-providers/iam/token_exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"github.com/IBM-Bluemix/bluemix-cli-sdk/common/rest"

"github.com/IBM/ibmcloud-storage-volume-lib/config"
"github.com/IBM/ibmcloud-storage-volume-lib/provider/auth"
"github.com/IBM/ibmcloud-storage-volume-lib/lib/utils"
"github.com/IBM/ibmcloud-storage-volume-lib/provider/auth"
)

type tokenExchangeService struct {
Expand All @@ -49,7 +49,7 @@ type tokenExchangeRequest struct {
tes *tokenExchangeService
request *rest.Request
client *rest.Client
logger zap.Logger
logger *zap.Logger
}

type tokenExchangeResponse struct {
Expand All @@ -59,7 +59,7 @@ type tokenExchangeResponse struct {
}

// ExchangeRefreshTokenForAccessToken ...
func (tes *tokenExchangeService) ExchangeRefreshTokenForAccessToken(refreshToken string, logger zap.Logger) (*auth.AccessToken, error) {
func (tes *tokenExchangeService) ExchangeRefreshTokenForAccessToken(refreshToken string, logger *zap.Logger) (*auth.AccessToken, error) {
r := tes.newTokenExchangeRequest(logger)

r.request.Field("grant_type", "refresh_token")
Expand All @@ -69,7 +69,7 @@ func (tes *tokenExchangeService) ExchangeRefreshTokenForAccessToken(refreshToken
}

// ExchangeAccessTokenForIMSToken ...
func (tes *tokenExchangeService) ExchangeAccessTokenForIMSToken(accessToken auth.AccessToken, logger zap.Logger) (*auth.IMSToken, error) {
func (tes *tokenExchangeService) ExchangeAccessTokenForIMSToken(accessToken auth.AccessToken, logger *zap.Logger) (*auth.IMSToken, error) {
r := tes.newTokenExchangeRequest(logger)

r.request.Field("grant_type", "urn:ibm:params:oauth:grant-type:derive")
Expand All @@ -80,7 +80,7 @@ func (tes *tokenExchangeService) ExchangeAccessTokenForIMSToken(accessToken auth
}

// ExchangeIAMAPIKeyForIMSToken ...
func (tes *tokenExchangeService) ExchangeIAMAPIKeyForIMSToken(iamAPIKey string, logger zap.Logger) (*auth.IMSToken, error) {
func (tes *tokenExchangeService) ExchangeIAMAPIKeyForIMSToken(iamAPIKey string, logger *zap.Logger) (*auth.IMSToken, error) {
r := tes.newTokenExchangeRequest(logger)

r.request.Field("grant_type", "urn:ibm:params:oauth:grant-type:apikey")
Expand All @@ -91,7 +91,7 @@ func (tes *tokenExchangeService) ExchangeIAMAPIKeyForIMSToken(iamAPIKey string,
}

// ExchangeIAMAPIKeyForAccessToken ...
func (tes *tokenExchangeService) ExchangeIAMAPIKeyForAccessToken(iamAPIKey string, logger zap.Logger) (*auth.AccessToken, error) {
func (tes *tokenExchangeService) ExchangeIAMAPIKeyForAccessToken(iamAPIKey string, logger *zap.Logger) (*auth.AccessToken, error) {
r := tes.newTokenExchangeRequest(logger)

r.request.Field("grant_type", "urn:ibm:params:oauth:grant-type:apikey")
Expand Down Expand Up @@ -119,7 +119,7 @@ func (r *tokenExchangeRequest) exchangeForIMSToken() (*auth.IMSToken, error) {
}, nil
}

func (tes *tokenExchangeService) newTokenExchangeRequest(logger zap.Logger) *tokenExchangeRequest {
func (tes *tokenExchangeService) newTokenExchangeRequest(logger *zap.Logger) *tokenExchangeRequest {
client := rest.NewClient()
client.HTTPClient = tes.httpClient

Expand Down
22 changes: 11 additions & 11 deletions volume-providers/iam/token_exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func Test_ExchangeRefreshTokenForAccessToken_Success(t *testing.T) {
tes, err := NewTokenExchangeService(&bluemixConf)
assert.NoError(t, err)

r, err := tes.ExchangeRefreshTokenForAccessToken("testrefreshtoken", *logger)
r, err := tes.ExchangeRefreshTokenForAccessToken("testrefreshtoken", logger)
assert.Nil(t, err)
if assert.NotNil(t, r) {
assert.Equal(t, (*r).Token, "at_success")
Expand Down Expand Up @@ -113,7 +113,7 @@ func Test_ExchangeRefreshTokenForAccessToken_FailedDuringRequest(t *testing.T) {
tes, err := NewTokenExchangeService(&bluemixConf)
assert.NoError(t, err)

r, err := tes.ExchangeRefreshTokenForAccessToken("badrefreshtoken", *logger)
r, err := tes.ExchangeRefreshTokenForAccessToken("badrefreshtoken", logger)
assert.Nil(t, r)
if assert.NotNil(t, err) {
assert.Equal(t, "IAM token exchange request failed: did not work", err.Error())
Expand Down Expand Up @@ -145,7 +145,7 @@ func Test_ExchangeRefreshTokenForAccessToken_FailedDuringRequest_no_message(t *t
tes, err := NewTokenExchangeService(&bluemixConf)
assert.NoError(t, err)

r, err := tes.ExchangeRefreshTokenForAccessToken("badrefreshtoken", *logger)
r, err := tes.ExchangeRefreshTokenForAccessToken("badrefreshtoken", logger)
assert.Nil(t, r)
if assert.NotNil(t, err) {
assert.Equal(t, "Unexpected IAM token exchange response", err.Error())
Expand Down Expand Up @@ -177,7 +177,7 @@ func Test_ExchangeRefreshTokenForAccessToken_FailedNoIamUrl(t *testing.T) {
tes, err := NewTokenExchangeService(&bluemixConf)
assert.NoError(t, err)

r, err := tes.ExchangeRefreshTokenForAccessToken("testrefreshtoken", *logger)
r, err := tes.ExchangeRefreshTokenForAccessToken("testrefreshtoken", logger)
assert.Nil(t, r)

if assert.NotNil(t, err) {
Expand Down Expand Up @@ -211,7 +211,7 @@ func Test_ExchangeRefreshTokenForAccessToken_FailedRequesting_empty_body(t *test
tes, err := NewTokenExchangeService(&bluemixConf)
assert.NoError(t, err)

r, err := tes.ExchangeRefreshTokenForAccessToken("badrefreshtoken", *logger)
r, err := tes.ExchangeRefreshTokenForAccessToken("badrefreshtoken", logger)
assert.Nil(t, r)

if assert.NotNil(t, err) {
Expand Down Expand Up @@ -246,7 +246,7 @@ func Test_ExchangeAccessTokenForIMSToken_Success(t *testing.T) {
tes, err := NewTokenExchangeService(&bluemixConf)
assert.NoError(t, err)

r, err := tes.ExchangeAccessTokenForIMSToken(auth.AccessToken{Token: "testaccesstoken"}, *logger)
r, err := tes.ExchangeAccessTokenForIMSToken(auth.AccessToken{Token: "testaccesstoken"}, logger)
assert.Nil(t, err)
if assert.NotNil(t, r) {
assert.Equal(t, (*r).UserID, 123)
Expand Down Expand Up @@ -282,7 +282,7 @@ func Test_ExchangeAccessTokenForIMSToken_FailedDuringRequest(t *testing.T) {
tes, err := NewTokenExchangeService(&bluemixConf)
assert.NoError(t, err)

r, err := tes.ExchangeAccessTokenForIMSToken(auth.AccessToken{Token: "badaccesstoken"}, *logger)
r, err := tes.ExchangeAccessTokenForIMSToken(auth.AccessToken{Token: "badaccesstoken"}, logger)
assert.Nil(t, r)
if assert.NotNil(t, err) {
assert.Equal(t, "IAM token exchange request failed: did not work", err.Error())
Expand Down Expand Up @@ -321,7 +321,7 @@ func Test_ExchangeAccessTokenForIMSToken_FailedAccountLocked(t *testing.T) {
tes, err := NewTokenExchangeService(&bluemixConf)
assert.NoError(t, err)

r, err := tes.ExchangeAccessTokenForIMSToken(auth.AccessToken{Token: "badaccesstoken"}, *logger)
r, err := tes.ExchangeAccessTokenForIMSToken(auth.AccessToken{Token: "badaccesstoken"}, logger)
assert.Nil(t, r)
if assert.NotNil(t, err) {
assert.Equal(t, "Infrastructure account is temporarily locked", err.Error())
Expand Down Expand Up @@ -354,7 +354,7 @@ func Test_ExchangeAccessTokenForIMSToken_FailedDuringRequest_no_message(t *testi
tes, err := NewTokenExchangeService(&bluemixConf)
assert.NoError(t, err)

r, err := tes.ExchangeAccessTokenForIMSToken(auth.AccessToken{Token: "badrefreshtoken"}, *logger)
r, err := tes.ExchangeAccessTokenForIMSToken(auth.AccessToken{Token: "badrefreshtoken"}, logger)
assert.Nil(t, r)
if assert.NotNil(t, err) {
assert.Equal(t, "Unexpected IAM token exchange response", err.Error())
Expand Down Expand Up @@ -385,7 +385,7 @@ func Test_ExchangeAccessTokenForIMSToken_FailedRequesting_empty_body(t *testing.
tes, err := NewTokenExchangeService(&bluemixConf)
assert.NoError(t, err)

r, err := tes.ExchangeAccessTokenForIMSToken(auth.AccessToken{Token: "badrefreshtoken"}, *logger)
r, err := tes.ExchangeAccessTokenForIMSToken(auth.AccessToken{Token: "badrefreshtoken"}, logger)
assert.Nil(t, r)

if assert.NotNil(t, err) {
Expand Down Expand Up @@ -465,7 +465,7 @@ func Test_ExchangeIAMAPIKeyForAccessToken(t *testing.T) {
tes, err := NewTokenExchangeService(&bluemixConf)
assert.NoError(t, err)

r, actualError := tes.ExchangeIAMAPIKeyForAccessToken("apikey1", *logger)
r, actualError := tes.ExchangeIAMAPIKeyForAccessToken("apikey1", logger)
if testCase.expectedError == nil {
assert.NoError(t, actualError)
if assert.NotNil(t, r) {
Expand Down
2 changes: 1 addition & 1 deletion volume-providers/softlayer/auth/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

// ForAPIKey ...
func (ccf *contextCredentialsFactory) ForIaaSAPIKey(iamAccountID, userid, apikey string, logger zap.Logger) (provider.ContextCredentials, error) {
func (ccf *contextCredentialsFactory) ForIaaSAPIKey(iamAccountID, userid, apikey string, logger *zap.Logger) (provider.ContextCredentials, error) {
return provider.ContextCredentials{
AuthType: provider.IaaSAPIKey,
IAMAccountID: iamAccountID,
Expand Down
2 changes: 1 addition & 1 deletion volume-providers/softlayer/auth/apikey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func Test_ForIaaSAPIKey(t *testing.T) {
},
}

contextCredentials, err := ccf.ForIaaSAPIKey(account, username, apiKey, *logger)
contextCredentials, err := ccf.ForIaaSAPIKey(account, username, apiKey, logger)

assert.NoError(t, err)

Expand Down
Loading

0 comments on commit 2218e46

Please sign in to comment.