github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/session/session.go (about) 1 package session 2 3 import ( 4 "fmt" 5 "strconv" 6 "time" 7 8 bluemix "github.com/IBM-Cloud/bluemix-go" 9 "github.com/IBM-Cloud/bluemix-go/endpoints" 10 "github.com/IBM-Cloud/bluemix-go/helpers" 11 "github.com/IBM-Cloud/bluemix-go/trace" 12 ) 13 14 //Session ... 15 type Session struct { 16 Config *bluemix.Config 17 } 18 19 //New ... 20 func New(configs ...*bluemix.Config) (*Session, error) { 21 var c *bluemix.Config 22 23 if len(configs) == 0 { 24 c = &bluemix.Config{} 25 } else { 26 c = configs[0] 27 } 28 sess := &Session{ 29 Config: c, 30 } 31 32 if len(c.IBMID) == 0 { 33 c.IBMID = helpers.EnvFallBack([]string{"IBMID"}, "") 34 } 35 36 if len(c.IBMIDPassword) == 0 { 37 c.IBMIDPassword = helpers.EnvFallBack([]string{"IBMID_PASSWORD"}, "") 38 } 39 40 if len(c.BluemixAPIKey) == 0 { 41 c.BluemixAPIKey = helpers.EnvFallBack([]string{"IC_API_KEY", "IBMCLOUD_API_KEY", "BM_API_KEY", "BLUEMIX_API_KEY"}, "") 42 } 43 44 if len(c.IAMAccessToken) == 0 { 45 c.IAMAccessToken = helpers.EnvFallBack([]string{"IC_IAM_TOKEN", "IBMCLOUD_IAM_TOKEN"}, "") 46 } 47 48 if len(c.IAMRefreshToken) == 0 { 49 c.IAMRefreshToken = helpers.EnvFallBack([]string{"IC_IAM_REFRESH_TOKEN", "IBMCLOUD_IAM_REFRESH_TOKEN"}, "") 50 } 51 52 if len(c.Region) == 0 { 53 c.Region = helpers.EnvFallBack([]string{"IC_REGION", "IBMCLOUD_REGION", "BM_REGION", "BLUEMIX_REGION"}, "us-south") 54 } 55 if c.MaxRetries == nil { 56 c.MaxRetries = helpers.Int(3) 57 retries := helpers.EnvFallBack([]string{"MAX_RETRIES"}, "3") 58 i, err := strconv.Atoi(retries) 59 if err != nil { 60 fmt.Printf("MAX_RETRIES has invalid retries format. Default retries will be set to %q", *c.MaxRetries) 61 } 62 if i < 0 { 63 fmt.Printf("MAX_RETRIES has invalid retries format. Default retries will be set to %q", *c.MaxRetries) 64 } 65 if err == nil && i >= 0 { 66 c.MaxRetries = &i 67 } 68 } 69 if c.HTTPTimeout == 0 { 70 c.HTTPTimeout = 180 * time.Second 71 timeout := helpers.EnvFallBack([]string{"IC_TIMEOUT", "IBMCLOUD_TIMEOUT", "BM_TIMEOUT", "BLUEMIX_TIMEOUT"}, "180") 72 timeoutDuration, err := time.ParseDuration(fmt.Sprintf("%ss", timeout)) 73 if err != nil { 74 fmt.Printf("IC_TIMEOUT or IBMCLOUD_TIMEOUT has invalid time format. Default timeout will be set to %q", c.HTTPTimeout) 75 } 76 if err == nil { 77 c.HTTPTimeout = timeoutDuration 78 } 79 } 80 if len(c.Visibility) == 0 { 81 c.Visibility = helpers.EnvFallBack([]string{"IC_VISIBILITY", "IBMCLOUD_VISIBILITY"}, "public") 82 } 83 if len(c.EndpointsFile) == 0 { 84 c.EndpointsFile = helpers.EnvFallBack([]string{"IC_ENDPOINTS_FILE_PATH", "IBMCLOUD_ENDPOINTS_FILE_PATH"}, "") 85 } 86 if c.RetryDelay == nil { 87 c.RetryDelay = helpers.Duration(30 * time.Second) 88 } 89 if c.EndpointLocator == nil { 90 c.EndpointLocator = endpoints.NewEndpointLocator(c.Region, c.Visibility, c.EndpointsFile) 91 } 92 93 if c.Debug { 94 trace.Logger = trace.NewLogger("true") 95 } 96 return sess, nil 97 } 98 99 //Copy allows sessions to create a copy of it and optionally override any defaults via the config 100 func (s *Session) Copy(mccpgs ...*bluemix.Config) *Session { 101 return &Session{ 102 Config: s.Config.Copy(mccpgs...), 103 } 104 }