-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathinstance.go
301 lines (252 loc) · 7.2 KB
/
instance.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright © 2019 - 2022 Dell Inc. or its subsidiaries. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package goscaleio
import (
"errors"
"fmt"
"net/http"
"time"
types "github.com/dell/goscaleio/types/v1"
)
// GetInstance returns an instance
func (c *Client) GetInstance(systemhref string) ([]*types.System, error) {
defer TimeSpent("GetInstance", time.Now())
var (
err error
system = &types.System{}
systems []*types.System
)
if systemhref == "" {
err = c.getJSONWithRetry(
http.MethodGet, "api/types/System/instances", nil, &systems)
} else {
err = c.getJSONWithRetry(
http.MethodGet, systemhref, nil, system)
}
if err != nil {
return nil, err
}
if systemhref != "" {
systems = append(systems, system)
}
return systems, nil
}
// GetVolume returns a volume
func (c *Client) GetVolume(
volumehref, volumeid, ancestorvolumeid, volumename string,
getSnapshots bool,
) ([]*types.Volume, error) {
defer TimeSpent("GetVolume", time.Now())
var (
err error
path string
volume = &types.Volume{}
volumes []*types.Volume
)
if volumename != "" {
volumeid, err = c.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 == "" {
path = "/api/types/Volume/instances"
} else {
path = volumehref
}
if volumehref == "" && volumeid == "" {
err = c.getJSONWithRetry(
http.MethodGet, path, nil, &volumes)
} else {
err = c.getJSONWithRetry(
http.MethodGet, path, nil, volume)
}
if err != nil {
return nil, err
}
if volumehref == "" && volumeid == "" {
var volumesNew []*types.Volume
for _, volume := range volumes {
if (!getSnapshots && volume.AncestorVolumeID == ancestorvolumeid) || (getSnapshots && volume.AncestorVolumeID != "") {
volumesNew = append(volumesNew, volume)
}
}
volumes = volumesNew
} else {
volumes = append(volumes, volume)
}
return volumes, nil
}
// FindVolumeID returns a VolumeID
func (c *Client) FindVolumeID(volumename string) (string, error) {
defer TimeSpent("FindVolumeID", time.Now())
volumeQeryIDByKeyParam := &types.VolumeQeryIDByKeyParam{
Name: volumename,
}
path := "/api/types/Volume/instances/action/queryIdByKey"
volumeID, err := c.getStringWithRetry(http.MethodPost, path,
volumeQeryIDByKeyParam)
fmt.Printf("[FindVolumeID] volumeID: %+v\n", volumeID)
if err != nil {
return "", err
}
return volumeID, nil
}
// CreateVolume creates a volume
func (c *Client) CreateVolume(
volume *types.VolumeParam,
storagePoolName, protectionDomain string,
) (*types.VolumeResp, error) {
defer TimeSpent("CreateVolume", time.Now())
path := "/api/types/Volume/instances"
storagePool, err := c.FindStoragePool("", storagePoolName, "", protectionDomain)
if err != nil {
return nil, err
}
volume.StoragePoolID = storagePool.ID
volume.ProtectionDomainID = storagePool.ProtectionDomainID
vol := &types.VolumeResp{}
err = c.getJSONWithRetry(
http.MethodPost, path, volume, vol)
if err != nil {
return nil, err
}
return vol, nil
}
// GetStoragePool returns a storagepool
func (c *Client) GetStoragePool(
storagepoolhref string,
) ([]*types.StoragePool, error) {
defer TimeSpent("GetStoragePool", time.Now())
var (
err error
storagePool = &types.StoragePool{}
storagePools []*types.StoragePool
)
if storagepoolhref == "" {
err = c.getJSONWithRetry(
http.MethodGet, "/api/types/StoragePool/instances",
nil, &storagePools)
} else {
err = c.getJSONWithRetry(
http.MethodGet, storagepoolhref, nil, storagePool)
}
if err != nil {
return nil, err
}
if storagepoolhref != "" {
storagePools = append(storagePools, storagePool)
}
return storagePools, nil
}
// FindStoragePool returns a StoragePool
func (c *Client) FindStoragePool(
id, name, href, protectionDomain string,
) (*types.StoragePool, error) {
defer TimeSpent("FindStoragePool", time.Now())
storagePools, err := c.GetStoragePool(href)
if err != nil {
return nil, fmt.Errorf("Error getting storage pool %s", err)
}
for _, storagePool := range storagePools {
if storagePool.ID == id || storagePool.Name == name || href != "" {
if storagePool.ProtectionDomainID == protectionDomain || protectionDomain == "" {
return storagePool, nil
}
}
}
return nil, errors.New("Couldn't find storage pool")
}
// SnapshotPolicy defines struct for SnapshotPolicy
type SnapshotPolicy struct {
SnapshotPolicy *types.SnapshotPolicy
client *Client
}
// NewSnapshotPolicy returns new SnapshotPolicy
func NewSnapshotPolicy(client *Client) *SnapshotPolicy {
return &SnapshotPolicy{
SnapshotPolicy: &types.SnapshotPolicy{},
client: client,
}
}
// FindSnapshotPolicyID retruns a Snapshot Policy ID based on name
func (c *Client) FindSnapshotPolicyID(spname string) (string, error) {
defer TimeSpent("FindSnapshotPolicyID", time.Now())
SnapshotPolicyQueryIDByKeyParam := &types.SnapshotPolicyQueryIDByKeyParam{
Name: spname,
}
path := fmt.Sprintf("/api/types/SnapshotPolicy/instances/action/queryIdByKey")
spID, err := c.getStringWithRetry(
http.MethodPost, path, SnapshotPolicyQueryIDByKeyParam)
if err != nil {
return "", err
}
return spID, nil
}
// GetSnapshotPolicy returns a list of snapshot policy
func (c *Client) GetSnapshotPolicy(
spname, spid string,
) ([]*types.SnapshotPolicy, error) {
defer TimeSpent("GetSnapshotPolicy", time.Now())
var (
err error
path string
sp = &types.SnapshotPolicy{}
sps []*types.SnapshotPolicy
)
if spname != "" {
spid, err = c.FindSnapshotPolicyID(spname)
if err != nil && err.Error() == "Not found" {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("Error: problem finding snapshot policy: %s", err)
}
}
if spid != "" {
path = fmt.Sprintf("/api/instances/SnapshotPolicy::%s", spid)
} else {
path = "/api/types/SnapshotPolicy/instances"
}
if spid == "" {
err = c.getJSONWithRetry(
http.MethodGet, path, nil, &sps)
} else {
err = c.getJSONWithRetry(
http.MethodGet, path, nil, sp)
}
if err != nil {
return nil, err
}
if spid == "" {
return sps, nil
}
sps = append(sps, sp)
return sps, nil
}
// GetStoragePoolVolumes returns list of volumes connected to storage pool Storagepool by ID
func (c *Client) GetStoragePoolVolumes(id string) ([]*types.Volume, error) {
defer TimeSpent("GetStoragePoolByID", time.Now())
path := fmt.Sprintf("/api/instances/StoragePool::%s/relationships/Volume", id)
var storagepoolVolumes []*types.Volume
err := c.getJSONWithRetry(
http.MethodGet, path, nil, &storagepoolVolumes)
if err != nil {
return nil, err
}
return storagepoolVolumes, err
}