2016-01-10 01:22:00 +01:00
|
|
|
package azurerm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2016-07-13 13:08:44 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2016-01-10 01:22:00 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestValidateArmStorageAccountType(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
input string
|
|
|
|
shouldError bool
|
|
|
|
}{
|
|
|
|
{"standard_lrs", false},
|
|
|
|
{"invalid", true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
_, es := validateArmStorageAccountType(test.input, "account_type")
|
|
|
|
|
|
|
|
if test.shouldError && len(es) == 0 {
|
|
|
|
t.Fatalf("Expected validating account_type %q to fail", test.input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateArmStorageAccountName(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
input string
|
|
|
|
shouldError bool
|
|
|
|
}{
|
|
|
|
{"ab", true},
|
|
|
|
{"ABC", true},
|
|
|
|
{"abc", false},
|
|
|
|
{"123456789012345678901234", false},
|
|
|
|
{"1234567890123456789012345", true},
|
|
|
|
{"abc12345", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
_, es := validateArmStorageAccountName(test.input, "name")
|
|
|
|
|
|
|
|
if test.shouldError && len(es) == 0 {
|
|
|
|
t.Fatalf("Expected validating name %q to fail", test.input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAzureRMStorageAccount_basic(t *testing.T) {
|
2016-07-13 13:08:44 +02:00
|
|
|
ri := acctest.RandInt()
|
|
|
|
rs := acctest.RandString(4)
|
|
|
|
preConfig := fmt.Sprintf(testAccAzureRMStorageAccount_basic, ri, rs)
|
|
|
|
postConfig := fmt.Sprintf(testAccAzureRMStorageAccount_update, ri, rs)
|
|
|
|
|
2016-01-10 01:22:00 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMStorageAccountDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-07-13 13:08:44 +02:00
|
|
|
Config: preConfig,
|
2016-01-10 01:22:00 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMStorageAccountExists("azurerm_storage_account.testsa"),
|
|
|
|
resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "account_type", "Standard_LRS"),
|
2016-06-10 17:07:02 +02:00
|
|
|
resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "tags.%", "1"),
|
2016-01-10 01:22:00 +01:00
|
|
|
resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "tags.environment", "production"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
2016-07-13 13:08:44 +02:00
|
|
|
Config: postConfig,
|
2016-01-10 01:22:00 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMStorageAccountExists("azurerm_storage_account.testsa"),
|
|
|
|
resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "account_type", "Standard_GRS"),
|
2016-06-10 17:07:02 +02:00
|
|
|
resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "tags.%", "1"),
|
2016-01-10 01:22:00 +01:00
|
|
|
resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "tags.environment", "staging"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCheckAzureRMStorageAccountExists(name string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
// Ensure we have enough information in state to look up in API
|
|
|
|
rs, ok := s.RootModule().Resources[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
storageAccount := rs.Primary.Attributes["name"]
|
|
|
|
resourceGroup := rs.Primary.Attributes["resource_group_name"]
|
|
|
|
|
|
|
|
// Ensure resource group exists in API
|
|
|
|
conn := testAccProvider.Meta().(*ArmClient).storageServiceClient
|
|
|
|
|
|
|
|
resp, err := conn.GetProperties(resourceGroup, storageAccount)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Bad: Get on storageServiceClient: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
return fmt.Errorf("Bad: StorageAccount %q (resource group: %q) does not exist", name, resourceGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCheckAzureRMStorageAccountDestroy(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*ArmClient).storageServiceClient
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "azurerm_storage_account" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
name := rs.Primary.Attributes["name"]
|
|
|
|
resourceGroup := rs.Primary.Attributes["resource_group_name"]
|
|
|
|
|
|
|
|
resp, err := conn.GetProperties(resourceGroup, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusNotFound {
|
|
|
|
return fmt.Errorf("Storage Account still exists:\n%#v", resp.Properties)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var testAccAzureRMStorageAccount_basic = `
|
|
|
|
resource "azurerm_resource_group" "testrg" {
|
2016-07-13 13:08:44 +02:00
|
|
|
name = "testAccAzureRMSA-%d"
|
2016-01-10 01:22:00 +01:00
|
|
|
location = "westus"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_storage_account" "testsa" {
|
2016-07-13 13:08:44 +02:00
|
|
|
name = "unlikely23exst2acct%s"
|
2016-01-10 01:22:00 +01:00
|
|
|
resource_group_name = "${azurerm_resource_group.testrg.name}"
|
|
|
|
|
|
|
|
location = "westus"
|
|
|
|
account_type = "Standard_LRS"
|
|
|
|
|
|
|
|
tags {
|
|
|
|
environment = "production"
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
var testAccAzureRMStorageAccount_update = `
|
|
|
|
resource "azurerm_resource_group" "testrg" {
|
2016-07-13 13:08:44 +02:00
|
|
|
name = "testAccAzureRMSA-%d"
|
2016-01-10 01:22:00 +01:00
|
|
|
location = "westus"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_storage_account" "testsa" {
|
2016-07-13 13:08:44 +02:00
|
|
|
name = "unlikely23exst2acct%s"
|
2016-01-10 01:22:00 +01:00
|
|
|
resource_group_name = "${azurerm_resource_group.testrg.name}"
|
|
|
|
|
|
|
|
location = "westus"
|
|
|
|
account_type = "Standard_GRS"
|
|
|
|
|
|
|
|
tags {
|
|
|
|
environment = "staging"
|
|
|
|
}
|
|
|
|
}`
|