hashicorp / terraform-provider-azurerm

Terraform provider for Azure Resource Manager
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
Mozilla Public License 2.0
4.6k stars 4.64k forks source link

Error: creating Storage Account Management Policy #23590

Open bpriyas92 opened 1 year ago

bpriyas92 commented 1 year ago

Is there an existing issue for this?

Community Note

Terraform Version

Terraform v1.3.0

AzureRM Provider Version

v3.49.0

Affected Resource(s)/Data Source(s)

azurerm_storage_management_policy

Terraform Configuration Files

resource "azurerm_storage_management_policy" "policy" {
  storage_account_id = azurerm_storage_account.monitoring_storage_account.id

  for_each = { for idx, rule in var.storage_rules : idx => rule }
  rule {
    name    = each.value.name
    enabled = each.value.enabled
    filters {
      blob_types   = each.value.filters.blob_types
      prefix_match = each.value.filters.prefix_match

      dynamic "match_blob_index_tag" {

        for_each = each.value.filters.match_blob_index_tag == null ? [] : [each.value.filters.match_blob_index_tag]
        content {
          name      = each.value.filters.match_blob_index_tag.name
          operation = each.value.filters.match_blob_index_tag.operation
          value     = each.value.filters.match_blob_index_tag.value
        }
      }
    }
    actions {
      base_blob {
        tier_to_cool_after_days_since_modification_greater_than        = each.value.actions.base_blob.tier_to_cool_after_days_since_modification_greater_than
        tier_to_cool_after_days_since_last_access_time_greater_than    = each.value.actions.base_blob.tier_to_cool_after_days_since_last_access_time_greater_than
        tier_to_cool_after_days_since_creation_greater_than            = each.value.actions.base_blob.tier_to_cool_after_days_since_creation_greater_than
        tier_to_archive_after_days_since_modification_greater_than     = each.value.actions.base_blob.tier_to_archive_after_days_since_modification_greater_than
        tier_to_archive_after_days_since_last_access_time_greater_than = each.value.actions.base_blob.tier_to_archive_after_days_since_last_access_time_greater_than
        tier_to_archive_after_days_since_creation_greater_than         = each.value.actions.base_blob.tier_to_archive_after_days_since_creation_greater_than
        auto_tier_to_hot_from_cool_enabled                             = each.value.actions.base_blob.auto_tier_to_hot_from_cool_enabled
        delete_after_days_since_modification_greater_than              = each.value.actions.base_blob.delete_after_days_since_modification_greater_than
        delete_after_days_since_last_access_time_greater_than          = each.value.actions.base_blob.delete_after_days_since_last_access_time_greater_than
        delete_after_days_since_creation_greater_than                  = each.value.actions.base_blob.delete_after_days_since_creation_greater_than
      }
      snapshot {
        change_tier_to_archive_after_days_since_creation               = each.value.actions.snapshot.change_tier_to_archive_after_days_since_creation
        tier_to_archive_after_days_since_last_tier_change_greater_than = each.value.actions.snapshot.tier_to_archive_after_days_since_last_tier_change_greater_than
        change_tier_to_cool_after_days_since_creation                  = each.value.actions.snapshot.change_tier_to_cool_after_days_since_creation
        delete_after_days_since_creation_greater_than                  = each.value.actions.snapshot.delete_after_days_since_creation_greater_than
      }
      version {
        change_tier_to_archive_after_days_since_creation               = each.value.actions.version.change_tier_to_archive_after_days_since_creation
        tier_to_archive_after_days_since_last_tier_change_greater_than = each.value.actions.version.tier_to_archive_after_days_since_last_tier_change_greater_than
        change_tier_to_cool_after_days_since_creation                  = each.value.actions.version.change_tier_to_cool_after_days_since_creation
        delete_after_days_since_creation                               = each.value.actions.version.delete_after_days_since_creation
      }
    }
  }
}

Debug Output/Panic Output

Error: creating Storage Account Management Policy: (Management Policy Name "default" / Storage Account Name "examplesa" / Resource Group "example-rg"): storage.ManagementPoliciesClient#CreateOrUpdate: Failure responding to request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="InvalidManagementPolicyRule" Message="ManagementPolicy rule rule2 is invalid. Invalid value for parameter : version For more information, see - https://aka.ms/managementpolicies"
│ 
│   with azurerm_storage_management_policy.policy["1"],
│   on main.tf line 65, in resource "azurerm_storage_management_policy" "policy":
│   65: resource "azurerm_storage_management_policy" "policy" {

Expected Behaviour

To Deploy policy since version is optional

Actual Behaviour

terraform apply failed with above error

Steps to Reproduce

name = "rule2" enabled = true filters ={ blob_types = ["blockBlob"] } actions ={ base_blob ={ tier_to_cool_after_days_since_modification_greater_than = 10 tier_to_archive_after_days_since_modification_greater_than = 50 delete_after_days_since_modification_greater_than = 100 } snapshot ={ delete_after_days_since_creation_greater_than = 30 } }

Important Factoids

No response

References

No response

daniel-anova commented 10 months ago

If you look at the error it says version is the incorrect field. You can't declare a block with default values.

You can work around it by wrapping base_blob, version and snapshot as a dynamic block so it's only included when there's defined values.