-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsnapshots.go
107 lines (86 loc) · 2.36 KB
/
snapshots.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
package goisilon
import (
"context"
"errors"
"fmt"
"path"
api "github.com/thecodeteam/goisilon/api/v1"
)
type SnapshotList []*api.IsiSnapshot
type Snapshot *api.IsiSnapshot
func (c *Client) GetSnapshots(ctx context.Context) (SnapshotList, error) {
snapshots, err := api.GetIsiSnapshots(ctx, c.API)
if err != nil {
return nil, err
}
return snapshots.SnapshotList, nil
}
func (c *Client) GetSnapshotsByPath(
ctx context.Context, path string) (SnapshotList, error) {
snapshots, err := api.GetIsiSnapshots(ctx, c.API)
if err != nil {
return nil, err
}
// find all the snapshots with the same path
snapshotsWithPath := make(SnapshotList, 0, len(snapshots.SnapshotList))
for _, snapshot := range snapshots.SnapshotList {
if snapshot.Path == c.API.VolumePath(path) {
snapshotsWithPath = append(snapshotsWithPath, snapshot)
}
}
return snapshotsWithPath, nil
}
func (c *Client) GetSnapshot(
ctx context.Context, id int64, name string) (Snapshot, error) {
// if we have an id, use it to find the snapshot
snapshot, err := api.GetIsiSnapshot(ctx, c.API, id)
if err == nil {
return snapshot, nil
}
// there's no id or it didn't match, iterate through all snapshots and match
// based on name
if name == "" {
return nil, err
}
snapshotList, err := c.GetSnapshots(ctx)
if err != nil {
return nil, err
}
for _, snapshot = range snapshotList {
if snapshot.Name == name {
return snapshot, nil
}
}
return nil, nil
}
func (c *Client) CreateSnapshot(
ctx context.Context, path, name string) (Snapshot, error) {
return api.CreateIsiSnapshot(ctx, c.API, c.API.VolumePath(path), name)
}
func (c *Client) RemoveSnapshot(
ctx context.Context, id int64, name string) error {
snapshot, err := c.GetSnapshot(ctx, id, name)
if err != nil {
return err
}
return api.RemoveIsiSnapshot(ctx, c.API, snapshot.Id)
}
func (c *Client) CopySnapshot(
ctx context.Context,
sourceId int64, sourceName, destinationName string) (Volume, error) {
snapshot, err := c.GetSnapshot(ctx, sourceId, sourceName)
if err != nil {
return nil, err
}
if snapshot == nil {
return nil, errors.New(fmt.Sprintf(
"Snapshot doesn't exist: (%d, %s)", sourceId, sourceName))
}
_, err = api.CopyIsiSnapshot(
ctx, c.API, snapshot.Name,
path.Base(snapshot.Path), destinationName)
if err != nil {
return nil, err
}
return c.GetVolume(ctx, destinationName, destinationName)
}