-
Notifications
You must be signed in to change notification settings - Fork 7
/
requests.go
171 lines (145 loc) · 5.15 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
package snapshots
import (
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud/pagination"
)
var requestOpts = golangsdk.RequestOpts{
MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
}
// CreateOptsBuilder allows extensions to add additional parameters to the
// Create request.
type CreateOptsBuilder interface {
ToSnapshotCreateMap() (map[string]interface{}, error)
}
// CreateOpts contains options for creating a Snapshot.
// This object is passed to the snapshots.Create function.
type CreateOpts struct {
VolumeID string `json:"volume_id" required:"true"`
Force bool `json:"force,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
}
// ToSnapshotCreateMap assembles a request body based on the contents of a
// CreateOpts.
func (opts CreateOpts) ToSnapshotCreateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "snapshot")
}
// Create will create a new Snapshot based on the values in CreateOpts. To
// extract the Snapshot object from the response, call the Extract method on the
// CreateResult.
func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToSnapshotCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{202},
})
return
}
// UpdateOptsBuilder allows extensions to add additional parameters to
// the Update request.
type UpdateOptsBuilder interface {
ToSnapshotUpdateMap() (map[string]interface{}, error)
}
// UpdateOpts contain options for updating an existing Snapshot.
// This object is passed to the snapshots.Update function.
type UpdateOpts struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
}
// ToSnapshotUpdateMap assembles a request body based on the contents of
// an UpdateOpts.
func (opts UpdateOpts) ToSnapshotUpdateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "snapshot")
}
// Update will update the Snapshot with provided information. To
// extract the updated Snapshot from the response, call the ExtractMetadata
// method on the UpdateResult.
func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
b, err := opts.ToSnapshotUpdateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Put(updateURL(client, id), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
return
}
// Delete will delete the existing Snapshot with the provided ID.
func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) {
_, r.Err = client.Delete(deleteURL(client, id), nil)
return
}
// Get retrieves the Snapshot with the provided ID. To extract the Snapshot
// object from the response, call the Extract method on the GetResult.
func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
_, r.Err = client.Get(getURL(client, id), &r.Body, nil)
return
}
// ListOptsBuilder allows extensions to add additional parameters to the List
// request.
type ListOptsBuilder interface {
ToSnapshotListQuery() (string, error)
}
// ListOpts hold options for listing Snapshots. It is passed to the
// snapshots.List function.
type ListOpts struct {
// Specifies the AZ.
AvailabilityZone string `q:"availability_zone"`
// enterprise projects that are within the permission scope will be queried.
EnterpriseProjectIDs string `q:"enterprise_project_ids"`
// Name will filter by the specified snapshot name.
Name string `q:"name"`
// Status will filter by the specified status.
Status string `q:"status"`
// VolumeID will filter by a specified volume ID.
VolumeID string `q:"volume_id"`
// ID will filter by a specific snapshot ID.
ID string `q:"id"`
Limit int `q:"limit"`
Offset int `q:"offset"`
}
// ToSnapshotListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToSnapshotListQuery() (string, error) {
q, err := golangsdk.BuildQueryString(opts)
return q.String(), err
}
// List returns Snapshots optionally limited by the conditions provided in
// ListOpts.
func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
url := listURL(client)
if opts != nil {
query, err := opts.ToSnapshotListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return SnapshotPage{pagination.SinglePageBase(r)}
})
}
func ListPage(client *golangsdk.ServiceClient, opts ListOptsBuilder) (*PagedList, error) {
url := listURL(client)
if opts != nil {
query, err := opts.ToSnapshotListQuery()
if err != nil {
return nil, err
}
url += query
}
var rst golangsdk.Result
_, err := client.Get(url, &rst.Body, &golangsdk.RequestOpts{
MoreHeaders: requestOpts.MoreHeaders,
})
if err != nil {
return nil, err
}
var r PagedList
err = rst.ExtractInto(&r)
return &r, err
}