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

r/storage_blob: switching to use the new SDK for assertions #4241

Merged
merged 6 commits into from
Sep 5, 2019
Merged
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
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 @@ -421,7 +422,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 @@ -515,30 +516,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 @@ -553,85 +558,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 @@ -644,34 +656,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