Terraform:如何编写 x3 嵌套 for 循环(DRY)

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

你好,很抱歉这个天真的问题 - 我一直在绞尽脑汁试图弄清楚这个问题,而不是重复自己。

我需要一个包含字符串数组的指标数组(根据 AWS 仪表板指标):

目前我的指标定义如下: local.keys 是一个字符串列表

      metrics = [
        for i, k in local.keys : [
          for metric in ["metric1", "metric2"] : [
            for method in ["method1", "method2"] : [
              "Server", metric, "dim1", k, "dim2", method,
              { "region" : "...", "id" : "ms${method}${i}", "visible" : false }
            ]
          ]
        ]
      ]

希望输出如下:

[["Server", metric, "dim1", k, "dim2", method,{ "region" : "...", "id" : "ms${method}${i}","visible" : false }] ... ... ...]

但事实并非如此。

terraform-provider-aws
1个回答
0
投票

弄清楚如何以及做什么:

locals {
  metrics = flatten([
    for i, k in ["Comp1", "Comp2"] : concat([
      for metric in ["metric1", "metric2"] : [
        for method in ["GET", "POST"] : {
          entry = [
            [
              "NameSpace", metric, "Dim1", k, "DIm2", method,
              {
                "id" : "${metric == "metric1" ? "ms" : "m"}${method}${i}",
                "visible" : false
              }
            ]
          ]
        }
      ]
      ], [{ "expr" : [{
        "expression" : "100*((msGET${i} + msPOST${i} + msOPTION${i} + msPUT${i} + msUPDATE${i} + msPATCH${i} + msTRACE${i} + msCONNECT${i} + msDELETE${i})/(mGET${i} + mPOST${i} + mOPTION${i} + mPUT${i} + mUPDATE${i} + mPATCH${i} + mTRACE${i} + mCONNECT${i} + mDELETE${i}))",
        "label" : k,
        "id" : "exp${i}"
    }] }])
  ])
  x = [for x in local.metrics : contains(keys(x), "entry") ? x.entry : x.expr]
}

这样我就可以获得所有的指标条目;加上每个公司所需的表达指标。

© www.soinside.com 2019 - 2024. All rights reserved.