forked from dell/gopowermax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VolumeSnapshot.go
467 lines (436 loc) · 17 KB
/
VolumeSnapshot.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
Copyright © 2020 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 pmax
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"time"
types "github.com/dell/gopowermax/v2/types/v100"
log "github.com/sirupsen/logrus"
)
// The following constants are for internal use within the pmax library.
const (
ReplicationX = "replication/"
PrivateX = "private/"
//PrivURLPrefix = RESTPrefix + PrivateX + APIVersion + "/"
XSnapshot = "/snapshot"
XGenereation = "/generation"
)
func (c *Client) privURLPrefix() string {
return RESTPrefix + PrivateX + c.version + "/"
}
// GetSnapVolumeList returns a list of all snapshot volumes on the array.
func (c *Client) GetSnapVolumeList(ctx context.Context, symID string, queryParams types.QueryParams) (*types.SymVolumeList, error) {
defer c.TimeSpent("GetSnapVolumeList", time.Now())
if _, err := c.IsAllowedArray(symID); err != nil {
return nil, err
}
URL := c.privURLPrefix() + ReplicationX + SymmetrixX + symID + XVolume
if queryParams != nil {
URL += "?"
for key, val := range queryParams {
switch val := val.(type) {
case bool:
URL += fmt.Sprintf("%s=%s", key, strconv.FormatBool(val))
case string:
URL += fmt.Sprintf("%s=%s", key, val)
}
URL += "&"
}
URL = URL[:len(URL)-1]
}
ctx, cancel := c.GetTimeoutContext(ctx)
defer cancel()
resp, err := c.api.DoAndGetResponseBody(ctx, http.MethodGet, URL, c.getDefaultHeaders(), nil)
if err != nil {
log.Error("GetSnapVolumeList failed: " + err.Error())
return nil, err
}
if err = c.checkResponse(resp); err != nil {
return nil, err
}
snapVolList := &types.SymVolumeList{}
decoder := json.NewDecoder(resp.Body)
if err = decoder.Decode(snapVolList); err != nil {
return nil, err
}
err = resp.Body.Close()
if err != nil {
return nil, err
}
return snapVolList, nil
}
// GetVolumeSnapInfo returns snapVx information associated with a volume.
func (c *Client) GetVolumeSnapInfo(ctx context.Context, symID string, volumeID string) (*types.SnapshotVolumeGeneration, error) {
defer c.TimeSpent("GetVolumeSnapInfo", time.Now())
if _, err := c.IsAllowedArray(symID); err != nil {
return nil, err
}
URL := c.privURLPrefix() + ReplicationX + SymmetrixX + symID + XVolume + "/" + volumeID + XSnapshot
ctx, cancel := c.GetTimeoutContext(ctx)
defer cancel()
resp, err := c.api.DoAndGetResponseBody(ctx, http.MethodGet, URL, c.getDefaultHeaders(), nil)
if err != nil {
log.Error("GetVolumeSnapInfo failed: " + err.Error())
return nil, err
}
if err = c.checkResponse(resp); err != nil {
return nil, err
}
snapinfo := &types.SnapshotVolumeGeneration{}
decoder := json.NewDecoder(resp.Body)
if err = decoder.Decode(snapinfo); err != nil {
return nil, err
}
err = resp.Body.Close()
if err != nil {
return nil, err
}
return snapinfo, nil
}
// GetSnapshotInfo returns snapVx information of the specified snapshot
func (c *Client) GetSnapshotInfo(ctx context.Context, symID, volumeID, snapID string) (*types.VolumeSnapshot, error) {
defer c.TimeSpent("GetSnapshotInfo", time.Now())
if _, err := c.IsAllowedArray(symID); err != nil {
return nil, err
}
URL := c.privURLPrefix() + ReplicationX + SymmetrixX + symID + XVolume + "/" + volumeID + XSnapshot + "/" + snapID
ctx, cancel := c.GetTimeoutContext(ctx)
defer cancel()
resp, err := c.api.DoAndGetResponseBody(ctx, http.MethodGet, URL, c.getDefaultHeaders(), nil)
if err != nil {
log.Error("GetSnapshotInfo failed: " + err.Error())
return nil, err
}
if err = c.checkResponse(resp); err != nil {
return nil, err
}
snapshotInfo := new(types.VolumeSnapshot)
if err := json.NewDecoder(resp.Body).Decode(snapshotInfo); err != nil {
return nil, err
}
err = resp.Body.Close()
if err != nil {
return nil, err
}
return snapshotInfo, nil
}
// CreateSnapshot creates a snapVx snapshot of a volume or on the list of volumes passed as sourceVolumeList
// BothSides flag is used in SRDF usecases to create snapshots on both R1 and R2 side
// Star flag is used if the source device is participating in SRDF star mode
// Use the Force flag to automate some scenarios to succeed
// TimeToLive value ins hour is set on the snapshot to automatically delete the snapshot after target is unlinked
func (c *Client) CreateSnapshot(ctx context.Context, symID string, snapID string, sourceVolumeList []types.VolumeList, ttl int64) error {
defer c.TimeSpent("CreateSnapshot", time.Now())
if _, err := c.IsAllowedArray(symID); err != nil {
return err
}
snapParam := &types.CreateVolumesSnapshot{
SourceVolumeList: sourceVolumeList,
BothSides: false,
Star: false,
Force: false,
TimeToLive: ttl,
ExecutionOption: types.ExecutionOptionSynchronous,
}
ifDebugLogPayload(snapParam)
URL := c.privURLPrefix() + ReplicationX + SymmetrixX + symID + XSnapshot + "/" + snapID
ctx, cancel := c.GetTimeoutContext(ctx)
defer cancel()
err := c.api.Post(ctx, URL, c.getDefaultHeaders(), snapParam, nil)
if err != nil {
log.Error("CreateSnapshot failed: " + err.Error())
}
return err
}
// DeleteSnapshot deletes a snapshot from a volume
// DeviceNameListSource is a list which contains the names of source volumes
// Symforce flag is used to automate some internal establish scenarios
// Star mode is used for devices in SRDF relations
// Use the Force flag in acceptable error conditions
// Restore, when set to true will terminate the Restore and the Snapshot as well
// Generation is used to tell which generation of snapshot needs to be deleted and is passed as int64
// ExecutionOption tells the Unisphere to perform the operation either in Synchronous mode or Asynchronous mode
func (c *Client) DeleteSnapshot(ctx context.Context, symID, snapID string, sourceVolumes []types.VolumeList, generation int64) error {
defer c.TimeSpent("DeleteSnapshot", time.Now())
if _, err := c.IsAllowedArray(symID); err != nil {
return err
}
deleteSnapshot := &types.DeleteVolumeSnapshot{
DeviceNameListSource: sourceVolumes,
Symforce: false,
Star: false,
Force: false,
Restore: false,
Generation: generation,
ExecutionOption: types.ExecutionOptionAsynchronous,
}
job := &types.Job{}
ifDebugLogPayload(deleteSnapshot)
URL := c.privURLPrefix() + ReplicationX + SymmetrixX + symID + XSnapshot + "/" + snapID
URL = strings.Replace(URL, "/90/", "/91/", 1)
ctx, cancel := c.GetTimeoutContext(ctx)
defer cancel()
err := c.api.DoWithHeaders(ctx, http.MethodDelete, URL, c.getDefaultHeaders(), deleteSnapshot, job)
if err != nil {
return err
}
job, err = c.WaitOnJobCompletion(ctx, symID, job.JobID)
if err != nil {
return err
}
if job.Status == types.JobStatusFailed || job.Status == types.JobStatusRunning {
return fmt.Errorf("Job status not successful for snapshot delete. Job status = %s and Job result = %s", job.Status, job.Result)
}
log.Info(fmt.Sprintf("Snapshot (%s) deleted successfully", snapID))
return nil
}
// DeleteSnapshotS - Deletes a snapshot synchronously
func (c *Client) DeleteSnapshotS(ctx context.Context, symID, snapID string, sourceVolumes []types.VolumeList, generation int64) error {
defer c.TimeSpent("DeleteSnapshotS", time.Now())
if _, err := c.IsAllowedArray(symID); err != nil {
return err
}
deleteSnapshot := &types.DeleteVolumeSnapshot{
DeviceNameListSource: sourceVolumes,
Symforce: false,
Star: false,
Force: false,
Restore: false,
Generation: generation,
ExecutionOption: types.ExecutionOptionSynchronous,
}
URL := c.privURLPrefix() + ReplicationX + SymmetrixX + symID + XSnapshot + "/" + snapID
URL = strings.Replace(URL, "/90/", "/91/", 1)
fields := map[string]interface{}{
http.MethodPut: URL,
}
ifDebugLogPayload(deleteSnapshot)
ctx, cancel := c.GetTimeoutContext(ctx)
defer cancel()
err := c.api.DoWithHeaders(ctx, http.MethodDelete, URL, c.getDefaultHeaders(), deleteSnapshot, nil)
if err != nil {
log.WithFields(fields).Errorf("Delete Snapshot (%s:%s) failed with error: %s", symID, snapID, err.Error())
return err
}
log.Info(fmt.Sprintf("Snapshot (%s) deleted successfully", snapID))
return nil
}
// ModifySnapshot executes actions on a snapshot
// VolumeNameListSource is a list which contains the names of source volumes
// VolumeNameListTarget is a list which contains the names of target volumes to which the snapshot is linked or going to be linked
// Symforce flag is used to automate some internal establish scenarios
// Star mode is used for devices in SRDF relations
// Use the Force flag in acceptable error conditions
// Restore, when set to true will terminate the Restore and the Snapshot as well
// Exact when specified, pairs source and link devices in their ordinal positions within the selection. When not set uses the source and link device selections as a pool that pairs by best match
// Copy when specified creates an exact copy of the source device, otherwise copies the references
// Remote when specified propagates the data to the remote mirror of the RDF device
// Generation is used to tell which generation of snapshot needs to be updated, it is passed as int64
// NewSnapshotName specifies the new snapshot name to which the old snapshot will be renamed
// ExecutionOption tells the Unisphere to perform the operation either in Synchronous mode or Asynchronous mode
// Action defined the operation which will be performed on the given snapshot
func (c *Client) ModifySnapshot(ctx context.Context, symID string, sourceVol []types.VolumeList,
targetVol []types.VolumeList, snapID string, action string,
newSnapID string, generation int64) error {
defer c.TimeSpent("ModifySnapshot", time.Now())
if _, err := c.IsAllowedArray(symID); err != nil {
return err
}
snapParam := &types.ModifyVolumeSnapshot{}
switch action {
case "Link", "Unlink":
snapParam = &types.ModifyVolumeSnapshot{
VolumeNameListSource: sourceVol,
VolumeNameListTarget: targetVol,
Force: false,
Star: false,
Exact: false,
Copy: false,
Remote: false,
Symforce: false,
Action: action,
Generation: generation,
ExecutionOption: types.ExecutionOptionAsynchronous,
}
case "Rename":
snapParam = &types.ModifyVolumeSnapshot{
VolumeNameListSource: sourceVol,
VolumeNameListTarget: targetVol,
NewSnapshotName: newSnapID,
Action: action,
ExecutionOption: types.ExecutionOptionAsynchronous,
}
default:
return fmt.Errorf("not a supported action on Snapshots")
}
URL := c.privURLPrefix() + ReplicationX + SymmetrixX + symID + XSnapshot + "/" + snapID
job := &types.Job{}
fields := map[string]interface{}{
http.MethodPut: URL,
}
ctx, cancel := c.GetTimeoutContext(ctx)
defer cancel()
err := c.api.Put(
ctx, URL, c.getDefaultHeaders(), snapParam, job)
if err != nil {
log.WithFields(fields).Error("Error in ModifySnapshot: " + err.Error())
return err
}
job, err = c.WaitOnJobCompletion(ctx, symID, job.JobID)
if err != nil {
return err
}
if job.Status == types.JobStatusFailed || job.Status == types.JobStatusRunning {
return fmt.Errorf("Job status not successful for snapshot %s. Job status = %s and Job result = %s", action, job.Status, job.Result)
}
log.Info(fmt.Sprintf("Action (%s) on Snapshot (%s) is successful", action, snapID))
return nil
}
// ModifySnapshotS executes actions on snapshots synchronously
func (c *Client) ModifySnapshotS(ctx context.Context, symID string, sourceVol []types.VolumeList,
targetVol []types.VolumeList, snapID string, action string,
newSnapID string, generation int64) error {
defer c.TimeSpent("ModifySnapshotS", time.Now())
if _, err := c.IsAllowedArray(symID); err != nil {
return err
}
snapParam := &types.ModifyVolumeSnapshot{}
switch action {
case "Link", "Unlink":
snapParam = &types.ModifyVolumeSnapshot{
VolumeNameListSource: sourceVol,
VolumeNameListTarget: targetVol,
Force: false,
Star: false,
Exact: false,
Copy: false,
Remote: false,
Symforce: false,
Action: action,
Generation: generation,
ExecutionOption: types.ExecutionOptionSynchronous,
}
case "Rename":
snapParam = &types.ModifyVolumeSnapshot{
VolumeNameListSource: sourceVol,
VolumeNameListTarget: targetVol,
NewSnapshotName: newSnapID,
Action: action,
ExecutionOption: types.ExecutionOptionSynchronous,
}
default:
return fmt.Errorf("not a supported action on Snapshots")
}
URL := c.privURLPrefix() + ReplicationX + SymmetrixX + symID + XSnapshot + "/" + snapID
fields := map[string]interface{}{
http.MethodPut: URL,
}
ctx, cancel := c.GetTimeoutContext(ctx)
defer cancel()
err := c.api.Put(ctx, URL, c.getDefaultHeaders(), snapParam, nil)
if err != nil {
log.WithFields(fields).Error("Error in ModifySnapshotS: " + err.Error())
return err
}
log.Info(fmt.Sprintf("Action (%s) on Snapshot (%s) is successful", action, snapID))
return nil
}
// GetPrivVolumeByID returns a Volume structure given the symmetrix and volume ID
func (c *Client) GetPrivVolumeByID(ctx context.Context, symID string, volumeID string) (*types.VolumeResultPrivate, error) {
defer c.TimeSpent("GetPrivVolumeByID", time.Now())
if _, err := c.IsAllowedArray(symID); err != nil {
return nil, err
}
vol, err := c.GetVolumeByID(ctx, symID, volumeID)
if err != nil {
log.Error("GetVolumeByID failed: " + err.Error())
return nil, err
}
wwn := vol.WWN
URL := c.privURLPrefix() + SLOProvisioningX + SymmetrixX + symID + XVolume
URL = fmt.Sprintf("%s?wwn=%s", URL, wwn)
//URL = URL + query
ctx, cancel := context.WithTimeout(ctx, 360*time.Second)
defer cancel()
resp, err := c.api.DoAndGetResponseBody(
ctx, http.MethodGet, URL, c.getDefaultHeaders(), nil)
if err != nil {
log.Error("GetPrivVolumeByID failed: " + err.Error())
return nil, err
}
if err = c.checkResponse(resp); err != nil {
return nil, err
}
//volume := &types.VolumeResultPrivate{}
privateVolumeIterator := new(types.PrivVolumeIterator)
decoder := json.NewDecoder(resp.Body)
if err = decoder.Decode(privateVolumeIterator); err != nil {
return nil, err
}
err = resp.Body.Close()
if err != nil {
return nil, err
}
return &privateVolumeIterator.ResultList.PrivVolumeList[0], nil
}
// GetSnapshotGenerations returns a list of all the snapshot generation on a specific snapshot
func (c *Client) GetSnapshotGenerations(ctx context.Context, symID, volumeID, snapID string) (*types.VolumeSnapshotGenerations, error) {
defer c.TimeSpent("GetSnapshotGenerations", time.Now())
if _, err := c.IsAllowedArray(symID); err != nil {
return nil, err
}
URL := c.privURLPrefix() + ReplicationX + SymmetrixX + symID + XVolume + "/" + volumeID + XSnapshot + "/" + snapID + XGenereation
volumeSnapshotGenerations := new(types.VolumeSnapshotGenerations)
ctx, cancel := c.GetTimeoutContext(ctx)
defer cancel()
err := c.api.Get(ctx, URL, c.getDefaultHeaders(), volumeSnapshotGenerations)
if err != nil {
return nil, err
}
return volumeSnapshotGenerations, nil
}
// GetSnapshotGenerationInfo returns the specific generation info related to a snapshot
func (c *Client) GetSnapshotGenerationInfo(ctx context.Context, symID, volumeID, snapID string, generation int64) (*types.VolumeSnapshotGeneration, error) {
defer c.TimeSpent("GetSnapshotGenerationInfo", time.Now())
if _, err := c.IsAllowedArray(symID); err != nil {
return nil, err
}
URL := c.privURLPrefix() + ReplicationX + SymmetrixX + symID + XVolume + "/" + volumeID + XSnapshot + "/" + snapID + XGenereation + "/" + strconv.FormatInt(generation, 10)
volumeSnapshotGeneration := new(types.VolumeSnapshotGeneration)
ctx, cancel := c.GetTimeoutContext(ctx)
defer cancel()
err := c.api.Get(ctx, URL, c.getDefaultHeaders(), volumeSnapshotGeneration)
if err != nil {
return nil, err
}
return volumeSnapshotGeneration, nil
}
// GetReplicationCapabilities returns details about SnapVX and SRDF
// execution capabilities on the Symmetrix array
func (c *Client) GetReplicationCapabilities(ctx context.Context) (*types.SymReplicationCapabilities, error) {
defer c.TimeSpent("GetReplicationCapabilities", time.Now())
URL := c.urlPrefix() + ReplicationX + "capabilities/symmetrix"
symReplicationCapabilities := new(types.SymReplicationCapabilities)
ctx, cancel := c.GetTimeoutContext(ctx)
defer cancel()
err := c.api.Get(ctx, URL, c.getDefaultHeaders(), symReplicationCapabilities)
if err != nil {
return nil, err
}
return symReplicationCapabilities, nil
}