Skip to content

Commit

Permalink
storage: always nil check resource group before deref (#3986)
Browse files Browse the repository at this point in the history
  • Loading branch information
katbyte authored Jul 31, 2019
1 parent 2193472 commit 43b848a
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 54 deletions.
17 changes: 10 additions & 7 deletions azurerm/resource_arm_storage_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ func resourceArmStorageContainerCreate(d *schema.ResourceData, meta interface{})

resourceGroup, err := storageClient.FindResourceGroup(ctx, accountName)
if err != nil {
return fmt.Errorf("Error locating Resource Group: %s", err)
return fmt.Errorf("Error locating Resource Group for Storage Container %q (Account %s): %s", containerName, accountName, err)
}
if resourceGroup == nil {
return fmt.Errorf("Unable to locate Resource Group for Storage Container %q (Account %s)", containerName, accountName)
}

client, err := storageClient.ContainersClient(ctx, *resourceGroup, accountName)
Expand Down Expand Up @@ -135,10 +138,10 @@ func resourceArmStorageContainerUpdate(d *schema.ResourceData, meta interface{})

resourceGroup, err := storageClient.FindResourceGroup(ctx, id.AccountName)
if err != nil {
return fmt.Errorf("Error locating Resource Group for Storage Account %q: %s", id.AccountName, err)
return fmt.Errorf("Error locating Resource Group for Storage Container %q (Account %s): %s", id.ContainerName, id.AccountName, err)
}
if resourceGroup == nil {
log.Printf("[DEBUG] Unable to locate Resource Group for Storage Account %q - assuming removed & removing from state", id.AccountName)
log.Printf("[DEBUG] Unable to locate Resource Group for Storage Container %q (Account %s) - assuming removed & removing from state", id.ContainerName, id.AccountName)
d.SetId("")
return nil
}
Expand Down Expand Up @@ -184,10 +187,10 @@ func resourceArmStorageContainerRead(d *schema.ResourceData, meta interface{}) e

resourceGroup, err := storageClient.FindResourceGroup(ctx, id.AccountName)
if err != nil {
return fmt.Errorf("Error locating Resource Group for Storage Account %q: %s", id.AccountName, err)
return fmt.Errorf("Error locating Resource Group for Storage Container %q (Account %s): %s", id.ContainerName, id.AccountName, err)
}
if resourceGroup == nil {
log.Printf("[DEBUG] Unable to locate Resource Group for Storage Account %q - assuming removed & removing from state", id.AccountName)
log.Printf("[DEBUG] Unable to locate Resource Group for Storage Container %q (Account %s) - assuming removed & removing from state", id.ContainerName, id.AccountName)
d.SetId("")
return nil
}
Expand Down Expand Up @@ -239,10 +242,10 @@ func resourceArmStorageContainerDelete(d *schema.ResourceData, meta interface{})

resourceGroup, err := storageClient.FindResourceGroup(ctx, id.AccountName)
if err != nil {
return fmt.Errorf("Error locating Resource Group for Storage Account %q: %s", id.AccountName, err)
return fmt.Errorf("Error locating Resource Group for Storage Container %q (Account %s): %s", id.ContainerName, id.AccountName, err)
}
if resourceGroup == nil {
log.Printf("[DEBUG] Unable to locate Resource Group for Storage Account %q - assuming removed & removing from state", id.AccountName)
log.Printf("[DEBUG] Unable to locate Resource Group for Storage Container %q (Account %s) - assuming removed & removing from state", id.ContainerName, id.AccountName)
d.SetId("")
return nil
}
Expand Down
12 changes: 9 additions & 3 deletions azurerm/resource_arm_storage_container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,10 @@ func testCheckAzureRMStorageContainerExists(resourceName string) resource.TestCh

resourceGroup, err := storageClient.FindResourceGroup(ctx, accountName)
if err != nil {
return fmt.Errorf("Error finding Resource Group: %s", err)
return fmt.Errorf("Error locating Resource Group for Storage Container %q (Account %s): %s", containerName, accountName, err)
}
if resourceGroup == nil {
return fmt.Errorf("Unable to locate Resource Group for Storage Container %q (Account %s) - assuming removed & removing from state", containerName, accountName)
}

client, err := storageClient.ContainersClient(ctx, *resourceGroup, accountName)
Expand Down Expand Up @@ -285,7 +288,10 @@ func testAccARMStorageContainerDisappears(resourceName string) resource.TestChec

resourceGroup, err := storageClient.FindResourceGroup(ctx, accountName)
if err != nil {
return fmt.Errorf("Error finding Resource Group: %s", err)
return fmt.Errorf("Error locating Resource Group for Storage Container %q (Account %s): %s", containerName, accountName, err)
}
if resourceGroup == nil {
return fmt.Errorf("Unable to locate Resource Group for Storage Container %q (Account %s) - assuming removed & removing from state", containerName, accountName)
}

client, err := storageClient.ContainersClient(ctx, *resourceGroup, accountName)
Expand Down Expand Up @@ -315,7 +321,7 @@ func testCheckAzureRMStorageContainerDestroy(s *terraform.State) error {

resourceGroup, err := storageClient.FindResourceGroup(ctx, accountName)
if err != nil {
return fmt.Errorf("Error finding Resource Group: %s", err)
return fmt.Errorf("Error locating Resource Group for Storage Container %q (Account %s): %s", containerName, accountName, err)
}

if resourceGroup == nil {
Expand Down
10 changes: 4 additions & 6 deletions azurerm/resource_arm_storage_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,10 @@ func resourceArmStorageQueueRead(d *schema.ResourceData, meta interface{}) error

resourceGroup, err := storageClient.FindResourceGroup(ctx, id.AccountName)
if err != nil {
return fmt.Errorf("Error locating Resource Group: %s", err)
return fmt.Errorf("Error locating Resource Group for Queue Container %q (Account %s): %s", id.QueueName, id.AccountName, err)
}

if resourceGroup == nil {
log.Printf("[WARN] Unable to determine Resource Group for Storage Account %q (assuming removed) - removing from state", id.AccountName)
log.Printf("[WARN] Unable to determine Resource Group for Storage Queue %q (Account %s) - assuming removed & removing from state", id.QueueName, id.AccountName)
d.SetId("")
return nil
}
Expand Down Expand Up @@ -181,11 +180,10 @@ func resourceArmStorageQueueDelete(d *schema.ResourceData, meta interface{}) err

resourceGroup, err := storageClient.FindResourceGroup(ctx, id.AccountName)
if err != nil {
return fmt.Errorf("Error locating Resource Group: %s", err)
return fmt.Errorf("Error locating Resource Group for Storage Queue %q (Account %s): %s", id.QueueName, id.AccountName, err)
}

if resourceGroup == nil {
log.Printf("[WARN] Unable to determine Resource Group for Storage Account %q (assuming removed) - removing from state", id.AccountName)
log.Printf("[WARN] Unable to determine Resource Group for Storage Queue %q (Account %s) - assuming removed & removing from state", id.QueueName, id.AccountName)
d.SetId("")
return nil
}
Expand Down
17 changes: 10 additions & 7 deletions azurerm/resource_arm_storage_share.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ func resourceArmStorageShareCreate(d *schema.ResourceData, meta interface{}) err

resourceGroup, err := storageClient.FindResourceGroup(ctx, accountName)
if err != nil {
return fmt.Errorf("Error locating Resource Group: %s", err)
return fmt.Errorf("Error locating Resource Group for Storage Share %q (Account %s): %s", shareName, accountName, err)
}
if resourceGroup == nil {
return fmt.Errorf("Unable to locate Resource Group for Storage Share %q (Account %s) - assuming removed & removing from state", shareName, accountName)
}

client, err := storageClient.FileSharesClient(ctx, *resourceGroup, accountName)
Expand Down Expand Up @@ -175,10 +178,10 @@ func resourceArmStorageShareRead(d *schema.ResourceData, meta interface{}) error

resourceGroup, err := storageClient.FindResourceGroup(ctx, id.AccountName)
if err != nil {
return fmt.Errorf("Error locating Resource Group for Storage Account %q: %s", id.AccountName, err)
return fmt.Errorf("Error locating Resource Group for Storage Share %q (Account %s): %s", id.ShareName, id.AccountName, err)
}
if resourceGroup == nil {
log.Printf("[DEBUG] Unable to locate Resource Group for Storage Account %q - assuming removed & removing from state", id.AccountName)
log.Printf("[WARN] Unable to determine Resource Group for Storage Share %q (Account %s) - assuming removed & removing from state", id.ShareName, id.AccountName)
d.SetId("")
return nil
}
Expand Down Expand Up @@ -234,10 +237,10 @@ func resourceArmStorageShareUpdate(d *schema.ResourceData, meta interface{}) err

resourceGroup, err := storageClient.FindResourceGroup(ctx, id.AccountName)
if err != nil {
return fmt.Errorf("Error locating Resource Group for Storage Account %q: %s", id.AccountName, err)
return fmt.Errorf("Error locating Resource Group for Storage Share %q (Account %s): %s", id.ShareName, id.AccountName, err)
}
if resourceGroup == nil {
log.Printf("[DEBUG] Unable to locate Resource Group for Storage Account %q - assuming removed & removing from state", id.AccountName)
log.Printf("[WARN] Unable to determine Resource Group for Storage Share %q (Account %s) - assuming removed & removing from state", id.ShareName, id.AccountName)
d.SetId("")
return nil
}
Expand Down Expand Up @@ -297,10 +300,10 @@ func resourceArmStorageShareDelete(d *schema.ResourceData, meta interface{}) err

resourceGroup, err := storageClient.FindResourceGroup(ctx, id.AccountName)
if err != nil {
return fmt.Errorf("Error locating Resource Group for Storage Account %q: %s", id.AccountName, err)
return fmt.Errorf("Error locating Resource Group for Storage Share %q (Account %s): %s", id.ShareName, id.AccountName, err)
}
if resourceGroup == nil {
log.Printf("[DEBUG] Unable to locate Resource Group for Storage Account %q - assuming removed & removing from state", id.AccountName)
log.Printf("[WARN] Unable to determine Resource Group for Storage Share %q (Account %s) - assuming removed & removing from state", id.ShareName, id.AccountName)
d.SetId("")
return nil
}
Expand Down
20 changes: 14 additions & 6 deletions azurerm/resource_arm_storage_share_directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ func resourceArmStorageShareDirectoryCreate(d *schema.ResourceData, meta interfa

resourceGroup, err := storageClient.FindResourceGroup(ctx, accountName)
if err != nil {
return fmt.Errorf("Error locating Resource Group: %s", err)
return fmt.Errorf("Error locating Resource Group for Storage Share Directory %q (Share %s, Account %s): %s", directoryName, shareName, accountName, err)
}
if resourceGroup == nil {
return fmt.Errorf("Unable to locate Resource Group for Storage Share Directory %q (Share %s, Account %s) ", directoryName, shareName, accountName)
}

client, err := storageClient.FileShareDirectoriesClient(ctx, *resourceGroup, accountName)
Expand Down Expand Up @@ -106,7 +109,12 @@ func resourceArmStorageShareDirectoryUpdate(d *schema.ResourceData, meta interfa

resourceGroup, err := storageClient.FindResourceGroup(ctx, id.AccountName)
if err != nil {
return fmt.Errorf("Error locating Resource Group: %s", err)
return fmt.Errorf("Error locating Resource Group for Storage Share Directory %q (Share %s, Account %s): %s", id.DirectoryName, id.ShareName, id.AccountName, err)
}
if resourceGroup == nil {
log.Printf("[WARN] Unable to determine Resource Group for Storage Share Directory %q (Share %s, Account %s) - assuming removed & removing from state", id.DirectoryName, id.ShareName, id.AccountName)
d.SetId("")
return nil
}

client, err := storageClient.FileShareDirectoriesClient(ctx, *resourceGroup, id.AccountName)
Expand All @@ -132,10 +140,10 @@ func resourceArmStorageShareDirectoryRead(d *schema.ResourceData, meta interface

resourceGroup, err := storageClient.FindResourceGroup(ctx, id.AccountName)
if err != nil {
return fmt.Errorf("Error locating Resource Group for Storage Account %q: %s", id.AccountName, err)
return fmt.Errorf("Error locating Resource Group for Storage Share Directory %q (Share %s, Account %s): %s", id.DirectoryName, id.ShareName, id.AccountName, err)
}
if resourceGroup == nil {
log.Printf("[DEBUG] Unable to locate Resource Group for Storage Account %q - assuming removed & removing from state", id.AccountName)
log.Printf("[WARN] Unable to determine Resource Group for Storage Share Directory %q (Share %s, Account %s) - assuming removed & removing from state", id.DirectoryName, id.ShareName, id.AccountName)
d.SetId("")
return nil
}
Expand Down Expand Up @@ -172,10 +180,10 @@ func resourceArmStorageShareDirectoryDelete(d *schema.ResourceData, meta interfa

resourceGroup, err := storageClient.FindResourceGroup(ctx, id.AccountName)
if err != nil {
return fmt.Errorf("Error locating Resource Group for Storage Account %q: %s", id.AccountName, err)
return fmt.Errorf("Error locating Resource Group for Storage Share Directory %q (Share %s, Account %s): %s", id.DirectoryName, id.ShareName, id.AccountName, err)
}
if resourceGroup == nil {
log.Printf("[DEBUG] Unable to locate Resource Group for Storage Account %q - assuming removed already", id.AccountName)
log.Printf("[WARN] Unable to determine Resource Group for Storage Share Directory %q (Share %s, Account %s) - assuming removed already", id.DirectoryName, id.ShareName, id.AccountName)
d.SetId("")
return nil
}
Expand Down
7 changes: 5 additions & 2 deletions azurerm/resource_arm_storage_share_directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ func testCheckAzureRMStorageShareDirectoryExists(resourceName string) resource.T

resourceGroup, err := storageClient.FindResourceGroup(ctx, accountName)
if err != nil {
return fmt.Errorf("Error finding Resource Group: %s", err)
return fmt.Errorf("Error locating Resource Group for Storage Share Directory %q (Share %s, Account %s): %s", name, shareName, accountName, err)
}
if resourceGroup == nil {
return fmt.Errorf("Unable to locate Resource Group for Storage Share Directory %q (Share %s, Account %s) ", name, shareName, accountName)
}

client, err := storageClient.FileShareDirectoriesClient(ctx, *resourceGroup, accountName)
Expand Down Expand Up @@ -204,7 +207,7 @@ func testCheckAzureRMStorageShareDirectoryDestroy(s *terraform.State) error {

resourceGroup, err := storageClient.FindResourceGroup(ctx, accountName)
if err != nil {
return fmt.Errorf("Error finding Resource Group: %s", err)
return fmt.Errorf("Error locating Resource Group for Storage Share Directory %q (Share %s, Account %s): %s", name, shareName, accountName, err)
}

// not found, the account's gone
Expand Down
12 changes: 9 additions & 3 deletions azurerm/resource_arm_storage_share_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,10 @@ func testCheckAzureRMStorageShareExists(resourceName string) resource.TestCheckF

resourceGroup, err := storageClient.FindResourceGroup(ctx, accountName)
if err != nil {
return fmt.Errorf("Error finding Resource Group: %s", err)
return fmt.Errorf("Error locating Resource Group for Storage Share %q (Account %s): %s", shareName, accountName, err)
}
if resourceGroup == nil {
return fmt.Errorf("Unable to locate Resource Group for Storage Share %q (Account %s) - assuming removed & removing from state", shareName, accountName)
}

client, err := storageClient.FileSharesClient(ctx, *resourceGroup, accountName)
Expand Down Expand Up @@ -239,7 +242,10 @@ func testCheckAzureRMStorageShareDisappears(resourceName string) resource.TestCh

resourceGroup, err := storageClient.FindResourceGroup(ctx, accountName)
if err != nil {
return fmt.Errorf("Error finding Resource Group: %s", err)
return fmt.Errorf("Error locating Resource Group for Storage Share %q (Account %s): %s", shareName, accountName, err)
}
if resourceGroup == nil {
return fmt.Errorf("Unable to locate Resource Group for Storage Share %q (Account %s) - assuming removed & removing from state", shareName, accountName)
}

client, err := storageClient.FileSharesClient(ctx, *resourceGroup, accountName)
Expand Down Expand Up @@ -269,7 +275,7 @@ func testCheckAzureRMStorageShareDestroy(s *terraform.State) error {

resourceGroup, err := storageClient.FindResourceGroup(ctx, accountName)
if err != nil {
return fmt.Errorf("Error finding Resource Group: %s", err)
return fmt.Errorf("Error locating Resource Group for Storage Share %q (Account %s): %s", shareName, accountName, err)
}
if resourceGroup == nil {
return nil
Expand Down
17 changes: 10 additions & 7 deletions azurerm/resource_arm_storage_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ func resourceArmStorageTableCreate(d *schema.ResourceData, meta interface{}) err

resourceGroup, err := storageClient.FindResourceGroup(ctx, accountName)
if err != nil {
return fmt.Errorf("Error locating Resource Group: %s", err)
return fmt.Errorf("Error locating Resource Group for Storage Table %q (Account %s): %s", tableName, accountName, err)
}
if resourceGroup == nil {
return fmt.Errorf("Unable to locate Resource Group for Storage Share %q (Account %s) - assuming removed & removing from state", tableName, accountName)
}

client, err := storageClient.TablesClient(ctx, *resourceGroup, accountName)
Expand Down Expand Up @@ -153,11 +156,11 @@ func resourceArmStorageTableRead(d *schema.ResourceData, meta interface{}) error

resourceGroup, err := storageClient.FindResourceGroup(ctx, id.AccountName)
if err != nil {
return fmt.Errorf("Error locating Resource Group: %s", err)
return fmt.Errorf("Error locating Resource Group for Storage Table %q (Account %s): %s", id.TableName, id.AccountName, err)
}

if resourceGroup == nil {
log.Printf("Unable to determine Resource Group for Storage Account %q (assuming removed)", id.AccountName)
log.Printf("Unable to determine Resource Group for Storage Storage Table %q (Account %s) - assuming removed & removing from state", id.TableName, id.AccountName)
d.SetId("")
return nil
}
Expand Down Expand Up @@ -205,11 +208,11 @@ func resourceArmStorageTableDelete(d *schema.ResourceData, meta interface{}) err

resourceGroup, err := storageClient.FindResourceGroup(ctx, id.AccountName)
if err != nil {
return fmt.Errorf("Error locating Resource Group: %s", err)
return fmt.Errorf("Error locating Resource Group for Storage Table %q (Account %s): %s", id.TableName, id.AccountName, err)
}

if resourceGroup == nil {
log.Printf("Unable to determine Resource Group for Storage Account %q (assuming removed)", id.AccountName)
log.Printf("Unable to determine Resource Group for Storage Storage Table %q (Account %s) - assuming removed & removing from state", id.TableName, id.AccountName)
return nil
}

Expand Down Expand Up @@ -237,11 +240,11 @@ func resourceArmStorageTableUpdate(d *schema.ResourceData, meta interface{}) err

resourceGroup, err := storageClient.FindResourceGroup(ctx, id.AccountName)
if err != nil {
return fmt.Errorf("Error locating Resource Group: %s", err)
return fmt.Errorf("Error locating Resource Group for Storage Table %q (Account %s): %s", id.TableName, id.AccountName, err)
}

if resourceGroup == nil {
log.Printf("Unable to determine Resource Group for Storage Account %q (assuming removed)", id.AccountName)
log.Printf("Unable to determine Resource Group for Storage Storage Table %q (Account %s) - assuming removed & removing from state", id.TableName, id.AccountName)
d.SetId("")
return nil
}
Expand Down
Loading

0 comments on commit 43b848a

Please sign in to comment.