-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvolume.go
433 lines (378 loc) · 15.6 KB
/
volume.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
/*
Copyright (c) 2019 Dell Corporation
All Rights Reserved
*/
package gounity
import (
"context"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/dell/gounity/api"
"github.com/dell/gounity/types"
"github.com/dell/gounity/util"
)
// LicenseType is string
type LicenseType string
// Constants
const (
LunNameMaxLength = 63
SnapForClone = "csi-snapforclone-"
ThinProvisioning LicenseType = "THIN_PROVISIONING"
DataReduction LicenseType = "DATA_REDUCTION"
)
// DependentClonesErrorCode stores error code of dependent clones
var DependentClonesErrorCode = "0x6701673"
// ErrorDependentClones stores dependent clones error message
var ErrorDependentClones = errors.New("the specified volume cannot be deleted because it has one or more dependent thin clones")
// VolumeNotFoundErrorCode stores Volume not found error code
var VolumeNotFoundErrorCode = "0x7d13005"
// ErrorVolumeNotFound stores Volume not found error
var ErrorVolumeNotFound = errors.New("Unable to find volume")
// ErrorCreateSnapshotFailed stores Create snapshot failed error message
var ErrorCreateSnapshotFailed = errors.New("create Snapshot Failed")
// ErrorCloningFailed stores Cloning failed error message
var ErrorCloningFailed = errors.New("volume Cloning Failed")
// MarkVolumeForDeletion stores mark of volume deletion
var MarkVolumeForDeletion = "csi-marked-vol-for-deletion"
// Volume structure
type Volume struct {
client *Client
}
// NewVolume function returns volume
func NewVolume(client *Client) *Volume {
return &Volume{client}
}
// CreateLun API create a Lun with the given arguments.
// Pre-validations: 1. Length of the Lun name should be less than 63 characters.
// 2. Size of Lun should be in bytes.
func (v *Volume) CreateLun(ctx context.Context, name, poolID, description string, size uint64, fastVPTieringPolicy int,
hostIOLimitID string, isThinEnabled, isDataReductionEnabled bool,
) (*types.Volume, error) {
log := util.GetRunIDLogger(ctx)
if name == "" {
return nil, errors.New("lun name should not be empty")
}
if len(name) > LunNameMaxLength {
return nil, fmt.Errorf("lun name %s should not exceed 63 characters", name)
}
poolAPI := NewStoragePool(v.client)
pool, err := poolAPI.FindStoragePoolByID(ctx, poolID)
if err != nil {
return nil, fmt.Errorf("unable to get PoolID (%s) Error:%v", poolID, err)
}
storagePool := types.StoragePoolID{
PoolID: pool.StoragePoolContent.ID,
}
lunParams := types.LunParameters{
StoragePool: &storagePool,
Size: size,
}
thinProvisioningLicenseInfoResp, err := v.isFeatureLicensed(ctx, ThinProvisioning)
if err != nil {
return nil, fmt.Errorf("unable to get license info for feature: %s", ThinProvisioning)
}
dataReductionLicenseInfoResp, err := v.isFeatureLicensed(ctx, DataReduction)
if err != nil {
return nil, fmt.Errorf("unable to get license info for feature: %s", DataReduction)
}
if thinProvisioningLicenseInfoResp.LicenseInfoContent.IsInstalled && thinProvisioningLicenseInfoResp.LicenseInfoContent.IsValid {
lunParams.IsThinEnabled = strconv.FormatBool(isThinEnabled)
} else if isThinEnabled == true {
return nil, fmt.Errorf("thin Provisioning is not supported on array and hence cannot create Volume")
}
if dataReductionLicenseInfoResp.LicenseInfoContent.IsInstalled && dataReductionLicenseInfoResp.LicenseInfoContent.IsValid {
lunParams.IsDataReductionEnabled = strconv.FormatBool(isDataReductionEnabled)
} else if isDataReductionEnabled == true {
return nil, fmt.Errorf("data Reduction is not supported on array and hence cannot create Volume")
}
if hostIOLimitID != "" {
ioLimitPolicyParam := types.IoLimitPolicyParam{
ID: hostIOLimitID,
}
ioLimitParameters := types.HostIoLimitParameters{
IoLimitPolicyParam: &ioLimitPolicyParam,
}
lunParams.IoLimitParameters = &ioLimitParameters
}
if pool != nil && pool.StoragePoolContent.PoolFastVP.Status != 0 {
log.Debug("FastVP is enabled")
fastVPParameters := types.FastVPParameters{
TieringPolicy: fastVPTieringPolicy,
}
lunParams.FastVPParameters = &fastVPParameters
} else {
log.Debug("FastVP is not enabled")
if fastVPTieringPolicy != 0 {
return nil, fmt.Errorf("fastVP is not enabled and requested tiering policy is: %d ", fastVPTieringPolicy)
}
}
volumeReqParam := types.LunCreateParam{
Name: name,
Description: description,
LunParameters: &lunParams,
}
volumeResp := &types.Volume{}
err = v.client.executeWithRetryAuthenticate(ctx,
http.MethodPost, fmt.Sprintf(api.UnityAPIStorageResourceActionURI, api.CreateLunAction), volumeReqParam, volumeResp)
if err != nil {
return nil, err
}
return volumeResp, nil
}
// FindVolumeByName - Find the volume by it's name. If the volume is not found, an error will be returned.
func (v *Volume) FindVolumeByName(ctx context.Context, volName string) (*types.Volume, error) {
if len(volName) == 0 {
return nil, fmt.Errorf("lun Name shouldn't be empty")
}
volumeResp := &types.Volume{}
err := v.client.executeWithRetryAuthenticate(ctx, http.MethodGet, fmt.Sprintf(api.UnityAPIGetResourceByNameWithFieldsURI, api.LunAction, volName, LunDisplayFields), nil, volumeResp)
if err != nil {
return nil, fmt.Errorf("unable to find volume by name %s", volName)
}
return volumeResp, nil
}
// FindVolumeByID - Find the volume by it's Id. If the volume is not found, an error will be returned.
func (v *Volume) FindVolumeByID(ctx context.Context, volID string) (*types.Volume, error) {
log := util.GetRunIDLogger(ctx)
if len(volID) == 0 {
return nil, errors.New("lun ID shouldn't be empty")
}
volumeResp := &types.Volume{}
err := v.client.executeWithRetryAuthenticate(ctx, http.MethodGet, fmt.Sprintf(api.UnityAPIGetResourceWithFieldsURI, api.LunAction, volID, LunDisplayFields), nil, volumeResp)
if err != nil {
if strings.Contains(err.Error(), VolumeNotFoundErrorCode) {
log.Debugf("Unable to find volume Id %s Error: %v", volID, err)
return nil, ErrorVolumeNotFound
}
return nil, err
}
return volumeResp, nil
}
// ListVolumes - list volumes
func (v *Volume) ListVolumes(ctx context.Context, startToken int, maxEntries int) ([]types.Volume, int, error) {
log := util.GetRunIDLogger(ctx)
volumeResp := &types.ListVolumes{}
nextToken := startToken + 1
lunURI := fmt.Sprintf(api.UnityAPIInstanceTypeResourcesWithFields, api.LunAction, LunDisplayFields)
if maxEntries != 0 {
lunURI = fmt.Sprintf(lunURI+"&per_page=%d", maxEntries)
// startToken should exists only when maxEntries are present
if startToken != 0 {
lunURI = fmt.Sprintf(lunURI+"&page=%d", startToken)
}
}
err := v.client.executeWithRetryAuthenticate(ctx, http.MethodGet, lunURI, nil, volumeResp)
if err != nil {
log.Errorf("executeWithRetryAuthenticate Error: %v", err)
}
return volumeResp.Volumes, nextToken, err
}
// DeleteVolume - Delete Volume by its ID. If the Volume is not present on the array, an error will be returned.
func (v *Volume) DeleteVolume(ctx context.Context, volumeID string) error {
log := util.GetRunIDLogger(ctx)
if len(volumeID) == 0 {
return errors.New("Volume Id cannot be empty")
}
volumeResp := &types.Volume{}
err := v.client.executeWithRetryAuthenticate(ctx, http.MethodGet, fmt.Sprintf(api.UnityAPIGetResourceURI, api.StorageResourceAction, volumeID), nil, volumeResp)
volResp, err := v.FindVolumeByID(ctx, volumeID)
if err != nil {
return err
}
sourceVolID := ""
if volResp.VolumeContent.IsThinClone {
// Check if parent volume is marked for deletion
sourceVolID = volResp.VolumeContent.ParentVolume.ID
}
deleteErr := v.client.executeWithRetryAuthenticate(ctx, http.MethodDelete, fmt.Sprintf(api.UnityAPIGetResourceURI, api.StorageResourceAction, volumeID), nil, nil)
deleteSourceVol := false
if sourceVolID != "" {
sourceVolResp, err := v.FindVolumeByID(ctx, sourceVolID)
if err != nil && err != ErrorVolumeNotFound {
return fmt.Errorf("find Source Volume %s Failed. Error: %v", sourceVolID, err)
}
if strings.Contains(sourceVolResp.VolumeContent.Name, MarkVolumeForDeletion) {
deleteSourceVol = true
}
}
if deleteSourceVol {
deleteSourceErr := v.client.executeWithRetryAuthenticate(ctx, http.MethodDelete, fmt.Sprintf(api.UnityAPIGetResourceURI, api.StorageResourceAction, volResp.VolumeContent.ParentVolume.ID), nil, nil)
if deleteSourceErr != nil {
log.Warnf("Deletion of source volume: %s marked for deletion failed with error: %v", volResp.VolumeContent.ParentVolume.ID, deleteSourceErr)
} else {
log.Debugf("Deletion of source volume: %s marked for deletion successful", volResp.VolumeContent.ParentVolume.ID)
}
}
if deleteErr != nil {
if strings.Contains(deleteErr.Error(), DependentClonesErrorCode) {
newName := MarkVolumeForDeletion + strconv.FormatInt(time.Now().Unix(), 10)
err := v.RenameVolume(ctx, newName, volumeID)
if err != nil {
// Unable to mark volume for deletion
log.Warnf("Unable to mark volume %s with dependent clones for deletion", volumeID)
} else {
log.Debugf("Volume %s has dependent clones and marked for deletion.", volumeID)
}
return nil
}
return fmt.Errorf("delete Volume %s Failed. Error: %v", volumeID, deleteErr)
}
log.Debugf("Delete Storage Resource %s Successful", volumeID)
return nil
}
// ExportVolume - Export volume to a host
func (v *Volume) ExportVolume(ctx context.Context, volID, hostID string) error {
hostIDContent := types.HostIDContent{
ID: hostID,
}
hostAccess := types.HostAccess{
HostIDContent: &hostIDContent,
AccessMask: "1", // Hardcoded as 1 so that the host can have access to production LUNs only.
}
hostAccessArray := []types.HostAccess{hostAccess}
lunParams := types.LunHostAccessParameters{
HostAccess: &hostAccessArray,
}
lunModifyParam := types.LunHostAccessModifyParam{
LunHostAccessParameters: &lunParams,
}
return v.client.executeWithRetryAuthenticate(ctx, http.MethodPost, fmt.Sprintf(api.UnityModifyLunURI, volID), lunModifyParam, nil)
}
// ModifyVolumeExport - Export volume to multiple hosts / Modify the host access list on a given Volume
func (v *Volume) ModifyVolumeExport(ctx context.Context, volID string, hostIDList []string) error {
hostAccessArray := []types.HostAccess{}
for _, hostID := range hostIDList {
hostIDContent := types.HostIDContent{
ID: hostID,
}
hostAccess := types.HostAccess{
HostIDContent: &hostIDContent,
AccessMask: "1", // Hardcoded as 1 so that the host can have access to production LUNs only.
}
hostAccessArray = append(hostAccessArray, hostAccess)
}
lunParams := types.LunHostAccessParameters{
HostAccess: &hostAccessArray,
}
lunModifyParam := types.LunHostAccessModifyParam{
LunHostAccessParameters: &lunParams,
}
return v.client.executeWithRetryAuthenticate(ctx, http.MethodPost, fmt.Sprintf(api.UnityModifyLunURI, volID), lunModifyParam, nil)
}
// UnexportVolume - Unexport volume
func (v *Volume) UnexportVolume(ctx context.Context, volID string) error {
hostAccessArray := []types.HostAccess{}
lunParams := types.LunHostAccessParameters{
HostAccess: &hostAccessArray,
}
lunModifyParam := types.LunHostAccessModifyParam{
LunHostAccessParameters: &lunParams,
}
return v.client.executeWithRetryAuthenticate(ctx, http.MethodPost, fmt.Sprintf(api.UnityModifyLunURI, volID), lunModifyParam, nil)
}
// ExpandVolume - Expand volume to provided capacity
func (v *Volume) ExpandVolume(ctx context.Context, volumeID string, newSize uint64) error {
log := util.GetRunIDLogger(ctx)
vol, err := v.FindVolumeByID(ctx, volumeID)
if err != nil {
return fmt.Errorf("unable to find volume Id %s Error: %v", volumeID, err)
}
if vol.VolumeContent.SizeTotal == newSize {
log.Infof("New Volume size (%d) is same as existing Volume size(%d). Ignoring expand volume operation.", newSize, vol.VolumeContent.SizeTotal)
return nil
} else if vol.VolumeContent.SizeTotal > newSize {
return fmt.Errorf("requested new capacity smaller than existing capacity")
}
lunParams := types.LunExpandParameters{
Size: newSize,
}
volumeReqParam := types.LunExpandModifyParam{
LunParameters: &lunParams,
}
return v.client.executeWithRetryAuthenticate(ctx, http.MethodPost, fmt.Sprintf(api.UnityAPIModifyLunURI, volumeID), volumeReqParam, nil)
}
// FindHostIOLimitByName - Find Host IO limit
func (v *Volume) FindHostIOLimitByName(ctx context.Context, hostIoPolicyName string) (*types.IoLimitPolicy, error) {
if len(hostIoPolicyName) == 0 {
return nil, errors.New("policy Name shouldn't be empty")
}
ioLimitPolicyResp := &types.IoLimitPolicy{}
err := v.client.executeWithRetryAuthenticate(ctx, http.MethodGet, fmt.Sprintf(api.UnityAPIGetResourceByNameWithFieldsURI, api.IOLimitPolicy, hostIoPolicyName, HostIOLimitFields), nil, ioLimitPolicyResp)
if err != nil {
return nil, fmt.Errorf("unable to find IO Limit Policy:%s Error: %v", hostIoPolicyName, err)
}
return ioLimitPolicyResp, nil
}
// CreteLunThinClone - Create a lun thin clone
func (v *Volume) CreteLunThinClone(ctx context.Context, name, snapID, volID string) (*types.Volume, error) {
snapIDContent := types.SnapshotIDContent{
ID: snapID,
}
createLunThinCloneParam := types.CreateLunThinCloneParam{
SnapIDContent: &snapIDContent,
Name: name,
}
volumeResp := &types.Volume{}
err := v.client.executeWithRetryAuthenticate(ctx, http.MethodPost, fmt.Sprintf(api.UnityAPICreateLunThinCloneURI, volID), createLunThinCloneParam, volumeResp)
return volumeResp, err
}
// isFeatureLicensed - Get License information
func (v *Volume) isFeatureLicensed(ctx context.Context, featureName LicenseType) (*types.LicenseInfo, error) {
licenseInfoResp := &types.LicenseInfo{}
err := v.client.executeWithRetryAuthenticate(ctx, http.MethodGet, fmt.Sprintf(api.UnityAPIGetResourceByNameWithFieldsURI, api.LicenseAction, featureName, LicenseInfoDisplayFields), nil, licenseInfoResp)
if err != nil {
return nil, fmt.Errorf("unable to get license info for feature: %s", featureName)
}
return licenseInfoResp, nil
}
// CreateCloneFromVolume - Volume cloning
func (v *Volume) CreateCloneFromVolume(ctx context.Context, name, volID string) (*types.Volume, error) {
log := util.GetRunIDLogger(ctx)
snapAPI := NewSnapshot(v.client)
// Create snapshot for cloning
snapName := SnapForClone + strconv.FormatInt(time.Now().Unix(), 10)
snapResp, err := snapAPI.CreateSnapshot(ctx, volID, snapName, "", "")
if err != nil {
return nil, ErrorCreateSnapshotFailed
}
// Clone Volume
cloned := true
volResp, err := v.CreteLunThinClone(ctx, name, snapResp.SnapshotContent.ResourceID, volID)
if err != nil {
cloned = false
}
// Delete Snapshot
err = snapAPI.DeleteSnapshot(ctx, snapResp.SnapshotContent.ResourceID)
if err != nil {
// If delete snapshot created to clone volume failed then error is only logged not returned
log.Warnf("Unable to Delete Snapshot: %s created to clone Volume: %s", snapName, volID)
}
if !cloned {
return nil, ErrorCloningFailed
}
return volResp, nil
}
// RenameVolume - Rename Volume
func (v *Volume) RenameVolume(ctx context.Context, newName, volID string) error {
lunParams := types.LunParameters{
Name: newName,
}
return v.client.executeWithRetryAuthenticate(ctx, http.MethodPost, fmt.Sprintf(api.UnityModifyLunURI, volID), lunParams, nil)
}
// GetMaxVolumeSize - Returns the max size of a volume supported by the array
func (v *Volume) GetMaxVolumeSize(ctx context.Context, systemLimitID string) (*types.MaxVolumSizeInfo, error) {
volumeResp := &types.MaxVolumSizeInfo{}
if len(systemLimitID) == 0 {
return nil, errors.New("system limit ID shouldn't be empty")
}
lunURI := fmt.Sprintf(api.UnityAPIGetMaxVolumeSize, systemLimitID, MaximumVolumeSize)
err := v.client.executeWithRetryAuthenticate(ctx, http.MethodGet, lunURI, nil, volumeResp)
if err != nil {
return nil, fmt.Errorf("unable to find system limit by ID %s", systemLimitID)
}
return volumeResp, nil
}