github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/messaging/v2/queues/requests.go (about) 1 package queues 2 3 import ( 4 "context" 5 6 "github.com/vnpaycloud-console/gophercloud/v2" 7 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 8 ) 9 10 // ListOptsBuilder allows extensions to add additional parameters to the 11 // List request. 12 type ListOptsBuilder interface { 13 ToQueueListQuery() (string, error) 14 } 15 16 // ListOpts params to be used with List 17 type ListOpts struct { 18 // Limit instructs List to refrain from sending excessively large lists of queues 19 Limit int `q:"limit,omitempty"` 20 21 // Marker and Limit control paging. Marker instructs List where to start listing from. 22 Marker string `q:"marker,omitempty"` 23 24 // Specifies if showing the detailed information when querying queues 25 Detailed bool `q:"detailed,omitempty"` 26 27 // Specifies if filter the queues by queue’s name when querying queues. 28 Name bool `q:"name,omitempty"` 29 30 // Specifies if showing the amount of queues when querying them. 31 WithCount bool `q:"with_count,omitempty"` 32 } 33 34 // ToQueueListQuery formats a ListOpts into a query string. 35 func (opts ListOpts) ToQueueListQuery() (string, error) { 36 q, err := gophercloud.BuildQueryString(opts) 37 return q.String(), err 38 } 39 40 // List instructs OpenStack to provide a list of queues. 41 func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 42 url := listURL(client) 43 if opts != nil { 44 query, err := opts.ToQueueListQuery() 45 if err != nil { 46 return pagination.Pager{Err: err} 47 } 48 url += query 49 } 50 51 pager := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 52 return QueuePage{pagination.LinkedPageBase{PageResult: r}} 53 54 }) 55 return pager 56 } 57 58 // CreateOptsBuilder allows extensions to add additional parameters to the 59 // Create request. 60 type CreateOptsBuilder interface { 61 ToQueueCreateMap() (map[string]any, error) 62 } 63 64 // CreateOpts specifies the queue creation parameters. 65 type CreateOpts struct { 66 // The name of the queue to create. 67 QueueName string `json:"queue_name" required:"true"` 68 69 // The target incoming messages will be moved to when a message can’t 70 // processed successfully after meet the max claim count is met. 71 DeadLetterQueue string `json:"_dead_letter_queue,omitempty"` 72 73 // The new TTL setting for messages when moved to dead letter queue. 74 DeadLetterQueueMessagesTTL int `json:"_dead_letter_queue_messages_ttl,omitempty"` 75 76 // The delay of messages defined for a queue. When the messages send to 77 // the queue, it will be delayed for some times and means it can not be 78 // claimed until the delay expired. 79 DefaultMessageDelay int `json:"_default_message_delay,omitempty"` 80 81 // The default TTL of messages defined for a queue, which will effect for 82 // any messages posted to the queue. 83 DefaultMessageTTL int `json:"_default_message_ttl" required:"true"` 84 85 // The flavor name which can tell Zaqar which storage pool will be used 86 // to create the queue. 87 Flavor string `json:"_flavor,omitempty"` 88 89 // The max number the message can be claimed. 90 MaxClaimCount int `json:"_max_claim_count,omitempty"` 91 92 // The max post size of messages defined for a queue, which will effect 93 // for any messages posted to the queue. 94 MaxMessagesPostSize int `json:"_max_messages_post_size,omitempty"` 95 96 // Does messages should be encrypted 97 EnableEncryptMessages *bool `json:"_enable_encrypt_messages,omitempty"` 98 99 // Extra is free-form extra key/value pairs to describe the queue. 100 Extra map[string]any `json:"-"` 101 } 102 103 // ToQueueCreateMap constructs a request body from CreateOpts. 104 func (opts CreateOpts) ToQueueCreateMap() (map[string]any, error) { 105 b, err := gophercloud.BuildRequestBody(opts, "") 106 if err != nil { 107 return nil, err 108 } 109 110 if opts.Extra != nil { 111 for key, value := range opts.Extra { 112 b[key] = value 113 } 114 115 } 116 return b, nil 117 } 118 119 // Create requests the creation of a new queue. 120 func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 121 b, err := opts.ToQueueCreateMap() 122 if err != nil { 123 r.Err = err 124 return 125 } 126 127 queueName := b["queue_name"].(string) 128 delete(b, "queue_name") 129 130 resp, err := client.Put(ctx, createURL(client, queueName), b, r.Body, &gophercloud.RequestOpts{ 131 OkCodes: []int{201, 204}, 132 }) 133 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 134 return 135 } 136 137 // UpdateOptsBuilder allows extensions to add additional parameters to the 138 // update request. 139 type UpdateOptsBuilder interface { 140 ToQueueUpdateMap() ([]map[string]any, error) 141 } 142 143 // BatchUpdateOpts is an array of UpdateOpts. 144 type BatchUpdateOpts []UpdateOpts 145 146 // UpdateOpts is the struct responsible for updating a property of a queue. 147 type UpdateOpts struct { 148 Op UpdateOp `json:"op" required:"true"` 149 Path string `json:"path" required:"true"` 150 Value any `json:"value" required:"true"` 151 } 152 153 type UpdateOp string 154 155 const ( 156 ReplaceOp UpdateOp = "replace" 157 AddOp UpdateOp = "add" 158 RemoveOp UpdateOp = "remove" 159 ) 160 161 // ToQueueUpdateMap constructs a request body from UpdateOpts. 162 func (opts BatchUpdateOpts) ToQueueUpdateMap() ([]map[string]any, error) { 163 queuesUpdates := make([]map[string]any, len(opts)) 164 for i, queue := range opts { 165 queueMap, err := queue.ToMap() 166 if err != nil { 167 return nil, err 168 } 169 queuesUpdates[i] = queueMap 170 } 171 return queuesUpdates, nil 172 } 173 174 // ToMap constructs a request body from UpdateOpts. 175 func (opts UpdateOpts) ToMap() (map[string]any, error) { 176 return gophercloud.BuildRequestBody(opts, "") 177 } 178 179 // Update Updates the specified queue. 180 func Update(ctx context.Context, client *gophercloud.ServiceClient, queueName string, opts UpdateOptsBuilder) (r UpdateResult) { 181 resp, err := client.Patch(ctx, updateURL(client, queueName), opts, &r.Body, &gophercloud.RequestOpts{ 182 OkCodes: []int{200, 201, 204}, 183 MoreHeaders: map[string]string{ 184 "Content-Type": "application/openstack-messaging-v2.0-json-patch"}, 185 }) 186 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 187 return 188 } 189 190 // Get requests details on a single queue, by name. 191 func Get(ctx context.Context, client *gophercloud.ServiceClient, queueName string) (r GetResult) { 192 resp, err := client.Get(ctx, getURL(client, queueName), &r.Body, &gophercloud.RequestOpts{ 193 OkCodes: []int{200}, 194 }) 195 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 196 return 197 } 198 199 // Delete deletes the specified queue. 200 func Delete(ctx context.Context, client *gophercloud.ServiceClient, queueName string) (r DeleteResult) { 201 resp, err := client.Delete(ctx, deleteURL(client, queueName), &gophercloud.RequestOpts{ 202 OkCodes: []int{204}, 203 }) 204 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 205 return 206 } 207 208 // GetStats returns statistics for the specified queue. 209 func GetStats(ctx context.Context, client *gophercloud.ServiceClient, queueName string) (r StatResult) { 210 resp, err := client.Get(ctx, statURL(client, queueName), &r.Body, &gophercloud.RequestOpts{ 211 OkCodes: []int{200}, 212 }) 213 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 214 return 215 } 216 217 type SharePath string 218 219 const ( 220 PathMessages SharePath = "messages" 221 PathClaims SharePath = "claims" 222 PathSubscriptions SharePath = "subscriptions" 223 ) 224 225 type ShareMethod string 226 227 const ( 228 MethodGet ShareMethod = "GET" 229 MethodPatch ShareMethod = "PATCH" 230 MethodPost ShareMethod = "POST" 231 MethodPut ShareMethod = "PUT" 232 ) 233 234 // ShareOpts specifies share creation parameters. 235 type ShareOpts struct { 236 Paths []SharePath `json:"paths,omitempty"` 237 Methods []ShareMethod `json:"methods,omitempty"` 238 Expires string `json:"expires,omitempty"` 239 } 240 241 // ShareOptsBuilder allows extensions to add additional attributes to the 242 // Share request. 243 type ShareOptsBuilder interface { 244 ToQueueShareMap() (map[string]any, error) 245 } 246 247 // ToShareQueueMap formats a ShareOpts structure into a request body. 248 func (opts ShareOpts) ToQueueShareMap() (map[string]any, error) { 249 b, err := gophercloud.BuildRequestBody(opts, "") 250 if err != nil { 251 return nil, err 252 } 253 return b, nil 254 } 255 256 // Share creates a pre-signed URL for a given queue. 257 func Share(ctx context.Context, client *gophercloud.ServiceClient, queueName string, opts ShareOptsBuilder) (r ShareResult) { 258 b, err := opts.ToQueueShareMap() 259 if err != nil { 260 r.Err = err 261 return r 262 } 263 resp, err := client.Post(ctx, shareURL(client, queueName), b, &r.Body, &gophercloud.RequestOpts{ 264 OkCodes: []int{200}, 265 }) 266 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 267 return 268 } 269 270 type PurgeResource string 271 272 const ( 273 ResourceMessages PurgeResource = "messages" 274 ResourceSubscriptions PurgeResource = "subscriptions" 275 ) 276 277 // PurgeOpts specifies the purge parameters. 278 type PurgeOpts struct { 279 ResourceTypes []PurgeResource `json:"resource_types" required:"true"` 280 } 281 282 // PurgeOptsBuilder allows extensions to add additional attributes to the 283 // Purge request. 284 type PurgeOptsBuilder interface { 285 ToQueuePurgeMap() (map[string]any, error) 286 } 287 288 // ToPurgeQueueMap formats a PurgeOpts structure into a request body 289 func (opts PurgeOpts) ToQueuePurgeMap() (map[string]any, error) { 290 b, err := gophercloud.BuildRequestBody(opts, "") 291 if err != nil { 292 return nil, err 293 } 294 295 return b, nil 296 } 297 298 // Purge purges particular resource of the queue. 299 func Purge(ctx context.Context, client *gophercloud.ServiceClient, queueName string, opts PurgeOptsBuilder) (r PurgeResult) { 300 b, err := opts.ToQueuePurgeMap() 301 if err != nil { 302 r.Err = err 303 return r 304 } 305 306 resp, err := client.Post(ctx, purgeURL(client, queueName), b, nil, &gophercloud.RequestOpts{ 307 OkCodes: []int{204}, 308 }) 309 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 310 return 311 }