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: Eliminating listing storage account when its resource ID is available to use GET #28617

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 26 additions & 0 deletions internal/services/storage/client/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,32 @@ func (c Client) FindAccount(ctx context.Context, subscriptionIdRaw, accountName
return nil, nil
}

func (c Client) GetAccount(ctx context.Context, id commonids.StorageAccountId) (*AccountDetails, error) {
cacheAccountsLock.Lock()
defer cacheAccountsLock.Unlock()

if existing, ok := storageAccountsCache[id.StorageAccountName]; ok {
return &existing, nil
}

resp, err := c.ResourceManager.StorageAccounts.GetProperties(ctx, id, storageaccounts.DefaultGetPropertiesOperationOptions())
if err != nil {
return nil, fmt.Errorf("retrieving %s: %v", id, err)
}

if resp.Model == nil {
return nil, fmt.Errorf("unexpected null model of %s", id)
}

account, err := populateAccountDetails(id, *resp.Model)
if err != nil {
return nil, fmt.Errorf("populating details for %s: %+v", id, err)
}

storageAccountsCache[id.StorageAccountName] = *account
return account, nil
}

func populateAccountDetails(accountId commonids.StorageAccountId, account storageaccounts.StorageAccount) (*AccountDetails, error) {
out := AccountDetails{
Kind: pointer.From(account.Kind),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func (s AccountQueuePropertiesResource) Create() sdk.ResourceFunc {
}
accountReplicationType := accountReplicationTypeParts[1]

accountDetails, err := storageClient.FindAccount(ctx, accountID.SubscriptionId, accountID.StorageAccountName)
accountDetails, err := storageClient.GetAccount(ctx, *accountID)
if err != nil {
return err
}
Expand Down Expand Up @@ -398,7 +398,7 @@ func (s AccountQueuePropertiesResource) Read() sdk.ResourceFunc {

state.StorageAccountId = id.ID()

account, err := storageClient.FindAccount(ctx, id.SubscriptionId, id.StorageAccountName)
account, err := storageClient.GetAccount(ctx, *id)
if err != nil {
return metadata.MarkAsGone(id)
}
Expand Down Expand Up @@ -481,7 +481,7 @@ func (s AccountQueuePropertiesResource) Delete() sdk.ResourceFunc {
return err
}

account, err := storageClient.FindAccount(ctx, id.SubscriptionId, id.StorageAccountName)
account, err := storageClient.GetAccount(ctx, *id)
if err != nil {
return fmt.Errorf("retrieving %s: %+v", *id, err)
}
Expand Down Expand Up @@ -514,7 +514,7 @@ func (s AccountQueuePropertiesResource) Update() sdk.ResourceFunc {
return err
}

account, err := storageClient.FindAccount(ctx, id.SubscriptionId, id.StorageAccountName)
account, err := storageClient.GetAccount(ctx, *id)
if err != nil {
return fmt.Errorf("retrieving %s: %+v", *id, err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (r AccountQueuePropertiesResource) Exists(ctx context.Context, client *clie
return nil, err
}

account, err := client.Storage.FindAccount(ctx, id.SubscriptionId, id.StorageAccountName)
account, err := client.Storage.GetAccount(ctx, *id)
if err != nil {
return nil, fmt.Errorf("retrieving %s: %+v", *id, err)
}
Expand Down
8 changes: 4 additions & 4 deletions internal/services/storage/storage_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -1492,7 +1492,7 @@ func resourceStorageAccountCreate(d *pluginsdk.ResourceData, meta interface{}) e
// Start of Data Plane access - this entire block can be removed for 5.0, as the data_plane_available flag becomes redundant at that time.
if !features.FivePointOhBeta() && dataPlaneAvailable {
dataPlaneClient := meta.(*clients.Client).Storage
dataPlaneAccount, err := storageUtils.FindAccount(ctx, id.SubscriptionId, id.StorageAccountName)
dataPlaneAccount, err := storageUtils.GetAccount(ctx, id)
if err != nil {
return fmt.Errorf("retrieving %s: %+v", id, err)
}
Expand Down Expand Up @@ -1925,7 +1925,7 @@ func resourceStorageAccountUpdate(d *pluginsdk.ResourceData, meta interface{}) e
return fmt.Errorf("`queue_properties` aren't supported for account kind %q in sku tier %q", accountKind, accountTier)
}

account, err := dataPlaneClient.FindAccount(ctx, id.SubscriptionId, id.StorageAccountName)
account, err := dataPlaneClient.GetAccount(ctx, *id)
if err != nil {
return fmt.Errorf("retrieving %s: %+v", *id, err)
}
Expand Down Expand Up @@ -1953,7 +1953,7 @@ func resourceStorageAccountUpdate(d *pluginsdk.ResourceData, meta interface{}) e
return fmt.Errorf("`static_website` aren't supported for account kind %q in sku tier %q", accountKind, accountTier)
}

account, err := dataPlaneClient.FindAccount(ctx, id.SubscriptionId, id.StorageAccountName)
account, err := dataPlaneClient.GetAccount(ctx, *id)
if err != nil {
return fmt.Errorf("retrieving %s: %+v", *id, err)
}
Expand Down Expand Up @@ -2030,7 +2030,7 @@ func resourceStorageAccountRead(d *pluginsdk.ResourceData, meta interface{}) err
}

// we then need to find the storage account
account, err := storageUtils.FindAccount(ctx, id.SubscriptionId, id.StorageAccountName)
account, err := storageUtils.GetAccount(ctx, *id)
if err != nil {
return fmt.Errorf("retrieving %s: %+v", *id, err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (a AccountStaticWebsiteResource) Create() sdk.ResourceFunc {
}
accountReplicationType := accountReplicationTypeParts[1]

accountDetails, err := storageClient.FindAccount(ctx, accountID.SubscriptionId, accountID.StorageAccountName)
accountDetails, err := storageClient.GetAccount(ctx, *accountID)
if err != nil {
return err
}
Expand Down Expand Up @@ -157,7 +157,7 @@ func (a AccountStaticWebsiteResource) Read() sdk.ResourceFunc {

state.StorageAccountId = id.ID()

accountDetails, err := storageClient.FindAccount(ctx, id.SubscriptionId, id.StorageAccountName)
accountDetails, err := storageClient.GetAccount(ctx, *id)
if err != nil {
return metadata.MarkAsGone(id)
}
Expand Down Expand Up @@ -193,7 +193,7 @@ func (a AccountStaticWebsiteResource) Delete() sdk.ResourceFunc {
return err
}

accountDetails, err := storageClient.FindAccount(ctx, id.SubscriptionId, id.StorageAccountName)
accountDetails, err := storageClient.GetAccount(ctx, *id)
if err != nil {
return nil // lint:ignore nilerr If we don't find the account we can safely assume we don't need to remove the website since it must already be deleted
}
Expand Down Expand Up @@ -233,7 +233,7 @@ func (a AccountStaticWebsiteResource) Update() sdk.ResourceFunc {
return err
}

accountDetails, err := storageClient.FindAccount(ctx, id.SubscriptionId, id.StorageAccountName)
accountDetails, err := storageClient.GetAccount(ctx, *id)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (r AccountStaticWebsiteResource) Exists(ctx context.Context, client *client
return nil, err
}

accountDetails, err := client.Storage.FindAccount(ctx, id.SubscriptionId, id.StorageAccountName)
accountDetails, err := client.Storage.GetAccount(ctx, *id)
if err != nil {
return nil, fmt.Errorf("retrieving %s: %+v", *id, err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ func (r storageContainersDataSource) Read() sdk.ResourceFunc {

Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
blobContainersClient := metadata.Client.Storage.ResourceManager.BlobContainers
subscriptionId := metadata.Client.Account.SubscriptionId

var plan storageContainersDataSourceModel
if err := metadata.Decode(&plan); err != nil {
Expand All @@ -101,7 +100,7 @@ func (r storageContainersDataSource) Read() sdk.ResourceFunc {
return err
}

account, err := metadata.Client.Storage.FindAccount(ctx, subscriptionId, id.StorageAccountName)
account, err := metadata.Client.Storage.GetAccount(ctx, *id)
if err != nil {
return fmt.Errorf("retrieving Storage Account %q: %v", id.StorageAccountName, err)
}
Expand Down
Loading