-
Notifications
You must be signed in to change notification settings - Fork 8
/
backups.go
103 lines (86 loc) · 2.91 KB
/
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
package scalingo
import (
"context"
"encoding/json"
"time"
"gopkg.in/errgo.v1"
"github.com/Scalingo/go-scalingo/v7/http"
)
type BackupsService interface {
BackupList(ctx context.Context, app, addonID string) ([]Backup, error)
BackupCreate(ctx context.Context, app, addonID string) (*Backup, error)
BackupShow(ctx context.Context, app, addonID, backupID string) (*Backup, error)
BackupDownloadURL(ctx context.Context, app, addonID, backupID string) (string, error)
}
type BackupStatus string
const (
BackupStatusScheduled BackupStatus = "scheduled"
BackupStatusRunning BackupStatus = "running"
BackupStatusDone BackupStatus = "done"
BackupStatusError BackupStatus = "error"
)
type BackupMethod string
const (
BackupMethodPeriodic BackupMethod = "periodic"
BackupMethodManual BackupMethod = "manual"
)
type Backup struct {
ID string `json:"id"`
CreatedAt time.Time `json:"created_at"`
StartedAt time.Time `json:"started_at"`
Name string `json:"name"`
Size uint64 `json:"size"`
Status BackupStatus `json:"status"`
DatabaseID string `json:"database_id"`
Method BackupMethod `json:"method"`
}
type BackupsRes struct {
Backups []Backup `json:"database_backups"`
}
type BackupRes struct {
Backup Backup `json:"database_backup"`
}
type DownloadURLRes struct {
DownloadURL string `json:"download_url"`
}
func (c *Client) BackupList(ctx context.Context, app string, addonID string) ([]Backup, error) {
var backupRes BackupsRes
err := c.DBAPI(app, addonID).SubresourceList(ctx, "databases", addonID, "backups", nil, &backupRes)
if err != nil {
return nil, errgo.Notef(err, "fail to get backup")
}
return backupRes.Backups, nil
}
func (c *Client) BackupCreate(ctx context.Context, app, addonID string) (*Backup, error) {
var backupRes BackupRes
err := c.DBAPI(app, addonID).SubresourceAdd(ctx, "databases", addonID, "backups", nil, &backupRes)
if err != nil {
return nil, errgo.Notef(err, "fail to schedule a new backup")
}
return &backupRes.Backup, nil
}
func (c *Client) BackupShow(ctx context.Context, app, addonID, backup string) (*Backup, error) {
var backupRes BackupRes
err := c.DBAPI(app, addonID).ResourceGet(ctx, "backups", backup, nil, &backupRes)
if err != nil {
return nil, errgo.Notef(err, "fail to get backup")
}
return &backupRes.Backup, nil
}
func (c *Client) BackupDownloadURL(ctx context.Context, app, addonID, backupID string) (string, error) {
req := &http.APIRequest{
Method: "GET",
Endpoint: "/databases/" + addonID + "/backups/" + backupID + "/archive",
}
resp, err := c.DBAPI(app, addonID).Do(ctx, req)
if err != nil {
return "", errgo.Notef(err, "fail to get backup archive")
}
defer resp.Body.Close()
var downloadRes DownloadURLRes
err = json.NewDecoder(resp.Body).Decode(&downloadRes)
if err != nil {
return "", errgo.Notef(err, "fail to decode backup archive")
}
return downloadRes.DownloadURL, nil
}