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

Fix bindings not being saved to state. #8

Merged
merged 3 commits into from
Sep 22, 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
32 changes: 20 additions & 12 deletions rabbitmq/resource_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package rabbitmq
import (
"fmt"
"log"
"net/url"
"strings"

"github.com/michaelklishin/rabbit-hole"

"github.com/hashicorp/terraform/helper/schema"
"github.com/michaelklishin/rabbit-hole"
)

func resourceBinding() *schema.Resource {
Expand Down Expand Up @@ -46,8 +46,7 @@ func resourceBinding() *schema.Resource {

"properties_key": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Computed: true,
},

"routing_key": &schema.Schema{
Expand All @@ -74,14 +73,16 @@ func CreateBinding(d *schema.ResourceData, meta interface{}) error {
Destination: d.Get("destination").(string),
DestinationType: d.Get("destination_type").(string),
RoutingKey: d.Get("routing_key").(string),
PropertiesKey: d.Get("properties_key").(string),
Arguments: d.Get("arguments").(map[string]interface{}),
}

if err := declareBinding(rmqc, vhost, bindingInfo); err != nil {
propertiesKey, err := declareBinding(rmqc, vhost, bindingInfo)
if err != nil {
return err
}

log.Printf("[DEBUG] RabbitMQ: Binding properties key: %s", propertiesKey)
bindingInfo.PropertiesKey = propertiesKey
name := fmt.Sprintf("%s/%s/%s/%s/%s", vhost, bindingInfo.Source, bindingInfo.Destination, bindingInfo.DestinationType, bindingInfo.PropertiesKey)
d.SetId(name)

Expand Down Expand Up @@ -177,19 +178,26 @@ func DeleteBinding(d *schema.ResourceData, meta interface{}) error {
return nil
}

func declareBinding(rmqc *rabbithole.Client, vhost string, bindingInfo rabbithole.BindingInfo) error {
log.Printf("[DEBUG] RabbitMQ: Attempting to declare binding for %s/%s/%s/%s/%s",
vhost, bindingInfo.Source, bindingInfo.Destination, bindingInfo.DestinationType, bindingInfo.PropertiesKey)
func declareBinding(rmqc *rabbithole.Client, vhost string, bindingInfo rabbithole.BindingInfo) (string, error) {
log.Printf("[DEBUG] RabbitMQ: Attempting to declare binding for %s/%s/%s/%s",
vhost, bindingInfo.Source, bindingInfo.Destination, bindingInfo.DestinationType)

resp, err := rmqc.DeclareBinding(vhost, bindingInfo)
log.Printf("[DEBUG] RabbitMQ: Binding declare response: %#v", resp)
if err != nil {
return err
return "", err
}

if resp.StatusCode >= 400 {
return fmt.Errorf("Error declaring RabbitMQ binding: %s", resp.Status)
return "", fmt.Errorf("Error declaring RabbitMQ binding: %s", resp.Status)
}

return nil
location := strings.Split(resp.Header.Get("Location"), "/")
propertiesKey, err := url.PathUnescape(location[len(location)-1])

if err != nil {
return "", err
}

return propertiesKey, nil
}
7 changes: 5 additions & 2 deletions rabbitmq/resource_binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ resource "rabbitmq_binding" "test" {
destination = "${rabbitmq_queue.test.name}"
destination_type = "queue"
routing_key = "#"
properties_key = "%23"
}`

const testAccBindingConfig_propertiesKey = `
Expand Down Expand Up @@ -177,6 +176,10 @@ resource "rabbitmq_binding" "test" {
destination = "${rabbitmq_queue.test.name}"
destination_type = "queue"
routing_key = "ANYTHING.#"
properties_key = "ANYTHING.%23"
arguments = {
key1 = "value1"
key2 = "value2"
key3 = "value3"
}
}
`
7 changes: 3 additions & 4 deletions website/docs/r/binding.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ resource "rabbitmq_binding" "test" {
destination = "${rabbitmq_queue.test.name}"
destination_type = "queue"
routing_key = "#"
properties_key = "%23"
}
```

Expand All @@ -72,15 +71,15 @@ The following arguments are supported:

* `destination_type` - (Required) The type of destination (queue or exchange).

* `properties_key` - (Required) A unique key to refer to the binding.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of being removed, this should be moved to the Attribute Reference section and listed as an exported attribute.


* `routing_key` - (Optional) A routing key for the binding.

* `arguments` - (Optional) Additional key/value arguments for the binding.

## Attributes Reference

No further attributes are exported.
In addition to all arguments above, the following attributes are exported:

* `properties_key` - A unique key to refer to the binding.

## Import

Expand Down