forked from thecodeteam/goscaleio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstance.go
More file actions
228 lines (182 loc) · 6.82 KB
/
instance.go
File metadata and controls
228 lines (182 loc) · 6.82 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
package goscaleio
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"strings"
types "github.com/codedellemc/goscaleio/types/v1"
)
func (client *Client) GetInstance(systemhref string) (systems []*types.System, err error) {
endpoint := client.SIOEndpoint
if systemhref == "" {
endpoint.Path += "/types/System/instances"
} else {
endpoint.Path = systemhref
}
req := client.NewRequest(map[string]string{}, "GET", endpoint, nil)
req.SetBasicAuth("", client.Token)
req.Header.Add("Accept", "application/json;version="+client.configConnect.Version)
resp, err := client.retryCheckResp(&client.Http, req)
if err != nil {
return []*types.System{}, fmt.Errorf("problem getting response: %v", err)
}
defer resp.Body.Close()
if systemhref == "" {
if err = client.decodeBody(resp, &systems); err != nil {
return []*types.System{}, fmt.Errorf("error decoding instances response: %s", err)
}
} else {
system := &types.System{}
if err = client.decodeBody(resp, &system); err != nil {
return []*types.System{}, fmt.Errorf("error decoding instances response: %s", err)
}
systems = append(systems, system)
}
// bs, err := ioutil.ReadAll(resp.Body)
// if err != nil {
// return types.Systems{}, errors.New("error reading body")
// }
return systems, nil
}
func (client *Client) GetVolume(volumehref, volumeid, ancestorvolumeid, volumename string, getSnapshots bool) (volumes []*types.Volume, err error) {
endpoint := client.SIOEndpoint
if volumename != "" {
volumeid, err = client.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 == "" {
endpoint.Path = "/api/types/Volume/instances"
} else {
endpoint.Path = volumehref
}
req := client.NewRequest(map[string]string{}, "GET", endpoint, nil)
req.SetBasicAuth("", client.Token)
req.Header.Add("Accept", "application/json;version="+client.configConnect.Version)
resp, err := client.retryCheckResp(&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 = 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 = 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 (client *Client) FindVolumeID(volumename string) (volumeID string, err error) {
endpoint := 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 := client.NewRequest(map[string]string{}, "POST", endpoint, bytes.NewBufferString(string(jsonOutput)))
req.SetBasicAuth("", client.Token)
req.Header.Add("Accept", "application/json;version="+client.configConnect.Version)
req.Header.Add("Content-Type", "application/json;version="+client.configConnect.Version)
resp, err := client.retryCheckResp(&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 (client *Client) CreateVolume(volume *types.VolumeParam, storagePoolName string) (volumeResp *types.VolumeResp, err error) {
endpoint := client.SIOEndpoint
endpoint.Path = "/api/types/Volume/instances"
storagePool, err := client.FindStoragePool("", storagePoolName, "")
if err != nil {
return nil, err
}
volume.StoragePoolID = storagePool.ID
volume.ProtectionDomainID = storagePool.ProtectionDomainID
jsonOutput, err := json.Marshal(&volume)
if err != nil {
return &types.VolumeResp{}, fmt.Errorf("error marshaling: %s", err)
}
req := client.NewRequest(map[string]string{}, "POST", endpoint, bytes.NewBufferString(string(jsonOutput)))
req.SetBasicAuth("", client.Token)
req.Header.Add("Accept", "application/json;version="+client.configConnect.Version)
req.Header.Add("Content-Type", "application/json;version="+client.configConnect.Version)
resp, err := client.retryCheckResp(&client.Http, req)
if err != nil {
return &types.VolumeResp{}, fmt.Errorf("problem getting response: %v", err)
}
defer resp.Body.Close()
if err = client.decodeBody(resp, &volumeResp); err != nil {
return &types.VolumeResp{}, fmt.Errorf("error decoding volume creation response: %s", err)
}
return volumeResp, nil
}
func (client *Client) GetStoragePool(storagepoolhref string) (storagePools []*types.StoragePool, err error) {
endpoint := client.SIOEndpoint
if storagepoolhref == "" {
endpoint.Path = "/api/types/StoragePool/instances"
} else {
endpoint.Path = storagepoolhref
}
req := client.NewRequest(map[string]string{}, "GET", endpoint, nil)
req.SetBasicAuth("", client.Token)
req.Header.Add("Accept", "application/json;version="+client.configConnect.Version)
resp, err := client.retryCheckResp(&client.Http, req)
if err != nil {
return []*types.StoragePool{}, fmt.Errorf("problem getting response: %v", err)
}
defer resp.Body.Close()
if storagepoolhref == "" {
if err = client.decodeBody(resp, &storagePools); err != nil {
return []*types.StoragePool{}, fmt.Errorf("error decoding storage pool response: %s", err)
}
} else {
storagePool := &types.StoragePool{}
if err = client.decodeBody(resp, &storagePool); err != nil {
return []*types.StoragePool{}, fmt.Errorf("error decoding instances response: %s", err)
}
storagePools = append(storagePools, storagePool)
}
return storagePools, nil
}
func (client *Client) FindStoragePool(id, name, href string) (storagePool *types.StoragePool, err error) {
storagePools, err := client.GetStoragePool(href)
if err != nil {
return &types.StoragePool{}, fmt.Errorf("Error getting storage pool %s", err)
}
for _, storagePool = range storagePools {
if storagePool.ID == id || storagePool.Name == name || href != "" {
return storagePool, nil
}
}
return &types.StoragePool{}, errors.New("Couldn't find storage pool")
}