Skip to content

Commit

Permalink
Defaults for some federation attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
niclic committed Jun 17, 2020
1 parent af2af1f commit c2497c3
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
11 changes: 11 additions & 0 deletions rabbitmq/resource_federation_upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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": {
Expand All @@ -78,6 +88,7 @@ func resourceFederationUpstream() *schema.Resource {
"max_hops": {
Type: schema.TypeInt,
Optional: true,
Default: 1,
},

"expires": {
Expand Down
69 changes: 67 additions & 2 deletions rabbitmq/resource_federation_upstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rabbitmq

import (
"fmt"
"regexp"
"strings"
"testing"

Expand Down Expand Up @@ -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"),
Expand All @@ -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]
Expand Down Expand Up @@ -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"
}
Expand All @@ -195,6 +259,7 @@ resource "rabbitmq_federation_upstream" "foo" {
definition {
uri = "amqp://server-name"
ack_mode = "not-valid"
}
}
`

0 comments on commit c2497c3

Please sign in to comment.