Skip to content
This repository has been archived by the owner on Nov 14, 2020. It is now read-only.

Queue: Add json_arguments parameter #6

Merged
merged 2 commits into from
Sep 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions rabbitmq/resource_queue.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rabbitmq

import (
"encoding/json"
"fmt"
"log"
"strings"
Expand Down Expand Up @@ -53,8 +54,16 @@ func resourceQueue() *schema.Resource {
},

"arguments": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
Type: schema.TypeMap,
Optional: true,
ConflictsWith: []string{"settings.0.arguments_json"},
},

"arguments_json": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateJsonString,
ConflictsWith: []string{"settings.0.arguments"},
},
},
},
Expand All @@ -75,6 +84,19 @@ func CreateQueue(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Unable to parse settings")
}

// If arguments_json is used, unmarshal it into a generic interface
// and use it as the "arguments" key for the queue.
if v, ok := settingsMap["arguments_json"].(string); ok && v != "" {
var arguments interface{}
err := json.Unmarshal([]byte(v), &arguments)
if err != nil {
return err
}

delete(settingsMap, "arguments_json")
settingsMap["arguments"] = arguments
}

if err := declareQueue(rmqc, vhost, name, settingsMap); err != nil {
return err
}
Expand Down
54 changes: 53 additions & 1 deletion rabbitmq/resource_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/hashicorp/terraform/terraform"
)

func TestAccQueue(t *testing.T) {
func TestAccQueue_basic(t *testing.T) {
var queueInfo rabbithole.QueueInfo
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -28,6 +28,23 @@ func TestAccQueue(t *testing.T) {
})
}

func TestAccQueue_jsonArguments(t *testing.T) {
var queueInfo rabbithole.QueueInfo
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccQueueCheckDestroy(&queueInfo),
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccQueueConfig_jsonArguments,
Check: testAccQueueCheck(
"rabbitmq_queue.test", &queueInfo,
),
},
},
})
}

func testAccQueueCheck(rn string, queueInfo *rabbithole.QueueInfo) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[rn]
Expand Down Expand Up @@ -100,3 +117,38 @@ resource "rabbitmq_queue" "test" {
auto_delete = true
}
}`

const testAccQueueConfig_jsonArguments = `
variable "arguments" {
default = <<EOF
{
"x-message-ttl": 5000,
"foo": "bar",
"baz": 50
}
EOF
}

resource "rabbitmq_vhost" "test" {
name = "test"
}

resource "rabbitmq_permissions" "guest" {
user = "guest"
vhost = "${rabbitmq_vhost.test.name}"
permissions {
configure = ".*"
write = ".*"
read = ".*"
}
}

resource "rabbitmq_queue" "test" {
name = "test"
vhost = "${rabbitmq_permissions.guest.vhost}"
settings {
durable = false
auto_delete = true
arguments_json = "${var.arguments}"
}
}`
33 changes: 33 additions & 0 deletions rabbitmq/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package rabbitmq

import (
"encoding/json"
"fmt"

"github.com/hashicorp/terraform/helper/schema"
)

Expand All @@ -12,3 +15,33 @@ func checkDeleted(d *schema.ResourceData, err error) error {

return err
}

// pulled from terraform-provider-aws/aws/validators.go
func validateJsonString(v interface{}, k string) (ws []string, errors []error) {
if _, err := normalizeJsonString(v); err != nil {
errors = append(errors, fmt.Errorf("%q contains invalid JSON: %s", k, err))
}
return
}

// pulled from terraform-provider-aws/aws/structure.go
func normalizeJsonString(jsonString interface{}) (string, error) {
var j interface{}

if jsonString == nil || jsonString.(string) == "" {
return "", nil
}

s := jsonString.(string)

err := json.Unmarshal([]byte(s), &j)
if err != nil {
return s, err
}

// The error is intentionally ignored here to allow empty policies to passthrough validation.
// This covers any interpolated values
bytes, _ := json.Marshal(j)

return string(bytes[:]), nil
}
46 changes: 46 additions & 0 deletions website/docs/r/queue.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ The ``rabbitmq_queue`` resource creates and manages a queue.

## Example Usage

### Basic Example

```hcl
resource "rabbitmq_vhost" "test" {
name = "test"
Expand Down Expand Up @@ -39,6 +41,44 @@ resource "rabbitmq_queue" "test" {
}
```

### Example With JSON Arguments

```hcl
variable "arguments" {
default = <<EOF
{
"x-message-ttl": 5000
}
EOF
}

resource "rabbitmq_vhost" "test" {
name = "test"
}

resource "rabbitmq_permissions" "guest" {
user = "guest"
vhost = "${rabbitmq_vhost.test.name}"

permissions {
configure = ".*"
write = ".*"
read = ".*"
}
}

resource "rabbitmq_queue" "test" {
name = "test"
vhost = "${rabbitmq_permissions.guest.vhost}"

settings {
durable = false
auto_delete = true
arguments_json = "${var.arguments}"
}
}
```

## Argument Reference

The following arguments are supported:
Expand All @@ -59,6 +99,12 @@ The `settings` block supports:
consumers have unsubscribed.

* `arguments` - (Optional) Additional key/value settings for the queue.
All values will be sent to RabbitMQ as a string. If you require non-string
values, use `arguments_json`.

* `arguments_json` - (Optional) A nested JSON string which contains additional
settings for the queue. This is useful for when the arguments contain
non-string values.

## Attributes Reference

Expand Down