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

storage: refactoring the Account Cache, Encryption Scopes and the Test Assertions to use hashicorp/go-azure-sdk rather than Azure/azure-sdk-for-go #25437

Merged
merged 16 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
fae4965
`storage`: updating `ID` to be a Storage Account ID
tombuildsstuff Mar 25, 2024
21bbe0c
`storage`: renaming to `StorageAccountId`
tombuildsstuff Mar 25, 2024
3aa5a63
`storage`: removing the separate `ResourceGroup` field and removing d…
tombuildsstuff Mar 25, 2024
fa68354
`storage`: refactoring to remove usage of the old SDK Properties dire…
tombuildsstuff Mar 25, 2024
da36655
`storage`: switching to pass in the Storage Account ID as the caching…
tombuildsstuff Mar 25, 2024
a984595
`storage`: threading `SubscriptionId` through to the `FindAccount` me…
tombuildsstuff Mar 25, 2024
a3bc249
`containers`: refactoring the state migration to use the regular `Lis…
tombuildsstuff Mar 25, 2024
a38a578
`storage`: updating the test checks to use `hashicorp/go-azure-sdk`
tombuildsstuff Mar 25, 2024
56f249d
`storage`: removing an unused alias
tombuildsstuff Mar 25, 2024
6a5b93e
r/`storage_encryption_scope`: updating the test check function to use…
tombuildsstuff Mar 25, 2024
3bd084a
(d|r)/`storage_encryption_scope`: refactoring to use `hashicorp/go-az…
tombuildsstuff Mar 25, 2024
a07fdbe
`storage`: fixing the test
tombuildsstuff Mar 25, 2024
88e0d25
r/storage_account_customer_managed_key: adding a missing deadline to …
tombuildsstuff Mar 25, 2024
f7f0bfe
r/storage_account_customer_managed_key: refactoring to use `hashicorp…
tombuildsstuff Mar 25, 2024
ea2d621
linting
tombuildsstuff Mar 27, 2024
be4a892
`storage`: updating the `populateAccountDetails` function to raise an…
tombuildsstuff Mar 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading