From e470ffd0be68999d308cfc2ea6e2316ce2945b3d Mon Sep 17 00:00:00 2001 From: stack72 Date: Wed, 27 Jan 2016 00:06:33 +0000 Subject: [PATCH] AzureRM storage container and blob tests and documentation --- .../azurerm/resource_arm_storage_blob.go | 19 +- .../azurerm/resource_arm_storage_blob_test.go | 208 ++++++++++++++++++ .../resource_arm_storage_container_test.go | 146 ++++++++++++ .../azurerm/r/storage_blob.html.markdown | 70 ++++++ .../azurerm/r/storage_container.html.markdown | 59 +++++ website/source/layouts/azurerm.erb | 8 + 6 files changed, 506 insertions(+), 4 deletions(-) create mode 100644 builtin/providers/azurerm/resource_arm_storage_blob_test.go create mode 100644 builtin/providers/azurerm/resource_arm_storage_container_test.go create mode 100644 website/source/docs/providers/azurerm/r/storage_blob.html.markdown create mode 100644 website/source/docs/providers/azurerm/r/storage_container.html.markdown diff --git a/builtin/providers/azurerm/resource_arm_storage_blob.go b/builtin/providers/azurerm/resource_arm_storage_blob.go index d075fe2156..5e17d915a4 100644 --- a/builtin/providers/azurerm/resource_arm_storage_blob.go +++ b/builtin/providers/azurerm/resource_arm_storage_blob.go @@ -43,10 +43,11 @@ func resourceArmStorageBlob() *schema.Resource { ValidateFunc: validateArmStorageBlobType, }, "size": &schema.Schema{ - Type: schema.TypeInt, - Optional: true, - ForceNew: true, - Default: 0, + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + Default: 0, + ValidateFunc: validateArmStorageBlobSize, }, "url": &schema.Schema{ Type: schema.TypeString, @@ -56,6 +57,16 @@ func resourceArmStorageBlob() *schema.Resource { } } +func validateArmStorageBlobSize(v interface{}, k string) (ws []string, errors []error) { + value := v.(int) + + if value%512 != 0 { + errors = append(errors, fmt.Errorf("Blob Size %q is invalid, must be a multiple of 512", value)) + } + + return +} + func validateArmStorageBlobType(v interface{}, k string) (ws []string, errors []error) { value := strings.ToLower(v.(string)) validTypes := map[string]struct{}{ diff --git a/builtin/providers/azurerm/resource_arm_storage_blob_test.go b/builtin/providers/azurerm/resource_arm_storage_blob_test.go new file mode 100644 index 0000000000..57ef9af01a --- /dev/null +++ b/builtin/providers/azurerm/resource_arm_storage_blob_test.go @@ -0,0 +1,208 @@ +package azurerm + +import ( + "fmt" + "testing" + + "strings" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestResourceAzureRMStorageBlobType_validation(t *testing.T) { + cases := []struct { + Value string + ErrCount int + }{ + { + Value: "unknown", + ErrCount: 1, + }, + { + Value: "page", + ErrCount: 0, + }, + { + Value: "blob", + ErrCount: 0, + }, + { + Value: "BLOB", + ErrCount: 0, + }, + { + Value: "Blob", + ErrCount: 0, + }, + } + + for _, tc := range cases { + _, errors := validateArmStorageBlobType(tc.Value, "azurerm_storage_blob") + + if len(errors) != tc.ErrCount { + t.Fatalf("Expected the Azure RM Storage Blob type to trigger a validation error") + } + } +} + +func TestResourceAzureRMStorageBlobSize_validation(t *testing.T) { + cases := []struct { + Value int + ErrCount int + }{ + { + Value: 511, + ErrCount: 1, + }, + { + Value: 512, + ErrCount: 0, + }, + { + Value: 1024, + ErrCount: 0, + }, + { + Value: 2048, + ErrCount: 0, + }, + { + Value: 5120, + ErrCount: 0, + }, + } + + for _, tc := range cases { + _, errors := validateArmStorageBlobSize(tc.Value, "azurerm_storage_blob") + + if len(errors) != tc.ErrCount { + t.Fatalf("Expected the Azure RM Storage Blob size to trigger a validation error") + } + } +} + +func TestAccAzureRMStorageBlob_basic(t *testing.T) { + ri := acctest.RandInt() + rs := strings.ToLower(acctest.RandString(11)) + config := fmt.Sprintf(testAccAzureRMStorageBlob_basic, ri, rs) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMStorageBlobDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMStorageBlobExists("azurerm_storage_blob.test"), + ), + }, + }, + }) +} + +func testCheckAzureRMStorageBlobExists(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + + rs, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("Not found: %s", name) + } + + 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) + } + + armClient := testAccProvider.Meta().(*ArmClient) + blobClient, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName) + if err != nil { + return err + } + + exists, err := blobClient.BlobExists(storageContainerName, name) + if err != nil { + return err + } + + if !exists { + return fmt.Errorf("Bad: Storage Blob %q (storage container: %q) does not exist", name, storageContainerName) + } + + return nil + } +} + +func testCheckAzureRMStorageBlobDestroy(s *terraform.State) error { + for _, rs := range s.RootModule().Resources { + if rs.Type != "azurerm_storage_blob" { + continue + } + + 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) + } + + armClient := testAccProvider.Meta().(*ArmClient) + blobClient, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName) + if err != nil { + return nil + } + + exists, err := blobClient.BlobExists(storageContainerName, name) + if err != nil { + return nil + } + + if exists { + return fmt.Errorf("Bad: Storage Blob %q (storage container: %q) still exists", name, storageContainerName) + } + } + + return nil +} + +var testAccAzureRMStorageBlob_basic = ` +resource "azurerm_resource_group" "test" { + name = "acctestrg-%d" + location = "westus" +} + +resource "azurerm_storage_account" "test" { + name = "acctestacc%s" + resource_group_name = "${azurerm_resource_group.test.name}" + location = "westus" + account_type = "Standard_LRS" + + tags { + environment = "staging" + } +} + +resource "azurerm_storage_container" "test" { + name = "vhds" + resource_group_name = "${azurerm_resource_group.test.name}" + storage_account_name = "${azurerm_storage_account.test.name}" + container_access_type = "private" +} + +resource "azurerm_storage_blob" "test" { + name = "herpderp1.vhd" + + resource_group_name = "${azurerm_resource_group.test.name}" + storage_account_name = "${azurerm_storage_account.test.name}" + storage_container_name = "${azurerm_storage_container.test.name}" + + type = "page" + size = 5120 +} +` diff --git a/builtin/providers/azurerm/resource_arm_storage_container_test.go b/builtin/providers/azurerm/resource_arm_storage_container_test.go new file mode 100644 index 0000000000..cd6dcb07d9 --- /dev/null +++ b/builtin/providers/azurerm/resource_arm_storage_container_test.go @@ -0,0 +1,146 @@ +package azurerm + +import ( + "fmt" + "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" +) + +func TestAccAzureRMStorageContainer_basic(t *testing.T) { + ri := acctest.RandInt() + rs := strings.ToLower(acctest.RandString(11)) + config := fmt.Sprintf(testAccAzureRMStorageContainer_basic, ri, rs) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMStorageContainerDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMStorageContainerExists("azurerm_storage_container.test"), + ), + }, + }, + }) +} + +func testCheckAzureRMStorageContainerExists(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + + rs, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("Not found: %s", name) + } + + name := rs.Primary.Attributes["name"] + storageAccountName := rs.Primary.Attributes["storage_account_name"] + resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] + if !hasResourceGroup { + return fmt.Errorf("Bad: no resource group found in state for storage container: %s", name) + } + + armClient := testAccProvider.Meta().(*ArmClient) + blobClient, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName) + if err != nil { + return err + } + + containers, err := blobClient.ListContainers(storage.ListContainersParameters{ + Prefix: name, + Timeout: 90, + }) + + if len(containers.Containers) == 0 { + return fmt.Errorf("Bad: Storage Container %q (storage account: %q) does not exist", name, storageAccountName) + } + + var found bool + for _, container := range containers.Containers { + if container.Name == name { + found = true + } + } + + if !found { + return fmt.Errorf("Bad: Storage Container %q (storage account: %q) does not exist", name, storageAccountName) + } + + return nil + } +} + +func testCheckAzureRMStorageContainerDestroy(s *terraform.State) error { + for _, rs := range s.RootModule().Resources { + if rs.Type != "azurerm_storage_container" { + continue + } + + name := rs.Primary.Attributes["name"] + storageAccountName := rs.Primary.Attributes["storage_account_name"] + resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] + if !hasResourceGroup { + return fmt.Errorf("Bad: no resource group found in state for storage container: %s", name) + } + + armClient := testAccProvider.Meta().(*ArmClient) + blobClient, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName) + if err != nil { + //If we can't get keys then the blob can't exist + return nil + } + + containers, err := blobClient.ListContainers(storage.ListContainersParameters{ + Prefix: name, + Timeout: 90, + }) + + if err != nil { + return nil + } + + var found bool + for _, container := range containers.Containers { + if container.Name == name { + found = true + } + } + + if found { + return fmt.Errorf("Bad: Storage Container %q (storage account: %q) still exist", name, storageAccountName) + } + } + + return nil +} + +var testAccAzureRMStorageContainer_basic = ` +resource "azurerm_resource_group" "test" { + name = "acctestrg-%d" + location = "westus" +} + +resource "azurerm_storage_account" "test" { + name = "acctestacc%s" + resource_group_name = "${azurerm_resource_group.test.name}" + location = "westus" + account_type = "Standard_LRS" + + tags { + environment = "staging" + } +} + +resource "azurerm_storage_container" "test" { + name = "vhds" + resource_group_name = "${azurerm_resource_group.test.name}" + storage_account_name = "${azurerm_storage_account.test.name}" + container_access_type = "private" +} +` diff --git a/website/source/docs/providers/azurerm/r/storage_blob.html.markdown b/website/source/docs/providers/azurerm/r/storage_blob.html.markdown new file mode 100644 index 0000000000..c216c0633f --- /dev/null +++ b/website/source/docs/providers/azurerm/r/storage_blob.html.markdown @@ -0,0 +1,70 @@ +--- +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_storage_blob" +sidebar_current: "docs-azurerm-resource-storage-blob" +description: |- + Create a Azure Storage Blob. +--- + +# azurerm\_storage\_blob + +Create an Azure Storage Blob. + +## Example Usage + +``` +resource "azurerm_resource_group" "test" { + name = "acctestrg-%d" + location = "westus" +} + +resource "azurerm_storage_account" "test" { + name = "acctestacc%s" + resource_group_name = "${azurerm_resource_group.test.name}" + location = "westus" + account_type = "Standard_LRS" +} + +resource "azurerm_storage_container" "test" { + name = "vhds" + resource_group_name = "${azurerm_resource_group.test.name}" + storage_account_name = "${azurerm_storage_account.test.name}" + container_access_type = "private" +} + +resource "azurerm_storage_blob" "testsb" { + name = "sample.vhd" + + resource_group_name = "${azurerm_resource_group.test.name}" + storage_account_name = "${azurerm_storage_account.test.name}" + storage_container_name = "${azurerm_storage_container.test.name}" + + type = "page" + size = 5120 +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) The name of the storage blob. Must be unique within the storage container the blob is located. + +* `resource_group_name` - (Required) The name of the resource group in which to + create the storage container. Changing this forces a new resource to be created. + +* `storage_account_name` - (Required) Specifies the storage account in which to create the storage container. + Changing this forces a new resource to be created. + +* `storage_container_name` - (Required) The name of the storage container in which this blob should be created. + +* `type` - (Required) The type of the storage blob to be created. One of either `block` or `page`. + +* `size` - (Optional) Used only for `page` blobs to specify the size in bytes of the blob to be created. Must be a multiple of 512. Defaults to 0. + +## Attributes Reference + +The following attributes are exported in addition to the arguments listed above: + +* `id` - The storage blob Resource ID. +* `url` - The URL of the blob diff --git a/website/source/docs/providers/azurerm/r/storage_container.html.markdown b/website/source/docs/providers/azurerm/r/storage_container.html.markdown new file mode 100644 index 0000000000..cb6d2c21bc --- /dev/null +++ b/website/source/docs/providers/azurerm/r/storage_container.html.markdown @@ -0,0 +1,59 @@ +--- +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_storage_container" +sidebar_current: "docs-azurerm-resource-storage-container" +description: |- + Create a Azure Storage Container. +--- + +# azurerm\_storage\_container + +Create an Azure Storage Container. + +## Example Usage + +``` +resource "azurerm_resource_group" "test" { + name = "acctestrg" + location = "westus" +} + +resource "azurerm_storage_account" "test" { + name = "accteststorageaccount" + resource_group_name = "${azurerm_resource_group.test.name}" + location = "westus" + account_type = "Standard_LRS" + + tags { + environment = "staging" + } +} + +resource "azurerm_storage_container" "test" { + name = "vhds" + resource_group_name = "${azurerm_resource_group.test.name}" + storage_account_name = "${azurerm_storage_account.test.name}" + container_access_type = "private" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) The name of the storage container. Must be unique within the storage service the container is located. + +* `resource_group_name` - (Required) The name of the resource group in which to + create the storage container. Changing this forces a new resource to be created. + +* `storage_account_name` - (Required) Specifies the storage account in which to create the storage container. + Changing this forces a new resource to be created. + +* `container_access_type` - (Required) The 'interface' for access the container provides. Can be either `blob`, `container` or `private`. + +## Attributes Reference + +The following attributes are exported in addition to the arguments listed above: + +* `id` - The storage container Resource ID. +* `properties` - Key-value definition of additional properties associated to the storage container diff --git a/website/source/layouts/azurerm.erb b/website/source/layouts/azurerm.erb index 8bf97c4d4c..b1b9d0d9ee 100644 --- a/website/source/layouts/azurerm.erb +++ b/website/source/layouts/azurerm.erb @@ -81,6 +81,14 @@ azurerm_storage_account + > + azurerm_storage_container + + + > + azurerm_storage_blob + +