forked from thecodeteam/goisilon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_v1_snapshots.go
99 lines (86 loc) · 2.77 KB
/
api_v1_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
package v1
import (
"context"
"errors"
"fmt"
"path"
"github.com/thecodeteam/goisilon/api"
)
// GetIsiSnapshots queries a list of all snapshots on the cluster
func GetIsiSnapshots(
ctx context.Context,
client api.Client) (resp *getIsiSnapshotsResp, err error) {
// PAPI call: GET https://1.2.3.4:8080/platform/1/snapshot/snapshots
err = client.Get(ctx, snapshotsPath, "", nil, nil, &resp)
if err != nil {
return nil, err
}
return resp, nil
}
// GetIsiSnapshot queries an individual snapshot on the cluster
func GetIsiSnapshot(
ctx context.Context,
client api.Client,
id int64) (*IsiSnapshot, error) {
// PAPI call: GET https://1.2.3.4:8080/platform/1/snapshot/snapshots/123
snapshotUrl := fmt.Sprintf("%s/%d", snapshotsPath, id)
var resp *getIsiSnapshotsResp
err := client.Get(ctx, snapshotUrl, "", nil, nil, &resp)
if err != nil {
return nil, err
}
// PAPI returns the snapshot data in a JSON list with the same structure as
// when querying all snapshots. Since this is for a single Id, we just
// want the first (and should be only) entry in the list.
return resp.SnapshotList[0], nil
}
// CreateIsiSnapshot makes a new snapshot on the cluster
func CreateIsiSnapshot(
ctx context.Context,
client api.Client,
path, name string) (resp *IsiSnapshot, err error) {
// PAPI call: POST https://1.2.3.4:8080/platform/1/snapshot/snapshots
// Content-Type: application/json
// {path: "/path/to/volume"
// name: "snapshot_name" <--- optional
// }
if path == "" {
return nil, errors.New("no path set")
}
data := &SnapshotPath{Path: path}
if name != "" {
data.Name = name
}
err = client.Post(ctx, snapshotsPath, "", nil, nil, data, &resp)
if err != nil {
return nil, err
}
return resp, nil
}
// CopyIsiSnaphost copies all files/directories in a snapshot to a new directory
func CopyIsiSnapshot(
ctx context.Context,
client api.Client,
sourceSnapshotName, sourceVolume, destinationName string) (resp *IsiVolume, err error) {
// PAPI calls: PUT https://1.2.3.4:8080/namespace/path/to/volumes/destination_volume_name
// x-isi-ifs-copy-source: /path/to/snapshot/volumes/source_volume_name
headers := map[string]string{
"x-isi-ifs-copy-source": path.Join(
"/",
realVolumeSnapshotPath(client, sourceSnapshotName),
sourceVolume),
}
// copy the volume
err = client.Put(ctx, realNamespacePath(client), destinationName, nil, headers, nil, &resp)
return resp, err
}
// RemoveIsiSnapshot deletes a snapshot from the cluster
func RemoveIsiSnapshot(
ctx context.Context,
client api.Client,
id int64) error {
// PAPI call: DELETE https://1.2.3.4:8080/platform/1/snapshot/snapshots/123
snapshotUrl := fmt.Sprintf("%s/%d", snapshotsPath, id)
err := client.Delete(ctx, snapshotUrl, "", nil, nil, nil)
return err
}