diff --git a/azurerm/internal/services/storage/helpers.go b/azurerm/internal/services/storage/helpers.go index 8512626a3c8a..9dcf82b5c623 100644 --- a/azurerm/internal/services/storage/helpers.go +++ b/azurerm/internal/services/storage/helpers.go @@ -3,12 +3,41 @@ package storage import ( "context" "fmt" + "log" "strings" + "sync" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" ) +var ( + accountKeysCache = map[string]string{} + resourceGroupNamesCache = map[string]string{} + writeLock = sync.RWMutex{} +) + +func (client Client) ClearFromCache(resourceGroup, accountName string) { + writeLock.Lock() + + log.Printf("[DEBUG] Removing Account %q (Resource Group %q) from the cache", accountName, resourceGroup) + accountCacheKey := fmt.Sprintf("%s-%s", resourceGroup, accountName) + delete(accountKeysCache, accountCacheKey) + + resourceGroupsCacheKey := accountName + delete(resourceGroupNamesCache, resourceGroupsCacheKey) + + log.Printf("[DEBUG] Removed Account %q (Resource Group %q) from the cache", accountName, resourceGroup) + writeLock.Unlock() +} + func (client Client) FindResourceGroup(ctx context.Context, accountName string) (*string, error) { + cacheKey := accountName + if v, ok := resourceGroupNamesCache[cacheKey]; ok { + return &v, nil + } + + log.Printf("[DEBUG] Cache Miss - looking up the resource group for storage account %q..", accountName) + writeLock.Lock() accounts, err := client.AccountsClient.List(ctx) if err != nil { return nil, fmt.Errorf("Error listing Storage Accounts (to find Resource Group for %q): %s", accountName, err) @@ -35,10 +64,23 @@ func (client Client) FindResourceGroup(ctx context.Context, accountName string) } } + if resourceGroup != nil { + resourceGroupNamesCache[cacheKey] = *resourceGroup + } + + writeLock.Unlock() + return resourceGroup, nil } func (client Client) findAccountKey(ctx context.Context, resourceGroup, accountName string) (*string, error) { + cacheKey := fmt.Sprintf("%s-%s", resourceGroup, accountName) + if v, ok := accountKeysCache[cacheKey]; ok { + return &v, nil + } + + writeLock.Lock() + log.Printf("[DEBUG] Cache Miss - looking up the account key for storage account %q..", accountName) props, err := client.AccountsClient.ListKeys(ctx, resourceGroup, accountName) if err != nil { return nil, fmt.Errorf("Error Listing Keys for Storage Account %q (Resource Group %q): %+v", accountName, resourceGroup, err) @@ -50,5 +92,9 @@ func (client Client) findAccountKey(ctx context.Context, resourceGroup, accountN keys := *props.Keys firstKey := keys[0].Value + + accountKeysCache[cacheKey] = *firstKey + writeLock.Unlock() + return firstKey, nil } diff --git a/azurerm/resource_arm_storage_account.go b/azurerm/resource_arm_storage_account.go index da29c7567583..cccfd91a49db 100644 --- a/azurerm/resource_arm_storage_account.go +++ b/azurerm/resource_arm_storage_account.go @@ -1100,7 +1100,8 @@ func resourceArmStorageAccountRead(d *schema.ResourceData, meta interface{}) err func resourceArmStorageAccountDelete(d *schema.ResourceData, meta interface{}) error { ctx := meta.(*ArmClient).StopContext - client := meta.(*ArmClient).storage.AccountsClient + storageClient := meta.(*ArmClient).storage + client := storageClient.AccountsClient id, err := azure.ParseAzureResourceID(d.Id()) if err != nil { @@ -1150,6 +1151,9 @@ func resourceArmStorageAccountDelete(d *schema.ResourceData, meta interface{}) e } } + // remove this from the cache + storageClient.ClearFromCache(resourceGroup, name) + return nil } diff --git a/azurerm/resource_arm_storage_container_test.go b/azurerm/resource_arm_storage_container_test.go index b49fbd22b968..df0243ca197e 100644 --- a/azurerm/resource_arm_storage_container_test.go +++ b/azurerm/resource_arm_storage_container_test.go @@ -336,12 +336,10 @@ func testCheckAzureRMStorageContainerDestroy(s *terraform.State) error { props, err := client.GetProperties(ctx, accountName, containerName) if err != nil { - if utils.ResponseWasNotFound(props.Response) { - return nil - } - - return fmt.Errorf("Error retrieving Container %q in Storage Account %q: %s", containerName, accountName, err) + return nil } + + return fmt.Errorf("Container still exists: %+v", props) } return nil diff --git a/azurerm/resource_arm_storage_queue_test.go b/azurerm/resource_arm_storage_queue_test.go index 5bfaa640c4c3..f235bec740f1 100644 --- a/azurerm/resource_arm_storage_queue_test.go +++ b/azurerm/resource_arm_storage_queue_test.go @@ -213,16 +213,12 @@ func testCheckAzureRMStorageQueueDestroy(s *terraform.State) error { return fmt.Errorf("Error building Queues Client: %s", err) } - metaData, err := queueClient.GetMetaData(ctx, accountName, name) + props, err := queueClient.GetMetaData(ctx, accountName, name) if err != nil { - if utils.ResponseWasNotFound(metaData.Response) { - return nil - } - - return fmt.Errorf("Unexpected error getting MetaData for Queue %q: %s", name, err) + return nil } - return fmt.Errorf("Bad: Storage Queue %q (storage account: %q) still exists", name, accountName) + return fmt.Errorf("Queue still exists: %+v", props) } return nil diff --git a/azurerm/resource_arm_storage_share_directory_test.go b/azurerm/resource_arm_storage_share_directory_test.go index 32e37bd52e5b..18d91692e32b 100644 --- a/azurerm/resource_arm_storage_share_directory_test.go +++ b/azurerm/resource_arm_storage_share_directory_test.go @@ -249,12 +249,10 @@ func testCheckAzureRMStorageShareDirectoryDestroy(s *terraform.State) error { resp, err := client.Get(ctx, accountName, shareName, name) if err != nil { - return fmt.Errorf("Bad: Get on FileShareDirectoriesClient: %+v", err) + return nil } - if resp.StatusCode != http.StatusNotFound { - return fmt.Errorf("File Share still exists:\n%#v", resp) - } + return fmt.Errorf("File Share still exists:\n%#v", resp) } return nil diff --git a/azurerm/resource_arm_storage_share_test.go b/azurerm/resource_arm_storage_share_test.go index 7def22abdf9f..5072be258c4e 100644 --- a/azurerm/resource_arm_storage_share_test.go +++ b/azurerm/resource_arm_storage_share_test.go @@ -10,7 +10,6 @@ import ( "github.com/hashicorp/terraform/terraform" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) func TestAccAzureRMStorageShare_basic(t *testing.T) { @@ -289,14 +288,10 @@ func testCheckAzureRMStorageShareDestroy(s *terraform.State) error { props, err := client.GetProperties(ctx, accountName, shareName) if err != nil { - if utils.ResponseWasNotFound(props.Response) { - return nil - } - - return fmt.Errorf("Error retrieving Share %q: %s", shareName, accountName) + return nil } - return fmt.Errorf("Bad: Share %q (storage account: %q) still exists", shareName, accountName) + return fmt.Errorf("Share still exists: %+v", props) } return nil diff --git a/azurerm/resource_arm_storage_table_test.go b/azurerm/resource_arm_storage_table_test.go index b1e44a0fa842..1970840d6dc3 100644 --- a/azurerm/resource_arm_storage_table_test.go +++ b/azurerm/resource_arm_storage_table_test.go @@ -240,14 +240,10 @@ func testCheckAzureRMStorageTableDestroy(s *terraform.State) error { props, err := client.Exists(ctx, accountName, tableName) if err != nil { - if utils.ResponseWasNotFound(props) { - return nil - } - - return fmt.Errorf("Error retrieving Table %q: %s", tableName, accountName) + return nil } - return fmt.Errorf("Bad: Table %q (storage account: %q) still exists", tableName, accountName) + return fmt.Errorf("Table still exists: %+v", props) } return nil