-
Notifications
You must be signed in to change notification settings - Fork 14
/
cloud_backup_recovery_point.go
312 lines (287 loc) · 10.9 KB
/
cloud_backup_recovery_point.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
package gobizfly
import (
"context"
"encoding/json"
"net/http"
"strings"
)
// CloudBackupRecoveryPoint represents a Cloud Backup Recovery Point.
type CloudBackupRecoveryPoint struct {
RecoveryPointType string `json:"recovery_point_type"`
Status string `json:"status"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Name string `json:"name"`
ID string `json:"id"`
}
// CloudBackupFile represents a Cloud Backup File.
type CloudBackupFile struct {
ID string `json:"id"`
ItemName string `json:"item_name"`
Size int `json:"size"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
ContentType string `json:"content_type"`
ETag string `json:"eTag"`
ItemType string `json:"item_type"`
LastModified string `json:"last_modified"`
Mode string `json:"mode"`
Status string `json:"status"`
}
// CloudBackupRecoveryPointActionPayload represents a Cloud Backup Recovery Point Action Payload.
type CloudBackupRecoveryPointActionPayload struct {
Action string `json:"action"`
}
// CloudBackupMachineRecoveryPoint represents a Cloud Backup Recovery Point Action.
type CloudBackupMachineRecoveryPoint struct {
BackupDirectory CloudBackupDirectory `json:"backup_directory"`
CreatedAt string `json:"created_at"`
ID string `json:"id"`
Name string `json:"name"`
RecoveryPointType string `json:"recovery_point_type"`
Status string `json:"status"`
UpdatedAt string `json:"updated_at"`
}
// CloudBackupExtendedRecoveryPoint represents a Cloud Backup Extended Recovery Point.
type CloudBackupExtendedRecoveryPoint struct {
CloudBackupMachineRecoveryPoint
IndexHash string `json:"index_hash,omitempty"`
LocalSize int `json:"local_size,omitempty"`
Method string `json:"method,omitempty"`
Progress string `json:"progress,omitempty"`
StorageSize int `json:"storage_size,omitempty"`
TotalFiles int `json:"total_files,omitempty"`
}
// CloudBackupRecoveryPointItem represents a Cloud Backup Recovery Point Item.
type CloudBackupRecoveryPointItem struct {
AccessMode string `json:"access_mode"`
AccessTime string `json:"access_time"`
ChangeTime string `json:"change_time"`
ContentType string `json:"content_type"`
CreatedAt string `json:"created_at"`
Gid string `json:"gid"`
ID string `json:"id"`
IsDir bool `json:"is_dir"`
ItemName string `json:"item_name"`
ItemType string `json:"item_type"`
Mode string `json:"mode"`
ModifyTime string `json:"modify_time"`
RealName string `json:"real_name"`
Size string `json:"size"`
Status string `json:"status"`
SymlinkPath string `json:"symlink_path"`
Uid string `json:"uid"`
UpdatedAt string `json:"updated_at"`
}
// CloudBackupRestoreRecoveryPointPayload represents a Cloud Backup Restore Recovery Point Payload.
type CloudBackupRestoreRecoveryPointPayload struct {
RecoveryPointID string `json:"recovery_point_id"`
Path string `json:"path"`
}
// CloudBackupStateDirectoryAction represents a Cloud Backup State Directory Action.
type CloudBackupStateDirectoryAction struct {
Action string `json:"action"`
BackupDirectories []string `json:"backup_directories"`
}
// CloudBackupCreateDirectoryPayload represents a Cloud Backup Create Directory Payload.
type CloudBackupCreateDirectoryPayload struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Path string `json:"path"`
Policies []string `json:"policies,omitempty"`
}
// CloudBackupPatchDirectoryPayload represents a Cloud Backup Patch Directory Payload.
type CloudBackupPatchDirectoryPayload struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Policies []string `json:"policies,omitempty"`
}
// CloudBackupDeleteDirectoryPayload represents a Cloud Backup Delete Directory Payload.
type CloudBackupDeleteDirectoryPayload struct {
Keep bool `json:"keep"`
}
// CloudBackupActionDirectoryPayload represents a Cloud Backup Action Directory Payload.
type CloudBackupActionDirectoryPayload struct {
Action string `json:"action"`
StorageType string `json:"storage_type,omitempty"`
Name string `json:"name,omitempty"`
}
// CloudBackupDeleteMultipleRecoveryPointPayload represents a Cloud Backup Delete Multiple Recovery Point Payload.
type CloudBackupDeleteMultipleRecoveryPointPayload struct {
RecoveryPointIDs []string `json:"recovery_point_ids"`
}
// CloudBackupDeleteMultipleDirectoriesPayload represents a Cloud Backup Delete Multiple Recovery Point Payload.
type CloudBackupDeleteMultipleDirectoriesPayload struct {
BackupDirectories []string `json:"backup_directories"`
Keep bool `json:"keep"`
}
// ListTenantRecoveryPoints lists all recovery points belonging to a tenant.
func (cb *cloudBackupService) ListTenantRecoveryPoints(ctx context.Context) ([]*CloudBackupMachineRecoveryPoint, error) {
req, err := cb.client.NewRequest(ctx, http.MethodGet, cloudBackupServiceName, cb.recoveryPointPath(), nil)
if err != nil {
return nil, err
}
resp, err := cb.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var recoveryPoints []*CloudBackupMachineRecoveryPoint
if err := json.NewDecoder(resp.Body).Decode(&recoveryPoints); err != nil {
return nil, err
}
return recoveryPoints, nil
}
// DeleteMultipleRecoveryPoints deletes multiple recovery points.
func (cb *cloudBackupService) DeleteMultipleRecoveryPoints(ctx context.Context, payload CloudBackupDeleteMultipleRecoveryPointPayload) error {
req, err := cb.client.NewRequest(ctx, http.MethodDelete, cloudBackupServiceName, cb.recoveryPointPath(), payload)
if err != nil {
return err
}
resp, err := cb.client.Do(ctx, req)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
// ListDirectoryRecoveryPoints lists all recovery points belonging to a directory.
func (cb *cloudBackupService) ListDirectoryRecoveryPoints(ctx context.Context, machineID string, directoryID string) ([]*CloudBackupMachineRecoveryPoint, error) {
req, err := cb.client.NewRequest(ctx, http.MethodGet, cloudBackupServiceName,
cb.machineDirectoryRecoveryPointPath(machineID, directoryID), nil)
if err != nil {
return nil, err
}
resp, err := cb.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var data struct {
RecoveryPoints []*CloudBackupMachineRecoveryPoint `json:"recovery_points"`
}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return data.RecoveryPoints, nil
}
// ListRecoveryPointFiles lists all files belonging to a recovery point.
func (cb *cloudBackupService) ListRecoveryPointFiles(ctx context.Context, recoveryPointID string) ([]*CloudBackupFile, error) {
req, err := cb.client.NewRequest(ctx, http.MethodGet, cloudBackupServiceName,
strings.Join([]string{cb.itemRecoveryPointPath(recoveryPointID), "files"}, "/"), nil)
if err != nil {
return nil, err
}
resp, err := cb.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var files []*CloudBackupFile
if err := json.NewDecoder(resp.Body).Decode(&files); err != nil {
return nil, err
}
return files, nil
}
// RecoveryPointAction performs an action on a recovery point.
func (cb *cloudBackupService) RecoveryPointAction(ctx context.Context, recoveryPointID string, payload *CloudBackupRecoveryPointActionPayload) (*CloudBackupMachineRecoveryPoint, error) {
req, err := cb.client.NewRequest(ctx, http.MethodPatch, cloudBackupServiceName,
strings.Join([]string{cb.itemRecoveryPointPath(recoveryPointID), "action"}, "/"), payload)
if err != nil {
return nil, err
}
resp, err := cb.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var recoveryPoint *CloudBackupMachineRecoveryPoint
if err = json.NewDecoder(resp.Body).Decode(&recoveryPoint); err != nil {
return nil, err
}
return recoveryPoint, nil
}
// ListMachineRecoveryPoints lists all recovery points belonging to a machine.
func (cb *cloudBackupService) ListMachineRecoveryPoints(ctx context.Context, machineID string) ([]*CloudBackupExtendedRecoveryPoint, error) {
req, err := cb.client.NewRequest(ctx, http.MethodGet, cloudBackupServiceName,
cb.itemMachinePath(machineID), nil)
if err != nil {
return nil, err
}
resp, err := cb.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var recoveryPoints struct {
RecoveryPoints []*CloudBackupExtendedRecoveryPoint `json:"recovery_points"`
}
if err := json.NewDecoder(resp.Body).Decode(&recoveryPoints); err != nil {
return nil, err
}
return recoveryPoints.RecoveryPoints, nil
}
// GetRecoveryPoint gets a recovery point.
func (cb *cloudBackupService) GetRecoveryPoint(ctx context.Context, recoveryPointID string) (*CloudBackupMachineRecoveryPoint, error) {
req, err := cb.client.NewRequest(ctx, http.MethodGet, cloudBackupServiceName,
cb.itemRecoveryPointPath(recoveryPointID), nil)
if err != nil {
return nil, err
}
resp, err := cb.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var recoveryPoint *CloudBackupMachineRecoveryPoint
if err := json.NewDecoder(resp.Body).Decode(&recoveryPoint); err != nil {
return nil, err
}
return recoveryPoint, nil
}
// DeleteRecoveryPoint deletes a recovery point.
func (cb *cloudBackupService) DeleteRecoveryPoint(ctx context.Context, recoveryPointID string) error {
req, err := cb.client.NewRequest(ctx, http.MethodDelete, cloudBackupServiceName,
cb.itemRecoveryPointPath(recoveryPointID), nil)
if err != nil {
return err
}
_, err = cb.client.Do(ctx, req)
if err != nil {
return err
}
return nil
}
// ListRecoveryPointItems lists all items belonging to a recovery point.
func (cb *cloudBackupService) ListRecoveryPointItems(ctx context.Context, recoveryPointID string) ([]*CloudBackupRecoveryPointItem, error) {
req, err := cb.client.NewRequest(ctx, http.MethodGet, cloudBackupServiceName,
strings.Join([]string{cb.itemRecoveryPointPath(recoveryPointID), "items"}, "/"), nil)
if err != nil {
return nil, err
}
resp, err := cb.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var data struct {
Items []*CloudBackupRecoveryPointItem `json:"items"`
}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return data.Items, nil
}
// RestoreRecoveryPoint restores a recovery point.
func (cb *cloudBackupService) RestoreRecoveryPoint(ctx context.Context, recoveryPointID string, payload *CloudBackupRestoreRecoveryPointPayload) error {
req, err := cb.client.NewRequest(ctx, http.MethodPost, cloudBackupServiceName,
strings.Join([]string{cb.itemRecoveryPointPath(recoveryPointID), "action"}, "/"), payload)
if err != nil {
return err
}
_, err = cb.client.Do(ctx, req)
if err != nil {
return err
}
return nil
}