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_eventhub_namespace: support latest released properties:local_authentication_enabled,public_network_access_enabled,minimum_tls_version #17194

Merged
merged 15 commits into from
Aug 4, 2022
Merged
2 changes: 1 addition & 1 deletion internal/services/eventhub/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/disasterrecoveryconfigs"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/eventhubs"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/eventhubsclusters"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/namespaces"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/networkrulesets"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2022-01-01-preview/namespaces"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/namespaces"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2022-01-01-preview/namespaces"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/locks"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"testing"

"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/namespaces"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2022-01-01-preview/namespaces"
"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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/authorizationrulesnamespaces"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/namespaces"
"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/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
Expand Down
115 changes: 100 additions & 15 deletions internal/services/eventhub/eventhub_namespace_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/authorizationrulesnamespaces"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/eventhubsclusters"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/namespaces"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/networkrulesets"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2022-01-01-preview/namespaces"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down Expand Up @@ -190,6 +190,29 @@ func resourceEventHubNamespace() *pluginsdk.Resource {
},
},

"local_authentication_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

is this also the default state the API returns when provisioning on an older version and upgrading to a newer api version?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, I tried the older version and the default value is the same

},

"minimum_tls_version": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
string(namespaces.TlsVersionOnePointZero),
string(namespaces.TlsVersionOnePointOne),
string(namespaces.TlsVersionOnePointTwo),
}, false),
},

"public_network_access_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"default_primary_connection_string_alias": {
Type: pluginsdk.TypeString,
Computed: true,
Expand Down Expand Up @@ -228,22 +251,26 @@ func resourceEventHubNamespace() *pluginsdk.Resource {

"tags": commonschema.Tags(),
},
CustomizeDiff: pluginsdk.CustomizeDiffShim(func(ctx context.Context, d *pluginsdk.ResourceDiff, v interface{}) error {
oldSku, newSku := d.GetChange("sku")
if d.HasChange("sku") {
if strings.EqualFold(newSku.(string), string(namespaces.SkuNamePremium)) || strings.EqualFold(oldSku.(string), string(namespaces.SkuTierPremium)) {
log.Printf("[DEBUG] cannot migrate a namespace from or to Premium SKU")
d.ForceNew("sku")
}
if strings.EqualFold(newSku.(string), string(namespaces.SkuTierPremium)) {
zoneRedundant := d.Get("zone_redundant").(bool)
if !zoneRedundant {
return fmt.Errorf("zone_redundant needs to be set to true when using premium SKU")

CustomizeDiff: pluginsdk.CustomDiffWithAll(
pluginsdk.CustomizeDiffShim(func(ctx context.Context, d *pluginsdk.ResourceDiff, v interface{}) error {
oldSku, newSku := d.GetChange("sku")
if d.HasChange("sku") {
if strings.EqualFold(newSku.(string), string(namespaces.SkuNamePremium)) || strings.EqualFold(oldSku.(string), string(namespaces.SkuTierPremium)) {
log.Printf("[DEBUG] cannot migrate a namespace from or to Premium SKU")
d.ForceNew("sku")
}
if strings.EqualFold(newSku.(string), string(namespaces.SkuTierPremium)) {
zoneRedundant := d.Get("zone_redundant").(bool)
if !zoneRedundant {
return fmt.Errorf("zone_redundant needs to be set to true when using premium SKU")
}
}
}
}
return nil
}),
return nil
}),
pluginsdk.CustomizeDiffShim(eventhubTLSVersionDiff),
),
}
}

Expand Down Expand Up @@ -278,6 +305,16 @@ func resourceEventHubNamespaceCreate(d *pluginsdk.ResourceData, meta interface{}
return fmt.Errorf("expanding `identity`: %+v", err)
}

publicNetworkEnabled := namespaces.PublicNetworkAccessEnabled
if !d.Get("public_network_access_enabled").(bool) {
publicNetworkEnabled = namespaces.PublicNetworkAccessDisabled
}

disableLocalAuth := false
if !d.Get("local_authentication_enabled").(bool) {
disableLocalAuth = true
}

parameters := namespaces.EHNamespace{
Location: &location,
Sku: &namespaces.Sku{
Expand All @@ -292,6 +329,8 @@ func resourceEventHubNamespaceCreate(d *pluginsdk.ResourceData, meta interface{}
Properties: &namespaces.EHNamespaceProperties{
IsAutoInflateEnabled: utils.Bool(autoInflateEnabled),
ZoneRedundant: utils.Bool(zoneRedundant),
DisableLocalAuth: utils.Bool(disableLocalAuth),
PublicNetworkAccess: &publicNetworkEnabled,
},
Tags: tags.Expand(t),
}
Expand All @@ -300,6 +339,11 @@ func resourceEventHubNamespaceCreate(d *pluginsdk.ResourceData, meta interface{}
parameters.Properties.ClusterArmId = utils.String(v)
}

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

if v, ok := d.GetOk("maximum_throughput_units"); ok {
parameters.Properties.MaximumThroughputUnits = utils.Int64(int64(v.(int)))
}
Expand Down Expand Up @@ -347,6 +391,16 @@ func resourceEventHubNamespaceUpdate(d *pluginsdk.ResourceData, meta interface{}
autoInflateEnabled := d.Get("auto_inflate_enabled").(bool)
zoneRedundant := d.Get("zone_redundant").(bool)

publicNetworkEnabled := namespaces.PublicNetworkAccessEnabled
if !d.Get("public_network_access_enabled").(bool) {
publicNetworkEnabled = namespaces.PublicNetworkAccessDisabled
}

disableLocalAuth := false
if !d.Get("local_authentication_enabled").(bool) {
disableLocalAuth = true
}

identity, err := identity.ExpandSystemAndUserAssignedMap(d.Get("identity").([]interface{}))
if err != nil {
return fmt.Errorf("expanding `identity`: %+v", err)
Expand All @@ -366,6 +420,8 @@ func resourceEventHubNamespaceUpdate(d *pluginsdk.ResourceData, meta interface{}
Properties: &namespaces.EHNamespaceProperties{
IsAutoInflateEnabled: utils.Bool(autoInflateEnabled),
ZoneRedundant: utils.Bool(zoneRedundant),
DisableLocalAuth: utils.Bool(disableLocalAuth),
PublicNetworkAccess: &publicNetworkEnabled,
},
Tags: tags.Expand(t),
}
Expand All @@ -374,6 +430,11 @@ func resourceEventHubNamespaceUpdate(d *pluginsdk.ResourceData, meta interface{}
parameters.Properties.ClusterArmId = utils.String(v)
}

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

if v, ok := d.GetOk("maximum_throughput_units"); ok {
parameters.Properties.MaximumThroughputUnits = utils.Int64(int64(v.(int)))
}
Expand Down Expand Up @@ -471,6 +532,22 @@ func resourceEventHubNamespaceRead(d *pluginsdk.ResourceData, meta interface{})
d.Set("maximum_throughput_units", int(*props.MaximumThroughputUnits))
d.Set("zone_redundant", props.ZoneRedundant)
d.Set("dedicated_cluster_id", props.ClusterArmId)

localAuthDisabled := false
if props.DisableLocalAuth != nil {
localAuthDisabled = *props.DisableLocalAuth
}
d.Set("local_authentication_enabled", !localAuthDisabled)

publicNetworkAccess := true
if props.PublicNetworkAccess != nil && *props.PublicNetworkAccess == namespaces.PublicNetworkAccessDisabled {
publicNetworkAccess = false
}
d.Set("public_network_access_enabled", publicNetworkAccess)

if props.MinimumTlsVersion != nil {
d.Set("minimum_tls_version", *props.MinimumTlsVersion)
}
}

if err := tags.FlattenAndSet(d, model.Tags); err != nil {
Expand Down Expand Up @@ -712,3 +789,11 @@ func resourceVnetRuleHash(v interface{}) int {
}
return pluginsdk.HashString(buf.String())
}

func eventhubTLSVersionDiff(ctx context.Context, d *pluginsdk.ResourceDiff, _ interface{}) (err error) {
old, new := d.GetChange("minimum_tls_version")
if old != "" && new == "" {
err = fmt.Errorf("`minimum_tls_version` has been set before, please set a valid value for this property ")
}
return
}
124 changes: 123 additions & 1 deletion internal/services/eventhub/eventhub_namespace_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"regexp"
"testing"

"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/namespaces"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2022-01-01-preview/namespaces"
"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"
Expand Down Expand Up @@ -326,6 +326,21 @@ func TestAccEventHubNamespace_BasicWithCapacity(t *testing.T) {
})
}

func TestAccEventHubNamespace_BasicWithLocalAuthProperty(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_eventhub_namespace", "test")
r := EventHubNamespaceResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.localAuthProperty(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("local_authentication_enabled").HasValue("false"),
),
},
})
}

func TestAccEventHubNamespace_BasicWithCapacityUpdate(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_eventhub_namespace", "test")
r := EventHubNamespaceResource{}
Expand Down Expand Up @@ -428,6 +443,50 @@ func TestAccEventHubNamespace_maximumThroughputUnitsUpdate(t *testing.T) {
})
}

func TestAccEventHubNamespace_publicNetworkAccessUpdate(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_eventhub_namespace", "test")
r := EventHubNamespaceResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("public_network_access_enabled").HasValue("true"),
),
},
{
Config: r.publicNetworkAccessUpdate(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("public_network_access_enabled").HasValue("false"),
),
},
})
}

func TestAccEventHubNamespace_minimumTLSUpdate(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_eventhub_namespace", "test")
r := EventHubNamespaceResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("minimum_tls_version").HasValue("1.2"),
),
},
{
Config: r.minimumTLSUpdate(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("minimum_tls_version").HasValue("1.1"),
),
},
})
}

func TestAccEventHubNamespace_autoInfalteDisabledWithAutoInflateUnits(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_eventhub_namespace", "test")
r := EventHubNamespaceResource{}
Expand Down Expand Up @@ -924,6 +983,69 @@ resource "azurerm_eventhub_namespace" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, capacity)
}

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

resource "azurerm_resource_group" "test" {
name = "acctestRG-%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 = "Basic"
local_authentication_enabled = false
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

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

resource "azurerm_resource_group" "test" {
name = "acctestRG-%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 = "Basic"
public_network_access_enabled = false
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

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

resource "azurerm_resource_group" "test" {
name = "acctestRG-%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 = "Basic"
minimum_tls_version = "1.1"
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func (EventHubNamespaceResource) maximumThroughputUnitsUpdate(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
2 changes: 1 addition & 1 deletion internal/services/eventhub/eventhub_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/eventhubs"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/namespaces"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2022-01-01-preview/namespaces"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/Azure/azure-sdk-for-go/services/operationalinsights/mgmt/2020-08-01/operationalinsights"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/eventhubs"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/namespaces"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2022-01-01-preview/namespaces"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down
Loading