github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/builtin/providers/azurerm/resource_arm_storage_container_test.go (about) 1 package azurerm 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/Azure/azure-sdk-for-go/storage" 9 "github.com/hashicorp/terraform/helper/acctest" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAzureRMStorageContainer_basic(t *testing.T) { 15 ri := acctest.RandInt() 16 rs := strings.ToLower(acctest.RandString(11)) 17 config := fmt.Sprintf(testAccAzureRMStorageContainer_basic, ri, rs) 18 19 resource.Test(t, resource.TestCase{ 20 PreCheck: func() { testAccPreCheck(t) }, 21 Providers: testAccProviders, 22 CheckDestroy: testCheckAzureRMStorageContainerDestroy, 23 Steps: []resource.TestStep{ 24 resource.TestStep{ 25 Config: config, 26 Check: resource.ComposeTestCheckFunc( 27 testCheckAzureRMStorageContainerExists("azurerm_storage_container.test"), 28 ), 29 }, 30 }, 31 }) 32 } 33 34 func testCheckAzureRMStorageContainerExists(name string) resource.TestCheckFunc { 35 return func(s *terraform.State) error { 36 37 rs, ok := s.RootModule().Resources[name] 38 if !ok { 39 return fmt.Errorf("Not found: %s", name) 40 } 41 42 name := rs.Primary.Attributes["name"] 43 storageAccountName := rs.Primary.Attributes["storage_account_name"] 44 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 45 if !hasResourceGroup { 46 return fmt.Errorf("Bad: no resource group found in state for storage container: %s", name) 47 } 48 49 armClient := testAccProvider.Meta().(*ArmClient) 50 blobClient, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName) 51 if err != nil { 52 return err 53 } 54 55 containers, err := blobClient.ListContainers(storage.ListContainersParameters{ 56 Prefix: name, 57 Timeout: 90, 58 }) 59 60 if len(containers.Containers) == 0 { 61 return fmt.Errorf("Bad: Storage Container %q (storage account: %q) does not exist", name, storageAccountName) 62 } 63 64 var found bool 65 for _, container := range containers.Containers { 66 if container.Name == name { 67 found = true 68 } 69 } 70 71 if !found { 72 return fmt.Errorf("Bad: Storage Container %q (storage account: %q) does not exist", name, storageAccountName) 73 } 74 75 return nil 76 } 77 } 78 79 func testCheckAzureRMStorageContainerDestroy(s *terraform.State) error { 80 for _, rs := range s.RootModule().Resources { 81 if rs.Type != "azurerm_storage_container" { 82 continue 83 } 84 85 name := rs.Primary.Attributes["name"] 86 storageAccountName := rs.Primary.Attributes["storage_account_name"] 87 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 88 if !hasResourceGroup { 89 return fmt.Errorf("Bad: no resource group found in state for storage container: %s", name) 90 } 91 92 armClient := testAccProvider.Meta().(*ArmClient) 93 blobClient, err := armClient.getBlobStorageClientForStorageAccount(resourceGroup, storageAccountName) 94 if err != nil { 95 //If we can't get keys then the blob can't exist 96 return nil 97 } 98 99 containers, err := blobClient.ListContainers(storage.ListContainersParameters{ 100 Prefix: name, 101 Timeout: 90, 102 }) 103 104 if err != nil { 105 return nil 106 } 107 108 var found bool 109 for _, container := range containers.Containers { 110 if container.Name == name { 111 found = true 112 } 113 } 114 115 if found { 116 return fmt.Errorf("Bad: Storage Container %q (storage account: %q) still exist", name, storageAccountName) 117 } 118 } 119 120 return nil 121 } 122 123 var testAccAzureRMStorageContainer_basic = ` 124 resource "azurerm_resource_group" "test" { 125 name = "acctestrg-%d" 126 location = "westus" 127 } 128 129 resource "azurerm_storage_account" "test" { 130 name = "acctestacc%s" 131 resource_group_name = "${azurerm_resource_group.test.name}" 132 location = "westus" 133 account_type = "Standard_LRS" 134 135 tags { 136 environment = "staging" 137 } 138 } 139 140 resource "azurerm_storage_container" "test" { 141 name = "vhds" 142 resource_group_name = "${azurerm_resource_group.test.name}" 143 storage_account_name = "${azurerm_storage_account.test.name}" 144 container_access_type = "private" 145 } 146 `