Skip to content

Commit

Permalink
including hashicorp#26611
Browse files Browse the repository at this point in the history
* Initial Check-in...

* Address PR comments...

* DOH! Missed one...

* Fix error msg casing lint error...

* terrafmt...

* Address PR comments...

* Fix error msg casing for test cases...
  • Loading branch information
WodansSon authored Jul 19, 2024
1 parent 1205ab9 commit dab553d
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 47 deletions.
23 changes: 16 additions & 7 deletions internal/services/eventhub/eventhub_namespace_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import (
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/authorizationrulesnamespaces"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2022-01-01-preview/namespaces"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
)

func EventHubNamespaceDataSource() *pluginsdk.Resource {
return &pluginsdk.Resource{
resource := &pluginsdk.Resource{
Read: EventHubNamespaceDataSourceRead,

Timeouts: &pluginsdk.ResourceTimeout{
Expand Down Expand Up @@ -54,11 +55,6 @@ func EventHubNamespaceDataSource() *pluginsdk.Resource {
Computed: true,
},

"zone_redundant": {
Type: pluginsdk.TypeBool,
Computed: true,
},

"dedicated_cluster_id": {
Type: pluginsdk.TypeString,
Computed: true,
Expand Down Expand Up @@ -111,6 +107,16 @@ func EventHubNamespaceDataSource() *pluginsdk.Resource {
"tags": commonschema.TagsDataSource(),
},
}

if !features.FourPointOhBeta() {
resource.Schema["zone_redundant"] = &pluginsdk.Schema{
Type: pluginsdk.TypeBool,
Computed: true,
Deprecated: "The `zone_redundant` property has been deprecated and will be removed in v4.0 of the provider.",
}
}

return resource
}

func EventHubNamespaceDataSourceRead(d *pluginsdk.ResourceData, meta interface{}) error {
Expand Down Expand Up @@ -146,8 +152,11 @@ func EventHubNamespaceDataSourceRead(d *pluginsdk.ResourceData, meta interface{}
d.Set("auto_inflate_enabled", props.IsAutoInflateEnabled)
d.Set("kafka_enabled", props.KafkaEnabled)
d.Set("maximum_throughput_units", int(*props.MaximumThroughputUnits))
d.Set("zone_redundant", props.ZoneRedundant)
d.Set("dedicated_cluster_id", props.ClusterArmId)

if !features.FourPointOhBeta() {
d.Set("zone_redundant", props.ZoneRedundant)
}
}

if err := tags.FlattenAndSet(d, model.Tags); err != nil {
Expand Down
32 changes: 16 additions & 16 deletions internal/services/eventhub/eventhub_namespace_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,6 @@ func resourceEventHubNamespace() *pluginsdk.Resource {
Default: false,
},

// for premium namespace, zone redundant is computed by service based on the availability of availability zone feature.
"zone_redundant": {
Type: pluginsdk.TypeBool,
Optional: true,
Computed: true,
ForceNew: true,
},

"dedicated_cluster_id": {
Type: pluginsdk.TypeString,
Optional: true,
Expand Down Expand Up @@ -278,12 +270,14 @@ func resourceEventHubNamespace() *pluginsdk.Resource {
pluginsdk.CustomizeDiffShim(eventhubTLSVersionDiff),
),
}

if !features.FourPointOhBeta() {
resource.Schema["zone_redundant"] = &pluginsdk.Schema{
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
ForceNew: true,
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
Deprecated: "The `zone_redundant` property has been deprecated and will be removed in v4.0 of the provider.",
ForceNew: true,
}

resource.Schema["minimum_tls_version"] = &pluginsdk.Schema{
Expand All @@ -297,6 +291,7 @@ func resourceEventHubNamespace() *pluginsdk.Resource {
}, false),
}
}

return resource
}

Expand Down Expand Up @@ -362,9 +357,11 @@ func resourceEventHubNamespaceCreate(d *pluginsdk.ResourceData, meta interface{}
Tags: tags.Expand(t),
}

// for premium namespace, the zone_redundant is computed based on the region, user's input will be overridden
if sku != string(namespaces.SkuNamePremium) {
parameters.Properties.ZoneRedundant = utils.Bool(d.Get("zone_redundant").(bool))
if !features.FourPointOhBeta() {
// for premium namespace, the zone_redundant is computed based on the region, user's input will be overridden
if sku != string(namespaces.SkuNamePremium) {
parameters.Properties.ZoneRedundant = utils.Bool(d.Get("zone_redundant").(bool))
}
}

if v := d.Get("dedicated_cluster_id").(string); v != "" {
Expand Down Expand Up @@ -575,9 +572,12 @@ func resourceEventHubNamespaceRead(d *pluginsdk.ResourceData, meta interface{})
if props := model.Properties; props != nil {
d.Set("auto_inflate_enabled", props.IsAutoInflateEnabled)
d.Set("maximum_throughput_units", int(*props.MaximumThroughputUnits))
d.Set("zone_redundant", props.ZoneRedundant)
d.Set("dedicated_cluster_id", props.ClusterArmId)

if !features.FourPointOhBeta() {
d.Set("zone_redundant", props.ZoneRedundant)
}

localAuthDisabled := false
if props.DisableLocalAuth != nil {
localAuthDisabled = *props.DisableLocalAuth
Expand Down
24 changes: 23 additions & 1 deletion internal/services/eventhub/eventhub_namespace_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
Expand Down Expand Up @@ -658,7 +659,8 @@ resource "azurerm_eventhub_namespace" "test" {
}

func (EventHubNamespaceResource) premium(data acceptance.TestData) string {
return fmt.Sprintf(`
if !features.FourPointOhBeta() {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
Expand All @@ -675,6 +677,26 @@ resource "azurerm_eventhub_namespace" "test" {
sku = "Premium"
capacity = "1"
zone_redundant = true
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-eh-%d"
location = "%s"
}
resource "azurerm_eventhub_namespace" "test" {
name = "acctesteventhubnamespace-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
sku = "Premium"
capacity = "1"
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}
Expand Down
23 changes: 16 additions & 7 deletions internal/services/servicebus/servicebus_namespace_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import (
"github.com/hashicorp/go-azure-sdk/resource-manager/servicebus/2021-06-01-preview/namespacesauthorizationrule"
"github.com/hashicorp/go-azure-sdk/resource-manager/servicebus/2022-10-01-preview/namespaces"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
)

func dataSourceServiceBusNamespace() *pluginsdk.Resource {
return &pluginsdk.Resource{
resource := &pluginsdk.Resource{
Read: dataSourceServiceBusNamespaceRead,

Timeouts: &pluginsdk.ResourceTimeout{
Expand Down Expand Up @@ -79,11 +80,6 @@ func dataSourceServiceBusNamespace() *pluginsdk.Resource {
Sensitive: true,
},

"zone_redundant": {
Type: pluginsdk.TypeBool,
Computed: true,
},

"endpoint": {
Type: pluginsdk.TypeString,
Computed: true,
Expand All @@ -92,6 +88,16 @@ func dataSourceServiceBusNamespace() *pluginsdk.Resource {
"tags": tags.SchemaDataSource(),
},
}

if !features.FourPointOhBeta() {
resource.Schema["zone_redundant"] = &pluginsdk.Schema{
Type: pluginsdk.TypeBool,
Computed: true,
Deprecated: "The `zone_redundant` property has been deprecated and will be removed in v4.0 of the provider.",
}
}

return resource
}

func dataSourceServiceBusNamespaceRead(d *pluginsdk.ResourceData, meta interface{}) error {
Expand Down Expand Up @@ -122,8 +128,11 @@ 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)

if !features.FourPointOhBeta() {
d.Set("zone_redundant", props.ZoneRedundant)
}
}
}

Expand Down
34 changes: 22 additions & 12 deletions internal/services/servicebus/servicebus_namespace_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,6 @@ func resourceServiceBusNamespace() *pluginsdk.Resource {
Sensitive: true,
},

"zone_redundant": {
Type: pluginsdk.TypeBool,
Optional: true,
ForceNew: true,
},

"network_rule_set": {
Type: pluginsdk.TypeList,
Optional: true,
Expand Down Expand Up @@ -276,6 +270,14 @@ func resourceServiceBusNamespace() *pluginsdk.Resource {
}

if !features.FourPointOhBeta() {
resource.Schema["zone_redundant"] = &pluginsdk.Schema{
Type: pluginsdk.TypeBool,
Optional: true,
Computed: true,
Deprecated: "The `zone_redundant` property has been deprecated and will be removed in v4.0 of the provider.",
ForceNew: true,
}

resource.Schema["minimum_tls_version"] = &pluginsdk.Schema{
Type: pluginsdk.TypeString,
Optional: true,
Expand All @@ -287,6 +289,7 @@ func resourceServiceBusNamespace() *pluginsdk.Resource {
}, false),
}
}

return resource
}

Expand Down Expand Up @@ -335,35 +338,38 @@ func resourceServiceBusNamespaceCreateUpdate(d *pluginsdk.ResourceData, meta int
Tier: &s,
},
Properties: &namespaces.SBNamespaceProperties{
ZoneRedundant: utils.Bool(d.Get("zone_redundant").(bool)),
Encryption: expandServiceBusNamespaceEncryption(d.Get("customer_managed_key").([]interface{})),
DisableLocalAuth: utils.Bool(!d.Get("local_auth_enabled").(bool)),
PublicNetworkAccess: &publicNetworkEnabled,
},
Tags: expandTags(t),
}

if !features.FourPointOhBeta() {
parameters.Properties.ZoneRedundant = utils.Bool(d.Get("zone_redundant").(bool))
}

if tlsValue := d.Get("minimum_tls_version").(string); tlsValue != "" {
minimumTls := namespaces.TlsVersion(tlsValue)
parameters.Properties.MinimumTlsVersion = &minimumTls
}

if capacity := d.Get("capacity"); capacity != nil {
if !strings.EqualFold(sku, string(namespaces.SkuNamePremium)) && capacity.(int) > 0 {
return fmt.Errorf("Service Bus SKU %q only supports `capacity` of 0", sku)
return fmt.Errorf("service bus SKU %q only supports `capacity` of 0", sku)
}
if strings.EqualFold(sku, string(namespaces.SkuNamePremium)) && capacity.(int) == 0 {
return fmt.Errorf("Service Bus SKU %q only supports `capacity` of 1, 2, 4, 8 or 16", sku)
return fmt.Errorf("service bus SKU %q only supports `capacity` of 1, 2, 4, 8 or 16", sku)
}
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)
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)
return fmt.Errorf("service bus SKU %q only supports `premium_messaging_partitions` of 1, 2, 4", sku)
}
parameters.Properties.PremiumMessagingPartitions = utils.Int64(int64(premiumMessagingUnit.(int)))
}
Expand Down Expand Up @@ -444,7 +450,11 @@ func resourceServiceBusNamespaceRead(d *pluginsdk.ResourceData, meta interface{}

if props := model.Properties; props != nil {
d.Set("premium_messaging_partitions", props.PremiumMessagingPartitions)
d.Set("zone_redundant", props.ZoneRedundant)

if !features.FourPointOhBeta() {
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 @@ -13,6 +13,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
Expand Down Expand Up @@ -123,7 +124,7 @@ func TestAccAzureRMServiceBusNamespace_basicCapacity(t *testing.T) {
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basicCapacity(data),
ExpectError: regexp.MustCompile("Service Bus SKU \"Basic\" only supports `capacity` of 0"),
ExpectError: regexp.MustCompile("service bus SKU \"Basic\" only supports `capacity` of 0"),
},
})
}
Expand All @@ -134,7 +135,7 @@ func TestAccAzureRMServiceBusNamespace_premiumCapacity(t *testing.T) {
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.premiumCapacity(data),
ExpectError: regexp.MustCompile("Service Bus SKU \"Premium\" only supports `capacity` of 1, 2, 4, 8 or 16"),
ExpectError: regexp.MustCompile("service bus SKU \"Premium\" only supports `capacity` of 1, 2, 4, 8 or 16"),
},
})
}
Expand All @@ -145,12 +146,16 @@ func TestAccAzureRMServiceBusNamespace_premiumMessagingPartition(t *testing.T) {
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"),
ExpectError: regexp.MustCompile("service bus SKU \"Premium\" only supports `premium_messaging_partitions` of 1, 2, 4"),
},
})
}

func TestAccAzureRMServiceBusNamespace_zoneRedundant(t *testing.T) {
if features.FourPointOhBeta() {
t.Skipf("Skipped as 'zone_redundant' property is deprecated in 4.0")
}

data := acceptance.BuildTestData(t, "azurerm_servicebus_namespace", "test")
r := ServiceBusNamespaceResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
Expand Down Expand Up @@ -489,6 +494,7 @@ resource "azurerm_servicebus_namespace" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

// TODO: Remove in v4.0
func (ServiceBusNamespaceResource) zoneRedundant(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/eventhub_namespace.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ The following arguments are supported:

* `zone_redundant` - (Optional) Specifies if the EventHub Namespace should be Zone Redundant (created across Availability Zones). Changing this forces a new resource to be created. Defaults to `false`.

~> **Note:** For eventhub premium namespace, `zone_redundant` is computed by api based on the availability zone feature in each region. User's input will be overridden. Please explicitly sets the property to `true` when creating the premium namespace in a region that supports availability zone since the default value is `false` in 3.0 provider.
~> **Note:** For eventhub premium namespace, `zone_redundant` is computed by the api based on the availability zone feature in each region, user's input will be overridden. Please explicitly sets the property to `true` when creating the premium namespace in a region that supports availability zone since the default value is `false` in 3.0 provider.

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

Expand Down

0 comments on commit dab553d

Please sign in to comment.