forked from thecodeteam/goscaleio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvolume.go
More file actions
278 lines (222 loc) · 8.24 KB
/
volume.go
File metadata and controls
278 lines (222 loc) · 8.24 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package goscaleio
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strings"
types "github.com/codedellemc/goscaleio/types/v1"
)
type SdcMappedVolume struct {
MdmID string
VolumeID string
SdcDevice string
// Mounted bool
// MountPath bool
// Mapped bool
}
type Volume struct {
Volume *types.Volume
client *Client
}
func NewVolume(client *Client) *Volume {
return &Volume{
Volume: new(types.Volume),
client: client,
}
}
func (storagePool *StoragePool) GetVolume(volumehref, volumeid, ancestorvolumeid, volumename string, getSnapshots bool) (volumes []*types.Volume, err error) {
endpoint := storagePool.client.SIOEndpoint
if volumename != "" {
volumeid, err = storagePool.FindVolumeID(volumename)
if err != nil && err.Error() == "Not found" {
return nil, nil
}
if err != nil {
return []*types.Volume{}, fmt.Errorf("Error: problem finding volume: %s", err)
}
}
if volumeid != "" {
endpoint.Path = fmt.Sprintf("/api/instances/Volume::%s", volumeid)
} else if volumehref == "" {
link, err := GetLink(storagePool.StoragePool.Links, "/api/StoragePool/relationship/Volume")
if err != nil {
return []*types.Volume{}, errors.New("Error: problem finding link")
}
endpoint.Path = link.HREF
} else {
endpoint.Path = volumehref
}
req := storagePool.client.NewRequest(map[string]string{}, "GET", endpoint, nil)
req.SetBasicAuth("", storagePool.client.Token)
req.Header.Add("Accept", "application/json;version="+storagePool.client.configConnect.Version)
resp, err := storagePool.client.retryCheckResp(&storagePool.client.Http, req)
if err != nil {
return []*types.Volume{}, fmt.Errorf("problem getting response: %v", err)
}
defer resp.Body.Close()
if volumehref == "" && volumeid == "" {
if err = storagePool.client.decodeBody(resp, &volumes); err != nil {
return []*types.Volume{}, fmt.Errorf("error decoding storage pool response: %s", err)
}
var volumesNew []*types.Volume
for _, volume := range volumes {
if (!getSnapshots && volume.AncestorVolumeID == ancestorvolumeid) || (getSnapshots && volume.AncestorVolumeID != "") {
volumesNew = append(volumesNew, volume)
}
}
volumes = volumesNew
} else {
volume := &types.Volume{}
if err = storagePool.client.decodeBody(resp, &volume); err != nil {
return []*types.Volume{}, fmt.Errorf("error decoding instances response: %s", err)
}
volumes = append(volumes, volume)
}
return volumes, nil
}
func (storagePool *StoragePool) FindVolumeID(volumename string) (volumeID string, err error) {
endpoint := storagePool.client.SIOEndpoint
volumeQeryIdByKeyParam := &types.VolumeQeryIdByKeyParam{}
volumeQeryIdByKeyParam.Name = volumename
jsonOutput, err := json.Marshal(&volumeQeryIdByKeyParam)
if err != nil {
return "", fmt.Errorf("error marshaling: %s", err)
}
endpoint.Path = fmt.Sprintf("/api/types/Volume/instances/action/queryIdByKey")
req := storagePool.client.NewRequest(map[string]string{}, "POST", endpoint, bytes.NewBufferString(string(jsonOutput)))
req.SetBasicAuth("", storagePool.client.Token)
req.Header.Add("Accept", "application/json;version="+storagePool.client.configConnect.Version)
req.Header.Add("Content-Type", "application/json;version="+storagePool.client.configConnect.Version)
resp, err := storagePool.client.retryCheckResp(&storagePool.client.Http, req)
if err != nil {
return "", err
}
defer resp.Body.Close()
bs, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", errors.New("error reading body")
}
volumeID = string(bs)
volumeID = strings.TrimRight(volumeID, `"`)
volumeID = strings.TrimLeft(volumeID, `"`)
return volumeID, nil
}
func GetLocalVolumeMap() (mappedVolumes []*SdcMappedVolume, err error) {
// get sdc kernel guid
// /bin/emc/scaleio/drv_cfg --query_guid
// sdcKernelGuid := "271bad82-08ee-44f2-a2b1-7e2787c27be1"
mappedVolumesMap := make(map[string]*SdcMappedVolume)
out, err := exec.Command("/opt/emc/scaleio/sdc/bin/drv_cfg", "--query_vols").Output()
if err != nil {
return []*SdcMappedVolume{}, fmt.Errorf("Error querying volumes: ", err)
}
result := string(out)
lines := strings.Split(result, "\n")
for _, line := range lines {
split := strings.Split(line, " ")
if split[0] == "VOL-ID" {
mappedVolume := &SdcMappedVolume{MdmID: split[3], VolumeID: split[1]}
mdmVolumeID := fmt.Sprintf("%s-%s", mappedVolume.MdmID, mappedVolume.VolumeID)
mappedVolumesMap[mdmVolumeID] = mappedVolume
}
}
diskIDPath := "/dev/disk/by-id"
files, _ := ioutil.ReadDir(diskIDPath)
r, _ := regexp.Compile(`^emc-vol-\w*-\w*$`)
for _, f := range files {
matched := r.MatchString(f.Name())
if matched {
mdmVolumeID := strings.Replace(f.Name(), "emc-vol-", "", 1)
devPath, _ := filepath.EvalSymlinks(fmt.Sprintf("%s/%s", diskIDPath, f.Name()))
if _, ok := mappedVolumesMap[mdmVolumeID]; ok {
mappedVolumesMap[mdmVolumeID].SdcDevice = devPath
}
}
}
keys := make([]string, 0, len(mappedVolumesMap))
for key := range mappedVolumesMap {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
mappedVolumes = append(mappedVolumes, mappedVolumesMap[key])
}
return mappedVolumes, nil
}
func (storagePool *StoragePool) CreateVolume(volume *types.VolumeParam) (volumeResp *types.VolumeResp, err error) {
endpoint := storagePool.client.SIOEndpoint
endpoint.Path = "/api/types/Volume/instances"
volume.StoragePoolID = storagePool.StoragePool.ID
volume.ProtectionDomainID = storagePool.StoragePool.ProtectionDomainID
jsonOutput, err := json.Marshal(&volume)
if err != nil {
return &types.VolumeResp{}, fmt.Errorf("error marshaling: %s", err)
}
req := storagePool.client.NewRequest(map[string]string{}, "POST", endpoint, bytes.NewBufferString(string(jsonOutput)))
req.SetBasicAuth("", storagePool.client.Token)
req.Header.Add("Accept", "application/json;version="+storagePool.client.configConnect.Version)
req.Header.Add("Content-Type", "application/json;version="+storagePool.client.configConnect.Version)
resp, err := storagePool.client.retryCheckResp(&storagePool.client.Http, req)
if err != nil {
return &types.VolumeResp{}, fmt.Errorf("problem getting response: %v", err)
}
defer resp.Body.Close()
if err = storagePool.client.decodeBody(resp, &volumeResp); err != nil {
return &types.VolumeResp{}, fmt.Errorf("error decoding volume creation response: %s", err)
}
return volumeResp, nil
}
func (volume *Volume) GetVTree() (vtree *types.VTree, err error) {
endpoint := volume.client.SIOEndpoint
link, err := GetLink(volume.Volume.Links, "/api/parent/relationship/vtreeId")
if err != nil {
return &types.VTree{}, errors.New("Error: problem finding link")
}
endpoint.Path = link.HREF
req := volume.client.NewRequest(map[string]string{}, "GET", endpoint, nil)
req.SetBasicAuth("", volume.client.Token)
req.Header.Add("Accept", "application/json;version="+volume.client.configConnect.Version)
resp, err := volume.client.retryCheckResp(&volume.client.Http, req)
if err != nil {
return &types.VTree{}, fmt.Errorf("problem getting response: %v", err)
}
defer resp.Body.Close()
if err = volume.client.decodeBody(resp, &vtree); err != nil {
return &types.VTree{}, fmt.Errorf("error decoding vtree response: %s", err)
}
return vtree, nil
}
func (volume *Volume) RemoveVolume(removeMode string) (err error) {
endpoint := volume.client.SIOEndpoint
link, err := GetLink(volume.Volume.Links, "self")
if err != nil {
return errors.New("Error: problem finding link")
}
endpoint.Path = fmt.Sprintf("%v/action/removeVolume", link.HREF)
if removeMode == "" {
removeMode = "ONLY_ME"
}
removeVolumeParam := &types.RemoveVolumeParam{
RemoveMode: removeMode,
}
jsonOutput, err := json.Marshal(&removeVolumeParam)
if err != nil {
return fmt.Errorf("error marshaling: %s", err)
}
req := volume.client.NewRequest(map[string]string{}, "POST", endpoint, bytes.NewBufferString(string(jsonOutput)))
req.SetBasicAuth("", volume.client.Token)
req.Header.Add("Accept", "application/json;version="+volume.client.configConnect.Version)
req.Header.Add("Content-Type", "application/json;version="+volume.client.configConnect.Version)
resp, err := volume.client.retryCheckResp(&volume.client.Http, req)
if err != nil {
return fmt.Errorf("problem getting response: %v", err)
}
defer resp.Body.Close()
return nil
}