-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update dependencies and remove dependency on csi-powerstore (#19)
- Loading branch information
Showing
10 changed files
with
255 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright (c) 2021 Dell Inc., or its subsidiaries. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
package common | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"path/filepath" | ||
"regexp" | ||
"strconv" | ||
|
||
csictx "github.com/dell/gocsi/context" | ||
|
||
"github.com/dell/csm-metrics-powerstore/internal/service" | ||
"github.com/dell/gopowerstore" | ||
"github.com/sirupsen/logrus" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
const ( | ||
// EnvThrottlingRateLimit sets a number of concurrent requests to APi | ||
EnvThrottlingRateLimit = "X_CSI_POWERSTORE_THROTTLING_RATE_LIMIT" | ||
) | ||
|
||
// GetPowerStoreArrays parses config.yaml file, initializes gopowerstore Clients and composes map of arrays for ease of access. | ||
// It will return array that can be used as default as a second return parameter. | ||
// If config does not have any array as a default then the first will be returned as a default. | ||
func GetPowerStoreArrays(filePath string, logger *logrus.Logger) (map[string]*service.PowerStoreArray, map[string]string, *service.PowerStoreArray, error) { | ||
type config struct { | ||
Arrays []*service.PowerStoreArray `yaml:"arrays"` | ||
} | ||
|
||
data, err := ioutil.ReadFile(filepath.Clean(filePath)) | ||
if err != nil { | ||
logger.WithError(err).Errorf("cannot read file %s", filePath) | ||
return nil, nil, nil, err | ||
} | ||
|
||
var cfg config | ||
err = yaml.Unmarshal(data, &cfg) | ||
if err != nil { | ||
logger.WithError(err).Errorf("cannot unmarshal data") | ||
return nil, nil, nil, err | ||
} | ||
|
||
arrayMap := make(map[string]*service.PowerStoreArray) | ||
mapper := make(map[string]string) | ||
var defaultArray *service.PowerStoreArray | ||
foundDefault := false | ||
|
||
if len(cfg.Arrays) == 0 { | ||
return arrayMap, mapper, defaultArray, nil | ||
} | ||
|
||
// Safeguard if user doesn't set any array as default, we just use first one | ||
defaultArray = cfg.Arrays[0] | ||
|
||
// Convert to map for convenience and init gopowerstore.Client | ||
for _, array := range cfg.Arrays { | ||
array := array | ||
if array == nil { | ||
return arrayMap, mapper, defaultArray, nil | ||
} | ||
if array.GlobalID == "" { | ||
return nil, nil, nil, errors.New("no GlobalID field found in config.yaml, update config.yaml according to the documentation") | ||
} | ||
clientOptions := gopowerstore.NewClientOptions() | ||
clientOptions.SetInsecure(array.Insecure) | ||
|
||
if throttlingRateLimit, ok := csictx.LookupEnv(context.Background(), EnvThrottlingRateLimit); ok { | ||
rateLimit, err := strconv.Atoi(throttlingRateLimit) | ||
if err != nil { | ||
logger.Errorf("can't get throttling rate limit, using default") | ||
} else { | ||
clientOptions.SetRateLimit(uint64(rateLimit)) | ||
} | ||
} | ||
|
||
c, err := gopowerstore.NewClientWithArgs( | ||
array.Endpoint, array.Username, array.Password, clientOptions) | ||
if err != nil { | ||
return nil, nil, nil, status.Errorf(codes.FailedPrecondition, | ||
"unable to create PowerStore client: %s", err.Error()) | ||
} | ||
array.Client = c | ||
|
||
ips := GetIPListFromString(array.Endpoint) | ||
if ips == nil { | ||
return nil, nil, nil, fmt.Errorf("can't get ips from endpoint: %s", array.Endpoint) | ||
} | ||
|
||
ip := ips[0] | ||
array.IP = ip | ||
logger.Infof("%s,%s,%s,%s,%t,%t,%s", array.Endpoint, array.GlobalID, array.Username, array.NasName, array.Insecure, array.IsDefault, array.BlockProtocol) | ||
arrayMap[array.GlobalID] = array | ||
mapper[ip] = array.GlobalID | ||
if array.IsDefault && !foundDefault { | ||
defaultArray = array | ||
foundDefault = true | ||
} | ||
} | ||
|
||
return arrayMap, mapper, defaultArray, nil | ||
} | ||
|
||
// GetIPListFromString returns list of ips in string form found in input string | ||
// A return value of nil indicates no match | ||
func GetIPListFromString(input string) []string { | ||
re := regexp.MustCompile(`(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}`) | ||
return re.FindAllString(input, -1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright (c) 2021 Dell Inc., or its subsidiaries. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
package common_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/dell/csm-metrics-powerstore/internal/common" | ||
csictx "github.com/dell/gocsi/context" | ||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_Run(t *testing.T) { | ||
|
||
tests := map[string]func(t *testing.T) (filePath string, env map[string]string, expectError bool){ | ||
|
||
"success": func(*testing.T) (string, map[string]string, bool) { | ||
return "testdata/sample-config.yaml", map[string]string{common.EnvThrottlingRateLimit: "123"}, false | ||
}, | ||
"invalid throttling value": func(*testing.T) (string, map[string]string, bool) { | ||
return "testdata/sample-config.yaml", map[string]string{common.EnvThrottlingRateLimit: "abc"}, false | ||
}, | ||
"file doesn't exist": func(*testing.T) (string, map[string]string, bool) { | ||
return "testdata/no-file.yaml", nil, true | ||
}, | ||
"file format": func(*testing.T) (string, map[string]string, bool) { | ||
return "testdata/invalid-format.yaml", nil, true | ||
}, | ||
"no global id": func(*testing.T) (string, map[string]string, bool) { | ||
return "testdata/no-global-id.yaml", nil, true | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
|
||
logger := logrus.New() | ||
filePath, envs, expectError := test(t) | ||
|
||
for k, v := range envs { | ||
csictx.Setenv(context.Background(), k, v) | ||
} | ||
|
||
arrays, mapper, defaultArray, err := common.GetPowerStoreArrays(filePath, logger) | ||
|
||
if expectError { | ||
assert.Nil(t, arrays) | ||
assert.Nil(t, mapper) | ||
assert.Nil(t, defaultArray) | ||
assert.NotNil(t, err) | ||
} else { | ||
assert.NotNil(t, arrays) | ||
assert.NotNil(t, mapper) | ||
assert.NotNil(t, defaultArray) | ||
assert.Nil(t, err) | ||
} | ||
|
||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
invalid_format | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
arrays: | ||
- endpoint: "https://10.0.0.1/api/rest" | ||
username: "user" | ||
password: "password" | ||
skipCertificateValidation: true | ||
isDefault: true | ||
blockProtocol: "auto" | ||
nasName: "nas-server" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
arrays: | ||
- endpoint: "https://10.0.0.1/api/rest" | ||
globalID: "unique" | ||
username: "user" | ||
password: "password" | ||
skipCertificateValidation: true | ||
isDefault: true | ||
blockProtocol: "auto" | ||
nasName: "nas-server" | ||
|
||
- endpoint: "https://11.0.0.1/api/rest" | ||
globalID: "unique" | ||
username: "user" | ||
password: "password" | ||
skipCertificateValidation: true | ||
blockProtocol: "FC" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.