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 - Add code check for restore_policy prerequisites #19822

Merged
merged 2 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

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