Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow 0 capacity for servicebus #2920

Merged
merged 5 commits into from
Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions azurerm/resource_arm_servicebus_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ func resourceArmServiceBusNamespace() *schema.Resource {
"capacity": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validate.IntInSlice([]int{1, 2, 4}),
Default: 0,
ValidateFunc: validate.IntInSlice([]int{0, 1, 2, 4}),
},

"default_primary_connection_string": {
Expand Down Expand Up @@ -93,20 +94,6 @@ func resourceArmServiceBusNamespace() *schema.Resource {

"tags": tagsSchema(),
},

CustomizeDiff: func(d *schema.ResourceDiff, v interface{}) error {

//If the SKU is not premium the API will always return 0 for capacity
//so lets only allow it to be set if the SKU is premium
if _, ok := d.GetOk("capacity"); ok {
sku := d.Get("sku").(string)
if !strings.EqualFold(sku, string(servicebus.Premium)) {
return fmt.Errorf("`capacity` can only be set for a Premium SKU")
}
}

return nil
},
}
}

Expand Down Expand Up @@ -144,7 +131,13 @@ func resourceArmServiceBusNamespaceCreateUpdate(d *schema.ResourceData, meta int
Tags: expandTags(tags),
}

if capacity, ok := d.GetOk("capacity"); ok {
if capacity := d.Get("capacity"); capacity != nil {
if !strings.EqualFold(sku, string(servicebus.Premium)) && capacity.(int) > 0 {
return fmt.Errorf("Service Bus SKU %q only supports `capacity` of 0", sku)
}
if strings.EqualFold(sku, string(servicebus.Premium)) && capacity.(int) == 0 {
return fmt.Errorf("Service Bus SKU %q only supports `capacity` of 1, 2 or 4", sku)
}
parameters.Sku.Capacity = utils.Int32(int32(capacity.(int)))
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
68 changes: 68 additions & 0 deletions azurerm/resource_arm_servicebus_namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,40 @@ func TestAccAzureRMServiceBusNamespace_premium(t *testing.T) {
})
}

func TestAccAzureRMServiceBusNamespace_basicCapacity(t *testing.T) {
ri := tf.AccRandTimeInt()
config := testAccAzureRMServiceBusNamespace_basicCapacity(ri, testLocation())

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMServiceBusNamespaceDestroy,
Steps: []resource.TestStep{
{
Config: config,
ExpectError: regexp.MustCompile("Service Bus SKU \"Basic\" only supports `capacity` of 0"),
},
},
})
}

func TestAccAzureRMServiceBusNamespace_premiumCapacity(t *testing.T) {
ri := tf.AccRandTimeInt()
config := testAccAzureRMServiceBusNamespace_premiumCapacity(ri, testLocation())

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMServiceBusNamespaceDestroy,
Steps: []resource.TestStep{
{
Config: config,
ExpectError: regexp.MustCompile("Service Bus SKU \"Premium\" only supports `capacity` of 1, 2 or 4"),
},
},
})
}

func testCheckAzureRMServiceBusNamespaceDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*ArmClient).serviceBusNamespacesClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext
Expand Down Expand Up @@ -263,3 +297,37 @@ resource "azurerm_servicebus_namespace" "test" {
}
`, rInt, location, rInt)
}

func testAccAzureRMServiceBusNamespace_basicCapacity(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_servicebus_namespace" "test" {
name = "acctestservicebusnamespace-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
sku = "Basic"
capacity = 1
}
`, rInt, location, rInt)
}

func testAccAzureRMServiceBusNamespace_premiumCapacity(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_servicebus_namespace" "test" {
name = "acctestservicebusnamespace-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
sku = "Premium"
capacity = 0
}
`, rInt, location, rInt)
}
2 changes: 1 addition & 1 deletion website/docs/r/servicebus_namespace.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ The following arguments are supported:

* `sku` - (Required) Defines which tier to use. Options are basic, standard or premium.

* `capacity` - (Optional) Specifies the capacity, can only be set when `sku` is `Premium` namespace. Can be `1`, `2` or `4`.
* `capacity` - (Optional) Specifies the capacity. When `sku` is `Premium` can be `1`, `2` or `4`. When `sku` is `Basic` or `Standard` can be `0` only.

* `tags` - (Optional) A mapping of tags to assign to the resource.

Expand Down