-
Notifications
You must be signed in to change notification settings - Fork 7
/
snapshot.go
127 lines (115 loc) · 3.39 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
package main
import (
"bufio"
"context"
log "github.com/sirupsen/logrus"
"os"
"github.com/gridscale/gsclient-go"
)
const locationUUID = "45ed677b-3702-4b36-be2a-a2eab9827950"
var emptyCtx = context.Background()
func main() {
uuid := os.Getenv("GRIDSCALE_UUID")
token := os.Getenv("GRIDSCALE_TOKEN")
config := gsclient.DefaultConfiguration(uuid, token)
client := gsclient.NewClient(config)
log.Info("gridscale client configured")
log.Info("Create storage and snapshot: Press 'Enter' to continue...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
//Create storage
cStorage, err := client.CreateStorage(
emptyCtx,
gsclient.StorageCreateRequest{
Capacity: 1,
LocationUUID: locationUUID,
Name: "go-client-storage",
})
if err != nil {
log.Error("Create storage has failed with error", err)
return
}
log.WithFields(log.Fields{
"storage_uuid": cStorage.ObjectUUID,
}).Info("Storage successfully created")
defer func() {
err := client.DeleteStorage(emptyCtx, cStorage.ObjectUUID)
if err != nil {
log.Error("Delete storage has failed with error", err)
return
}
log.Info("Storage successfully deleted")
}()
//Create a snapshot
cSnapshot, err := client.CreateStorageSnapshot(
emptyCtx,
cStorage.ObjectUUID,
gsclient.StorageSnapshotCreateRequest{
Name: "go-client-snapshot",
})
if err != nil {
log.Error("Create snapshot has failed with error", err)
return
}
log.WithFields(log.Fields{
"snapshot_uuid": cStorage.ObjectUUID,
}).Info("Snapshot successfully created")
defer func() {
err := client.DeleteStorageSnapshot(emptyCtx, cStorage.ObjectUUID, cSnapshot.ObjectUUID)
if err != nil {
log.Error("Delete storage snapshot has failed with error", err)
return
}
log.Info("Storage snapshot successfully deleted")
log.Info("Get deleted snapshots: Press 'Enter' to continue...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
snapshots, err := client.GetDeletedSnapshots(emptyCtx)
if err != nil {
log.Error("Get deleted snapshots has failed with error", err)
return
}
log.WithFields(log.Fields{
"snapshots": snapshots,
}).Info("Retrieved deleted snapshots successfully")
}()
//Get a snapshot to update
snapshot, err := client.GetStorageSnapshot(emptyCtx, cStorage.ObjectUUID, cSnapshot.ObjectUUID)
if err != nil {
log.Error("Get snapshot has failed with error", err)
return
}
log.WithFields(log.Fields{
"snapshot_uuid": snapshot.Properties.ObjectUUID,
}).Info("Snapshot successfully retrieved")
log.Info("Update snapshot: press 'Enter' to continue...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
//Update a snapshot
err = client.UpdateStorageSnapshot(
emptyCtx,
cStorage.ObjectUUID,
snapshot.Properties.ObjectUUID,
gsclient.StorageSnapshotUpdateRequest{
Name: "updated snapshot",
})
if err != nil {
log.Error("Update snapshot has failed with error", err)
return
}
log.Info("Snapshot successfully updated")
log.Info("Rollback storage: press 'Enter' to continue...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
//Rollback
err = client.RollbackStorage(
emptyCtx,
cStorage.ObjectUUID,
snapshot.Properties.ObjectUUID,
gsclient.StorageRollbackRequest{
Rollback: true,
})
if err != nil {
log.Error("Rollback storage has failed with error", err)
return
}
log.Info("Storage successfully rollbacked")
log.Info("Delete snapshot and storage: press 'Enter' to continue...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
}