This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
requests.go
293 lines (255 loc) · 9.33 KB
/
requests.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
package policies
import (
"reflect"
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/pagination"
)
// CreateOptsBuilder allows extensions to add additional parameters to the
// Create request.
type CreateOptsBuilder interface {
ToPolicyCreateMap() (map[string]interface{}, error)
}
// CreateOpts contains the options for create a Policy. This object is
// passed to Create(). For more information about these parameters,
// please refer to the Policy object, or the volume backup service API v2
// documentation
type CreateOpts struct {
//Backup policy name.It cannot start with default.
Name string `json:"backup_policy_name" required:"true"`
//Details about the scheduling policy
ScheduledPolicy ScheduledPolicy `json:"scheduled_policy" required:"true"`
// Tags to be configured for the backup policy
Tags []Tag `json:"tags,omitempty"`
}
// ScheduledPolicy defines the details about scheduling policy for create
type ScheduledPolicy struct {
//Start time of the backup job.
StartTime string `json:"start_time" required:"true"`
//Backup interval (1 to 14 days)
Frequency int `json:"frequency,omitempty"`
//Specifies on which days of each week backup jobs are ececuted.
WeekFrequency []string `json:"week_frequency,omitempty"`
//Number of retained backups, minimum 2.
RententionNum int `json:"rentention_num,omitempty"`
//Days of retained backups, minimum 2.
RententionDay int `json:"rentention_day,omitempty"`
//Whether to retain the first backup in the current month, possible values Y or N
RemainFirstBackup string `json:"remain_first_backup_of_curMonth" required:"true"`
//Backup policy status, ON or OFF
Status string `json:"status" required:"true"`
}
type Tag struct {
//Tag key. A tag key consists of up to 36 characters
Key string `json:"key" required:"true"`
//Tag value. A tag value consists of 0 to 43 characters
Value string `json:"value" required:"true"`
}
// ToPolicyCreateMap assembles a request body based on the contents of a
// CreateOpts.
func (opts CreateOpts) ToPolicyCreateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// Create will create a new Policy based on the values in CreateOpts. To extract
// the Policy object from the response, call the Extract method on the
// CreateResult.
func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToPolicyCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(commonURL(client), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
return
}
// UpdateOptsBuilder allows extensions to add additional parameters to the
// Update request.
type UpdateOptsBuilder interface {
ToPolicyUpdateMap() (map[string]interface{}, error)
}
// UpdateOpts contains the options for Update a Policy.
type UpdateOpts struct {
//Backup policy name.It cannot start with default.
Name string `json:"backup_policy_name,omitempty"`
//Details about the scheduling policy
ScheduledPolicy UpdateSchedule `json:"scheduled_policy,omitempty"`
}
// UpdateSchedule defiens the details about scheduling policy for update.
type UpdateSchedule struct {
//Start time of the backup job.
StartTime string `json:"start_time,omitempty"`
//Backup interval (1 to 14 days)
Frequency int `json:"frequency,omitempty"`
//Specifies on which days of each week backup jobs are ececuted.
WeekFrequency []string `json:"week_frequency,omitempty"`
//Number of retained backups, minimum 2.
RententionNum int `json:"rentention_num,omitempty"`
//Days of retained backups, minimum 2.
RententionDay int `json:"rentention_day,omitempty"`
//Number of retained backups, minimum 2.
RemainFirstBackup string `json:"remain_first_backup_of_curMonth,omitempty"`
//Backup policy status, ON or OFF
Status string `json:"status,omitempty"`
}
// ToPolicyUpdateMap assembles a request body based on the contents of a
// UpdateOpts.
func (opts UpdateOpts) ToPolicyUpdateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
//Update will Update an existing backup Policy based on the values in UpdateOpts.To extract
// the Policy object from the response, call the Extract method on the
// UpdateResult.
func Update(c *golangsdk.ServiceClient, policyID string, opts UpdateOptsBuilder) (r UpdateResult) {
b, err := opts.ToPolicyUpdateMap()
if err != nil {
r.Err = err
return
}
reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
_, r.Err = c.Put(resourceURL(c, policyID), b, &r.Body, reqOpt)
return
}
//Delete will delete the specified backup policy.
func Delete(c *golangsdk.ServiceClient, policyID string) (r DeleteResult) {
_, r.Err = c.Delete(resourceURL(c, policyID), nil)
return
}
//ListOpts allows filtering policies
type ListOpts struct {
//Backup policy ID
ID string
//Backup policy name
Name string
//Backup policy status
Status string
}
// List returns a Pager which allows you to iterate over a collection of
// Policies. It accepts a ListOpts struct, which allows you to
// filter the returned collection for greater efficiency.
func List(c *golangsdk.ServiceClient, opts ListOpts) ([]Policy, error) {
pages, err := pagination.NewPager(c, commonURL(c), func(r pagination.PageResult) pagination.Page {
return PolicyPage{pagination.LinkedPageBase{PageResult: r}}
}).AllPages()
if err != nil {
return nil, err
}
allPolicies, err := ExtractPolicies(pages)
if err != nil {
return nil, err
}
return FilterPolicies(allPolicies, opts)
}
func FilterPolicies(policies []Policy, opts ListOpts) ([]Policy, error) {
var refinedPolicies []Policy
var matched bool
m := map[string]FilterStruct{}
if opts.ID != "" {
m["ID"] = FilterStruct{Value: opts.ID}
}
if opts.Name != "" {
m["Name"] = FilterStruct{Value: opts.Name}
}
if opts.Status != "" {
m["Status"] = FilterStruct{Value: opts.Status, Driller: []string{"ScheduledPolicy"}}
}
if len(m) > 0 && len(policies) > 0 {
for _, policies := range policies {
matched = true
for key, value := range m {
if sVal := GetStructNestedField(&policies, key, value.Driller); !(sVal == value.Value) {
matched = false
}
}
if matched {
refinedPolicies = append(refinedPolicies, policies)
}
}
} else {
refinedPolicies = policies
}
return refinedPolicies, nil
}
func GetStructNestedField(v *Policy, field string, structDriller []string) string {
r := reflect.ValueOf(v)
for _, drillField := range structDriller {
f := reflect.Indirect(r).FieldByName(drillField).Interface()
r = reflect.ValueOf(f)
}
f1 := reflect.Indirect(r).FieldByName(field)
return string(f1.String())
}
type FilterStruct struct {
Value string
Driller []string
}
// AssociateOptsBuilder allows extensions to add additional parameters to the
// Associate request.
type AssociateOptsBuilder interface {
ToPolicyAssociateMap() (map[string]interface{}, error)
}
// AssociateOpts contains the options to associate a resource to a Policy.
type AssociateOpts struct {
//Backup policy ID, to which the resource is to be associated.
PolicyID string `json:"backup_policy_id" required:"true"`
//Details about the resources to associate with the policy.
Resources []AssociateResource `json:"resources" required:"true"`
}
type AssociateResource struct {
//The ID of the resource to associate with policy.
ResourceID string `json:"resource_id" required:"true"`
//Type of the resource , e.g. volume.
ResourceType string `json:"resource_type" required:"true"`
}
// ToPolicyAssociateMap assembles a request body based on the contents of a
// AssociateOpts.
func (opts AssociateOpts) ToPolicyAssociateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// Associate will associate a resource tp a backup policy based on the values in AssociateOpts. To extract
// the associated resources from the response, call the ExtractResource method on the
// ResourceResult.
func Associate(client *golangsdk.ServiceClient, opts AssociateOpts) (r ResourceResult) {
b, err := opts.ToPolicyAssociateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(associateURL(client), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
return
}
// DisassociateOptsBuilder allows extensions to add additional parameters to the
// Disassociate request.
type DisassociateOptsBuilder interface {
ToPolicyDisassociateMap() (map[string]interface{}, error)
}
// DisassociateOpts contains the options disassociate a resource from a Policy.
type DisassociateOpts struct {
//Disassociate Resources
Resources []DisassociateResource `json:"resources" required:"true"`
}
type DisassociateResource struct {
//ResourceID
ResourceID string `json:"resource_id" required:"true"`
}
// ToPolicyDisassociateMap assembles a request body based on the contents of a
// DisassociateOpts.
func (opts DisassociateOpts) ToPolicyDisassociateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// Disassociate will disassociate a resource from a backup policy based on the values in DisassociateOpts. To extract
// the disassociated resources from the response, call the ExtractResource method on the
// ResourceResult.
func Disassociate(client *golangsdk.ServiceClient, policyID string, opts DisassociateOpts) (r ResourceResult) {
b, err := opts.ToPolicyDisassociateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(disassociateURL(client, policyID), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
return
}