/
requests.go
292 lines (240 loc) · 8.37 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
package queues
import (
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/pagination"
)
// ListOptsBuilder allows extensions to add additional parameters to the
// List request.
type ListOptsBuilder interface {
ToQueueListQuery() (string, error)
}
// ListOpts params to be used with List
type ListOpts struct {
// Limit instructs List to refrain from sending excessively large lists of queues
Limit int `q:"limit,omitempty"`
// Marker and Limit control paging. Marker instructs List where to start listing from.
Marker string `q:"marker,omitempty"`
// Specifies if showing the detailed information when querying queues
Detailed bool `q:"detailed,omitempty"`
}
// ToQueueListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToQueueListQuery() (string, error) {
q, err := gophercloud.BuildQueryString(opts)
return q.String(), err
}
// List instructs OpenStack to provide a list of queues.
func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
url := listURL(client)
if opts != nil {
query, err := opts.ToQueueListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
pager := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return QueuePage{pagination.LinkedPageBase{PageResult: r}}
})
return pager
}
// CreateOptsBuilder allows extensions to add additional parameters to the
// Create request.
type CreateOptsBuilder interface {
ToQueueCreateMap() (map[string]interface{}, error)
}
// CreateOpts specifies the queue creation parameters.
type CreateOpts struct {
// The name of the queue to create.
QueueName string `json:"queue_name" required:"true"`
// The target incoming messages will be moved to when a message can’t
// processed successfully after meet the max claim count is met.
DeadLetterQueue string `json:"_dead_letter_queue,omitempty"`
// The new TTL setting for messages when moved to dead letter queue.
DeadLetterQueueMessagesTTL int `json:"_dead_letter_queue_messages_ttl,omitempty"`
// The delay of messages defined for a queue. When the messages send to
// the queue, it will be delayed for some times and means it can not be
// claimed until the delay expired.
DefaultMessageDelay int `json:"_default_message_delay,omitempty"`
// The default TTL of messages defined for a queue, which will effect for
// any messages posted to the queue.
DefaultMessageTTL int `json:"_default_message_ttl" required:"true"`
// The flavor name which can tell Zaqar which storage pool will be used
// to create the queue.
Flavor string `json:"_flavor,omitempty"`
// The max number the message can be claimed.
MaxClaimCount int `json:"_max_claim_count,omitempty"`
// The max post size of messages defined for a queue, which will effect
// for any messages posted to the queue.
MaxMessagesPostSize int `json:"_max_messages_post_size,omitempty"`
// Extra is free-form extra key/value pairs to describe the queue.
Extra map[string]interface{} `json:"-"`
}
// ToQueueCreateMap constructs a request body from CreateOpts.
func (opts CreateOpts) ToQueueCreateMap() (map[string]interface{}, error) {
b, err := gophercloud.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
if opts.Extra != nil {
for key, value := range opts.Extra {
b[key] = value
}
}
return b, nil
}
// Create requests the creation of a new queue.
func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToQueueCreateMap()
if err != nil {
r.Err = err
return
}
queueName := b["queue_name"].(string)
delete(b, "queue_name")
_, r.Err = client.Put(createURL(client, queueName), b, r.Body, &gophercloud.RequestOpts{
OkCodes: []int{201, 204},
})
return
}
// UpdateOptsBuilder allows extensions to add additional parameters to the
// update request.
type UpdateOptsBuilder interface {
ToQueueUpdateMap() ([]map[string]interface{}, error)
}
// BatchUpdateOpts is an array of UpdateOpts.
type BatchUpdateOpts []UpdateOpts
// UpdateOpts is the struct responsible for updating a property of a queue.
type UpdateOpts struct {
Op UpdateOp `json:"op" required:"true"`
Path string `json:"path" required:"true"`
Value interface{} `json:"value" required:"true"`
}
type UpdateOp string
const (
ReplaceOp UpdateOp = "replace"
AddOp UpdateOp = "add"
RemoveOp UpdateOp = "remove"
)
// ToQueueUpdateMap constructs a request body from UpdateOpts.
func (opts BatchUpdateOpts) ToQueueUpdateMap() ([]map[string]interface{}, error) {
queuesUpdates := make([]map[string]interface{}, len(opts))
for i, queue := range opts {
queueMap, err := queue.ToMap()
if err != nil {
return nil, err
}
queuesUpdates[i] = queueMap
}
return queuesUpdates, nil
}
// ToMap constructs a request body from UpdateOpts.
func (opts UpdateOpts) ToMap() (map[string]interface{}, error) {
return gophercloud.BuildRequestBody(opts, "")
}
// Update Updates the specified queue.
func Update(client *gophercloud.ServiceClient, queueName string, opts UpdateOptsBuilder) (r UpdateResult) {
_, r.Err = client.Patch(updateURL(client, queueName), opts, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200, 201, 204},
MoreHeaders: map[string]string{
"Content-Type": "application/openstack-messaging-v2.0-json-patch"},
})
return
}
// Get requests details on a single queue, by name.
func Get(client *gophercloud.ServiceClient, queueName string) (r GetResult) {
_, r.Err = client.Get(getURL(client, queueName), &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200},
})
return
}
// Delete deletes the specified queue.
func Delete(client *gophercloud.ServiceClient, queueName string) (r DeleteResult) {
_, r.Err = client.Delete(deleteURL(client, queueName), &gophercloud.RequestOpts{
OkCodes: []int{204},
})
return
}
// GetStats returns statistics for the specified queue.
func GetStats(client *gophercloud.ServiceClient, queueName string) (r StatResult) {
_, r.Err = client.Get(statURL(client, queueName), &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200}})
return
}
type SharePath string
const (
PathMessages SharePath = "messages"
PathClaims SharePath = "claims"
PathSubscriptions SharePath = "subscriptions"
)
type ShareMethod string
const (
MethodGet ShareMethod = "GET"
MethodPatch ShareMethod = "PATCH"
MethodPost ShareMethod = "POST"
MethodPut ShareMethod = "PUT"
)
// ShareOpts specifies share creation parameters.
type ShareOpts struct {
Paths []SharePath `json:"paths,omitempty"`
Methods []ShareMethod `json:"methods,omitempty"`
Expires string `json:"expires,omitempty"`
}
// ShareOptsBuilder allows extensions to add additional attributes to the
// Share request.
type ShareOptsBuilder interface {
ToQueueShareMap() (map[string]interface{}, error)
}
// ToShareQueueMap formats a ShareOpts structure into a request body.
func (opts ShareOpts) ToQueueShareMap() (map[string]interface{}, error) {
b, err := gophercloud.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
return b, nil
}
// Share creates a pre-signed URL for a given queue.
func Share(client *gophercloud.ServiceClient, queueName string, opts ShareOptsBuilder) (r ShareResult) {
b, err := opts.ToQueueShareMap()
if err != nil {
r.Err = err
return r
}
_, r.Err = client.Post(shareURL(client, queueName), b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200},
})
return
}
type PurgeResource string
const (
ResourceMessages PurgeResource = "messages"
ResourceSubscriptions PurgeResource = "subscriptions"
)
// PurgeOpts specifies the purge parameters.
type PurgeOpts struct {
ResourceTypes []PurgeResource `json:"resource_types" required:"true"`
}
// PurgeOptsBuilder allows extensions to add additional attributes to the
// Purge request.
type PurgeOptsBuilder interface {
ToQueuePurgeMap() (map[string]interface{}, error)
}
// ToPurgeQueueMap formats a PurgeOpts structure into a request body
func (opts PurgeOpts) ToQueuePurgeMap() (map[string]interface{}, error) {
b, err := gophercloud.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
return b, nil
}
// Purge purges particular resource of the queue.
func Purge(client *gophercloud.ServiceClient, queueName string, opts PurgeOptsBuilder) (r PurgeResult) {
b, err := opts.ToQueuePurgeMap()
if err != nil {
r.Err = err
return r
}
_, r.Err = client.Post(purgeURL(client, queueName), b, nil, &gophercloud.RequestOpts{
OkCodes: []int{204},
})
return
}