Skip to content

Commit

Permalink
Ensure GetConfigFromEnv sets the retry mode and max time parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
ddelnano committed Oct 4, 2023
1 parent 5d1f509 commit 75ddbf2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
28 changes: 28 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,20 @@ var dialer = gorillawebsocket.Dialer{
WriteBufferSize: MaxMessageSize,
}

var (
retryModeMap = map[string]RetryMode{
"none": None,
"backoff": Backoff,
}
)

func GetConfigFromEnv() Config {
var wsURL string
var username string
var password string
insecure := false
retryMode := None
retryMaxTime := 5 * time.Minute
if v := os.Getenv("XOA_URL"); v != "" {
wsURL = v
}
Expand All @@ -168,11 +177,29 @@ func GetConfigFromEnv() Config {
if v := os.Getenv("XOA_INSECURE"); v != "" {
insecure = true
}
if v := os.Getenv("XOA_RETRY_MODE"); v != "" {
retry, ok := retryModeMap[v]
if !ok {
fmt.Println("[ERROR] failed to set retry mode, disabling retries")
} else {
retryMode = retry
}
}
if v := os.Getenv("XOA_RETRY_MAX_TIME"); v != "" {
duration, err := time.ParseDuration(v)
if err == nil {
retryMaxTime = duration
} else {
fmt.Println("[ERROR] failed to set retry mode, disabling retries\n")
}
}
return Config{
Url: wsURL,
Username: username,
Password: password,
InsecureSkipVerify: insecure,
RetryMode: retryMode,
RetryMaxTime: retryMaxTime,
}
}

Expand Down Expand Up @@ -248,6 +275,7 @@ func NewClient(config Config) (XOClient, error) {

func (c *Client) IsRetryableError(err jsonrpc2.Error) bool {

fmt.Printf("[ERROR] retry mode: %d and timeout %v, Handling jsonrpc2: %v\n", c.RetryMode, c.RetryMaxTime, err)
if c.RetryMode == None {
return false
}
Expand Down
12 changes: 1 addition & 11 deletions xoa/internal/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,5 @@ func newFailToStartAndHaltClient(config client.Config) (client.XOClient, error)
}

func GetFailToStartAndHaltXOClient(d *schema.ResourceData) (interface{}, error) {
url := d.Get("url").(string)
username := d.Get("username").(string)
password := d.Get("password").(string)
insecure := d.Get("insecure").(bool)
config := client.Config{
Url: url,
Username: username,
Password: password,
InsecureSkipVerify: insecure,
}
return newFailToStartAndHaltClient(config)
return newFailToStartAndHaltClient(GetConfigFromEnv())

Check failure on line 37 in xoa/internal/mocks.go

View workflow job for this annotation

GitHub Actions / Build (1.16, ubuntu-latest)

undefined: GetConfigFromEnv
}
6 changes: 6 additions & 0 deletions xoa/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,10 @@ func testAccPreCheck(t *testing.T) {
if v := os.Getenv("XOA_ISO_SR"); v == "" {
t.Fatal("The XOA_ISO_SR environment variable must be set")
}
if v := os.Getenv("XOA_RETRY_MAX_TIME"); v == "" {
t.Fatal("The XOA_RETRY_MAX_TIME environment variable must be set")
}
if v := os.Getenv("XOA_RETRY_MODE"); v == "" {
t.Fatal("The XOA_RETRY_MODE environment variable must be set")
}
}

0 comments on commit 75ddbf2

Please sign in to comment.