-
Notifications
You must be signed in to change notification settings - Fork 7
/
requests.go
178 lines (153 loc) · 5.19 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
package backups
import (
"reflect"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud/pagination"
)
// ListOpts allows the filtering and sorting of paginated collections through
// the API. Filtering is achieved by passing in struct field values that map to
// the backup attributes you want to see returned.
type ListOpts struct {
Id string
SnapshotId string
Name string `q:"name"`
Status string `q:"status"`
Limit int `q:"limit"`
Offset int `q:"offset"`
VolumeId string `q:"volume_id"`
}
// List returns collection of
// Backup. It accepts a ListOpts struct, which allows you to filter and sort
// the returned collection for greater efficiency.
//
// Default policy settings return only those Backup that are owned by the
// tenant who submits the request, unless an admin user submits the request.
func List(c *golangsdk.ServiceClient, opts ListOpts) ([]Backup, error) {
q, err := golangsdk.BuildQueryString(&opts)
if err != nil {
return nil, err
}
u := listURL(c) + q.String()
pages, err := pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page {
return BackupPage{pagination.LinkedPageBase{PageResult: r}}
}).AllPages()
if err != nil {
return nil, err
}
allBackups, err := ExtractBackups(pages)
if err != nil {
return nil, err
}
return FilterBackups(allBackups, opts)
}
func FilterBackups(backup []Backup, opts ListOpts) ([]Backup, error) {
var refinedBackup []Backup
var matched bool
m := map[string]interface{}{}
if opts.Id != "" {
m["Id"] = opts.Id
}
if opts.SnapshotId != "" {
m["SnapshotId"] = opts.SnapshotId
}
if len(m) > 0 && len(backup) > 0 {
for _, backup := range backup {
matched = true
for key, value := range m {
if sVal := getStructField(&backup, key); !(sVal == value) {
matched = false
}
}
if matched {
refinedBackup = append(refinedBackup, backup)
}
}
} else {
refinedBackup = backup
}
return refinedBackup, nil
}
func getStructField(v *Backup, field string) string {
r := reflect.ValueOf(v)
f := reflect.Indirect(r).FieldByName(field)
return string(f.String())
}
// CreateOptsBuilder allows extensions to add additional parameters to the
// Create request.
type CreateOptsBuilder interface {
ToBackupCreateMap() (map[string]interface{}, error)
}
// CreateOpts contains all the values needed to create a new backup.
type CreateOpts struct {
//ID of the disk to be backed up
VolumeId string `json:"volume_id" required:"true"`
//Snapshot ID of the disk to be backed up
SnapshotId string `json:"snapshot_id,omitempty" `
//Backup name, which cannot start with autobk
Name string `json:"name" required:"true"`
//Backup description
Description string `json:"description,omitempty"`
//List of tags to be configured for the backup resources
Tags []Tag `json:"tags,omitempty"`
}
type Tag struct {
//Tag key
Key string `json:"key" required:"true"`
//Tag value
Value string `json:"value" required:"true"`
}
// ToBackupCreateMap builds a create request body from CreateOpts.
func (opts CreateOpts) ToBackupCreateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "backup")
}
// Create will create a new Backup based on the values in CreateOpts. To extract
// the Backup object from the response, call the ExtractJobResponse method on the
// JobResult.
func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r JobResult) {
b, err := opts.ToBackupCreateMap()
if err != nil {
r.Err = err
return
}
reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
_, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt)
return
}
// RestoreOptsBuilder allows extensions to add additional parameters to the
// Create request.
type RestoreOptsBuilder interface {
ToRestoreCreateMap() (map[string]interface{}, error)
}
// BackupRestoreOpts contains all the values needed to create a new backup.
type BackupRestoreOpts struct {
//ID of the disk to be backed up
VolumeId string `json:"volume_id" required:"true"`
}
// ToRestoreCreateMap builds a create request body from BackupRestoreOpts.
func (opts BackupRestoreOpts) ToRestoreCreateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "restore")
}
// CreateBackupRestore will create a new Restore based on the values in BackupRestoreOpts. To extract
// the BackupRestoreInfo object from the response, call the ExtractBackupRestore method on the
// CreateResult.
func CreateBackupRestore(c *golangsdk.ServiceClient, id string, opts RestoreOptsBuilder) (r CreateResult) {
b, err := opts.ToRestoreCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = c.Post(restoreURL(c, id), b, &r.Body, nil)
return
}
// Get retrieves a particular backup based on its unique ID. To extract
//// the Backup object from the response, call the Extract method on the
//// GetResult.
func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
return
}
// Delete will permanently delete a particular backup based on its unique ID.
func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
_, r.Err = c.Delete(resourceURL(c, id), nil)
return
}