From 51ba5979a77d71799e4afab19369518150916495 Mon Sep 17 00:00:00 2001 From: Connor Doria <128822122+doriac11@users.noreply.github.com> Date: Fri, 15 Nov 2024 08:01:33 -0500 Subject: [PATCH] Fix DS & Add Retry (#244) --- powerflex/helper/helper.go | 2 +- powerflex/provider/provider.go | 62 +++++++++++++++++++--------- powerflex/provider/sdc_datasource.go | 21 ++++++---- 3 files changed, 56 insertions(+), 29 deletions(-) diff --git a/powerflex/helper/helper.go b/powerflex/helper/helper.go index e2251152..0035a751 100644 --- a/powerflex/helper/helper.go +++ b/powerflex/helper/helper.go @@ -43,7 +43,7 @@ import ( func GetFirstSystem(rc *goscaleio.Client) (*goscaleio.System, error) { allSystems, err := rc.GetSystems() if err != nil { - return nil, fmt.Errorf("Error in goscaleio GetSystems") + return nil, fmt.Errorf("Error in goscaleio GetSystems %s", err.Error()) } if numSys := len((allSystems)); numSys == 0 { return nil, fmt.Errorf("no systems found") diff --git a/powerflex/provider/provider.go b/powerflex/provider/provider.go index 3bb26ac4..e998e7a2 100644 --- a/powerflex/provider/provider.go +++ b/powerflex/provider/provider.go @@ -21,6 +21,8 @@ import ( "context" "os" "strconv" + "strings" + "time" "github.com/dell/goscaleio" "github.com/hashicorp/terraform-plugin-framework/datasource" @@ -102,7 +104,7 @@ func (p *powerflexProvider) Schema(_ context.Context, _ provider.SchemaRequest, // Configure - provider pre-initiate calle function. func (p *powerflexProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) { tflog.Info(ctx, "Configuring powerflex client") - + retryEOFCounter := 5 var config powerflexProviderModel var timeout int diags := req.Config.Get(ctx, &config) @@ -233,30 +235,50 @@ func (p *powerflexProvider) Configure(ctx context.Context, req provider.Configur goscaleioConf.Password = password goscaleioConf.Insecure = insecure - // Create a new PowerFlex gateway client using the configuration values - gatewayClient, err := goscaleio.NewGateway(goscaleioConf.Endpoint, goscaleioConf.Username, goscaleioConf.Password, goscaleioConf.Insecure, true) - if err != nil { - resp.Diagnostics.AddError( - "Unable to Create gateway API Client", - "An unexpected error occurred when creating the gateway API client. "+ - "If the error is not clear, please contact the provider developers.\n\n"+ - "gateway Client Error: "+err.Error(), - ) - return - } - - p.gatewayClient = gatewayClient + for i := 0; i < retryEOFCounter; i++ { + // Create a new PowerFlex gateway client using the configuration values + gatewayClient, err := goscaleio.NewGateway(goscaleioConf.Endpoint, goscaleioConf.Username, goscaleioConf.Password, goscaleioConf.Insecure, true) + if err != nil { + // Sometimes the Powerflex Gateway gets inidated with requests + // In these cases just wait 5 seconds and try again + // We will retry up to 5 times before just failing + if strings.Contains(err.Error(), "EOF") { + time.Sleep(2 * time.Second) + continue + } + resp.Diagnostics.AddError( + "Unable to Create gateway API Client", + "An unexpected error occurred when creating the gateway API client. "+ + "If the error is not clear, please contact the provider developers.\n\n"+ + "gateway Client Error: "+err.Error(), + ) + return + } - _, err = Client.Authenticate(&goscaleioConf) + p.gatewayClient = gatewayClient + break + } - if err != nil { + for i := 0; i < retryEOFCounter; i++ { + // Create a new PowerFlex gateway client using the configuration values + _, err = Client.Authenticate(&goscaleioConf) - p.clientError = "An unexpected error occurred when authenticating the Goscaleio API Client. " + - "Unable to Authenticate Goscaleio API Client.\n\n" + - "powerflex Client Error: " + err.Error() + if err != nil { + // Sometimes the Powerflex Gateway gets inidated with request + // In these cases just wait 2 seconds and try again + // We will retry up to 5 times before just failing + if strings.Contains(err.Error(), "EOF") { + time.Sleep(2 * time.Second) + continue + } + p.clientError = "An unexpected error occurred when authenticating the Goscaleio API Client. " + + "Unable to Authenticate Goscaleio API Client.\n\n" + + "powerflex Client Error: " + err.Error() + return + } - } else { p.client = Client + break } resp.DataSourceData = p diff --git a/powerflex/provider/sdc_datasource.go b/powerflex/provider/sdc_datasource.go index fb930692..080486d6 100644 --- a/powerflex/provider/sdc_datasource.go +++ b/powerflex/provider/sdc_datasource.go @@ -19,6 +19,7 @@ package provider import ( "context" + "strings" "terraform-provider-powerflex/powerflex/helper" "terraform-provider-powerflex/powerflex/models" @@ -96,16 +97,20 @@ func (d *sdcDataSource) Read(ctx context.Context, req datasource.ReadRequest, re ) return } - - // SDC endpoints returns both SDC and NVMe host. Need to filter out NVMe host - n := 0 - for _, sdc := range sdcs { - if sdc.HostType == "SdcHost" { - sdcs[n] = sdc - n++ + tflog.Info(ctx, "system Version"+system.System.SystemVersionName) + + // If it is < 4_0 there is no NVME/HostType value so we should skip this filter + if !strings.Contains(system.System.SystemVersionName, "3_6") { + // SDC endpoints returns both SDC and NVMe host. Need to filter out NVMe host + n := 0 + for _, sdc := range sdcs { + if sdc.HostType == "SdcHost" { + sdcs[n] = sdc + n++ + } } + sdcs = sdcs[:n] } - sdcs = sdcs[:n] // Set state searchFilter := helper.SdcFilterType.All