This repository was archived by the owner on Aug 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathvolume.go
More file actions
196 lines (160 loc) · 4.2 KB
/
Copy pathvolume.go
File metadata and controls
196 lines (160 loc) · 4.2 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
package goscaleio
import (
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
"regexp"
"sort"
"strings"
types "github.com/thecodeteam/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: &types.Volume{},
client: client,
}
}
func (sp *StoragePool) GetVolume(
volumehref, volumeid, ancestorvolumeid, volumename string,
getSnapshots bool) ([]*types.Volume, error) {
var (
err error
path string
volume = &types.Volume{}
volumes []*types.Volume
)
if volumename != "" {
volumeid, err = sp.FindVolumeID(volumename)
if err != nil && err.Error() == "Not found" {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("Error: problem finding volume: %s", err)
}
}
if volumeid != "" {
path = fmt.Sprintf("/api/instances/Volume::%s", volumeid)
} else if volumehref == "" {
link, err := GetLink(sp.StoragePool.Links,
"/api/StoragePool/relationship/Volume")
if err != nil {
return nil, err
}
path = link.HREF
} else {
path = volumehref
}
if volumehref == "" && volumeid == "" {
err = sp.client.getJSONWithRetry(
http.MethodGet, path, nil, &volumes)
} else {
err = sp.client.getJSONWithRetry(
http.MethodGet, path, nil, volume)
}
if err != nil {
return nil, err
}
if volumehref == "" && volumeid == "" {
var volumesNew []*types.Volume
for _, v := range volumes {
if (!getSnapshots && v.AncestorVolumeID == ancestorvolumeid) || (getSnapshots && v.AncestorVolumeID != "") {
volumesNew = append(volumesNew, v)
}
}
volumes = volumesNew
} else {
volumes = append(volumes, volume)
}
return volumes, nil
}
func (sp *StoragePool) FindVolumeID(volumename string) (string, error) {
volumeQeryIdByKeyParam := &types.VolumeQeryIdByKeyParam{
Name: volumename,
}
path := fmt.Sprintf("/api/types/Volume/instances/action/queryIdByKey")
volumeID, err := sp.client.getStringWithRetry(
http.MethodPost, path, volumeQeryIdByKeyParam)
if err != nil {
return "", err
}
return volumeID, nil
}
func GetLocalVolumeMap() (mappedVolumes []*SdcMappedVolume, err error) {
mappedVolumesMap := make(map[string]*SdcMappedVolume)
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 {
split := strings.Split(f.Name(), "-")
mdmVolumeID := fmt.Sprintf("%s-%s", split[2], split[3])
devPath, _ := filepath.EvalSymlinks(fmt.Sprintf("%s/%s", diskIDPath, f.Name()))
mappedVolumesMap[mdmVolumeID] = &SdcMappedVolume{MdmID: split[2], VolumeID: split[3], 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 (sp *StoragePool) CreateVolume(
volume *types.VolumeParam) (*types.VolumeResp, error) {
path := "/api/types/Volume/instances"
volume.StoragePoolID = sp.StoragePool.ID
volume.ProtectionDomainID = sp.StoragePool.ProtectionDomainID
volumeResp := &types.VolumeResp{}
err := sp.client.getJSONWithRetry(
http.MethodPost, path, volume, volumeResp)
if err != nil {
return nil, err
}
return volumeResp, nil
}
func (v *Volume) GetVTree() (*types.VTree, error) {
link, err := GetLink(v.Volume.Links, "/api/parent/relationship/vtreeId")
if err != nil {
return nil, err
}
vtree := &types.VTree{}
err = v.client.getJSONWithRetry(
http.MethodGet, link.HREF, nil, vtree)
if err != nil {
return nil, err
}
return vtree, nil
}
func (v *Volume) RemoveVolume(removeMode string) error {
link, err := GetLink(v.Volume.Links, "self")
if err != nil {
return err
}
path := fmt.Sprintf("%v/action/removeVolume", link.HREF)
if removeMode == "" {
removeMode = "ONLY_ME"
}
removeVolumeParam := &types.RemoveVolumeParam{
RemoveMode: removeMode,
}
err = v.client.getJSONWithRetry(
http.MethodPost, path, removeVolumeParam, nil)
return err
}