2015-06-05 16:12:21 +02:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccAzureStorageBlockBlob(t *testing.T) {
|
|
|
|
name := "azure_storage_blob.foo"
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
2015-06-11 20:26:49 +02:00
|
|
|
CheckDestroy: testAccCheckAzureStorageBlobDeleted("block"),
|
2015-06-05 16:12:21 +02:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAzureStorageBlockBlobConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2015-06-11 20:26:49 +02:00
|
|
|
testAccCheckAzureStorageBlobExists(name, "block"),
|
2015-06-05 16:12:21 +02:00
|
|
|
resource.TestCheckResourceAttr(name, "name", "tftesting-blob"),
|
|
|
|
resource.TestCheckResourceAttr(name, "type", "BlockBlob"),
|
2015-06-11 20:26:49 +02:00
|
|
|
resource.TestCheckResourceAttr(name, "storage_container_name",
|
|
|
|
fmt.Sprintf("%s-block", testAccStorageContainerName)),
|
2015-06-05 16:12:21 +02:00
|
|
|
resource.TestCheckResourceAttr(name, "storage_service_name", testAccStorageServiceName),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAzureStoragePageBlob(t *testing.T) {
|
|
|
|
name := "azure_storage_blob.foo"
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
2015-06-11 20:26:49 +02:00
|
|
|
CheckDestroy: testAccCheckAzureStorageBlobDeleted("page"),
|
2015-06-05 16:12:21 +02:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAzureStoragePageBlobConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2015-06-11 20:26:49 +02:00
|
|
|
testAccCheckAzureStorageBlobExists(name, "page"),
|
2015-06-05 16:12:21 +02:00
|
|
|
resource.TestCheckResourceAttr(name, "name", "tftesting-blob"),
|
|
|
|
resource.TestCheckResourceAttr(name, "type", "PageBlob"),
|
|
|
|
resource.TestCheckResourceAttr(name, "size", "512"),
|
2015-06-11 20:26:49 +02:00
|
|
|
resource.TestCheckResourceAttr(name, "storage_container_name",
|
|
|
|
fmt.Sprintf("%s-page", testAccStorageContainerName)),
|
2015-06-05 16:12:21 +02:00
|
|
|
resource.TestCheckResourceAttr(name, "storage_service_name", testAccStorageServiceName),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-06-11 20:26:49 +02:00
|
|
|
func testAccCheckAzureStorageBlobExists(name, typ string) resource.TestCheckFunc {
|
2015-06-05 16:12:21 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
resource, ok := s.RootModule().Resources[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Azure Storage Container resource not found: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resource.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("Azure Storage Container ID not set: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
mgmtClient := testAccProvider.Meta().(*Client).mgmtClient
|
|
|
|
blobClient, err := getStorageServiceBlobClient(mgmtClient, testAccStorageServiceName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-11 20:26:49 +02:00
|
|
|
exists, err := blobClient.BlobExists(fmt.Sprintf("%s-%s", testAccStorageContainerName, typ),
|
|
|
|
resource.Primary.ID)
|
2015-06-05 16:12:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
return fmt.Errorf("Azure Storage Blob %s doesn't exist.", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-11 20:26:49 +02:00
|
|
|
func testAccCheckAzureStorageBlobDeleted(typ string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
for _, resource := range s.RootModule().Resources {
|
|
|
|
if resource.Type != "azure_storage_blob" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
mgmtClient := testAccProvider.Meta().(*Client).mgmtClient
|
|
|
|
blobClient, err := getStorageServiceBlobClient(mgmtClient, testAccStorageServiceName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
exists, err := blobClient.BlobExists(fmt.Sprintf("%s-%s", testAccStorageContainerName,
|
|
|
|
typ), resource.Primary.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if exists {
|
|
|
|
return fmt.Errorf("Azure Storage Blob still exists.")
|
|
|
|
}
|
2015-06-05 16:12:21 +02:00
|
|
|
}
|
|
|
|
|
2015-06-11 20:26:49 +02:00
|
|
|
return nil
|
2015-06-05 16:12:21 +02:00
|
|
|
}
|
2015-06-11 20:26:49 +02:00
|
|
|
}
|
2015-06-05 16:12:21 +02:00
|
|
|
|
2015-06-11 20:26:49 +02:00
|
|
|
var testAccAzureStorageBlockBlobConfig = fmt.Sprintf(`
|
|
|
|
resource "azure_storage_container" "foo" {
|
|
|
|
name = "%s-block"
|
|
|
|
container_access_type = "blob"
|
|
|
|
# NOTE: A pre-existing Storage Service is used here so as to avoid
|
|
|
|
# the huge wait for creation of one.
|
|
|
|
storage_service_name = "%s"
|
2015-06-05 16:12:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "azure_storage_blob" "foo" {
|
|
|
|
name = "tftesting-blob"
|
|
|
|
type = "BlockBlob"
|
|
|
|
# NOTE: A pre-existing Storage Service is used here so as to avoid
|
2015-06-11 20:26:49 +02:00
|
|
|
# the huge wait for creation of one.
|
|
|
|
storage_service_name = "${azure_storage_container.foo.storage_service_name}"
|
|
|
|
storage_container_name = "${azure_storage_container.foo.name}"
|
|
|
|
}
|
|
|
|
`, testAccStorageContainerName, testAccStorageServiceName)
|
|
|
|
|
|
|
|
var testAccAzureStoragePageBlobConfig = fmt.Sprintf(`
|
|
|
|
resource "azure_storage_container" "foo" {
|
|
|
|
name = "%s-page"
|
|
|
|
container_access_type = "blob"
|
|
|
|
# NOTE: A pre-existing Storage Service is used here so as to avoid
|
2015-06-05 16:12:21 +02:00
|
|
|
# the huge wait for creation of one.
|
|
|
|
storage_service_name = "%s"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azure_storage_blob" "foo" {
|
|
|
|
name = "tftesting-blob"
|
|
|
|
type = "PageBlob"
|
|
|
|
# NOTE: A pre-existing Storage Service is used here so as to avoid
|
|
|
|
# the huge wait for creation of one.
|
2015-06-11 20:26:49 +02:00
|
|
|
storage_service_name = "${azure_storage_container.foo.storage_service_name}"
|
|
|
|
storage_container_name = "${azure_storage_container.foo.name}"
|
2015-06-05 16:12:21 +02:00
|
|
|
# NOTE: must be a multiple of 512:
|
|
|
|
size = 512
|
|
|
|
}
|
2015-06-11 20:26:49 +02:00
|
|
|
`, testAccStorageContainerName, testAccStorageServiceName)
|