From 6aa6e09908040d86da685cbdb56f04a2fda4fd2a Mon Sep 17 00:00:00 2001 From: Yun Liu Date: Fri, 17 Feb 2023 18:30:36 +0800 Subject: [PATCH 1/2] Fix 20503 --- internal/services/batch/batch_pool.go | 4 +- .../batch/batch_pool_resource_test.go | 46 +++++++++++++------ 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/internal/services/batch/batch_pool.go b/internal/services/batch/batch_pool.go index 0f8a80f436fc..821e572081d3 100644 --- a/internal/services/batch/batch_pool.go +++ b/internal/services/batch/batch_pool.go @@ -1014,9 +1014,9 @@ func expandBatchPoolAzureBlobFileSystemConfiguration(list []interface{}) (*pool. RelativeMountPath: configMap["relative_mount_path"].(string), } - if accountKey, ok := configMap["account_key"]; ok { + if accountKey, ok := configMap["account_key"]; ok && accountKey != "" { result.AccountKey = utils.String(accountKey.(string)) - } else if sasKey, ok := configMap["sas_key"]; ok { + } else if sasKey, ok := configMap["sas_key"]; ok && sasKey != "" { result.SasKey = utils.String(sasKey.(string)) } else if computedIDRef, err := expandBatchPoolIdentityReference(configMap); err == nil { result.IdentityReference = computedIDRef diff --git a/internal/services/batch/batch_pool_resource_test.go b/internal/services/batch/batch_pool_resource_test.go index 37269a3e887c..8a8e27b9a62e 100644 --- a/internal/services/batch/batch_pool_resource_test.go +++ b/internal/services/batch/batch_pool_resource_test.go @@ -1981,41 +1981,61 @@ resource "azurerm_storage_account" "test" { account_tier = "Standard" account_replication_type = "LRS" } + resource "azurerm_storage_container" "test" { name = "accbatchsc%s" storage_account_name = azurerm_storage_account.test.name container_access_type = "blob" } + resource "azurerm_batch_account" "test" { name = "testaccbatch%s" resource_group_name = azurerm_resource_group.test.name location = azurerm_resource_group.test.location } + +resource "azurerm_user_assigned_identity" "test" { + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + + name = "testidentity%s" +} + +resource "azurerm_role_assignment" "blob_contributor" { + principal_id = azurerm_user_assigned_identity.test.principal_id + role_definition_name = "Storage Blob Data Contributor" + scope = azurerm_storage_account.test.id +} + resource "azurerm_batch_pool" "test" { name = "testaccpool%s" resource_group_name = azurerm_resource_group.test.name account_name = azurerm_batch_account.test.name - node_agent_sku_id = "batch.node.ubuntu 18.04" + display_name = "Test Acc Pool Auto" vm_size = "Standard_A1" + node_agent_sku_id = "batch.node.ubuntu 20.04" + + fixed_scale { + target_dedicated_nodes = 0 + } + + storage_image_reference { + publisher = "microsoft-azure-batch" + offer = "ubuntu-server-container" + sku = "20-04-lts" + version = "latest" + } + mount { azure_blob_file_system { account_name = azurerm_storage_account.test.name container_name = azurerm_storage_container.test.name - account_key = azurerm_storage_account.test.primary_access_key - relative_mount_path = "/mnt/" + relative_mount_path = "external" + identity_id = azurerm_user_assigned_identity.test.id } } - fixed_scale { - target_dedicated_nodes = 1 - } - storage_image_reference { - publisher = "Canonical" - offer = "UbuntuServer" - sku = "18.04-lts" - version = "latest" - } } -`, template, data.RandomString, data.RandomString, data.RandomString, data.RandomString) +`, template, data.RandomString, data.RandomString, data.RandomString, data.RandomString, data.RandomString) } func (BatchPoolResource) mountConfigurationAzureFileShare(data acceptance.TestData) string { From 0fa38b54eb9cd3d110260946aa3a28f9020ab4e1 Mon Sep 17 00:00:00 2001 From: Yun Liu Date: Mon, 20 Feb 2023 10:14:18 +0800 Subject: [PATCH 2/2] Add a new acctest for batch_pool --- .../batch/batch_pool_resource_test.go | 71 ++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/internal/services/batch/batch_pool_resource_test.go b/internal/services/batch/batch_pool_resource_test.go index 8a8e27b9a62e..94744c6f3dd2 100644 --- a/internal/services/batch/batch_pool_resource_test.go +++ b/internal/services/batch/batch_pool_resource_test.go @@ -536,6 +536,27 @@ func TestAccBatchPool_mountConfigurationAzureBlobFileSystem(t *testing.T) { }) } +func TestAccBatchPool_mountConfigurationAzureBlobFileSystemWithUserAssignedIdentity(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_batch_pool", "test") + r := BatchPoolResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.mountConfigurationAzureBlobFileSystemWithUserAssignedIdentity(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + check.That(data.ResourceName).Key("mount.0.azure_blob_file_system.#").HasValue("1"), + check.That(data.ResourceName).Key("mount.0.azure_blob_file_system.0.relative_mount_path").HasValue("/mnt/"), + check.That(data.ResourceName).Key("mount.0.azure_blob_file_system.0.identity_id").Exists(), + ), + }, + data.ImportStep( + "stop_pending_resize_operation", + "mount.0.azure_blob_file_system.0.account_key", + ), + }) +} + func TestAccBatchPool_mountConfigurationAzureFileShare(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_batch_pool", "test") r := BatchPoolResource{} @@ -1981,6 +2002,54 @@ resource "azurerm_storage_account" "test" { account_tier = "Standard" account_replication_type = "LRS" } +resource "azurerm_storage_container" "test" { + name = "accbatchsc%s" + storage_account_name = azurerm_storage_account.test.name + container_access_type = "blob" +} +resource "azurerm_batch_account" "test" { + name = "testaccbatch%s" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location +} +resource "azurerm_batch_pool" "test" { + name = "testaccpool%s" + resource_group_name = azurerm_resource_group.test.name + account_name = azurerm_batch_account.test.name + node_agent_sku_id = "batch.node.ubuntu 18.04" + vm_size = "Standard_A1" + mount { + azure_blob_file_system { + account_name = azurerm_storage_account.test.name + container_name = azurerm_storage_container.test.name + account_key = azurerm_storage_account.test.primary_access_key + relative_mount_path = "/mnt/" + } + } + fixed_scale { + target_dedicated_nodes = 1 + } + storage_image_reference { + publisher = "Canonical" + offer = "UbuntuServer" + sku = "18.04-lts" + version = "latest" + } +} +`, template, data.RandomString, data.RandomString, data.RandomString, data.RandomString) +} + +func (BatchPoolResource) mountConfigurationAzureBlobFileSystemWithUserAssignedIdentity(data acceptance.TestData) string { + template := BatchPoolResource{}.template(data) + return fmt.Sprintf(` +%s +resource "azurerm_storage_account" "test" { + name = "accbatchsa%s" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + account_tier = "Standard" + account_replication_type = "LRS" +} resource "azurerm_storage_container" "test" { name = "accbatchsc%s" @@ -2030,7 +2099,7 @@ resource "azurerm_batch_pool" "test" { azure_blob_file_system { account_name = azurerm_storage_account.test.name container_name = azurerm_storage_container.test.name - relative_mount_path = "external" + relative_mount_path = "/mnt/" identity_id = azurerm_user_assigned_identity.test.id } }