From c2497c3fde2e6df709bb24eea95c0b58699a855c Mon Sep 17 00:00:00 2001 From: Raymond Fallon Date: Wed, 6 May 2020 13:38:15 -0400 Subject: [PATCH] Defaults for some federation attributes. --- rabbitmq/resource_federation_upstream.go | 11 +++ rabbitmq/resource_federation_upstream_test.go | 69 ++++++++++++++++++- 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/rabbitmq/resource_federation_upstream.go b/rabbitmq/resource_federation_upstream.go index 3b31d6cd..35e75871 100644 --- a/rabbitmq/resource_federation_upstream.go +++ b/rabbitmq/resource_federation_upstream.go @@ -5,6 +5,7 @@ import ( "log" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" rabbithole "github.com/michaelklishin/rabbit-hole/v2" ) @@ -53,21 +54,30 @@ func resourceFederationUpstream() *schema.Resource { "prefetch_count": { Type: schema.TypeInt, Optional: true, + Default: 1000, }, "reconnect_delay": { Type: schema.TypeInt, Optional: true, + Default: 5, }, "ack_mode": { Type: schema.TypeString, Optional: true, + Default: "on-confirm", + ValidateFunc: validation.StringInSlice([]string{ + "on-confirm", + "on-publish", + "no-ack", + }, false), }, "trust_user_id": { Type: schema.TypeBool, Optional: true, + Default: false, }, // applicable to federated exchanges only "exchange": { @@ -78,6 +88,7 @@ func resourceFederationUpstream() *schema.Resource { "max_hops": { Type: schema.TypeInt, Optional: true, + Default: 1, }, "expires": { diff --git a/rabbitmq/resource_federation_upstream_test.go b/rabbitmq/resource_federation_upstream_test.go index aa3a32d0..e5233809 100644 --- a/rabbitmq/resource_federation_upstream_test.go +++ b/rabbitmq/resource_federation_upstream_test.go @@ -2,6 +2,7 @@ package rabbitmq import ( "fmt" + "regexp" "strings" "testing" @@ -41,7 +42,7 @@ func TestAccFederationUpstream_hasComponent(t *testing.T) { CheckDestroy: testAccFederationUpstreamCheckDestroy(&upstream), Steps: []resource.TestStep{ { - Config: testAccFederationUpstream_has_component, + Config: testAccFederationUpstream_basic, Check: resource.ComposeTestCheckFunc( testAccFederationUpstreamCheck("rabbitmq_federation_upstream.foo", &upstream), resource.TestCheckResourceAttr("rabbitmq_federation_upstream.foo", "component", "federation-upstream"), @@ -51,6 +52,44 @@ func TestAccFederationUpstream_hasComponent(t *testing.T) { }) } +func TestAccFederationUpstream_defaults(t *testing.T) { + var upstream rabbithole.FederationUpstream + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccFederationUpstreamCheckDestroy(&upstream), + Steps: []resource.TestStep{ + { + Config: testAccFederationUpstream_basic, + Check: resource.ComposeTestCheckFunc( + testAccFederationUpstreamCheck("rabbitmq_federation_upstream.foo", &upstream), + resource.TestCheckResourceAttr("rabbitmq_federation_upstream.foo", "definition.#", "1"), + resource.TestCheckResourceAttr("rabbitmq_federation_upstream.foo", "definition.0.prefetch_count", "1000"), + resource.TestCheckResourceAttr("rabbitmq_federation_upstream.foo", "definition.0.reconnect_delay", "5"), + resource.TestCheckResourceAttr("rabbitmq_federation_upstream.foo", "definition.0.ack_mode", "on-confirm"), + resource.TestCheckResourceAttr("rabbitmq_federation_upstream.foo", "definition.0.trust_user_id", "false"), + resource.TestCheckResourceAttr("rabbitmq_federation_upstream.foo", "definition.0.max_hops", "1"), + ), + }, + }, + }) +} + +func TestAccFederationUpstream_validation(t *testing.T) { + var upstream rabbithole.FederationUpstream + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccFederationUpstreamCheckDestroy(&upstream), + Steps: []resource.TestStep{ + { + Config: testAccFederationUpstream_validation, + ExpectError: regexp.MustCompile("^config is invalid"), + }, + }, + }) +} + func testAccFederationUpstreamCheck(rn string, upstream *rabbithole.FederationUpstream) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[rn] @@ -174,7 +213,32 @@ resource "rabbitmq_federation_upstream" "foo" { } ` -const testAccFederationUpstream_has_component = ` +const testAccFederationUpstream_basic = ` +resource "rabbitmq_vhost" "test" { + name = "test" +} + +resource "rabbitmq_permissions" "guest" { + user = "guest" + vhost = rabbitmq_vhost.test.name + permissions { + configure = ".*" + write = ".*" + read = ".*" + } +} + +resource "rabbitmq_federation_upstream" "foo" { + name = "foo" + vhost = rabbitmq_permissions.guest.vhost + + definition { + uri = "amqp://server-name" + } +} +` + +const testAccFederationUpstream_validation = ` resource "rabbitmq_vhost" "test" { name = "test" } @@ -195,6 +259,7 @@ resource "rabbitmq_federation_upstream" "foo" { definition { uri = "amqp://server-name" + ack_mode = "not-valid" } } `