From 646be9397c89a52ff84bccf43fbef7e9d8cb2043 Mon Sep 17 00:00:00 2001 From: Stephen Garlick Date: Thu, 21 Sep 2017 11:12:57 -0400 Subject: [PATCH 1/3] remove bindings propertiesKey variable and instead get it from rabbitmq after creating a binding --- rabbitmq/resource_binding.go | 35 +++++++++++++++------------- rabbitmq/resource_binding_test.go | 2 -- website/docs/r/binding.html.markdown | 3 --- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/rabbitmq/resource_binding.go b/rabbitmq/resource_binding.go index dff6bf00..b389700e 100644 --- a/rabbitmq/resource_binding.go +++ b/rabbitmq/resource_binding.go @@ -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" ) func resourceBinding() *schema.Resource { @@ -44,12 +44,6 @@ func resourceBinding() *schema.Resource { ForceNew: true, }, - "properties_key": &schema.Schema{ - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, - "routing_key": &schema.Schema{ Type: schema.TypeString, Optional: true, @@ -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) @@ -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 } diff --git a/rabbitmq/resource_binding_test.go b/rabbitmq/resource_binding_test.go index ccd9d646..890b2240 100644 --- a/rabbitmq/resource_binding_test.go +++ b/rabbitmq/resource_binding_test.go @@ -134,7 +134,6 @@ resource "rabbitmq_binding" "test" { destination = "${rabbitmq_queue.test.name}" destination_type = "queue" routing_key = "#" - properties_key = "%23" }` const testAccBindingConfig_propertiesKey = ` @@ -177,6 +176,5 @@ resource "rabbitmq_binding" "test" { destination = "${rabbitmq_queue.test.name}" destination_type = "queue" routing_key = "ANYTHING.#" - properties_key = "ANYTHING.%23" } ` diff --git a/website/docs/r/binding.html.markdown b/website/docs/r/binding.html.markdown index da797065..fe58785d 100644 --- a/website/docs/r/binding.html.markdown +++ b/website/docs/r/binding.html.markdown @@ -56,7 +56,6 @@ resource "rabbitmq_binding" "test" { destination = "${rabbitmq_queue.test.name}" destination_type = "queue" routing_key = "#" - properties_key = "%23" } ``` @@ -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. - * `routing_key` - (Optional) A routing key for the binding. * `arguments` - (Optional) Additional key/value arguments for the binding. From cbe78a7010971ce2d47458eac96a1d849bba92a6 Mon Sep 17 00:00:00 2001 From: Stephen Garlick Date: Thu, 21 Sep 2017 12:59:47 -0400 Subject: [PATCH 2/3] add args to propertiesKey config --- rabbitmq/resource_binding_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/rabbitmq/resource_binding_test.go b/rabbitmq/resource_binding_test.go index 890b2240..0c476d7a 100644 --- a/rabbitmq/resource_binding_test.go +++ b/rabbitmq/resource_binding_test.go @@ -176,5 +176,10 @@ resource "rabbitmq_binding" "test" { destination = "${rabbitmq_queue.test.name}" destination_type = "queue" routing_key = "ANYTHING.#" + arguments = { + key1 = "value1" + key2 = "value2" + key3 = "value3" + } } ` From 01cd2dc23d653fde77841046f2cccbdec2b2fbc9 Mon Sep 17 00:00:00 2001 From: Stephen Garlick Date: Fri, 22 Sep 2017 12:32:30 -0400 Subject: [PATCH 3/3] add properties_key back as a computed parameter --- rabbitmq/resource_binding.go | 7 ++++++- website/docs/r/binding.html.markdown | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/rabbitmq/resource_binding.go b/rabbitmq/resource_binding.go index b389700e..4beaa667 100644 --- a/rabbitmq/resource_binding.go +++ b/rabbitmq/resource_binding.go @@ -3,11 +3,11 @@ package rabbitmq import ( "fmt" "log" + "net/url" "strings" "github.com/hashicorp/terraform/helper/schema" "github.com/michaelklishin/rabbit-hole" - "net/url" ) func resourceBinding() *schema.Resource { @@ -44,6 +44,11 @@ func resourceBinding() *schema.Resource { ForceNew: true, }, + "properties_key": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "routing_key": &schema.Schema{ Type: schema.TypeString, Optional: true, diff --git a/website/docs/r/binding.html.markdown b/website/docs/r/binding.html.markdown index fe58785d..fe76b5fc 100644 --- a/website/docs/r/binding.html.markdown +++ b/website/docs/r/binding.html.markdown @@ -77,7 +77,9 @@ The following arguments are supported: ## 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