Terraform:将地图列表转换为修订的地图列表

时间:2020-07-27 19:01:27

标签: arrays list dictionary object terraform

我的输入是:

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button1_Click(object sender, RoutedEventArgs e)
    {
        Button button1 = sender as Button;
        button1.Content = "Something";
    }

    private void Button2_Click(object sender, RoutedEventArgs e)
    {
        Button1.Content = "Something2";
    }

我想要的输出是:

input = [
  {
    x = "X1"
    y = "Y1"
  },
  {
    x = "X2"
    y = "Y2"
  },
  {
    x = "X3"
    y = "Y3"
  },
]

如何在Terraform中实现?

2 个答案:

答案 0 :(得分:1)

为此,我能够在terraform中使用flatten()函数:

flatten([
  for a_map in var.input : [
    { val = a_map.x, description = "This is a value of X" },
    { val = a_map.y, description = "This is a value of Y" },
  ]
])

此方法的一个优点是它将保持值的顺序。

所以输出将是:

[
  {
    "description" = "This is a value of X"
    "val" = "X1"
  },
  {
    "description" = "This is a value of Y"
    "val" = "Y1"
  },
  {
    "description" = "This is a value of X"
    "val" = "X2"
  },
  {
    "description" = "This is a value of Y"
    "val" = "Y2"
  },
  {
    "description" = "This is a value of X"
    "val" = "X3"
  },
  {
    "description" = "This is a value of Y"
    "val" = "Y3"
  },
]

答案 1 :(得分:0)

以下产生这样的输出:



variable "input" {

  default = [
    {
      x = "X1"
      y = "Y1"
    },
    {
      x = "X2"
      y = "Y2"
    },
    {
      x = "X3"
      y = "Y3"
    },
  ]

}



locals  {
  part1 = [for v in var.input:
      {
        "val" = v.x,
        "description" = "This is a value of X"
      }]
      
  part2 = [for v in var.input:
      {
        "val" = v.y,
        "description" = "This is a value of Y"
      }]      
}



output "output" {

  value = concat(local.part1, local.part2)

}

我的测试输出:

utput = [
  {
    "description" = "This is a value of X"
    "val" = "X1"
  },
  {
    "description" = "This is a value of X"
    "val" = "X2"
  },
  {
    "description" = "This is a value of X"
    "val" = "X3"
  },
  {
    "description" = "This is a value of Y"
    "val" = "Y1"
  },
  {
    "description" = "This is a value of Y"
    "val" = "Y2"
  },
  {
    "description" = "This is a value of Y"
    "val" = "Y3"
  },
]