/
request.go
94 lines (86 loc) · 2.44 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2018 NetApp, Inc. All Rights Reserved.
package storageattribute
import (
"encoding/json"
"fmt"
"strconv"
"strings"
)
func UnmarshalRequestMap(mapJSON json.RawMessage) (
map[string]Request, error,
) {
var tmp map[string]string
ret := make(map[string]Request)
if mapJSON == nil {
return nil, nil
}
err := json.Unmarshal(mapJSON, &tmp)
if err != nil {
return nil, fmt.Errorf("unable to unmarshal map: %v", err)
}
for name, stringVal := range tmp {
ret[name], err = CreateAttributeRequestFromAttributeValue(name, stringVal)
if err != nil {
return nil, err
}
}
return ret, nil
}
func MarshalRequestMap(requestMap map[string]Request) ([]byte, error) {
if requestMap == nil {
return nil, nil
}
genericMap := make(map[string]string, len(requestMap))
for k, v := range requestMap {
genericMap[k] = v.String()
}
return json.Marshal(genericMap)
}
func CreateAttributeRequestFromAttributeValue(name, val string) (Request, error) {
var req Request
var err error
valType, ok := attrTypes[name]
if !ok {
return nil, fmt.Errorf("unrecognized storage attribute: %s", name)
}
switch valType {
case boolType:
v, err := strconv.ParseBool(val)
if err != nil {
return nil, fmt.Errorf("storage attribute value (%s) doesn't match the specified type (%s)", val, valType)
}
req = NewBoolRequest(v)
case intType:
v, err := strconv.ParseInt(val, 10, 0)
if err != nil {
return nil, fmt.Errorf("storage attribute value (%s) doesn't match the specified type (%s)", val, valType)
}
req = NewIntRequest(int(v))
case stringType:
req = NewStringRequest(val)
case labelType:
req, err = NewLabelRequest(val)
if err != nil {
return nil, fmt.Errorf("storage attribute value (%s) doesn't match the specified type (%s)", val, valType)
}
default:
return nil, fmt.Errorf("unrecognized type for a storage attribute request: %s", valType)
}
return req, nil
}
func CreateBackendStoragePoolsMapFromEncodedString(
arg string,
) (map[string][]string, error) {
backendPoolsMap := make(map[string][]string)
backendPoolsList := strings.Split(arg, ";")
for _, backendPools := range backendPoolsList {
vals := strings.SplitN(backendPools, ":", 2)
if len(vals) != 2 || vals[0] == "" || vals[1] == "" {
return nil, fmt.Errorf("the encoded backend-storage pool string does not have the right format")
}
backend := vals[0]
Pools := strings.Split(vals[1], ",")
backendPoolsMap[backend] = Pools
}
return backendPoolsMap, nil
}