Skip to content

Commit

Permalink
Fix DS & Add Retry (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
doriac11 authored Nov 15, 2024
1 parent aaea760 commit 51ba597
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 29 deletions.
2 changes: 1 addition & 1 deletion powerflex/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
62 changes: 42 additions & 20 deletions powerflex/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"context"
"os"
"strconv"
"strings"
"time"

"github.com/dell/goscaleio"
"github.com/hashicorp/terraform-plugin-framework/datasource"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
21 changes: 13 additions & 8 deletions powerflex/provider/sdc_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package provider

import (
"context"
"strings"

"terraform-provider-powerflex/powerflex/helper"
"terraform-provider-powerflex/powerflex/models"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 51ba597

Please sign in to comment.