gpt4 book ai didi

azure - 根据条件迭代 map map 内的列表

转载 作者:行者123 更新时间:2023-12-02 08:14:26 25 4
gpt4 key购买 nike

变量:

gateways:
gateway1:
routingConfig:
sslCertificates: [
"a",
"b"
],
## amended for brievity
gateway2:
routingConfig: {}
## amended for brievity

代码尝试:

data "azurerm_key_vault_secret" "ssl_certificates" {
for_each = {
for k, v in local.gateways : v.routing_config.ssl_certificates
if v.routing_config != {}
}

name = each.value
key_vault_id = data.azurerm_key_vault.ssl_certificates.id
}

这不起作用,它只为每个网关创建一次迭代,并尝试将元组插入名称中:

each.value is tuple with 2 element

这是预期的,如果你仔细想想,但我不知道如何在循环中进行循环

对于网关,相同的数据结构有效,因为我可以对 ssl_certificates 属性进行动态处理,但我认为我不能在这里做到这一点

最终解决方案:

certificates_flat = merge([
for gtw, gtw_details in local.gateways : {
for ssl_cert in gtw_details.routing_config.ssl_certificates :
ssl_cert => ssl_cert
} if gtw_details.routing_config != {}
]...)

data "azurerm_key_vault_secret" "ssl_certificates" {
for_each = local.certificates_flat
name = each.value
key_vault_id = data.azurerm_key_vault.ssl_certificates.id
}

最佳答案

how do I do a loop inside a loop

您必须展平您的变量。例如如下:

locals {
gateways_flat = merge([
for gtw, gtw_details in local.gateways: {
for ssl_cert in gtw_details.routing_config.ssl_certificates:
"${gtw}-${ssl_cert}" => {
ssl_certificate = ssl_cert
}
} if gtw_details.routing_config != {}

]...)
}

给予:

{
"gateway1-a" = {
"ssl_certificate" = "a"
}
"gateway1-b" = {
"ssl_certificate" = "b"
}
}

然后

data "azurerm_key_vault_secret" "ssl_certificates" {
for_each = local.gateways_flat
name = each.value.ssl_certificate
key_vault_id = data.azurerm_key_vault.ssl_certificates.id
}

关于azure - 根据条件迭代 map map 内的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74473275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com