Skip to content

Commit

Permalink
[Enhance] -azurerm_machine_learning_datastore_datalake_gen2 - Suppo…
Browse files Browse the repository at this point in the history
…rt crosse sub storage account (#28123)

* support crosse sub storage account

* update crosssub test case

* update test names
  • Loading branch information
xuzhang3 authored Dec 4, 2024
1 parent 66c6086 commit f536cac
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package machinelearning_test
import (
"context"
"fmt"
"os"
"testing"

"github.com/hashicorp/go-azure-helpers/lang/response"
Expand Down Expand Up @@ -49,6 +50,25 @@ func TestAccMachineLearningDataStoreDataLakeGen2_spn(t *testing.T) {
})
}

func TestAccMachineLearningDataStoreDataLakeGen2_crossSubStorageAccount(t *testing.T) {
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")
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{}
Expand Down Expand Up @@ -156,6 +176,64 @@ 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 = "acctestdatastore%[2]d"
workspace_id = azurerm_machine_learning_workspace.test.id
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 = "acctestdcrosssub%[5]d"
workspace_id = azurerm_machine_learning_workspace.test.id
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 {
template := r.dataLakeGen2Basic(data)
return fmt.Sprintf(`
Expand Down

0 comments on commit f536cac

Please sign in to comment.