-
Notifications
You must be signed in to change notification settings - Fork 14
/
cloud_backup_policy.go
179 lines (166 loc) · 5.75 KB
/
cloud_backup_policy.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
package gobizfly
import (
"context"
"encoding/json"
"net/http"
"strings"
)
// CloudBackupCreatePolicyPayload represents the payload for creating a backup policy
type CloudBackupCreatePolicyPayload struct {
Name string `json:"name"`
StorageType string `json:"storage_type"`
SchedulePattern string `json:"schedule_pattern"`
RetentionDays int `json:"retention_days"`
Description string `json:"description,omitempty"`
}
// CloudBackupPolicy represents a backup policy
type CloudBackupPolicy struct {
ID string `json:"id"`
Name string `json:"name"`
SchedulePattern string `json:"schedule_pattern"`
RetentionDays int `json:"retention_days"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
TenantID string `json:"tenant_id"`
Description string `json:"description"`
LimitDownload int `json:"limit_download,omitempty"`
LimitUpload int `json:"limit_upload,omitempty"`
PolicyType string `json:"policy_type"`
Retentions int `json:"retentions"`
}
// CloudBackupPatchPolicyPayload represents the payload for patching a backup policy
type CloudBackupPatchPolicyPayload struct {
Name string `json:"name,omitempty"`
SchedulePattern string `json:"schedule_pattern,omitempty"`
RetentionDays int `json:"retention_days,omitempty"`
}
// CloudBackupFullPolicy represents a backup policy
type CloudBackupFullPolicy struct {
CloudBackupPolicy
RetentionDays int `json:"retention_days"`
RetentionHours int `json:"retention_hours"`
RetentionWeeks int `json:"retention_weeks"`
RetentionMonths int `json:"retention_months"`
BackupDirectories []string `json:"backup_directories"`
}
// CloudBackupActionPolicyDirectoryPayload represents the payload for creating a backup policy
type CloudBackupActionPolicyDirectoryPayload struct {
Action string `json:"action"`
DirectoryID string `json:"directory_id"`
}
// CreatePolicy creates a backup policy
func (cb *cloudBackupService) CreatePolicy(ctx context.Context, payload *CloudBackupCreatePolicyPayload) (*CloudBackupPolicy, error) {
req, err := cb.client.NewRequest(ctx, http.MethodPost, cloudBackupServiceName,
cb.policyPath(), 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 policy *CloudBackupPolicy
if err := json.NewDecoder(resp.Body).Decode(&policy); err != nil {
return nil, err
}
return policy, nil
}
// GetBackupDirectoryPolicy gets a backup policy
func (cb *cloudBackupService) GetBackupDirectoryPolicy(ctx context.Context, machineID string, directoryID string) (*CloudBackupPolicy, error) {
req, err := cb.client.NewRequest(ctx, http.MethodGet, cloudBackupServiceName,
strings.Join([]string{cb.itemMachinePath(machineID), "directories", directoryID, "policies"}, "/"), 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 policy *CloudBackupPolicy
if err := json.NewDecoder(resp.Body).Decode(&policy); err != nil {
return nil, err
}
return policy, nil
}
// GetPolicy gets a backup policy
func (cb *cloudBackupService) GetPolicy(ctx context.Context, policyID string) (*CloudBackupPolicy, error) {
req, err := cb.client.NewRequest(ctx, http.MethodGet, cloudBackupServiceName,
cb.itemPolicyPath(policyID), 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 policy *CloudBackupPolicy
if err := json.NewDecoder(resp.Body).Decode(&policy); err != nil {
return nil, err
}
return policy, nil
}
// PatchPolicy patches a backup policy
func (cb *cloudBackupService) PatchPolicy(ctx context.Context, policyID string, payload *CloudBackupPatchPolicyPayload) (*CloudBackupPolicy, error) {
req, err := cb.client.NewRequest(ctx, http.MethodPatch, cloudBackupServiceName,
cb.itemPolicyPath(policyID), 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 policy *CloudBackupPolicy
if err := json.NewDecoder(resp.Body).Decode(&policy); err != nil {
return nil, err
}
return policy, nil
}
// DeletePolicy deletes a backup policy
func (cb *cloudBackupService) DeletePolicy(ctx context.Context, policyID string) error {
req, err := cb.client.NewRequest(ctx, http.MethodDelete, cloudBackupServiceName,
cb.itemPolicyPath(policyID), nil)
if err != nil {
return err
}
_, err = cb.client.Do(ctx, req)
if err != nil {
return err
}
return nil
}
// ListAppliedPolicyDirectories lists the directories that have a backup policy applied
func (cb *cloudBackupService) ListAppliedPolicyDirectories(ctx context.Context, policyID string) ([]*CloudBackupDirectory, error) {
req, err := cb.client.NewRequest(ctx, http.MethodGet, cloudBackupServiceName,
strings.Join([]string{cb.itemPolicyPath(policyID), "directories"}, "/"), 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 directories []*CloudBackupDirectory
if err := json.NewDecoder(resp.Body).Decode(&directories); err != nil {
return nil, err
}
return directories, nil
}
// ActionPolicyDirectory applies an action to a backup policy
func (cb *cloudBackupService) ActionPolicyDirectory(ctx context.Context, policyID string, payload *CloudBackupActionPolicyDirectoryPayload) error {
req, err := cb.client.NewRequest(ctx, http.MethodPost, cloudBackupServiceName,
strings.Join([]string{cb.itemPolicyPath(policyID), "action"}, "/"), payload)
if err != nil {
return err
}
resp, err := cb.client.Do(ctx, req)
if err != nil {
return err
}
return resp.Body.Close()
}