-
Notifications
You must be signed in to change notification settings - Fork 6
/
snapshot.go
306 lines (268 loc) · 10.9 KB
/
snapshot.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
/*
Copyright © 2019 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 gounity
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"github.com/dell/gounity/api"
"github.com/dell/gounity/types"
"github.com/dell/gounity/util"
)
// FilesystemAccessType is integer
type FilesystemAccessType int
// FilesystemAccessType constants
const (
BlockAccessType FilesystemAccessType = 0 // Parameter not applicable for block
CheckpointAccessType FilesystemAccessType = 1 // Checkpoint access to enable access through a .ckpt folder in the file system.
ProtocolAccessType FilesystemAccessType = 2 // Protocol access to enable access through a file share.
)
// SnapshotNotFoundErrorCode stores snapshot not found error code
var SnapshotNotFoundErrorCode = "0x7d13005"
// ErrorSnapshotNotFound stores Snapshot not found error
var ErrorSnapshotNotFound = errors.New("Unable to find filesystem")
// Snapshot structure
type Snapshot struct {
client *Client
}
// NewSnapshot function returns snapshot
func NewSnapshot(client *Client) *Snapshot {
return &Snapshot{client}
}
// CreateSnapshot creates a snapshot of a volume
//
// Parameters:
//
// - `storageResourceID` : the array to check
// - `name` : the value to search for
//
// Returns:
// - *types.Snapshot
// - an error if create snapshot fails
func (s *Snapshot) CreateSnapshot(ctx context.Context, storageResourceID, snapshotName, description, retentionDuration string) (*types.Snapshot, error) {
return s.CreateSnapshotWithFsAccesType(ctx, storageResourceID, snapshotName, description, retentionDuration, BlockAccessType)
}
// CreateSnapshotWithFsAccesType - Creates snashot with FsAccess type
func (s *Snapshot) CreateSnapshotWithFsAccesType(ctx context.Context, storageResourceID, snapshotName, _, retentionDuration string, filesystemAccessType FilesystemAccessType) (*types.Snapshot, error) {
var createSnapshot types.CreateSnapshotParam
if len(storageResourceID) == 0 {
return nil, errors.New("storage Resource ID cannot be empty")
}
var err error
createSnapshot.Name, err = util.ValidateResourceName(snapshotName, api.MaxResourceNameLength)
if err != nil {
return nil, fmt.Errorf("invalid snapshot name Error:%v", err)
}
if retentionDuration != "" {
seconds, err := util.ValidateDuration(retentionDuration)
if err != nil {
return nil, err
}
if seconds != 0 {
createSnapshot.RetentionDuration = seconds
}
}
storageResource := types.StorageResourceParam{
ID: storageResourceID,
}
createSnapshot.StorageResource = &storageResource
createSnapshot.FilesystemAccessType = int(filesystemAccessType)
snapshotResp := &types.Snapshot{}
err = s.client.executeWithRetryAuthenticate(ctx, http.MethodPost, fmt.Sprintf(api.UnityAPIInstanceTypeResources, api.SnapAction), createSnapshot, snapshotResp)
if err != nil {
return nil, err
}
return snapshotResp, nil
}
// DeleteFilesystemAsSnapshot - Delete Snapshots acting as filesystem on array
func (s *Snapshot) DeleteFilesystemAsSnapshot(ctx context.Context, snapshotID string, sourceFs *types.Filesystem) error {
log := util.GetRunIDLogger(ctx)
deleteSourceFs := false
if strings.Contains(sourceFs.FileContent.Description, MarkFilesystemForDeletion) {
deleteSourceFs = true
}
err := s.DeleteSnapshot(ctx, snapshotID)
if err != nil {
return err
}
if deleteSourceFs {
// Try deleting the marked filesystem for deletion
f := NewFilesystem(s.client)
err = f.DeleteFilesystem(ctx, sourceFs.FileContent.ID)
if err != nil {
log.Warnf("Deletion of source filesystem: %s marked for deletion failed with error: %v", sourceFs.FileContent.ID, err)
}
}
return nil
}
// DeleteSnapshot deletes a snapshot based on Snapshot ID
//
// Parameters:
//
// - `snapshotID` : User need to provide snapshot CLI Id.
//
// Returns:
// - an error if delete snapshot fails
func (s *Snapshot) DeleteSnapshot(ctx context.Context, snapshotID string) error {
log := util.GetRunIDLogger(ctx)
if snapshotID == "" {
return errors.New("snapshot ID cannot be empty")
}
deleteErr := s.client.executeWithRetryAuthenticate(ctx, http.MethodDelete, fmt.Sprintf(api.UnityAPIGetResourceURI, api.SnapAction, snapshotID), nil, nil)
if deleteErr != nil {
return fmt.Errorf("delete Snapshot Id-%s Failed: %v ", snapshotID, deleteErr)
}
log.Debugf("Delete Snapshot ID-%s Successful", snapshotID)
return nil
}
// ListSnapshots lists all snapshots based on Snapshot ID or source-volume-id
// Returns a chunk of data on a single page, as specified by the maxEntries and page (startToken) parameters.
func (s *Snapshot) ListSnapshots(ctx context.Context, startToken int, maxEntries int, sourceVolumeID, snapshotID string) ([]types.Snapshot, int, error) {
snapResp := &types.ListSnapshot{}
if snapshotID != "" {
snapshotURI := fmt.Sprintf(api.UnityAPIGetResourceWithFieldsURI, api.SnapAction, snapshotID, SnapshotDisplayFields)
snapshotResp := &types.Snapshot{}
err := s.client.executeWithRetryAuthenticate(ctx, http.MethodGet, snapshotURI, nil, snapshotResp)
if err != nil {
return nil, 0, nil
}
return []types.Snapshot{*snapshotResp}, 0, nil
}
nextToken := startToken + 1
snapshotURI := fmt.Sprintf(api.UnityAPIInstanceTypeResourcesWithFields, api.SnapAction, SnapshotDisplayFields)
// Pagination will apply only for list all snapshots. If user provides snapshotID or sourceVolumeID then pagination will not apply
if sourceVolumeID == "" {
if maxEntries != 0 {
snapshotURI = fmt.Sprintf(snapshotURI+"&per_page=%d", maxEntries)
// startToken should exists only when maxEntries are present
if startToken != 0 {
snapshotURI = fmt.Sprintf(snapshotURI+"&page=%d", startToken)
}
}
}
err := s.client.executeWithRetryAuthenticate(ctx, http.MethodGet, snapshotURI, nil, snapResp)
if err != nil {
return nil, 0, err
}
var snapshots []types.Snapshot
if sourceVolumeID != "" {
for _, snapshot := range snapResp.Snapshots {
if snapshot.SnapshotContent.StorageResource.ID == sourceVolumeID {
snapshots = append(snapshots, snapshot)
}
}
return snapshots, 0, nil
}
return snapResp.Snapshots, nextToken, nil
}
// FindSnapshotByName - To find snapshot using snapshot-name
func (s *Snapshot) FindSnapshotByName(ctx context.Context, snapshotName string) (*types.Snapshot, error) {
log := util.GetRunIDLogger(ctx)
snapshotName, err := util.ValidateResourceName(snapshotName, api.MaxResourceNameLength)
if err != nil {
return nil, err
}
snapshotResp := &types.Snapshot{}
err = s.client.executeWithRetryAuthenticate(ctx, http.MethodGet, fmt.Sprintf(api.UnityAPIGetResourceByNameWithFieldsURI, api.SnapAction, snapshotName, SnapshotDisplayFields), nil, snapshotResp)
if err != nil {
if strings.Contains(err.Error(), SnapshotNotFoundErrorCode) {
return nil, ErrorSnapshotNotFound
}
return nil, fmt.Errorf("unable to find Snapshot Name %s Error: %v", snapshotName, err)
}
log.Debugf("Snapshot name: %s Id: %s", snapshotResp.SnapshotContent.Name, snapshotResp.SnapshotContent.ResourceID)
return snapshotResp, nil
}
// FindSnapshotByID - To find snapshot using snapshot-id
func (s *Snapshot) FindSnapshotByID(ctx context.Context, snapshotID string) (*types.Snapshot, error) {
log := util.GetRunIDLogger(ctx)
if snapshotID == "" {
return nil, errors.New("snapshot ID cannot be empty")
}
snapshotResp := &types.Snapshot{}
err := s.client.executeWithRetryAuthenticate(ctx, http.MethodGet, fmt.Sprintf(api.UnityAPIGetResourceWithFieldsURI, api.SnapAction, snapshotID, SnapshotDisplayFields), nil, snapshotResp)
if err != nil {
if strings.Contains(err.Error(), SnapshotNotFoundErrorCode) {
return nil, ErrorSnapshotNotFound
}
return nil, fmt.Errorf("unable to find Snapshot id %s Error: %v", snapshotID, err)
}
log.Debugf("Snapshot name: %s Id: %s", snapshotResp.SnapshotContent.Name, snapshotResp.SnapshotContent.ResourceID)
return snapshotResp, nil
}
// ModifySnapshotAutoDeleteParameter - Modify Snapshot (currently used to disable auto-delete parameter)
func (s *Snapshot) ModifySnapshotAutoDeleteParameter(ctx context.Context, snapshotID string) error {
log := util.GetRunIDLogger(ctx)
if snapshotID == "" {
return errors.New("snapshot ID cannot be empty")
}
modifySnapshot := types.CreateSnapshotParam{
IsAutoDelete: false,
}
snapshotResp := &types.Snapshot{}
err := s.client.executeWithRetryAuthenticate(ctx, http.MethodPost, fmt.Sprintf(api.UnityModifySnapshotURI, api.SnapAction, snapshotID), modifySnapshot, snapshotResp)
if err != nil {
return fmt.Errorf("unable to modify Snapshot %s Error: %v", snapshotID, err)
}
log.Debugf("Changed AutoDelete to false for Snapshot name: %s Id: %s", snapshotResp.SnapshotContent.Name, snapshotResp.SnapshotContent.ResourceID)
return nil
}
// CopySnapshot - Creates a copy of the source snapshot which can be used for NFS export, and returns the ID of the copy snapshot
func (s *Snapshot) CopySnapshot(ctx context.Context, sourceSnapshotID, name string) (*types.Snapshot, error) {
if name == "" {
return nil, errors.New("Snapshot Name cannot be empty")
}
if sourceSnapshotID == "" {
return nil, errors.New("Source Snapshot ID cannot be empty")
}
copySnapshotReq := types.CopySnapshot{
Name: name,
Child: true,
}
snapsResp := &types.CopySnapshots{}
err := s.client.executeWithRetryAuthenticate(ctx, http.MethodPost, fmt.Sprintf(api.UnityCopySnapshotURI, api.SnapAction, sourceSnapshotID), copySnapshotReq, snapsResp)
if err != nil {
return nil, fmt.Errorf("unable to Copy Snapshot %s. Error: %v", sourceSnapshotID, err)
}
snapResp, err := s.FindSnapshotByID(ctx, snapsResp.CopySnapshotsContent.Copies[0].ID)
if err != nil {
return nil, err
}
return snapResp, nil
}
// ModifySnapshot - Modify Snapshot's description and retention duration parameters
func (s *Snapshot) ModifySnapshot(ctx context.Context, snapshotID, description, retentionDuration string) error {
if snapshotID == "" {
return errors.New("snapshot ID cannot be empty")
}
modifySnapshot := types.CreateSnapshotParam{
Description: description,
}
if retentionDuration != "" {
seconds, err := util.ValidateDuration(retentionDuration)
if err != nil {
return err
}
if seconds != 0 {
modifySnapshot.RetentionDuration = seconds
}
}
snapshotResp := &types.Snapshot{}
err := s.client.executeWithRetryAuthenticate(ctx, http.MethodPost, fmt.Sprintf(api.UnityModifySnapshotURI, api.SnapAction, snapshotID), modifySnapshot, snapshotResp)
if err != nil {
return fmt.Errorf("unable to modify Snapshot %s Error: %v", snapshotID, err)
}
return nil
}