Skip to content

Commit

Permalink
azurerm_mssql_server - Fix azurerm_mssql_server automatically set…
Browse files Browse the repository at this point in the history
…s `minimum_tls_version` even if not provided issue (#16595)
  • Loading branch information
sinbai authored Aug 16, 2022
1 parent 0357a57 commit 57bdd87
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
13 changes: 9 additions & 4 deletions internal/services/mssql/mssql_server_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func resourceMsSqlServer() *pluginsdk.Resource {
"1.0",
"1.1",
"1.2",
"Disabled",
}, false),
},

Expand Down Expand Up @@ -256,7 +257,7 @@ func resourceMsSqlServerCreate(d *pluginsdk.ResourceData, meta interface{}) erro
props.ServerProperties.RestrictOutboundNetworkAccess = sql.ServerNetworkAccessFlagEnabled
}

if v := d.Get("minimum_tls_version"); v.(string) != "" {
if v := d.Get("minimum_tls_version"); v.(string) != "Disabled" {
props.ServerProperties.MinimalTLSVersion = utils.String(v.(string))
}

Expand Down Expand Up @@ -346,7 +347,7 @@ func resourceMsSqlServerUpdate(d *pluginsdk.ResourceData, meta interface{}) erro
props.ServerProperties.AdministratorLoginPassword = utils.String(adminPassword)
}

if v := d.Get("minimum_tls_version"); v.(string) != "" {
if v := d.Get("minimum_tls_version"); v.(string) != "Disabled" {
props.ServerProperties.MinimalTLSVersion = utils.String(v.(string))
}

Expand Down Expand Up @@ -466,7 +467,11 @@ func resourceMsSqlServerRead(d *pluginsdk.ResourceData, meta interface{}) error
d.Set("version", props.Version)
d.Set("administrator_login", props.AdministratorLogin)
d.Set("fully_qualified_domain_name", props.FullyQualifiedDomainName)
d.Set("minimum_tls_version", props.MinimalTLSVersion)
if v := props.MinimalTLSVersion; v == nil {
d.Set("minimum_tls_version", "Disabled")
} else {
d.Set("minimum_tls_version", props.MinimalTLSVersion)
}
d.Set("public_network_access_enabled", props.PublicNetworkAccess == sql.ServerNetworkAccessFlagEnabled)
d.Set("outbound_network_restriction_enabled", props.RestrictOutboundNetworkAccess == sql.ServerNetworkAccessFlagEnabled)
primaryUserAssignedIdentityID := ""
Expand Down Expand Up @@ -681,7 +686,7 @@ func flattenSqlServerRestorableDatabases(resp sql.RestorableDroppedDatabaseListR

func msSqlMinimumTLSVersionDiff(ctx context.Context, d *pluginsdk.ResourceDiff, _ interface{}) (err error) {
old, new := d.GetChange("minimum_tls_version")
if old != "" && new == "" {
if old != "" && old != "Disabled" && new == "Disabled" {
err = fmt.Errorf("`minimum_tls_version` cannot be removed once set, please set a valid value for this property")
}
return
Expand Down
42 changes: 42 additions & 0 deletions internal/services/mssql/mssql_server_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ func TestAccMsSqlServer_complete(t *testing.T) {
})
}

func TestAccMsSqlServer_minimumTLSVersionDisabled(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mssql_server", "test")
r := MsSqlServerResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basicWithMinimumTLSVersionDisabled(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("administrator_login_password"),
})
}

func TestAccMsSqlServer_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mssql_server", "test")
r := MsSqlServerResource{}
Expand Down Expand Up @@ -255,6 +270,33 @@ resource "azurerm_mssql_server" "test" {
`, data.RandomInteger, data.Locations.Primary)
}

func (MsSqlServerResource) basicWithMinimumTLSVersionDisabled(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-mssql-%[1]d"
location = "%[2]s"
}
resource "azurerm_mssql_server" "test" {
name = "acctestsqlserver%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
version = "12.0"
administrator_login = "missadministrator"
administrator_login_password = "thisIsKat11"
minimum_tls_version = "Disabled"
identity {
type = "SystemAssigned"
}
}
`, data.RandomInteger, data.Locations.Primary)
}

func (MsSqlServerResource) basicWithMinimumTLSVersion(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
4 changes: 2 additions & 2 deletions website/docs/r/mssql_server.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ The following arguments are supported:

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

* `minimum_tls_version` - (Optional) The Minimum TLS Version for all SQL Database and SQL Data Warehouse databases associated with the server. Valid values are: `1.0`, `1.1` and `1.2`.
* `minimum_tls_version` - (Optional) The Minimum TLS Version for all SQL Database and SQL Data Warehouse databases associated with the server. Valid values are: `1.0`, `1.1` , `1.2` and `Disabled`. Defaults to `1.2`.

~> **NOTE:** Once `minimum_tls_version` is set it is not possible to remove this setting and must be given a valid value for any further updates to the resource.
~> **NOTE:** The `minimum_tls_version` is set to `Disabled` means all TLS versions are allowed. After you enforce a version of `minimum_tls_version`, it's not possible to revert to `Disabled`.

* `public_network_access_enabled` - (Optional) Whether public network access is allowed for this server. Defaults to `true`.

Expand Down

0 comments on commit 57bdd87

Please sign in to comment.