Skip to content

Commit

Permalink
fixes #5758
Browse files Browse the repository at this point in the history
* Add resource migration for Storage Account

* Update docs to show new behavior

* Update config name to indicate it's a prepare step

Co-authored-by: Jeffrey Cline <20408400+WodansSon@users.noreply.github.com>
  • Loading branch information
HappyTobi and WodansSon committed May 14, 2020
1 parent 62ff362 commit 02c1024
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 1 deletion.
Expand Up @@ -66,7 +66,6 @@ func resourceArmStorageAccount() *schema.Resource {
"account_kind": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(storage.Storage),
string(storage.BlobStorage),
Expand Down Expand Up @@ -541,6 +540,20 @@ func resourceArmStorageAccount() *schema.Resource {
},
},
},
CustomizeDiff: func(d *schema.ResourceDiff, v interface{}) error {
if d.HasChange("account_kind") {
accountKind, changedKind := d.GetChange("account_kind")

if accountKind != string(storage.Storage) && changedKind != string(storage.StorageV2) {
log.Printf("[DEBUG] recreate storage account, could't be migrated from %s to %s", accountKind, changedKind)
d.ForceNew("account_kind")
} else {
log.Printf("[DEBUG] storage account can be upgraded from %s to %s", accountKind, changedKind)
}
}

return nil
},
}
}

Expand Down Expand Up @@ -787,6 +800,18 @@ func resourceArmStorageAccountUpdate(d *schema.ResourceData, meta interface{}) e
d.SetPartial("account_replication_type")
}

if d.HasChange("account_kind") {
opts := storage.AccountUpdateParameters{
Kind: storage.Kind(accountKind),
}

if _, err := client.Update(ctx, resourceGroupName, storageAccountName, opts); err != nil {
return fmt.Errorf("Error updating Azure Storage Account account_kind %q: %+v", storageAccountName, err)
}

d.SetPartial("access_kind")
}

if d.HasChange("access_tier") {
accessTier := d.Get("access_tier").(string)

Expand Down
Expand Up @@ -368,6 +368,33 @@ func TestAccAzureRMStorageAccount_storageV2WithUpdate(t *testing.T) {
})
}

func TestAccAzureRMStorageAccount_storageV1ToV2Update(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_storage_account", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMStorageAccountDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMStorageAccount_storageToV2Prep(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMStorageAccountExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "account_kind", "Storage"),
),
},
data.ImportStep(),
{
Config: testAccAzureRMStorageAccount_storageToV2Update(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMStorageAccountExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "account_kind", "StorageV2"),
),
},
},
})
}

func TestAccAzureRMStorageAccount_NonStandardCasing(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_storage_account", "test")

Expand Down Expand Up @@ -1129,6 +1156,60 @@ resource "azurerm_storage_account" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomString)
}

func testAccAzureRMStorageAccount_storageToV2Prep(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-storage-%d"
location = "%s"
}
resource "azurerm_storage_account" "test" {
name = "unlikely23exst2acct%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_kind = "Storage"
account_tier = "Standard"
account_replication_type = "LRS"
tags = {
environment = "production"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString)
}

func testAccAzureRMStorageAccount_storageToV2Update(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-storage-%d"
location = "%s"
}
resource "azurerm_storage_account" "test" {
name = "unlikely23exst2acct%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_kind = "StorageV2"
account_tier = "Standard"
account_replication_type = "LRS"
tags = {
environment = "production"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString)
}

func testAccAzureRMStorageAccount_nonStandardCasing(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/storage_account.html.markdown
Expand Up @@ -86,6 +86,8 @@ The following arguments are supported:

* `account_kind` - (Optional) Defines the Kind of account. Valid options are `BlobStorage`, `BlockBlobStorage`, `FileStorage`, `Storage` and `StorageV2`. Changing this forces a new resource to be created. Defaults to `StorageV2`.

-> **NOTE:** Changing the `account_kind` value from `Storage` to `StorageV2` will not trigger a force new on the storage account, it will only upgrade the existing storage account from `Storage` to `StorageV2` keeping the existing storage account in place.

* `account_tier` - (Required) Defines the Tier to use for this storage account. Valid options are `Standard` and `Premium`. For `FileStorage` accounts only `Premium` is valid. Changing this forces a new resource to be created.

* `account_replication_type` - (Required) Defines the type of replication to use for this storage account. Valid options are `LRS`, `GRS`, `RAGRS` and `ZRS`.
Expand Down

0 comments on commit 02c1024

Please sign in to comment.