Skip to content

Commit 8873eb1

Browse files
problemvjtopjian
authored andcommitted
Messaging Queue Create (gophercloud#846)
* Add NewMessagingV2 client * Add create function for queues * Add unit tests for create queues * Remove queueName from function signature & fix createOpts metadata * Fix unit tests to use Extra and proper function signature * Fix documentation * Update urls to use contants * Fix Extra comments * Fix formatting * Fix doc formatting * Remove Client-ID from create function * Add clientID parameter to NewMessagingV2
1 parent 7664a7c commit 8873eb1

8 files changed

Lines changed: 200 additions & 0 deletions

File tree

openstack/client.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,3 +400,11 @@ func NewLoadBalancerV2(client *gophercloud.ProviderClient, eo gophercloud.Endpoi
400400
func NewClusteringV1(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
401401
return initClientOpts(client, eo, "clustering")
402402
}
403+
404+
// NewMessagingV2 creates a ServiceClient that may be used with the v2 messaging
405+
// service.
406+
func NewMessagingV2(client *gophercloud.ProviderClient, clientID string, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error) {
407+
sc, err := initClientOpts(client, eo, "messaging")
408+
sc.MoreHeaders = map[string]string{"Client-ID": clientID}
409+
return sc, err
410+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Package queues provides information and interaction with the queues through
3+
the OpenStack Messaging (Zaqar) service.
4+
5+
Lists all queues and creates, shows information for updates, deletes, and actions on a queue.
6+
7+
Example to Create a Queue
8+
9+
createOpts := queues.CreateOpts{
10+
QueueName: "My_Queue",
11+
MaxMessagesPostSize: 262143,
12+
DefaultMessageTTL: 3700,
13+
DefaultMessageDelay: 25,
14+
DeadLetterQueueMessageTTL: 3500,
15+
MaxClaimCount: 10,
16+
Extra: map[string]interface{}{"description": "Test queue."},
17+
}
18+
19+
err := queues.Create(client, createOpts).ExtractErr()
20+
if err != nil {
21+
panic(err)
22+
}
23+
*/
24+
package queues
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package queues
2+
3+
import (
4+
"github.com/gophercloud/gophercloud"
5+
)
6+
7+
// CreateOptsBuilder allows extensions to add additional parameters to the
8+
// Create request.
9+
type CreateOptsBuilder interface {
10+
ToQueueCreateMap() (map[string]interface{}, error)
11+
}
12+
13+
// CreateOpts specifies the queue creation parameters.
14+
type CreateOpts struct {
15+
// The name of the queue to create.
16+
QueueName string `json:"queue_name" required:"true"`
17+
18+
// The target incoming messages will be moved to when a message can’t
19+
// processed successfully after meet the max claim count is met.
20+
DeadLetterQueue string `json:"_dead_letter_queue,omitempty"`
21+
22+
// The new TTL setting for messages when moved to dead letter queue.
23+
DeadLetterQueueMessagesTTL int `json:"_dead_letter_queue_messages_ttl,omitempty"`
24+
25+
// The delay of messages defined for a queue. When the messages send to
26+
// the queue, it will be delayed for some times and means it can not be
27+
// claimed until the delay expired.
28+
DefaultMessageDelay int `json:"_default_message_delay,omitempty"`
29+
30+
// The default TTL of messages defined for a queue, which will effect for
31+
// any messages posted to the queue.
32+
DefaultMessageTTL int `json:"_default_message_ttl" required:"true"`
33+
34+
// The flavor name which can tell Zaqar which storage pool will be used
35+
// to create the queue.
36+
Flavor string `json:"_flavor,omitempty"`
37+
38+
// The max number the message can be claimed.
39+
MaxClaimCount int `json:"_max_claim_count,omitempty"`
40+
41+
// The max post size of messages defined for a queue, which will effect
42+
// for any messages posted to the queue.
43+
MaxMessagesPostSize int `json:"_max_messages_post_size,omitempty"`
44+
45+
// Extra is free-form extra key/value pairs to describe the queue.
46+
Extra map[string]interface{} `json:"-"`
47+
}
48+
49+
// ToQueueCreateMap constructs a request body from CreateOpts.
50+
func (opts CreateOpts) ToQueueCreateMap() (map[string]interface{}, error) {
51+
b, err := gophercloud.BuildRequestBody(opts, "")
52+
if err != nil {
53+
return nil, err
54+
}
55+
56+
if opts.Extra != nil {
57+
for key, value := range opts.Extra {
58+
b[key] = value
59+
}
60+
61+
}
62+
return b, nil
63+
}
64+
65+
// Create requests the creation of a new queue.
66+
func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
67+
b, err := opts.ToQueueCreateMap()
68+
if err != nil {
69+
r.Err = err
70+
return
71+
}
72+
73+
queueName := b["queue_name"].(string)
74+
delete(b, "queue_name")
75+
76+
_, r.Err = client.Put(createURL(client, queueName), b, r.Body, &gophercloud.RequestOpts{
77+
OkCodes: []int{201, 204},
78+
})
79+
return
80+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package queues
2+
3+
import (
4+
"github.com/gophercloud/gophercloud"
5+
)
6+
7+
// CreateResult is the response of a Create operation.
8+
type CreateResult struct {
9+
gophercloud.ErrResult
10+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// queues unit tests
2+
package testing
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package testing
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"testing"
7+
8+
th "github.com/gophercloud/gophercloud/testhelper"
9+
fake "github.com/gophercloud/gophercloud/testhelper/client"
10+
)
11+
12+
// QueueName is the name of the queue
13+
var QueueName = "FakeTestQueue"
14+
15+
// CreateQueueRequest is a sample request to create a queue.
16+
const CreateQueueRequest = `
17+
{
18+
"_max_messages_post_size": 262144,
19+
"_default_message_ttl": 3600,
20+
"_default_message_delay": 30,
21+
"_dead_letter_queue": "dead_letter",
22+
"_dead_letter_queue_messages_ttl": 3600,
23+
"_max_claim_count": 10,
24+
"description": "Queue for unit testing."
25+
}`
26+
27+
// HandleCreateSuccessfully configures the test server to respond to a Create request.
28+
func HandleCreateSuccessfully(t *testing.T) {
29+
th.Mux.HandleFunc(fmt.Sprintf("/v2/queues/%s", QueueName),
30+
func(w http.ResponseWriter, r *http.Request) {
31+
th.TestMethod(t, r, "PUT")
32+
th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
33+
th.TestJSONRequest(t, r, CreateQueueRequest)
34+
35+
w.WriteHeader(http.StatusNoContent)
36+
})
37+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package testing
2+
3+
import (
4+
"testing"
5+
6+
"github.com/gophercloud/gophercloud/openstack/messaging/v2/queues"
7+
th "github.com/gophercloud/gophercloud/testhelper"
8+
fake "github.com/gophercloud/gophercloud/testhelper/client"
9+
)
10+
11+
func TestCreate(t *testing.T) {
12+
th.SetupHTTP()
13+
defer th.TeardownHTTP()
14+
HandleCreateSuccessfully(t)
15+
16+
createOpts := queues.CreateOpts{
17+
QueueName: QueueName,
18+
MaxMessagesPostSize: 262144,
19+
DefaultMessageTTL: 3600,
20+
DefaultMessageDelay: 30,
21+
DeadLetterQueue: "dead_letter",
22+
DeadLetterQueueMessagesTTL: 3600,
23+
MaxClaimCount: 10,
24+
Extra: map[string]interface{}{"description": "Queue for unit testing."},
25+
}
26+
27+
err := queues.Create(fake.ServiceClient(), createOpts).ExtractErr()
28+
th.AssertNoErr(t, err)
29+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package queues
2+
3+
import "github.com/gophercloud/gophercloud"
4+
5+
const ApiVersion = "v2"
6+
const ApiName = "queues"
7+
8+
func createURL(client *gophercloud.ServiceClient, queueName string) string {
9+
return client.ServiceURL(ApiVersion, ApiName, queueName)
10+
}

0 commit comments

Comments
 (0)