forked from arangodb/go-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_admin_backup_impl.go
305 lines (292 loc) · 8.52 KB
/
client_admin_backup_impl.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
//
// DISCLAIMER
//
// Copyright 2017 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Lars Maier
//
package driver
import (
"context"
"time"
)
type clientBackup struct {
conn Connection
}
func (c *client) Backup() ClientBackup {
return &clientBackup{
conn: c.conn,
}
}
// Create creates a new backup and returns its id
func (c *clientBackup) Create(ctx context.Context, opt *BackupCreateOptions) (BackupID, BackupCreateResponse, error) {
req, err := c.conn.NewRequest("POST", "_admin/backup/create")
if err != nil {
return "", BackupCreateResponse{}, WithStack(err)
}
applyContextSettings(ctx, req)
if opt != nil {
body := struct {
Label string `json:"label,omitempty"`
AllowInconsistent bool `json:"allowInconsistent,omitempty"`
Timeout float64 `json:"timeout,omitempty"`
}{
Label: opt.Label,
AllowInconsistent: opt.AllowInconsistent,
Timeout: opt.Timeout.Seconds(),
}
req, err = req.SetBody(body)
if err != nil {
return "", BackupCreateResponse{}, WithStack(err)
}
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return "", BackupCreateResponse{}, WithStack(err)
}
if err := resp.CheckStatus(201); err != nil {
return "", BackupCreateResponse{}, WithStack(err)
}
var result struct {
ID BackupID `json:"id,omitempty"`
PotentiallyInconsistent bool `json:"potentiallyInconsistent,omitempty"`
NumberOfFiles uint `json:"nrFiles,omitempty"`
NumberOfDBServers uint `json:"nrDBServers,omitempty"`
SizeInBytes uint64 `json:"sizeInBytes,omitempty"`
CreationTime time.Time `json:"datetime,omitempty"`
}
if err := resp.ParseBody("result", &result); err != nil {
return "", BackupCreateResponse{}, WithStack(err)
}
return result.ID, BackupCreateResponse{
PotentiallyInconsistent: result.PotentiallyInconsistent,
NumberOfFiles: result.NumberOfFiles,
NumberOfDBServers: result.NumberOfDBServers,
SizeInBytes: result.SizeInBytes,
CreationTime: result.CreationTime,
}, nil
}
// Delete deletes the backup with given id
func (c *clientBackup) Delete(ctx context.Context, id BackupID) error {
req, err := c.conn.NewRequest("POST", "_admin/backup/delete")
if err != nil {
return WithStack(err)
}
applyContextSettings(ctx, req)
body := struct {
ID BackupID `json:"id,omitempty"`
}{
ID: id,
}
req, err = req.SetBody(body)
if err != nil {
return WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return WithStack(err)
}
return nil
}
// Restore restores the backup with given id
func (c *clientBackup) Restore(ctx context.Context, id BackupID, opt *BackupRestoreOptions) error {
req, err := c.conn.NewRequest("POST", "_admin/backup/restore")
if err != nil {
return WithStack(err)
}
applyContextSettings(ctx, req)
body := struct {
ID BackupID `json:"id,omitempty"`
IgnoreVersion bool `json:"ignoreVersion,omitempty"`
}{
ID: id,
}
if opt != nil {
body.IgnoreVersion = opt.IgnoreVersion
}
req, err = req.SetBody(body)
if err != nil {
return WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
// THIS SHOULD BE 202 ACCEPTED and not OK, because it is not completed when returns (at least for single server)
if err := resp.CheckStatus(200); err != nil {
return WithStack(err)
}
return nil
}
// List returns meta data about some/all backups available
func (c *clientBackup) List(ctx context.Context, opt *BackupListOptions) (map[BackupID]BackupMeta, error) {
req, err := c.conn.NewRequest("POST", "_admin/backup/list")
if err != nil {
return nil, WithStack(err)
}
applyContextSettings(ctx, req)
if opt != nil {
req, err = req.SetBody(opt)
if err != nil {
return nil, WithStack(err)
}
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, WithStack(err)
}
var result struct {
List map[BackupID]BackupMeta `json:"list,omitempty"`
}
if err := resp.ParseBody("result", &result); err != nil {
return nil, WithStack(err)
}
return result.List, nil
}
// Upload triggers an upload to the remote repository of backup with id using the given config
// and returns the job id.
func (c *clientBackup) Upload(ctx context.Context, id BackupID, remoteRepository string, config interface{}) (BackupTransferJobID, error) {
req, err := c.conn.NewRequest("POST", "_admin/backup/upload")
if err != nil {
return "", WithStack(err)
}
applyContextSettings(ctx, req)
body := struct {
ID BackupID `json:"id,omitempty"`
RemoteRepo string `json:"remoteRepository,omitempty"`
Config interface{} `json:"config,omitempty"`
}{
ID: id,
RemoteRepo: remoteRepository,
Config: config,
}
req, err = req.SetBody(body)
if err != nil {
return "", WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return "", WithStack(err)
}
if err := resp.CheckStatus(202); err != nil {
return "", WithStack(err)
}
var result struct {
UploadID BackupTransferJobID `json:"uploadId,omitempty"`
}
if err := resp.ParseBody("result", &result); err != nil {
return "", WithStack(err)
}
return result.UploadID, nil
}
// Download triggers an download to the remote repository of backup with id using the given config
// and returns the job id.
func (c *clientBackup) Download(ctx context.Context, id BackupID, remoteRepository string, config interface{}) (BackupTransferJobID, error) {
req, err := c.conn.NewRequest("POST", "_admin/backup/download")
if err != nil {
return "", WithStack(err)
}
applyContextSettings(ctx, req)
body := struct {
ID BackupID `json:"id,omitempty"`
RemoteRepo string `json:"remoteRepository,omitempty"`
Config interface{} `json:"config,omitempty"`
}{
ID: id,
RemoteRepo: remoteRepository,
Config: config,
}
req, err = req.SetBody(body)
if err != nil {
return "", WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return "", WithStack(err)
}
if err := resp.CheckStatus(202); err != nil {
return "", WithStack(err)
}
var result struct {
DownloadID BackupTransferJobID `json:"downloadId,omitempty"`
}
if err := resp.ParseBody("result", &result); err != nil {
return "", WithStack(err)
}
return result.DownloadID, nil
}
// Progress returns the progress state of the given Transfer job
func (c *clientBackup) Progress(ctx context.Context, job BackupTransferJobID) (result BackupTransferProgressReport, error error) {
req, err := c.conn.NewRequest("POST", "_admin/backup/upload")
if err != nil {
return BackupTransferProgressReport{}, WithStack(err)
}
applyContextSettings(ctx, req)
body := struct {
ID BackupTransferJobID `json:"uploadId,omitempty"`
}{
ID: job,
}
req, err = req.SetBody(body)
if err != nil {
return BackupTransferProgressReport{}, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return BackupTransferProgressReport{}, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return BackupTransferProgressReport{}, WithStack(err)
}
if err := resp.ParseBody("result", &result); err != nil {
return BackupTransferProgressReport{}, WithStack(err)
}
return result, nil
}
// Abort aborts the Transfer job if possible
func (c *clientBackup) Abort(ctx context.Context, job BackupTransferJobID) error {
req, err := c.conn.NewRequest("POST", "_admin/backup/upload")
if err != nil {
return WithStack(err)
}
applyContextSettings(ctx, req)
body := struct {
ID BackupTransferJobID `json:"uploadId,omitempty"`
Abort bool `json:"abort,omitempty"`
}{
ID: job,
Abort: true,
}
req, err = req.SetBody(body)
if err != nil {
return WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(202); err != nil {
return WithStack(err)
}
return nil
}