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_mssql_virtual_machine: fix tests and persistent diff, use relative expiry for service principal password #10125

Merged
merged 5 commits into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 26 additions & 10 deletions azurerm/internal/services/mssql/mssql_virtual_machine_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,15 +486,31 @@ func flattenSqlVirtualMachineStorageConfigurationSettings(input *sqlvirtualmachi
return []interface{}{}
}

return []interface{}{
map[string]interface{}{
"disk_type": string(input.DiskConfigurationType),
"storage_workload_type": storageWorkloadType,
"data_settings": flattenSqlVirtualMachineStorageSettings(input.SQLDataSettings),
"log_settings": flattenSqlVirtualMachineStorageSettings(input.SQLLogSettings),
"temp_db_settings": flattenSqlVirtualMachineStorageSettings(input.SQLTempDbSettings),
},
output := map[string]interface{}{
"storage_workload_type": storageWorkloadType,
}

if v := string(input.DiskConfigurationType); v != "" {
output["disk_type"] = v
}

if v := flattenSqlVirtualMachineStorageSettings(input.SQLDataSettings); len(v) > 0 {
output["data_settings"] = v
}

if v := flattenSqlVirtualMachineStorageSettings(input.SQLLogSettings); len(v) > 0 {
output["log_settings"] = v
}

if v := flattenSqlVirtualMachineStorageSettings(input.SQLTempDbSettings); len(v) > 0 {
output["temp_db_settings"] = v
}

if len(output) <= 1 && output["storage_workload_type"].(string) == "" {
return []interface{}{}
}

return []interface{}{output}
}

func expandSqlVirtualMachineDataStorageSettings(input []interface{}) *sqlvirtualmachine.SQLStorageSettings {
Expand All @@ -510,10 +526,10 @@ func expandSqlVirtualMachineDataStorageSettings(input []interface{}) *sqlvirtual
}

func expandSqlVirtualMachineStorageSettingsLuns(input []interface{}) *[]int32 {
expandedLuns := make([]int32, len(input))
expandedLuns := make([]int32, 0)
for i := range input {
if input[i] != nil {
expandedLuns[i] = int32(input[i].(int))
expandedLuns = append(expandedLuns, int32(input[i].(int)))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance/check"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
Expand Down Expand Up @@ -71,7 +70,7 @@ func TestAccMsSqlVirtualMachine_complete(t *testing.T) {
})
}

func TestAccMsSqlVirtualMachine_updateAutoPatching(t *testing.T) {
func TestAccMsSqlVirtualMachine_withAutoPatching(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mssql_virtual_machine", "test")
r := MsSqlVirtualMachineResource{}

Expand All @@ -93,8 +92,9 @@ func TestAccMsSqlVirtualMachine_updateAutoPatching(t *testing.T) {
})
}

func TestAccMsSqlVirtualMachine_updateKeyVault(t *testing.T) {
func TestAccMsSqlVirtualMachine_withKeyVault(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mssql_virtual_machine", "test")

r := MsSqlVirtualMachineResource{}
value, err := uuid.GenerateUUID()
if err != nil {
Expand All @@ -106,7 +106,7 @@ func TestAccMsSqlVirtualMachine_updateKeyVault(t *testing.T) {
Config: r.withKeyVault(data, value),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("r_services_enabled").MatchesRegex(regexp.MustCompile("/*:acctestkv")),
check.That(data.ResourceName).Key("key_vault_credential.0.name").MatchesRegex(regexp.MustCompile("/*:acctestkv")),
),
},
data.ImportStep("key_vault_credential.0.key_vault_url", "key_vault_credential.0.service_principal_name", "key_vault_credential.0.service_principal_secret"),
Expand All @@ -115,20 +115,20 @@ func TestAccMsSqlVirtualMachine_updateKeyVault(t *testing.T) {
Config: r.withKeyVaultUpdated(data, value),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("r_services_enabled").MatchesRegex(regexp.MustCompile("/*:acctestkv2")),
check.That(data.ResourceName).Key("key_vault_credential.0.name").MatchesRegex(regexp.MustCompile("/*:acctestkv2")),
),
},
data.ImportStep("key_vault_credential.0.key_vault_url", "key_vault_credential.0.service_principal_name", "key_vault_credential.0.service_principal_secret"),
})
}

func TestAccMsSqlVirtualMachine_storageConfigurationSettings(t *testing.T) {
func TestAccMsSqlVirtualMachine_withStorageConfiguration(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mssql_virtual_machine", "test")
r := MsSqlVirtualMachineResource{}

data.ResourceTest(t, r, []resource.TestStep{
{
Config: r.storageConfigurationSettings(data),
Config: r.withStorageConfiguration(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
Expand Down Expand Up @@ -434,7 +434,7 @@ resource "azuread_service_principal" "test" {
resource "azuread_service_principal_password" "test" {
service_principal_id = azuread_service_principal.test.id
value = "%[3]s"
end_date = "2021-01-01T01:02:03Z"
end_date_relative = "240h"
}

resource "azurerm_mssql_virtual_machine" "test" {
Expand Down Expand Up @@ -514,7 +514,7 @@ resource "azuread_service_principal" "test" {
resource "azuread_service_principal_password" "test" {
service_principal_id = azuread_service_principal.test.id
value = "%[3]s"
end_date = "2021-01-01T01:02:03Z"
end_date_relative = "240h"
}

resource "azurerm_mssql_virtual_machine" "test" {
Expand All @@ -530,7 +530,7 @@ resource "azurerm_mssql_virtual_machine" "test" {
`, r.template(data), data.RandomInteger, value)
}

func (r MsSqlVirtualMachineResource) storageConfigurationSettings(data acceptance.TestData) string {
func (r MsSqlVirtualMachineResource) withStorageConfiguration(data acceptance.TestData) string {
return fmt.Sprintf(`
%[1]s

Expand Down