条件性地将对象添加到数组中

huangapple go评论58阅读模式
英文:

Conditionally add object to array

问题

我尝试有条件地将一个对象添加到数组中

我有这段代码:

module "r53_records" {
  for_each = var.s3_static_websites[terraform.workspace]
  source   = "terraform-aws-modules/route53/aws//modules/records"
  version  = "~> 2.0"

  zone_name = each.value.zone

  records = [
    {
      name    = each.value.sub_domain
      type    = "CNAME"
      records = [module.cdn[each.key].cloudfront_distribution_domain_name]
      ttl     = 300
    },

    // 仅在 each.value.create_wild_card == true 时添加此对象
    {
      name    = "*"
      type    = "CNAME"
      records = [module.cdn[each.key].cloudfront_distribution_domain_name]
      ttl     = 300
    }
  ]
}

我看不到可以根据 foreach 中的值有条件地添加通配符 Route53 记录的方法。

英文:

I am trying to conditionally add an object to an array

I have this code:

module "r53_records" {
  for_each = var.s3_static_websites[terraform.workspace]
  source   = "terraform-aws-modules/route53/aws//modules/records"
  version  = "~> 2.0"

  zone_name = each.value.zone

  records = [
    {
      name    = each.value.sub_domain
      type    = "CNAME"
      records = [module.cdn[each.key].cloudfront_distribution_domain_name]
      ttl     = 300
    },

    // Only add this object if each.value.create_wild_card == true
    {
      name    = "*"
      type    = "CNAME"
      records = [module.cdn[each.key].cloudfront_distribution_domain_name]
      ttl     = 300
    }
  ]
}

I don't see a method I can use to onlyadd the wildcard route53 record based upon a value in the foreach

答案1

得分: 4

以下是代码部分的翻译:

Basically we need to dynamically construct the elements of the `records` module parameter value. We want one mandatory element with one optional element. Therefore, we need to approach this by `concat` one constant `list(object)` with an optional `list(object)` based on a ternary conditional with your specified logic:

records = concat(
  [{
    name    = each.value.sub_domain
    type    = "CNAME"
    records = [module.cdn[each.key].cloudfront_distribution_domain_name]
    ttl     = 300
  }],
  each.value.create_wild_card == true ?
  [{
    name    = "*"
    type    = "CNAME"
    records = [module.cdn[each.key].cloudfront_distribution_domain_name]
    ttl     = 300
  }]
  : []
)
英文:

Basically we need to dynamically construct the elements of the records module parameter value. We want one mandatory element with one optional element. Therefore, we need to approach this by concat one constant list(object) with an optional list(object) based on a ternary conditional with your specified logic:

records = concat(
  [{
    name    = each.value.sub_domain
    type    = "CNAME"
    records = [module.cdn[each.key].cloudfront_distribution_domain_name]
    ttl     = 300
  }],
  each.value.create_wild_card == true ?
  [{
    name    = "*"
    type    = "CNAME"
    records = [module.cdn[each.key].cloudfront_distribution_domain_name]
    ttl     = 300
  }]
  : []
)

huangapple
  • 本文由 发表于 2023年3月9日 23:22:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686654.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定