Skip to content

Commit

Permalink
azurerm_storage_account - Add code check for restore_policy prere…
Browse files Browse the repository at this point in the history
…quisites (#19822)

Co-authored-by: kt <kt@katbyte.me>
Fix #19799
  • Loading branch information
magodo authored Jan 10, 2023
1 parent c00d361 commit fdeef95
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
27 changes: 22 additions & 5 deletions internal/services/storage/storage_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,10 @@ func resourceStorageAccountCreate(d *pluginsdk.ResourceData, meta interface{}) e
}
blobClient := meta.(*clients.Client).Storage.BlobServicesClient

blobProperties := expandBlobProperties(val.([]interface{}))
blobProperties, err := expandBlobProperties(val.([]interface{}))
if err != nil {
return err
}

// last_access_time_enabled and container_delete_retention_policy are not supported in USGov
// Fix issue https://github.com/hashicorp/terraform-provider-azurerm/issues/11772
Expand Down Expand Up @@ -1774,7 +1777,10 @@ func resourceStorageAccountUpdate(d *pluginsdk.ResourceData, meta interface{}) e
}

blobClient := meta.(*clients.Client).Storage.BlobServicesClient
blobProperties := expandBlobProperties(d.Get("blob_properties").([]interface{}))
blobProperties, err := expandBlobProperties(d.Get("blob_properties").([]interface{}))
if err != nil {
return err
}

// last_access_time_enabled and container_delete_retention_policy are not supported in USGov
// Fix issue https://github.com/hashicorp/terraform-provider-azurerm/issues/11772
Expand Down Expand Up @@ -2579,7 +2585,7 @@ func expandStorageAccountPrivateLinkAccess(inputs []interface{}, tenantId string
return &privateLinkAccess
}

func expandBlobProperties(input []interface{}) *storage.BlobServiceProperties {
func expandBlobProperties(input []interface{}) (*storage.BlobServiceProperties, error) {
props := storage.BlobServiceProperties{
BlobServicePropertiesProperties: &storage.BlobServicePropertiesProperties{
Cors: &storage.CorsRules{
Expand All @@ -2596,7 +2602,7 @@ func expandBlobProperties(input []interface{}) *storage.BlobServiceProperties {
}

if len(input) == 0 || input[0] == nil {
return &props
return &props, nil
}

v := input[0].(map[string]interface{})
Expand Down Expand Up @@ -2624,7 +2630,18 @@ func expandBlobProperties(input []interface{}) *storage.BlobServiceProperties {
props.DefaultServiceVersion = utils.String(version)
}

return &props
// Sanity check for the prerequisites of restore_policy
// Ref: https://learn.microsoft.com/en-us/azure/storage/blobs/point-in-time-restore-overview#prerequisites-for-point-in-time-restore
if p := props.BlobServicePropertiesProperties.RestorePolicy; p != nil && p.Enabled != nil && *p.Enabled {
if props.ChangeFeed == nil || props.ChangeFeed.Enabled == nil || !*props.ChangeFeed.Enabled {
return nil, fmt.Errorf("`change_feed_enabled` must be `true` when `restore_policy` is set")
}
if props.IsVersioningEnabled == nil || !*props.IsVersioningEnabled {
return nil, fmt.Errorf("`versioning_enabled` must be `true` when `restore_policy` is set")
}
}

return &props, nil
}

func expandBlobPropertiesDeleteRetentionPolicy(input []interface{}) *storage.DeleteRetentionPolicy {
Expand Down
40 changes: 40 additions & 0 deletions internal/services/storage/storage_account_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,13 @@ func TestAccStorageAccount_blobProperties(t *testing.T) {
r := StorageAccountResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.restorePolicyMinimal(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.blobProperties(data),
Check: acceptance.ComposeTestCheckFunc(
Expand Down Expand Up @@ -2507,6 +2514,39 @@ resource "azurerm_storage_account" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomString)
}

func (r StorageAccountResource) restorePolicyMinimal(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestAzureRMSA-%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_tier = "Standard"
account_replication_type = "LRS"
blob_properties {
delete_retention_policy {}
restore_policy {
days = 6
}
versioning_enabled = true
change_feed_enabled = true
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString)
}

func (r StorageAccountResource) blobPropertiesContainerAndLastAccessTimeDisabled(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/storage_account.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ A `blob_properties` block supports the following:

* `delete_retention_policy` - (Optional) A `delete_retention_policy` block as defined below.

* `restore_policy` - (Optional) A `restore_policy` block as defined below. This must be used together with `delete_retention_policy` set and `versioning_enabled` set to `true`.
* `restore_policy` - (Optional) A `restore_policy` block as defined below. This must be used together with `delete_retention_policy` set, `versioning_enabled` and `change_feed_enabled` set to `true`.

* `versioning_enabled` - (Optional) Is versioning enabled? Default to `false`.

Expand Down

0 comments on commit fdeef95

Please sign in to comment.