I have created an module for Az NetApp Files setup, where we have defined list of objects.
This works as expected. But issue is when we create 3 SMB volumes it would be referenced as azurerm_netapp_volume.anf[0]… Later when we plan to delete an 2nd item from the list it would try to delete both 2nd item and 3rd item and then recreates.
Instead of index number can we use named index or map here, how to leverage that for list(objects)
variable "anf_volume" {
default = null
description = "Az NetApp Volume"
type = list(
object(
{
name = string
network_features = string # Basic / Standard (Recommended)
protocols = list(string)
storage_quota_in_gb = number # In GB, Min 100
anf_cpool_name = string #Note: Same as anf_cpool.name
anf_cpool_service_level = string #Note: Same as anf_cpool.service_level
snapshot_directory_visible = bool
snapshot_policy_name = string # snapshot_pol_daily_to_monthly, snapshot_pol_daily_to_weekly
}
)
)
anf_volume = [
{
name = "hs-vol1-std"
storage_quota_in_gb = 100 # Min 100
protocols = ["CIFS"]
network_features = "Standard"
anf_cpool_name = "hs-tf-std-cpool"
anf_cpool_service_level = "Standard"
snapshot_directory_visible = false
snapshot_policy_name = "snapshot_pol_daily_to_monthly"
},
{
name = "hs-vol2-std"
storage_quota_in_gb = 120 # Min 100
protocols = ["CIFS"]
network_features = "Standard"
anf_cpool_name = "hs-tf-std-cpool"
anf_cpool_service_level = "Standard"
snapshot_directory_visible = false
snapshot_policy_name = "snapshot_pol_daily_to_weekly"
},
{
name = "hs-vol3-std-long-path-share-name"
storage_quota_in_gb = 110 # Min 100
protocols = ["CIFS"]
network_features = "Standard"
anf_cpool_name = "hs-tf-std-cpool"
anf_cpool_service_level = "Standard"
snapshot_directory_visible = true
snapshot_policy_name = null
}
]
//logic
resource "azurerm_netapp_volume" "anf" {
lifecycle {
# prevent_destroy = true
ignore_changes = [
# Ignore changes to tags, e.g. because a management agent
# updates these based on some ruleset managed elsewhere.
tags,
zone
]
}
for_each = { for idx, val in var.anf_volume : idx => val }
name = each.value.name
location = data.azurerm_resource_group.anf.location
resource_group_name = data.azurerm_resource_group.anf.name
account_name = azurerm_netapp_account.anf.name
pool_name = each.value.anf_cpool_name
volume_path = each.value.name
service_level = each.value.anf_cpool_service_level
subnet_id = data.azurerm_subnet.anf.id
network_features = each.value.network_features
protocols = each.value.protocols
storage_quota_in_gb = each.value.storage_quota_in_gb
snapshot_directory_visible = each.value.snapshot_directory_visible
tags = var.default_tags
dynamic "data_protection_snapshot_policy" {
for_each = each.value.snapshot_policy_name != null ? [each.value.snapshot_policy_name] : []
content {
snapshot_policy_id = local.policy_lookup_table[data_protection_snapshot_policy.value].id
}
}
depends_on = [
azurerm_netapp_pool.anf,
azurerm_netapp_snapshot_policy.snapshot_pol_daily_to_weekly,
azurerm_netapp_snapshot_policy.snapshot_pol_daily_to_monthly
]
}