From 5ff3849af5c0dce87639108142806643cd219706 Mon Sep 17 00:00:00 2001 From: Xu Zhang Date: Wed, 27 Nov 2024 15:26:42 +0800 Subject: [PATCH 1/2] support crosse sub storage account --- ...arning_datastore_datalake_gen2_resource.go | 16 +++++-- ...g_datastore_datalake_gen2_resource_test.go | 44 +++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/internal/services/machinelearning/machine_learning_datastore_datalake_gen2_resource.go b/internal/services/machinelearning/machine_learning_datastore_datalake_gen2_resource.go index df06b33c9d3d..088f899f0860 100644 --- a/internal/services/machinelearning/machine_learning_datastore_datalake_gen2_resource.go +++ b/internal/services/machinelearning/machine_learning_datastore_datalake_gen2_resource.go @@ -19,6 +19,7 @@ import ( "github.com/hashicorp/terraform-provider-azurerm/helpers/tf" "github.com/hashicorp/terraform-provider-azurerm/internal/sdk" "github.com/hashicorp/terraform-provider-azurerm/internal/services/machinelearning/validate" + storageAccountHelper "github.com/hashicorp/terraform-provider-azurerm/internal/services/storage/client" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation" "github.com/hashicorp/terraform-provider-azurerm/utils" @@ -176,9 +177,11 @@ func (r MachineLearningDataStoreDataLakeGen2) Create() sdk.ResourceFunc { } props := &datastore.AzureDataLakeGen2Datastore{ + SubscriptionId: &containerId.SubscriptionId, + ResourceGroup: &containerId.ResourceGroupName, AccountName: containerId.StorageAccountName, - Endpoint: pointer.To(metadata.Client.Storage.StorageDomainSuffix), Filesystem: containerId.ContainerName, + Endpoint: pointer.To(metadata.Client.Storage.StorageDomainSuffix), Description: utils.String(model.Description), ServiceDataAccessAuthIdentity: pointer.To(datastore.ServiceDataAccessAuthIdentity(model.ServiceDataIdentity)), Tags: pointer.To(model.Tags), @@ -241,6 +244,8 @@ func (r MachineLearningDataStoreDataLakeGen2) Update() sdk.ResourceFunc { } props := &datastore.AzureDataLakeGen2Datastore{ + SubscriptionId: &containerId.SubscriptionId, + ResourceGroup: &containerId.ResourceGroupName, AccountName: containerId.StorageAccountName, Filesystem: containerId.ContainerName, Description: utils.String(state.Description), @@ -311,8 +316,13 @@ func (r MachineLearningDataStoreDataLakeGen2) Read() sdk.ResourceFunc { serviceDataIdentity = string(*v) } model.ServiceDataIdentity = serviceDataIdentity - - storageAccount, err := storageClient.FindAccount(ctx, subscriptionId, data.AccountName) + var storageAccount *storageAccountHelper.AccountDetails + // try to get storage account from the storage subscription if subscription exists otherwise delegate to the default subscription + if data.SubscriptionId != nil && *data.SubscriptionId != "" { + storageAccount, err = storageClient.FindAccount(ctx, *data.SubscriptionId, data.AccountName) + } else { + storageAccount, err = storageClient.FindAccount(ctx, subscriptionId, data.AccountName) + } if err != nil { return fmt.Errorf("retrieving Account %q for Data Lake Gen2 File System %q: %s", data.AccountName, data.Filesystem, err) } diff --git a/internal/services/machinelearning/machine_learning_datastore_datalake_gen2_resource_test.go b/internal/services/machinelearning/machine_learning_datastore_datalake_gen2_resource_test.go index bba57a14225f..b3967a766cff 100644 --- a/internal/services/machinelearning/machine_learning_datastore_datalake_gen2_resource_test.go +++ b/internal/services/machinelearning/machine_learning_datastore_datalake_gen2_resource_test.go @@ -6,6 +6,7 @@ package machinelearning_test import ( "context" "fmt" + "os" "testing" "github.com/hashicorp/go-azure-helpers/lang/response" @@ -49,6 +50,25 @@ func TestAccMachineLearningDataStoreDataLakeGen2_spn(t *testing.T) { }) } +func TestAccMachineLearningDataStoreDataLakeGen2_crossSubStorageAccount(t *testing.T) { + if os.Getenv("ARM_TEST_ACC_DATASTORE_GEN2_CROSS_SUB_SA_CONTAINER") == "" { + t.Skip("ARM_TEST_ACC_DATASTORE_GEN2_CROSS_SUB_SA_CONTAINER not set") + } + + data := acceptance.BuildTestData(t, "azurerm_machine_learning_datastore_datalake_gen2", "test") + r := MachineLearningDataStoreDataLakeGen2{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.dataLakeGen2CrossSubStorageAccount(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + func TestAccMachineLearningDataStoreDataLakeGen2_Update(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_machine_learning_datastore_datalake_gen2", "test") r := MachineLearningDataStoreDataLakeGen2{} @@ -156,6 +176,30 @@ resource "azurerm_machine_learning_datastore_datalake_gen2" "test" { `, template, data.RandomInteger) } +func (r MachineLearningDataStoreDataLakeGen2) dataLakeGen2CrossSubStorageAccount(data acceptance.TestData) string { + template := r.template(data) + return fmt.Sprintf(` +%s + +resource "azurerm_storage_container" "test" { + name = "acctestcontainer%[2]d" + storage_account_name = azurerm_storage_account.test.name + container_access_type = "private" +} + +resource "azurerm_machine_learning_datastore_datalake_gen2" "test" { + name = "accdatastore%[2]d" + workspace_id = azurerm_machine_learning_workspace.test.id + storage_container_id = azurerm_storage_container.test.resource_manager_id +} + +resource "azurerm_machine_learning_datastore_datalake_gen2" "crosssub" { + name = "accdcrosssub%[2]d" + workspace_id = azurerm_machine_learning_workspace.test.id + storage_container_id = "%[3]s" +}`, template, data.RandomInteger, os.Getenv("ARM_TEST_ACC_DATASTORE_GEN2_CROSS_SUB_SA_CONTAINER")) +} + func (r MachineLearningDataStoreDataLakeGen2) requiresImport(data acceptance.TestData) string { template := r.dataLakeGen2Basic(data) return fmt.Sprintf(` From 957b972537fd79df5c1d766c724443fe204c56a9 Mon Sep 17 00:00:00 2001 From: Xu Zhang Date: Fri, 29 Nov 2024 16:49:04 +0800 Subject: [PATCH 2/2] update crosssub test case --- ...g_datastore_datalake_gen2_resource_test.go | 44 ++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/internal/services/machinelearning/machine_learning_datastore_datalake_gen2_resource_test.go b/internal/services/machinelearning/machine_learning_datastore_datalake_gen2_resource_test.go index b3967a766cff..bb573f238bd8 100644 --- a/internal/services/machinelearning/machine_learning_datastore_datalake_gen2_resource_test.go +++ b/internal/services/machinelearning/machine_learning_datastore_datalake_gen2_resource_test.go @@ -51,8 +51,8 @@ func TestAccMachineLearningDataStoreDataLakeGen2_spn(t *testing.T) { } func TestAccMachineLearningDataStoreDataLakeGen2_crossSubStorageAccount(t *testing.T) { - if os.Getenv("ARM_TEST_ACC_DATASTORE_GEN2_CROSS_SUB_SA_CONTAINER") == "" { - t.Skip("ARM_TEST_ACC_DATASTORE_GEN2_CROSS_SUB_SA_CONTAINER not set") + if os.Getenv("ARM_SUBSCRIPTION_ID_ALT") == "" { + t.Skip("ARM_SUBSCRIPTION_ID_ALT not set") } data := acceptance.BuildTestData(t, "azurerm_machine_learning_datastore_datalake_gen2", "test") @@ -193,11 +193,45 @@ resource "azurerm_machine_learning_datastore_datalake_gen2" "test" { storage_container_id = azurerm_storage_container.test.resource_manager_id } +provider "azurerm-alt" { + subscription_id = "%[3]s" + + features { + key_vault { + purge_soft_delete_on_destroy = false + purge_soft_deleted_keys_on_destroy = false + } + } +} + +resource "azurerm_resource_group" "testalt" { + provider = azurerm-alt + name = "acctestRG-alt-%[2]d" + location = "%[4]s" +} + +resource "azurerm_storage_account" "testalt" { + provider = azurerm-alt + name = "acctestsaalt%[5]d" + location = azurerm_resource_group.testalt.location + resource_group_name = azurerm_resource_group.testalt.name + account_tier = "Standard" + account_replication_type = "LRS" +} + +resource "azurerm_storage_container" "testalt" { + provider = azurerm-alt + name = "acctestcontaineralt%[5]d" + storage_account_name = azurerm_storage_account.testalt.name + container_access_type = "private" +} + resource "azurerm_machine_learning_datastore_datalake_gen2" "crosssub" { - name = "accdcrosssub%[2]d" + name = "accdcrosssub%[5]d" workspace_id = azurerm_machine_learning_workspace.test.id - storage_container_id = "%[3]s" -}`, template, data.RandomInteger, os.Getenv("ARM_TEST_ACC_DATASTORE_GEN2_CROSS_SUB_SA_CONTAINER")) + storage_container_id = azurerm_storage_container.testalt.resource_manager_id +} + `, template, data.RandomInteger, os.Getenv("ARM_SUBSCRIPTION_ID_ALT"), data.Locations.Primary, data.RandomIntOfLength(10)) } func (r MachineLearningDataStoreDataLakeGen2) requiresImport(data acceptance.TestData) string {