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 2 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
35 changes: 19 additions & 16 deletions rabbitmq/resource_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"log"
"strings"

"github.com/michaelklishin/rabbit-hole"

"github.com/hashicorp/terraform/helper/schema"
"github.com/michaelklishin/rabbit-hole"
"net/url"
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: net/url should be inbetween log and strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, for some reason intellij reorged it to the bottom like that, just alpha order is expected?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ordering is usually something like: first standard packages, blank space, external packages. See the other files for an example.

)

func resourceBinding() *schema.Resource {
Expand Down Expand Up @@ -44,12 +44,6 @@ func resourceBinding() *schema.Resource {
ForceNew: true,
},

"properties_key": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
Copy link
Contributor

Choose a reason for hiding this comment

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

Rather than removing properties_key entirely, it should be converted into a read-only computed parameter:

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do, thanks for the tip.


"routing_key": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Expand All @@ -74,14 +68,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 +173,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"
}
}
`
3 changes: 0 additions & 3 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,8 +71,6 @@ 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.
Expand Down