Skip to content

Commit

Permalink
provider/azurerm: fix servicebus_topic updating values
Browse files Browse the repository at this point in the history
enable_partitioning set to ForceNew
requires_duplicate_detection set to ForceNew

max_size_in_megabytes would cause a loop if enable_partitioning was true as this
causes the value to be multiplied by 16 for it's effective value, this computed
value is then returned by the ARM API in the same field which caused Terraform
to always detect a change

```
TF_ACC=1 go test ./builtin/providers/azurerm -v -run TestAccAzureRMServiceBusTopic -timeout 120m
=== RUN   TestAccAzureRMServiceBusTopic_importBasic
--- PASS: TestAccAzureRMServiceBusTopic_importBasic (345.08s)
=== RUN   TestAccAzureRMServiceBusTopic_basic
--- PASS: TestAccAzureRMServiceBusTopic_basic (342.23s)
=== RUN   TestAccAzureRMServiceBusTopic_update
--- PASS: TestAccAzureRMServiceBusTopic_update (359.56s)
=== RUN   TestAccAzureRMServiceBusTopic_enablePartitioning
--- PASS: TestAccAzureRMServiceBusTopic_enablePartitioning (362.80s)
=== RUN   TestAccAzureRMServiceBusTopic_enableDuplicateDetection
--- PASS: TestAccAzureRMServiceBusTopic_enableDuplicateDetection (364.97s)
PASS
ok  	github.com/hashicorp/terraform/builtin/providers/azurerm	1774.657s
```
  • Loading branch information
Peter McAtominey committed Oct 11, 2016
1 parent 7fddaf0 commit 54e4a81
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 6 deletions.
28 changes: 24 additions & 4 deletions builtin/providers/azurerm/resource_arm_servicebus_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,20 @@ func resourceArmServiceBusTopic() *schema.Resource {
"enable_partitioning": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},

"max_size_in_megabytes": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Type: schema.TypeInt,
Optional: true,
Computed: true,
ValidateFunc: validateArmServiceBusTopicMaxSize,
},

"requires_duplicate_detection": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},

"support_ordering": {
Expand Down Expand Up @@ -199,10 +202,18 @@ func resourceArmServiceBusTopicRead(d *schema.ResourceData, meta interface{}) er
d.Set("enable_express", props.EnableExpress)
d.Set("enable_filtering_messages_before_publishing", props.FilteringMessagesBeforePublishing)
d.Set("enable_partitioning", props.EnablePartitioning)
d.Set("max_size_in_megabytes", int(*props.MaxSizeInMegabytes))
d.Set("requires_duplicate_detection", props.RequiresDuplicateDetection)
d.Set("support_ordering", props.SupportOrdering)

// if partitioning is enabled then the max size returned by the API will be
// 16 times greater than the value set by the user
if *props.EnablePartitioning {
const partitionCount = 16
d.Set("max_size_in_megabytes", int(*props.MaxSizeInMegabytes/partitionCount))
} else {
d.Set("max_size_in_megabytes", int(*props.MaxSizeInMegabytes))
}

return nil
}

Expand All @@ -221,3 +232,12 @@ func resourceArmServiceBusTopicDelete(d *schema.ResourceData, meta interface{})

return err
}

func validateArmServiceBusTopicMaxSize(i interface{}, k string) (s []string, es []error) {
v := i.(int)
if v%1024 != 0 || v < 0 || v > 10240 {
es = append(es, fmt.Errorf("%q must be a multiple of 1024 up to and including 10240", k))
}

return
}
102 changes: 102 additions & 0 deletions builtin/providers/azurerm/resource_arm_servicebus_topic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,63 @@ func TestAccAzureRMServiceBusTopic_update(t *testing.T) {
})
}

func TestAccAzureRMServiceBusTopic_enablePartitioning(t *testing.T) {
ri := acctest.RandInt()
preConfig := fmt.Sprintf(testAccAzureRMServiceBusTopic_basic, ri, ri, ri)
postConfig := fmt.Sprintf(testAccAzureRMServiceBusTopic_enablePartitioning, ri, ri, ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMServiceBusTopicDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMServiceBusTopicExists("azurerm_servicebus_topic.test"),
),
},
resource.TestStep{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"azurerm_servicebus_topic.test", "enable_partitioning", "true"),
// Ensure size is read back in it's original value and not the x16 value returned by Azure
resource.TestCheckResourceAttr(
"azurerm_servicebus_topic.test", "max_size_in_megabytes", "10240"),
),
},
},
})
}

func TestAccAzureRMServiceBusTopic_enableDuplicateDetection(t *testing.T) {
ri := acctest.RandInt()
preConfig := fmt.Sprintf(testAccAzureRMServiceBusTopic_basic, ri, ri, ri)
postConfig := fmt.Sprintf(testAccAzureRMServiceBusTopic_enableDuplicateDetection, ri, ri, ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMServiceBusTopicDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMServiceBusTopicExists("azurerm_servicebus_topic.test"),
),
},
resource.TestStep{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"azurerm_servicebus_topic.test", "requires_duplicate_detection", "true"),
),
},
},
})
}

func testCheckAzureRMServiceBusTopicDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*ArmClient).serviceBusTopicsClient

Expand Down Expand Up @@ -159,3 +216,48 @@ resource "azurerm_servicebus_topic" "test" {
enable_express = true
}
`

var testAccAzureRMServiceBusTopic_enablePartitioning = `
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "West US"
}
resource "azurerm_servicebus_namespace" "test" {
name = "acctestservicebusnamespace-%d"
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
sku = "standard"
}
resource "azurerm_servicebus_topic" "test" {
name = "acctestservicebustopic-%d"
location = "West US"
namespace_name = "${azurerm_servicebus_namespace.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
enable_partitioning = true
max_size_in_megabytes = 10240
}
`

var testAccAzureRMServiceBusTopic_enableDuplicateDetection = `
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "West US"
}
resource "azurerm_servicebus_namespace" "test" {
name = "acctestservicebusnamespace-%d"
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
sku = "standard"
}
resource "azurerm_servicebus_topic" "test" {
name = "acctestservicebustopic-%d"
location = "West US"
namespace_name = "${azurerm_servicebus_namespace.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
requires_duplicate_detection = true
}
`
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,16 @@ The following arguments are supported:

* `enable_partitioning` - (Optional) Boolean flag which controls whether to enable
the topic to be partitioned across multiple message brokers. Defaults to false.
Changing this forces a new resource to be created.

* `max_size_in_megabytes` - (Optional) Integer value which controls the size of
memory allocated for the topic.
memory allocated for the topic. Supported values are multiples of 1024 up to
10240, if `enable_partitioning` is enabled then 16 partitions will be created
per GB, making the maximum possible topic size 163840 (10240 * 16).

* `requires_duplicate_detection` - (Optional) Boolean flag which controls whether
the Topic requires duplicate detection. Defaults to false.
the Topic requires duplicate detection. Defaults to false. Changing this forces
a new resource to be created.

* `support_ordering` - (Optional) Boolean flag which controls whether the Topic
supports ordering. Defaults to false.
Expand Down

0 comments on commit 54e4a81

Please sign in to comment.