Skip to content

Commit

Permalink
Merge pull request #25437 from hashicorp/refactor/storage-to-go-azure…
Browse files Browse the repository at this point in the history
…-sdk

`storage`: refactoring the Account Cache, Encryption Scopes and the Test Assertions to use `hashicorp/go-azure-sdk` rather than `Azure/azure-sdk-for-go`
  • Loading branch information
tombuildsstuff authored Apr 2, 2024
2 parents 611f843 + be4a892 commit 7b2098e
Show file tree
Hide file tree
Showing 43 changed files with 585 additions and 530 deletions.
25 changes: 7 additions & 18 deletions internal/services/containers/migration/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package migration
import (
"context"
"fmt"
"strings"
"time"

"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down Expand Up @@ -44,30 +43,20 @@ func (RegistryV1ToV2) UpgradeFunc() pluginsdk.StateUpgraderFunc {

storageAccountId := ""
if v, ok := rawState["storage_account"]; ok {
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := context.WithTimeout(meta.(*clients.Client).StopContext, time.Minute*5)
defer cancel()

raw := v.(*pluginsdk.Set).List()
rawVals := raw[0].(map[string]interface{})
storageAccountName := rawVals["name"].(string)

client := meta.(*clients.Client).Storage.AccountsClient
ctx, cancel := context.WithTimeout(meta.(*clients.Client).StopContext, time.Minute*5)
defer cancel()

accounts, err := client.ListComplete(ctx)
account, err := meta.(*clients.Client).Storage.FindAccount(ctx, subscriptionId, storageAccountName)
if err != nil {
return rawState, fmt.Errorf("listing storage accounts")
return nil, fmt.Errorf("finding Storage Account %q: %+v", storageAccountName, err)
}

for accounts.NotDone() {
account := accounts.Value()
if strings.EqualFold(*account.Name, storageAccountName) {
storageAccountId = *account.ID
break
}

if err := accounts.NextWithContext(ctx); err != nil {
return rawState, fmt.Errorf("retrieving accounts: %+v", err)
}
}
storageAccountId = account.StorageAccountId.ID()
}

if storageAccountId == "" {
Expand Down
10 changes: 5 additions & 5 deletions internal/services/legacy/virtual_machine_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ func resourceVirtualMachineDelete(d *pluginsdk.ResourceData, meta interface{}) e
}

if osDisk.Vhd != nil {
if err = resourceVirtualMachineDeleteVhd(ctx, storageClient, osDisk.Vhd); err != nil {
if err = resourceVirtualMachineDeleteVhd(ctx, storageClient, id.SubscriptionId, osDisk.Vhd); err != nil {
return fmt.Errorf("deleting OS Disk VHD: %+v", err)
}
} else if osDisk.ManagedDisk != nil {
Expand All @@ -1009,7 +1009,7 @@ func resourceVirtualMachineDelete(d *pluginsdk.ResourceData, meta interface{}) e
}

if disk.Vhd != nil {
if err = resourceVirtualMachineDeleteVhd(ctx, storageClient, disk.Vhd); err != nil {
if err = resourceVirtualMachineDeleteVhd(ctx, storageClient, id.SubscriptionId, disk.Vhd); err != nil {
return fmt.Errorf("deleting Data Disk VHD: %+v", err)
}
} else if disk.ManagedDisk != nil {
Expand All @@ -1024,7 +1024,7 @@ func resourceVirtualMachineDelete(d *pluginsdk.ResourceData, meta interface{}) e
return nil
}

func resourceVirtualMachineDeleteVhd(ctx context.Context, storageClient *intStor.Client, vhd *compute.VirtualHardDisk) error {
func resourceVirtualMachineDeleteVhd(ctx context.Context, storageClient *intStor.Client, subscriptionId string, vhd *compute.VirtualHardDisk) error {
if vhd == nil {
return fmt.Errorf("`vhd` was nil`")
}
Expand All @@ -1038,7 +1038,7 @@ func resourceVirtualMachineDeleteVhd(ctx context.Context, storageClient *intStor
return fmt.Errorf("parsing %q: %s", uri, err)
}

account, err := storageClient.FindAccount(ctx, id.AccountId.AccountName)
account, err := storageClient.FindAccount(ctx, subscriptionId, id.AccountId.AccountName)
if err != nil {
return fmt.Errorf("retrieving Account %q for Blob %q (Container %q): %s", id.AccountId.AccountName, id.BlobName, id.ContainerName, err)
}
Expand All @@ -1059,7 +1059,7 @@ func resourceVirtualMachineDeleteVhd(ctx context.Context, storageClient *intStor
DeleteSnapshots: false,
}
if _, err := blobsClient.Delete(ctx, id.ContainerName, id.BlobName, input); err != nil {
return fmt.Errorf("deleting Blob %q (Container %q / Account %q / Resource Group %q): %s", id.BlobName, id.ContainerName, id.AccountId.AccountName, account.ResourceGroup, err)
return fmt.Errorf("deleting Blob %q (Container %q in %s): %+v", id.BlobName, id.ContainerName, id.AccountId, err)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/services/legacy/virtual_machine_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func (VirtualMachineResource) unmanagedDiskExistsInContainer(blobName string, sh
accountName := state.Attributes["storage_account_name"]
containerName := state.Attributes["name"]

account, err := clients.Storage.FindAccount(ctx, accountName)
account, err := clients.Storage.FindAccount(ctx, clients.Account.SubscriptionId, accountName)
if err != nil {
return fmt.Errorf("retrieving Account %q for Blob %q (Container %q): %s", accountName, blobName, containerName, err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,14 @@ func (r MachineLearningDataStoreBlobStorage) Read() sdk.ResourceFunc {
}
model.ServiceDataAuthIdentity = serviceDataAuth

storageAccount, err := storageClient.FindAccount(ctx, *data.AccountName)
storageAccount, err := storageClient.FindAccount(ctx, subscriptionId, *data.AccountName)
if err != nil {
return fmt.Errorf("retrieving Account %q for Container %q: %s", *data.AccountName, *data.ContainerName, err)
}
if storageAccount == nil {
return fmt.Errorf("Unable to locate Storage Account %q!", *data.AccountName)
}
containerId := commonids.NewStorageContainerID(subscriptionId, storageAccount.ResourceGroup, *data.AccountName, *data.ContainerName)
containerId := commonids.NewStorageContainerID(storageAccount.StorageAccountId.SubscriptionId, storageAccount.StorageAccountId.ResourceGroupName, *data.AccountName, *data.ContainerName)
model.StorageContainerID = containerId.ID()

model.IsDefault = *data.IsDefault
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,14 +313,14 @@ func (r MachineLearningDataStoreDataLakeGen2) Read() sdk.ResourceFunc {
}
model.ServiceDataIdentity = serviceDataIdentity

storageAccount, err := storageClient.FindAccount(ctx, data.AccountName)
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)
}
if storageAccount == nil {
return fmt.Errorf("Unable to locate Storage Account %q!", data.AccountName)
}
containerId := commonids.NewStorageContainerID(subscriptionId, storageAccount.ResourceGroup, data.AccountName, data.Filesystem)
containerId := commonids.NewStorageContainerID(storageAccount.StorageAccountId.SubscriptionId, storageAccount.StorageAccountId.ResourceGroupName, data.AccountName, data.Filesystem)
model.StorageContainerID = containerId.ID()

model.IsDefault = *data.IsDefault
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,14 +303,14 @@ func (r MachineLearningDataStoreFileShare) Read() sdk.ResourceFunc {
}
model.ServiceDataIdentity = serviceDataIdentity

storageAccount, err := storageClient.FindAccount(ctx, data.AccountName)
storageAccount, err := storageClient.FindAccount(ctx, subscriptionId, data.AccountName)
if err != nil {
return fmt.Errorf("retrieving Account %q for Share %q: %s", data.AccountName, data.FileShareName, err)
}
if storageAccount == nil {
return fmt.Errorf("Unable to locate Storage Account %q!", data.AccountName)
}
fileShareId := storageparse.NewStorageShareResourceManagerID(subscriptionId, storageAccount.ResourceGroup, data.AccountName, "default", data.FileShareName)
fileShareId := storageparse.NewStorageShareResourceManagerID(storageAccount.StorageAccountId.SubscriptionId, storageAccount.StorageAccountId.ResourceGroupName, data.AccountName, "default", data.FileShareName)
model.StorageFileShareID = fileShareId.ID()

model.IsDefault = *data.IsDefault
Expand Down
20 changes: 9 additions & 11 deletions internal/services/storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@ import (
var StorageDomainSuffix *string

type Client struct {
AccountsClient *storage.AccountsClient
BlobServicesClient *storage.BlobServicesClient
EncryptionScopesClient *storage.EncryptionScopesClient
FileServicesClient *storage.FileServicesClient
StorageDomainSuffix string

// NOTE: These SDK clients use `hashicorp/go-azure-sdk` and should be used going forwards
ResourceManager *storage_v2023_01_01.Client
SyncCloudEndpointsClient *cloudendpointresource.CloudEndpointResourceClient
SyncGroupsClient *syncgroupresource.SyncGroupResourceClient
SyncServiceClient *storagesyncservicesresource.StorageSyncServicesResourceClient

ResourceManager *storage_v2023_01_01.Client

StorageDomainSuffix string
// NOTE: these SDK clients use the legacy `Azure/azure-sdk-for-go` and should no longer be used
// for new functionality - please instead use the `hashicorp/go-azure-sdk` clients above.
AccountsClient *storage.AccountsClient
BlobServicesClient *storage.BlobServicesClient
FileServicesClient *storage.FileServicesClient

authorizerForAad auth.Authorizer
}
Expand All @@ -50,9 +52,6 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
blobServicesClient := storage.NewBlobServicesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&blobServicesClient.Client, o.ResourceManagerAuthorizer)

encryptionScopesClient := storage.NewEncryptionScopesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&encryptionScopesClient.Client, o.ResourceManagerAuthorizer)

fileServicesClient := storage.NewFileServicesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&fileServicesClient.Client, o.ResourceManagerAuthorizer)

Expand Down Expand Up @@ -86,7 +85,6 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
client := Client{
AccountsClient: &accountsClient,
BlobServicesClient: &blobServicesClient,
EncryptionScopesClient: &encryptionScopesClient,
FileServicesClient: &fileServicesClient,
ResourceManager: resourceManager,
SyncCloudEndpointsClient: syncCloudEndpointsClient,
Expand Down
2 changes: 1 addition & 1 deletion internal/services/storage/client/data_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (c Client) configureDataPlane(ctx context.Context, clientName string, baseC
return fmt.Errorf("retrieving Storage Account Key: %s", err)
}

storageAuth, err := auth.NewSharedKeyAuthorizer(account.name, *accountKey, operation.sharedKeyAuthenticationType)
storageAuth, err := auth.NewSharedKeyAuthorizer(account.StorageAccountId.StorageAccountName, *accountKey, operation.sharedKeyAuthenticationType)
if err != nil {
return fmt.Errorf("building Shared Key Authorizer for %s client: %+v", clientName, err)
}
Expand Down
Loading

0 comments on commit 7b2098e

Please sign in to comment.