-
Notifications
You must be signed in to change notification settings - Fork 14
/
cloud_server_scheduled_volume_backup.go
159 lines (141 loc) · 4.5 KB
/
cloud_server_scheduled_volume_backup.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
package gobizfly
import (
"context"
"encoding/json"
"net/http"
)
const (
backupPath = "/backup"
)
var _ ScheduledVolumeBackup = (*cloudServerScheduledVolumeBackupResource)(nil)
type cloudServerScheduledVolumeBackupResource struct {
client *Client
}
func (cs *cloudServerService) ScheduledVolumeBackups() *cloudServerScheduledVolumeBackupResource {
return &cloudServerScheduledVolumeBackupResource{client: cs.client}
}
type ScheduledVolumeBackup interface {
Create(ctx context.Context, payload *CreateBackupPayload) (*ExtendedBackup, error)
Get(ctx context.Context, backupID string) (*ExtendedBackup, error)
List(ctx context.Context) ([]*Backup, error)
Delete(ctx context.Context, backupID string) error
Update(ctx context.Context, backupID string, payload *UpdateBackupPayload) (*ExtendedBackup, error)
}
type BackupOption struct {
Frequency string `json:"frequency"`
Size string `json:"size"`
}
type Backup struct {
ID string `json:"_id"`
CreatedAt string `json:"created_at"`
NextRunAt string `json:"next_run_at"`
BillingPlan string `json:"billing_plan"`
Options BackupOption `json:"options"`
ResourceID string `json:"resource_id"`
ResourceType string `json:"resource_type"`
ScheduledHour int `json:"scheduled_hour"`
TenantID string `json:"tenant_id"`
Type string `json:"type"`
UpdatedAt string `json:"updated_at"`
}
type ExtendedBackup struct {
Backup
Snapshots []Snapshot `json:"snapshots"`
VolumeID string `json:"volume_id"`
Volume Volume `json:"volume"`
}
type CreateBackupPayload struct {
ResourceID string `json:"resource_id"`
Frequency string `json:"frequency"`
Size string `json:"size"`
Hour int `json:"hour,omitempty"`
}
type UpdateBackupPayload struct {
Frequency string `json:"frequency,omitempty"`
Size string `json:"size,omitempty"`
Hour int `json:"hour,omitempty"`
}
func (b cloudServerScheduledVolumeBackupResource) resourcePath() string {
return backupPath
}
func (b cloudServerScheduledVolumeBackupResource) itemPath(id string) string {
return b.resourcePath() + "/" + id
}
func (b cloudServerScheduledVolumeBackupResource) Create(ctx context.Context, payload *CreateBackupPayload) (*ExtendedBackup, error) {
req, err := b.client.NewRequest(ctx, http.MethodPost, serverServiceName, b.resourcePath(), payload)
if err != nil {
return nil, err
}
var dataResponse struct {
Data *ExtendedBackup `json:"data"`
}
resp, err := b.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&dataResponse); err != nil {
return nil, err
}
return dataResponse.Data, nil
}
func (b cloudServerScheduledVolumeBackupResource) Get(ctx context.Context, backupID string) (*ExtendedBackup, error) {
req, err := b.client.NewRequest(ctx, http.MethodGet, serverServiceName, b.itemPath(backupID), nil)
if err != nil {
return nil, err
}
var backup *ExtendedBackup
resp, err := b.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&backup); err != nil {
return nil, err
}
return backup, nil
}
func (b cloudServerScheduledVolumeBackupResource) List(ctx context.Context) ([]*Backup, error) {
req, err := b.client.NewRequest(ctx, http.MethodGet, serverServiceName, b.resourcePath(), nil)
if err != nil {
return nil, err
}
var backups []*Backup
resp, err := b.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&backups); err != nil {
return nil, err
}
return backups, nil
}
func (b cloudServerScheduledVolumeBackupResource) Delete(ctx context.Context, backupID string) error {
req, err := b.client.NewRequest(ctx, http.MethodDelete, serverServiceName, b.itemPath(backupID), nil)
if err != nil {
return err
}
resp, err := b.client.Do(ctx, req)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
func (b cloudServerScheduledVolumeBackupResource) Update(ctx context.Context, backupID string, payload *UpdateBackupPayload) (*ExtendedBackup, error) {
req, err := b.client.NewRequest(ctx, http.MethodPut, serverServiceName, b.itemPath(backupID), payload)
if err != nil {
return nil, err
}
var backup *ExtendedBackup
resp, err := b.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&backup); err != nil {
return nil, err
}
return backup, nil
}