util: modify GetMonsAndClusterID() to take clusterID instead of options

This commit:
- modifies GetMonsAndClusterID() to take clusterID instead of options.
- moves out validation of clusterID is set or not out of GetMonsAndClusterID().
- defines ErrClusterIDNotSet new error for reusability.
- add GetClusterID() to obtain clusterID from options.

Signed-off-by: Rakshith R <rar@redhat.com>
This commit is contained in:
Rakshith R
2021-09-07 11:35:11 +05:30
committed by mergify[bot]
parent 9d1e98ca60
commit 82d09d81cf
4 changed files with 36 additions and 22 deletions

View File

@ -19,7 +19,6 @@ package util
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"strings"
@ -32,6 +31,9 @@ const (
// CsiConfigFile is the location of the CSI config file.
CsiConfigFile = "/etc/ceph-csi-config/config.json"
// ClusterIDKey is the name of the key containing clusterID.
clusterIDKey = "clusterID"
)
// ClusterInfo strongly typed JSON spec for the below JSON structure.
@ -132,11 +134,7 @@ func CephFSSubvolumeGroup(pathToConfig, clusterID string) (string, error) {
// GetMonsAndClusterID returns monitors and clusterID information read from
// configfile.
func GetMonsAndClusterID(ctx context.Context, options map[string]string, checkClusterIDMapping bool) (string, string, error) {
clusterID, ok := options["clusterID"]
if !ok {
return "", "", errors.New("clusterID must be set")
}
func GetMonsAndClusterID(ctx context.Context, clusterID string, checkClusterIDMapping bool) (string, string, error) {
if checkClusterIDMapping {
monitors, mappedClusterID, err := FetchMappedClusterIDAndMons(ctx, clusterID)
if err != nil {
@ -153,3 +151,13 @@ func GetMonsAndClusterID(ctx context.Context, options map[string]string, checkCl
return monitors, clusterID, nil
}
// GetClusterID fetches clusterID from given options map.
func GetClusterID(options map[string]string) (string, error) {
clusterID, ok := options[clusterIDKey]
if !ok {
return "", ErrClusterIDNotSet
}
return clusterID, nil
}