From b739f27c0840541442774f49f34c77076662f36b Mon Sep 17 00:00:00 2001 From: avitsidis Date: Wed, 15 Mar 2023 17:58:44 +0100 Subject: [PATCH] Get only bindings related to source/destination when checking if bindings exists or has been changed (#43) --- rabbitmq/resource_binding.go | 20 +++++++++++++++--- rabbitmq/resource_binding_test.go | 34 +++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/rabbitmq/resource_binding.go b/rabbitmq/resource_binding.go index 105a5f04..76e4f3bb 100644 --- a/rabbitmq/resource_binding.go +++ b/rabbitmq/resource_binding.go @@ -133,9 +133,23 @@ func ReadBinding(d *schema.ResourceData, meta interface{}) error { log.Printf("[DEBUG] RabbitMQ: Attempting to find binding for: vhost=%s source=%s destination=%s destinationType=%s propertiesKey=%s", vhost, source, destination, destinationType, propertiesKey) - bindings, err := rmqc.ListBindingsIn(vhost) - if err != nil { - return err + var bindings []rabbithole.BindingInfo + var err error + if destinationType == "queue" { + bindings, err = rmqc.ListQueueBindingsBetween(vhost, source, destination) + if err != nil { + return err + } + } else if destinationType == "exchange" { + bindings, err = rmqc.ListExchangeBindingsBetween(vhost, source, destination) + if err != nil { + return err + } + } else { + bindings, err = rmqc.ListBindingsIn(vhost) + if err != nil { + return err + } } log.Printf("[DEBUG] RabbitMQ: Bindings retrieved: %#v", bindings) diff --git a/rabbitmq/resource_binding_test.go b/rabbitmq/resource_binding_test.go index b74056d2..e3a60d2f 100644 --- a/rabbitmq/resource_binding_test.go +++ b/rabbitmq/resource_binding_test.go @@ -26,6 +26,12 @@ func TestAccBinding_basic(t *testing.T) { "rabbitmq_binding.test", &bindingInfo, ), }, + { + Config: testAccBindingConfig_basic, + Check: testAccBindingCheck( + "rabbitmq_binding.foo_to_bar", &bindingInfo, + ), + }, }, }) } @@ -174,6 +180,26 @@ resource "rabbitmq_permissions" "guest" { } } +resource "rabbitmq_exchange" "foo" { + name = "foo" + vhost = "${rabbitmq_permissions.guest.vhost}" + settings { + type = "fanout" + durable = false + auto_delete = true + } +} + +resource "rabbitmq_exchange" "bar" { + name = "foo" + vhost = "${rabbitmq_permissions.guest.vhost}" + settings { + type = "fanout" + durable = false + auto_delete = true + } +} + resource "rabbitmq_exchange" "test" { name = "test" vhost = "${rabbitmq_permissions.guest.vhost}" @@ -193,6 +219,14 @@ resource "rabbitmq_queue" "test" { } } +resource "rabbitmq_binding" "foo_to_bar" { + source = "${rabbitmq_exchange.foo.name}" + vhost = "${rabbitmq_vhost.test.name}" + destination = "${rabbitmq_exchange.bar.name}" + destination_type = "exchange" + routing_key = "#" +} + resource "rabbitmq_binding" "test" { source = "${rabbitmq_exchange.test.name}" vhost = "${rabbitmq_vhost.test.name}"