Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_storage_account - Enable migration of account_kind from Storage to StorageV2 #6580

Merged
merged 3 commits into from May 14, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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_storageToV2(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_storageToV2(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