From 1219f203eb3721176f23f087e27f766ce21333df Mon Sep 17 00:00:00 2001 From: Dom Del Nano Date: Wed, 4 Oct 2023 16:54:41 -0700 Subject: [PATCH] Ensure all invocations of XO client creation provide retry parameters --- client/client.go | 28 ++++++++++++++++++++++++++++ xoa/internal/mocks.go | 12 +----------- xoa/provider_test.go | 6 ++++++ 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/client/client.go b/client/client.go index a2a84c0d..6f729d84 100644 --- a/client/client.go +++ b/client/client.go @@ -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 } @@ -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, } } @@ -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 } diff --git a/xoa/internal/mocks.go b/xoa/internal/mocks.go index 36453734..a7cb3dc5 100644 --- a/xoa/internal/mocks.go +++ b/xoa/internal/mocks.go @@ -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()) } diff --git a/xoa/provider_test.go b/xoa/provider_test.go index 5d83404b..cb911afa 100644 --- a/xoa/provider_test.go +++ b/xoa/provider_test.go @@ -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") + } }