首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将列表转换成地形地图?

如何将列表转换成地形地图?
EN

Stack Overflow用户
提问于 2022-07-12 05:22:40
回答 1查看 139关注 0票数 -2
代码语言:javascript
复制
    string vals=var.vals // which will contain string in the format below
//"hr:recruitment,department:human,tool:sap,fruit:apple"

    labels={
       hostname=var.hostname
        monitored=var.monitored
        customer=var.cust1
        machine-type=var.machinetype
    }

我需要为google_compute_instance设置标签(键、值)对,方法是将上面的2个属性组合成一个映射。

因此,我将split(",",var.vals)代码转换为列表

这给了我一份清单

代码语言:javascript
复制
    tolist([
  "hr:recruitment",
  "department:human",
  "tool:sap",
  "fruit:apple",
])

期望的输出,仅由组合标签和值的映射(字符串)组成

代码语言:javascript
复制
labels:
{
            hostname:var.hostname
            monitored:var.monitored
            customer:var.cust1
            machine-type:var.machinetype
            hr:"recruitment",
           department:"human",
          tool:"sap",
          fruit:"apple"
}

如何将此列表转换为地图,并将其与标签结合?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-12 07:57:17

我在下面尝试过&它似乎正在工作,但它严重依赖于vals字符串的数据质量(不应该包含重复的键)。在它的核心部分,它依赖于拉链图从键和值列表构建映射。

代码语言:javascript
复制
// Having vals as a string variable
variable "vals" {
  description = "The name of availability set"
  type        = string
  default     = "hr:recruitment,department:human,tool:sap,fruit:apple"
}

// hostname, monitored, cust1 & machinetype vars
variable "hostname" {
    type = string
    default = "dummy.net"
}

variable "monitored" {
    type = bool
    default = true
}

variable "cust1" {
    type = string
    default = "xyz"
}

variable "machinetype" {
    type = string
    default = "linux"
}
    
locals {
  // parsing locally to split the vals string to fetch the keys
  keys = [
    for s in split(",", var.vals) : split(":", s)[0]
  ]

  // parsing locally to split the vals string to fetch the values
  values = [
    for s in split(",", var.vals) : split(":", s)[1]
  ]

  //I used labels also as vars
  labels = {
    hostname     = var.hostname
    monitored    = var.monitored
    customer     = var.cust1
    machine-type = var.machinetype
  }
}

// Finally, zipmap to construct map from keys & values & merge with local.labels
output "final_map" {
  value = merge(local.labels, zipmap(local.keys, local.values))
}

最后,输出如下所示:

代码语言:javascript
复制
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

final_map = {
  "customer" = "xyz"
  "department" = "human"
  "fruit" = "apple"
  "hostname" = "dummy.net"
  "hr" = "recruitment"
  "machine-type" = "linux"
  "monitored" = true
  "tool" = "sap"
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72947371

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档