-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcloud_database_backups.go
351 lines (281 loc) · 10.4 KB
/
cloud_database_backups.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// This file is part of gobizfly
package gobizfly
import (
"context"
"encoding/json"
"net/http"
)
const (
cloudDatabaseBackupsResourcePath = "/backups"
cloudDatabaseBackupSchedulesResourcePath = "/schedules"
)
type cloudDatabaseBackups struct {
client *Client
}
type cloudDatabaseBackupSchedules struct {
client *Client
}
// CloudDatabaseBackup contains detail of a backup.
type CloudDatabaseBackup struct {
Created string `json:"created"`
Datastore CloudDatabaseDatastore `json:"datastore"`
Description string `json:"description"`
ID string `json:"id"`
Message string `json:"message"`
Name string `json:"name"`
NodeID string `json:"node_id"`
ParentID string `json:"parent_id"`
ProjectID string `json:"project_id"`
Size float32 `json:"size"`
Status string `json:"status"`
Type string `json:"type"`
Updated string `json:"updated"`
}
// CloudDatabaseBackupCreate contains payload require create backup.
type CloudDatabaseBackupCreate struct {
Name string `json:"backup_name,omitempty" validate:"required"`
NodeID string `json:"node_id,omitempty"`
ParentID string `json:"parent_id,omitempty"`
Suggestion bool `json:"suggestion,omitempty"`
}
// CloudDatabaseBackupResource contains option list backup.
type CloudDatabaseBackupResource struct {
ResourceID string `json:"resource_id"`
ResourceType string `json:"resource_type"`
}
// CloudDatabase Schedule Backup Struct
// CloudDatabaseBackupSchedule contains detail of a schedule.
type CloudDatabaseBackupSchedule struct {
CronExpression string `json:"pattern"`
FirstExecutionTime string `json:"first_execution_time"`
ID string `json:"id"`
InstanceID string `json:"instance_id"`
InstanceName string `json:"instance_name"`
LimitBackup int `json:"limit_backup"`
Message string `json:"message"`
Name string `json:"name"`
NextExecutionTime string `json:"next_execution_time"`
NodeID string `json:"node_id"`
NodeName string `json:"node_name"`
ProjectID string `json:"project_id"`
}
// CloudDatabaseBackupScheduleCreate contains schedule create payload info.
type CloudDatabaseBackupScheduleCreate struct {
CronExpression string `json:"pattern"`
LimitBackup int `json:"limit_backup,omitempty" validate:"required"`
Name string `json:"schedule_name,omitempty" validate:"required"`
}
// CloudDatabaseBackupScheduleDelete contains option when delete a schedule.
type CloudDatabaseBackupScheduleDelete struct {
PurgeBackup bool `json:"purge_backup"`
}
// CloudDatabaseBackupScheduleListResourceOption contains option when list a database schedule.
type CloudDatabaseBackupScheduleListResourceOption struct {
All bool `json:"all,omitempty"`
ListBackup bool `json:"list_backup,omitempty"`
ResourceID string `json:"resource_id,omitempty"`
ResourceType string `json:"resource_type,omitempty"`
}
func (db *cloudDatabaseService) Backups() *cloudDatabaseBackups {
return &cloudDatabaseBackups{client: db.client}
}
func (db *cloudDatabaseService) BackupSchedules() *cloudDatabaseBackupSchedules {
return &cloudDatabaseBackupSchedules{client: db.client}
}
// CloudDatabase Backup Resource Path
func (bk *cloudDatabaseBackups) resourcePath(backupID string) string {
return cloudDatabaseBackupsResourcePath + "/" + backupID
}
// CloudDatabase Backup resourceType Create Path
func (bk *cloudDatabaseBackups) resourceCreatePath(resourceType string, resourceID string) string {
return "/" + resourceType + "/" + resourceID + cloudDatabaseBackupsResourcePath
}
// CloudDatabase Schedule Resource List Path
func (sc *cloudDatabaseBackupSchedules) resourcePath(scheduleID string) string {
return cloudDatabaseBackupSchedulesResourcePath + "/" + scheduleID
}
// CloudDatabase Schedule List In resourceType Path
func (sc *cloudDatabaseBackupSchedules) resourceCreatePath(resourceType string, resourceID string) string {
return "/" + resourceType + "/" + resourceID + cloudDatabaseBackupSchedulesResourcePath
}
// CloudDatabase Schedule Resource Backup Path
func (sc *cloudDatabaseBackupSchedules) resourceBackupPath(scheduleID string) string {
return cloudDatabaseBackupSchedulesResourcePath + "/" + scheduleID + cloudDatabaseBackupsResourcePath
}
// List all backups.
func (bk *cloudDatabaseBackups) List(ctx context.Context, resource *CloudDatabaseBackupResource, opts *CloudDatabaseListOption) ([]*CloudDatabaseBackup, error) {
resourcePath := cloudDatabaseBackupsResourcePath
if resource != nil {
resourcePath = bk.resourceCreatePath(resource.ResourceType, resource.ResourceID)
}
req, err := bk.client.NewRequest(ctx, http.MethodGet, databaseServiceName, resourcePath, nil)
if err != nil {
return nil, err
}
AddParamsListOption(req, opts)
resp, err := bk.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var data struct {
Backups []*CloudDatabaseBackup `json:"backups"`
}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return data.Backups, nil
}
// Create a backups.
func (bk *cloudDatabaseBackups) Create(ctx context.Context, resourceType string, resourceID string, cr *CloudDatabaseBackupCreate) (*CloudDatabaseBackup, error) {
req, err := bk.client.NewRequest(ctx, http.MethodPost, databaseServiceName, bk.resourceCreatePath(resourceType, resourceID), &cr)
if err != nil {
return nil, err
}
resp, err := bk.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var backups *CloudDatabaseBackup
if err := json.NewDecoder(resp.Body).Decode(&backups); err != nil {
return nil, err
}
return backups, nil
}
// Get a backups.
func (bk *cloudDatabaseBackups) Get(ctx context.Context, backupID string) (*CloudDatabaseBackup, error) {
req, err := bk.client.NewRequest(ctx, http.MethodGet, databaseServiceName, bk.resourcePath(backupID), nil)
if err != nil {
return nil, err
}
resp, err := bk.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var backups *CloudDatabaseBackup
if err := json.NewDecoder(resp.Body).Decode(&backups); err != nil {
return nil, err
}
return backups, nil
}
// Delete a backups.
func (bk *cloudDatabaseBackups) Delete(ctx context.Context, backupID string) (*CloudDatabaseMessageResponse, error) {
req, err := bk.client.NewRequest(ctx, http.MethodDelete, databaseServiceName, bk.resourcePath(backupID), nil)
if err != nil {
return nil, err
}
resp, err := bk.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var dmr *CloudDatabaseMessageResponse
if err := json.NewDecoder(resp.Body).Decode(&dmr); err != nil {
return nil, err
}
return dmr, nil
}
// List all schedule.
func (sc *cloudDatabaseBackupSchedules) List(ctx context.Context, resource *CloudDatabaseBackupScheduleListResourceOption, opts *CloudDatabaseListOption) ([]*CloudDatabaseBackupSchedule, error) {
var resourcePath string
switch {
case resource.All:
resourcePath = cloudDatabaseBackupSchedulesResourcePath
case resource.ListBackup && resource.ResourceID != "":
resourcePath = sc.resourceBackupPath(resource.ResourceID)
case resource.ResourceType != "" && resource.ResourceID != "":
resourcePath = sc.resourceCreatePath(resource.ResourceType, resource.ResourceID)
default:
resourcePath = cloudDatabaseBackupSchedulesResourcePath
}
req, err := sc.client.NewRequest(ctx, http.MethodGet, databaseServiceName, resourcePath, nil)
if err != nil {
return nil, err
}
AddParamsListOption(req, opts)
resp, err := sc.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var data struct {
BackupSchedules []*CloudDatabaseBackupSchedule `json:"schedules"`
}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return data.BackupSchedules, nil
}
// ListBackups list all backup in schedule.
func (sc *cloudDatabaseBackupSchedules) ListBackups(ctx context.Context, scheduleID string, opts *CloudDatabaseListOption) ([]*CloudDatabaseBackup, error) {
req, err := sc.client.NewRequest(ctx, http.MethodGet, databaseServiceName, sc.resourceBackupPath(scheduleID), nil)
if err != nil {
return nil, err
}
AddParamsListOption(req, opts)
resp, err := sc.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var data struct {
Backups []*CloudDatabaseBackup `json:"backups"`
}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return data.Backups, nil
}
// Create new schedule.
func (sc *cloudDatabaseBackupSchedules) Create(ctx context.Context, nodeID string, scc *CloudDatabaseBackupScheduleCreate) (*CloudDatabaseBackupSchedule, error) {
req, err := sc.client.NewRequest(ctx, http.MethodPost, databaseServiceName, sc.resourceCreatePath("nodes", nodeID), &scc)
if err != nil {
return nil, err
}
resp, err := sc.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var schedules *CloudDatabaseBackupSchedule
if err := json.NewDecoder(resp.Body).Decode(&schedules); err != nil {
return nil, err
}
return schedules, nil
}
// Get a schedule.
func (sc *cloudDatabaseBackupSchedules) Get(ctx context.Context, scheduleID string) (*CloudDatabaseBackupSchedule, error) {
req, err := sc.client.NewRequest(ctx, http.MethodGet, databaseServiceName, sc.resourcePath(scheduleID), nil)
if err != nil {
return nil, err
}
resp, err := sc.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var schedules *CloudDatabaseBackupSchedule
if err := json.NewDecoder(resp.Body).Decode(&schedules); err != nil {
return nil, err
}
return schedules, nil
}
// Delete a schedule.
func (sc *cloudDatabaseBackupSchedules) Delete(ctx context.Context, scheduleID string, option *CloudDatabaseBackupScheduleDelete) (*CloudDatabaseMessageResponse, error) {
req, err := sc.client.NewRequest(ctx, http.MethodDelete, databaseServiceName, sc.resourcePath(scheduleID), option)
if err != nil {
return nil, err
}
resp, err := sc.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var dmr *CloudDatabaseMessageResponse
if err := json.NewDecoder(resp.Body).Decode(&dmr); err != nil {
return nil, err
}
return dmr, nil
}