Skip to content

Commit 4119420

Browse files
sneharai4Sneha Rai
andauthored
Added csp client timeout to be configurable (#149)
* Added csp client timeout to be configurable Signed-off-by: Sneha Rai <[email protected]> * Updated as per review and added UT Signed-off-by: Sneha Rai <[email protected]> * Updated as per review Signed-off-by: Sneha Rai <[email protected]> * Updated UT Signed-off-by: Sneha Rai <[email protected]> * Updated UT Signed-off-by: Sneha Rai <[email protected]> Co-authored-by: Sneha Rai <[email protected]>
1 parent 8206d7f commit 4119420

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

storageprovider/csp/container_storage_provider.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -936,12 +936,15 @@ func getCspClient(credentials *storageprovider.Credentials) (*connectivity.Clien
936936
// Off-Array CSP
937937
cspURI := fmt.Sprintf("http://%s:%d", credentials.ServiceName, credentials.ServicePort)
938938

939-
log.Tracef(">>>>> getCspClient (service) using URI %s and username %s", cspURI, credentials.Username)
939+
if credentials.CspClientTimeout == 0 {
940+
credentials.CspClientTimeout = storageprovider.DefaultCSPClientTimeout
941+
}
942+
log.Tracef(">>>>> getCspClient (service) using URI %s and username %s with timeout set to %d seconds", cspURI, credentials.Username, credentials.CspClientTimeout)
940943
defer log.Trace("<<<<< getCspClient")
941944

945+
cspHTTPClientTimeout := time.Duration(credentials.CspClientTimeout)*time.Second
942946
// Setup HTTP client to the CSP service
943-
cspClient := connectivity.NewHTTPClient(cspURI)
944-
947+
cspClient := connectivity.NewHTTPClientWithTimeout(cspURI, cspHTTPClientTimeout)
945948
return cspClient, nil
946949
}
947950

storageprovider/storage_provider.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ package storageprovider
44

55
import (
66
"fmt"
7-
"strconv"
8-
97
log "github.com/hpe-storage/common-host-libs/logger"
108
"github.com/hpe-storage/common-host-libs/model"
9+
"strconv"
1110
)
1211

1312
const (
@@ -21,6 +20,8 @@ const (
2120
DefaultContextPath = "/csp"
2221
// DefaultServicePort if not set for on-array csp
2322
DefaultServicePort = 443
23+
// DefaultCSPClientTimeout if not set for off-array csp
24+
DefaultCSPClientTimeout = 60
2425
)
2526

2627
// StorageProvider defines the interface to any storage related operations required by CSI and hopefully docker
@@ -50,12 +51,13 @@ type StorageProvider interface {
5051

5152
// Credentials defines how a StorageProvider is accessed
5253
type Credentials struct {
53-
Username string
54-
Password string
55-
Backend string
56-
ServicePort int
57-
ContextPath string
58-
ServiceName string
54+
Username string
55+
Password string
56+
Backend string
57+
ServicePort int
58+
ContextPath string
59+
ServiceName string
60+
CspClientTimeout int64
5961
}
6062

6163
// CreateCredentials creates the credentail object from the given secrets
@@ -65,7 +67,7 @@ func CreateCredentials(secrets map[string]string) (*Credentials, error) {
6567

6668
// When secrets specified
6769
if secrets == nil || len(secrets) == 0 {
68-
return nil, fmt.Errorf("No secrets have been provided")
70+
return nil, fmt.Errorf("no secrets have been provided")
6971
}
7072

7173
credentials := &Credentials{}
@@ -116,5 +118,9 @@ func CreateCredentials(secrets map[string]string) (*Credentials, error) {
116118
return nil, fmt.Errorf("Missing port in the secrets")
117119
}
118120

121+
if credentials.ServiceName != "" && credentials.CspClientTimeout == 0 {
122+
credentials.CspClientTimeout = 60
123+
}
124+
119125
return credentials, nil
120126
}

storageprovider/storage_provider_test.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func TestCreateCredentials(t *testing.T) {
2727
Backend: "1.1.1.1",
2828
ServiceName: "csp-service",
2929
ServicePort: 8080,
30+
CspClientTimeout: DefaultCSPClientTimeout,
3031
}
3132

3233
// Valid On-Array params
@@ -81,19 +82,38 @@ func TestCreateCredentials(t *testing.T) {
8182
// Empty params map
8283
map7 := map[string]string{}
8384

85+
// Valid Off-Array params with csp client timeout
86+
map8 := map[string]string{
87+
usernameKey: "admin",
88+
passwordKey: "admin",
89+
backendKey: "1.1.1.1",
90+
serviceNameKey: "csp-service",
91+
servicePortKey: "8080",
92+
}
93+
// Valid Off-Array credential with csp client timeout
94+
cred8 := &Credentials{
95+
Username: "admin",
96+
Password: "admin",
97+
Backend: "1.1.1.1",
98+
ServiceName: "csp-service",
99+
ServicePort: 8080,
100+
CspClientTimeout: DefaultCSPClientTimeout,
101+
}
102+
84103
tests := []struct {
85104
name string
86105
args args
87106
want *Credentials
88107
wantErr bool
89108
}{
90-
{"Test valid on-array args", args{map1}, cred1, false},
91-
{"Test valid off-array args", args{map2}, cred2, false},
109+
{"Test valid off-array args", args{map1}, cred1, false},
110+
{"Test valid on-array args", args{map2}, cred2, false},
92111
{"Test missing/invalid port", args{map3}, nil, true},
93112
{"Test missing backend", args{map4}, nil, true},
94113
{"Test missing username", args{map5}, nil, true},
95114
{"Test missing password", args{map6}, nil, true},
96115
{"Test empty credentials", args{map7}, nil, true},
116+
{"Test valid off-array credentials with csp client timeout", args{map8}, cred8, false},
97117
}
98118
for _, tt := range tests {
99119
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)