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

azurerm_servicebus_namespace - support for the premium_messaging_partitions property #24676

Merged
merged 12 commits into from
Feb 20, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ func dataSourceServiceBusNamespace() *pluginsdk.Resource {
Computed: true,
},

"premium_messaging_partitions": {
Type: pluginsdk.TypeInt,
Computed: true,
},

"default_primary_connection_string": {
Type: pluginsdk.TypeString,
Computed: true,
Expand Down Expand Up @@ -116,6 +121,7 @@ func dataSourceServiceBusNamespaceRead(d *pluginsdk.ResourceData, meta interface
}

if props := model.Properties; props != nil {
d.Set("premium_messaging_partitions", props.PremiumMessagingPartitions)
d.Set("zone_redundant", props.ZoneRedundant)
d.Set("endpoint", props.ServiceBusEndpoint)
}
Expand Down
19 changes: 19 additions & 0 deletions internal/services/servicebus/servicebus_namespace_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ func resourceServiceBusNamespace() *pluginsdk.Resource {
ValidateFunc: validation.IntInSlice([]int{0, 1, 2, 4, 8, 16}),
},

"premium_messaging_partitions": {
Type: pluginsdk.TypeInt,
ForceNew: true,
Default: 0,
Optional: true,
ValidateFunc: validation.IntInSlice([]int{0, 1, 2, 4}),
},

"customer_managed_key": {
Type: pluginsdk.TypeList,
Optional: true,
Expand Down Expand Up @@ -336,6 +344,16 @@ func resourceServiceBusNamespaceCreateUpdate(d *pluginsdk.ResourceData, meta int
parameters.Sku.Capacity = utils.Int64(int64(capacity.(int)))
}

if premiumMessagingUnit := d.Get("premium_messaging_partitions"); premiumMessagingUnit != nil {
if !strings.EqualFold(sku, string(namespaces.SkuNamePremium)) && premiumMessagingUnit.(int) > 0 {
return fmt.Errorf("Premium messaging partition is not supported by service Bus SKU %q and it can only be set to 0", sku)
}
if strings.EqualFold(sku, string(namespaces.SkuNamePremium)) && premiumMessagingUnit.(int) == 0 {
return fmt.Errorf("Service Bus SKU %q only supports `premium_messaging_partitions` of 1, 2, 4", sku)
}
Comment on lines +351 to +353
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't match the schema validation above? The property defaults to 0 also.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for the premium namespace, 0 is not a valid value for premium namespace.

parameters.Properties.PremiumMessagingPartitions = utils.Int64(int64(premiumMessagingUnit.(int)))
}

if err := client.CreateOrUpdateThenPoll(ctx, id, parameters); err != nil {
return fmt.Errorf("creating/updating %s: %+v", id, err)
}
Expand Down Expand Up @@ -411,6 +429,7 @@ func resourceServiceBusNamespaceRead(d *pluginsdk.ResourceData, meta interface{}
d.Set("capacity", sku.Capacity)

if props := model.Properties; props != nil {
d.Set("premium_messaging_partitions", props.PremiumMessagingPartitions)
d.Set("zone_redundant", props.ZoneRedundant)
if customerManagedKey, err := flattenServiceBusNamespaceEncryption(props.Encryption); err == nil {
d.Set("customer_managed_key", customerManagedKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ func TestAccAzureRMServiceBusNamespace_premiumCapacity(t *testing.T) {
})
}

func TestAccAzureRMServiceBusNamespace_premiumMessagingPartition(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_servicebus_namespace", "test")
r := ServiceBusNamespaceResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.premiumMessagingPartition(data),
ExpectError: regexp.MustCompile("Service Bus SKU \"Premium\" only supports `premium_messaging_partitions` of 1, 2, 4"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't match the schema validation above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The validation error is for the premium namespaces

},
})
}

func TestAccAzureRMServiceBusNamespace_zoneRedundant(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_servicebus_namespace", "test")
r := ServiceBusNamespaceResource{}
Expand Down Expand Up @@ -404,11 +415,12 @@ resource "azurerm_resource_group" "test" {
}

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 = 1
name = "acctestservicebusnamespace-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
sku = "Premium"
capacity = 4
premium_messaging_partitions = 1
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}
Expand Down Expand Up @@ -455,6 +467,27 @@ resource "azurerm_servicebus_namespace" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func (ServiceBusNamespaceResource) premiumMessagingPartition(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

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 = 2
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func (ServiceBusNamespaceResource) zoneRedundant(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
2 changes: 2 additions & 0 deletions website/docs/d/servicebus_namespace.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ output "location" {

* `capacity` - The capacity of the ServiceBus Namespace.

* `premium_messaging_partitions` - The messaging partitions of the ServiceBus Namespace.

* `zone_redundant` - Whether or not this ServiceBus Namespace is zone redundant.

* `endpoint` - The URL to access the ServiceBus Namespace.
Expand Down
4 changes: 4 additions & 0 deletions website/docs/r/servicebus_namespace.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ The following arguments are supported:

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

* `premium_messaging_partitions` - (Optional) Specifies the number messaging partitions. Only valid when `sku` is `Premium` and the minimum number is `1`. Possible values include `0`, `1`, `2`, and `4`. Defaults to `0` for Standard, Basic namespace. Changing this forces a new resource to be created.

-> **Note:** It's not possible to change the partitioning option on any existing namespace. The number of partitions can only be set during namespace creation. Please check the doc https://learn.microsoft.com/en-us/azure/service-bus-messaging/enable-partitions-premium for more feature restrictions.

* `customer_managed_key` - (Optional) An `customer_managed_key` block as defined below.

* `local_auth_enabled` - (Optional) Whether or not SAS authentication is enabled for the Service Bus namespace. Defaults to `true`.
Expand Down
Loading