Skip to content

Commit

Permalink
Merge pull request #4241 from terraform-providers/f/storage-blob-remo…
Browse files Browse the repository at this point in the history
…ving-old-sdk

r/storage_blob: switching to use the new SDK for assertions
  • Loading branch information
tombuildsstuff authored Sep 5, 2019
2 parents 6769e6c + 6e07064 commit 93231b5
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 78 deletions.
3 changes: 3 additions & 0 deletions azurerm/internal/services/storage/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
legacy "github.com/Azure/azure-sdk-for-go/storage"
)

// NOTE: we'll remove this once #4238 and #4235 have been merged to avoid conflicts

// nolint: deadcode unused
func (client *Client) LegacyBlobClient(ctx context.Context, resourceGroup, accountName string) (*legacy.BlobStorageClient, bool, error) {
accountKey, err := client.findAccountKey(ctx, resourceGroup, accountName)
if err != nil {
Expand Down
173 changes: 95 additions & 78 deletions azurerm/resource_arm_storage_blob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
"strings"
"testing"

"github.com/Azure/azure-sdk-for-go/storage"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"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"
"github.com/tombuildsstuff/giovanni/storage/2018-11-09/blob/blobs"
)

// TODO: with the new SDK: changing the Tier of Blobs. Content type for Block blobs
Expand Down Expand Up @@ -264,7 +265,7 @@ func TestAccAzureRMStorageBlob_blockFromLocalFile(t *testing.T) {
Config: testAccAzureRMStorageBlob_blockFromLocalBlob(ri, rs, location, sourceBlob.Name()),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMStorageBlobExists(resourceName),
testCheckAzureRMStorageBlobMatchesFile(resourceName, storage.BlobTypeBlock, sourceBlob.Name()),
testCheckAzureRMStorageBlobMatchesFile(resourceName, blobs.BlockBlob, sourceBlob.Name()),
),
},
{
Expand Down Expand Up @@ -475,7 +476,7 @@ func TestAccAzureRMStorageBlob_pageFromLocalFile(t *testing.T) {
Config: testAccAzureRMStorageBlob_pageFromLocalBlob(ri, rs, location, sourceBlob.Name()),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMStorageBlobExists(resourceName),
testCheckAzureRMStorageBlobMatchesFile(resourceName, storage.BlobTypePage, sourceBlob.Name()),
testCheckAzureRMStorageBlobMatchesFile(resourceName, blobs.PageBlob, sourceBlob.Name()),
),
},
{
Expand Down Expand Up @@ -569,30 +570,34 @@ func testCheckAzureRMStorageBlobExists(resourceName string) resource.TestCheckFu
return fmt.Errorf("Not found: %s", resourceName)
}

storageClient := testAccProvider.Meta().(*ArmClient).storage
ctx := testAccProvider.Meta().(*ArmClient).StopContext

name := rs.Primary.Attributes["name"]
storageAccountName := rs.Primary.Attributes["storage_account_name"]
storageContainerName := rs.Primary.Attributes["storage_container_name"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]
containerName := rs.Primary.Attributes["storage_container_name"]
accountName := rs.Primary.Attributes["storage_account_name"]

armClient := testAccProvider.Meta().(*ArmClient)
ctx := armClient.StopContext
blobClient, accountExists, err := armClient.storage.LegacyBlobClient(ctx, resourceGroup, storageAccountName)
resourceGroup, err := storageClient.FindResourceGroup(ctx, accountName)
if err != nil {
return err
return fmt.Errorf("Error locating Resource Group for Storage Blob %q (Container %q / Account %q): %s", name, containerName, accountName, err)
}
if !accountExists {
return fmt.Errorf("Bad: Storage Account %q does not exist", storageAccountName)
if resourceGroup == nil {
return fmt.Errorf("Unable to locate Resource Group for Storage Blob %q (Container %q / Account %q) - assuming removed & removing from state", name, containerName, accountName)
}

container := blobClient.GetContainerReference(storageContainerName)
blob := container.GetBlobReference(name)
exists, err := blob.Exists()
client, err := storageClient.BlobsClient(ctx, *resourceGroup, accountName)
if err != nil {
return err
return fmt.Errorf("Error building Blobs Client: %s", err)
}

if !exists {
return fmt.Errorf("Bad: Storage Blob %q (storage container: %q) does not exist", name, storageContainerName)
input := blobs.GetPropertiesInput{}
resp, err := client.GetProperties(ctx, accountName, containerName, name, input)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Bad: Blob %q (Container %q / Account %q / Resource Group %q) does not exist", name, containerName, accountName, *resourceGroup)
}

return fmt.Errorf("Bad: Get on BlobsClient: %+v", err)
}

return nil
Expand All @@ -607,85 +612,92 @@ func testCheckAzureRMStorageBlobDisappears(resourceName string) resource.TestChe
return fmt.Errorf("Not found: %s", resourceName)
}

storageClient := testAccProvider.Meta().(*ArmClient).storage
ctx := testAccProvider.Meta().(*ArmClient).StopContext

name := rs.Primary.Attributes["name"]
storageAccountName := rs.Primary.Attributes["storage_account_name"]
storageContainerName := rs.Primary.Attributes["storage_container_name"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]
containerName := rs.Primary.Attributes["storage_container_name"]
accountName := rs.Primary.Attributes["storage_account_name"]

armClient := testAccProvider.Meta().(*ArmClient)
ctx := armClient.StopContext
blobClient, accountExists, err := armClient.storage.LegacyBlobClient(ctx, resourceGroup, storageAccountName)
resourceGroup, err := storageClient.FindResourceGroup(ctx, accountName)
if err != nil {
return err
return fmt.Errorf("Error locating Resource Group for Storage Blob %q (Container %q / Account %q): %s", name, containerName, accountName, err)
}
if !accountExists {
return fmt.Errorf("Bad: Storage Account %q does not exist", storageAccountName)
if resourceGroup == nil {
return fmt.Errorf("Unable to locate Resource Group for Storage Blob %q (Container %q / Account %q) - assuming removed & removing from state", name, containerName, accountName)
}

container := blobClient.GetContainerReference(storageContainerName)
blob := container.GetBlobReference(name)
options := &storage.DeleteBlobOptions{}
_, err = blob.DeleteIfExists(options)
return err
client, err := storageClient.BlobsClient(ctx, *resourceGroup, accountName)
if err != nil {
return fmt.Errorf("Error building Blobs Client: %s", err)
}

input := blobs.DeleteInput{
DeleteSnapshots: false,
}
if _, err := client.Delete(ctx, accountName, containerName, name, input); err != nil {
return fmt.Errorf("Error deleting Blob %q (Container %q / Account %q / Resource Group %q): %s", name, containerName, accountName, *resourceGroup, err)
}

return nil
}
}

func testCheckAzureRMStorageBlobMatchesFile(resourceName string, kind storage.BlobType, filePath string) resource.TestCheckFunc {
func testCheckAzureRMStorageBlobMatchesFile(resourceName string, kind blobs.BlobType, filePath string) resource.TestCheckFunc {
return func(s *terraform.State) error {

rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Not found: %s", resourceName)
}

storageClient := testAccProvider.Meta().(*ArmClient).storage
ctx := testAccProvider.Meta().(*ArmClient).StopContext

name := rs.Primary.Attributes["name"]
storageAccountName := rs.Primary.Attributes["storage_account_name"]
storageContainerName := rs.Primary.Attributes["storage_container_name"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]
containerName := rs.Primary.Attributes["storage_container_name"]
accountName := rs.Primary.Attributes["storage_account_name"]

armClient := testAccProvider.Meta().(*ArmClient)
ctx := armClient.StopContext
blobClient, accountExists, err := armClient.storage.LegacyBlobClient(ctx, resourceGroup, storageAccountName)
resourceGroup, err := storageClient.FindResourceGroup(ctx, accountName)
if err != nil {
return err
return fmt.Errorf("Error locating Resource Group for Storage Blob %q (Container %q / Account %q): %s", name, containerName, accountName, err)
}
if !accountExists {
return fmt.Errorf("Bad: Storage Account %q does not exist", storageAccountName)
if resourceGroup == nil {
return fmt.Errorf("Unable to locate Resource Group for Storage Blob %q (Container %q / Account %q) - assuming removed & removing from state", name, containerName, accountName)
}

containerReference := blobClient.GetContainerReference(storageContainerName)
blobReference := containerReference.GetBlobReference(name)
propertyOptions := &storage.GetBlobPropertiesOptions{}
err = blobReference.GetProperties(propertyOptions)
client, err := storageClient.BlobsClient(ctx, *resourceGroup, accountName)
if err != nil {
return err
return fmt.Errorf("Error building Blobs Client: %s", err)
}

properties := blobReference.Properties

if properties.BlobType != kind {
return fmt.Errorf("Bad: blob type %q does not match expected type %q", properties.BlobType, kind)
// first check the type
getPropsInput := blobs.GetPropertiesInput{}
props, err := client.GetProperties(ctx, accountName, containerName, name, getPropsInput)
if err != nil {
return fmt.Errorf("Error retrieving Properties for Blob %q (Container %q): %s", name, containerName, err)
}

getOptions := &storage.GetBlobOptions{}
blob, err := blobReference.Get(getOptions)
if err != nil {
return err
if props.BlobType != kind {
return fmt.Errorf("Bad: blob type %q does not match expected type %q", props.BlobType, kind)
}

contents, err := ioutil.ReadAll(blob)
// then compare the content itself
getInput := blobs.GetInput{}
actualProps, err := client.Get(ctx, accountName, containerName, name, getInput)
if err != nil {
return err
return fmt.Errorf("Error retrieving Blob %q (Container %q): %s", name, containerName, err)
}
defer blob.Close()

actualContents := actualProps.Contents

// local file for comparison
expectedContents, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}

if string(contents) != string(expectedContents) {
return fmt.Errorf("Bad: Storage Blob %q (storage container: %q) does not match contents", name, storageContainerName)
if string(actualContents) != string(expectedContents) {
return fmt.Errorf("Bad: Storage Blob %q (storage container: %q) does not match contents", name, containerName)
}

return nil
Expand All @@ -698,34 +710,39 @@ func testCheckAzureRMStorageBlobDestroy(s *terraform.State) error {
continue
}

storageClient := testAccProvider.Meta().(*ArmClient).storage
ctx := testAccProvider.Meta().(*ArmClient).StopContext

name := rs.Primary.Attributes["name"]
storageAccountName := rs.Primary.Attributes["storage_account_name"]
storageContainerName := rs.Primary.Attributes["storage_container_name"]
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
if !hasResourceGroup {
return fmt.Errorf("Bad: no resource group found in state for storage blob: %s", name)
}
containerName := rs.Primary.Attributes["storage_container_name"]
accountName := rs.Primary.Attributes["storage_account_name"]

armClient := testAccProvider.Meta().(*ArmClient)
ctx := armClient.StopContext
blobClient, accountExists, err := armClient.storage.LegacyBlobClient(ctx, resourceGroup, storageAccountName)
resourceGroup, err := storageClient.FindResourceGroup(ctx, accountName)
if err != nil {
return nil
return fmt.Errorf("Error locating Resource Group for Storage Blob %q (Container %q / Account %q): %s", name, containerName, accountName, err)
}
if !accountExists {
if resourceGroup == nil {
return nil
}

container := blobClient.GetContainerReference(storageContainerName)
blob := container.GetBlobReference(name)
exists, err := blob.Exists()
client, err := storageClient.BlobsClient(ctx, *resourceGroup, accountName)
if err != nil {
return nil
return fmt.Errorf("Error building Blobs Client: %s", err)
}

input := blobs.GetPropertiesInput{}
props, err := client.GetProperties(ctx, accountName, containerName, name, input)
if err != nil {
if !utils.ResponseWasNotFound(props.Response) {
return fmt.Errorf("Error retrieving Blob %q (Container %q / Account %q): %s", name, containerName, accountName, err)
}
}

if exists {
return fmt.Errorf("Bad: Storage Blob %q (storage container: %q) still exists", name, storageContainerName)
if utils.ResponseWasNotFound(props.Response) {
return nil
}

return fmt.Errorf("Bad: Storage Blob %q (Storage Container: %q) still exists", name, containerName)
}

return nil
Expand Down

0 comments on commit 93231b5

Please sign in to comment.