Skip to content

Commit

Permalink
Ensure all invocations of XO client creation provide retry parameters (
Browse files Browse the repository at this point in the history
  • Loading branch information
ddelnano authored Oct 4, 2023
1 parent 0267d33 commit dec8d50
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
27 changes: 27 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
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(client.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 dec8d50

Please sign in to comment.