当内部映射是可选且为空时,我无法弄清楚如何为嵌套的 for_each 构建扁平结构

问题描述 投票:0回答:1

我正在尝试构建这个扁平结构:

locals {
  containers = flatten([
    for storage_key, st in var.Storage : [
      for cont_key, cont in st.containers : {
        storage_key  = storage_key,
        storage_name = st.name,
        cont_name    = cont_key,
        access_type  = cont.container_access_type
      }
    ]
  ])
}

但我的问题是变量规格允许容器映射为空:

variable "Storage" {
  type = map(object({
    name                     = string
    containers = optional(map(object({
      container_access_type = string
    })))
  }))
}
with these default values:
Storage = {
  stor1 = {
    name                      = "accntpriv"
    containers = {
      mgmt-media = {
        container_access_type = "private"
      }
      mgmt-comm = {
        container_access_type = "private"
      }
      mgmt-static = {
        container_access_type = "blob"
      }
    }
  }
}

我想将容器添加到由存储名称标识的现有存储帐户。

感谢您的帮助! 沃尔夫冈

terraform
1个回答
0
投票

这有效,感谢您的耐心

locals {
  containers = flatten([
    for storage_key, st in var.Storage : [
      for cont_key, cont in coalesce(st.containers, {}) : 
        {
          storage_key  = storage_key,
          storage_name = st.name,
          cont_name    = cont_key,
          access_type  = cont.container_access_type
        }
    ]
  ])
}
© www.soinside.com 2019 - 2024. All rights reserved.