gpt4 book ai didi

ruby - 如何从 Terraform 中的外部数据源访问 JSON?

转载 作者:太空宇宙 更新时间:2023-11-03 16:40:37 24 4
gpt4 key购买 nike

我正在从 http terraform 数据源接收 JSON

data "http" "example" {
url = "${var.cloudwatch_endpoint}/api/v0/components"

# Optional request headers
request_headers {
"Accept" = "application/json"
"X-Api-Key" = "${var.api_key}"
}
}

它输出以下内容。

http = [{"componentID":"k8QEbeuHdDnU","name":"Jenkins","description":"","status":"Partial Outage","order":1553796836},{"componentID":"ui","name":"ui","description":"","status":"Operational","order":1554483781},{"componentID":"auth","name":"auth","description":"","status":"Operational","order":1554483781},{"componentID":"elig","name":"elig","description":"","status":"Operational","order":1554483781},{"componentID":"kong","name":"kong","description":"","status":"Operational","order":1554483781}]

这是 terraform 中的字符串。为了将此字符串转换为 JSON,我将其传递给一个 external 数据源,这是一个简单的 ruby​​ 函数。这是传递它的地形。

data "external" "component_ids" {
program = ["ruby", "./fetchComponent.rb",]

query = {
data = "${data.http.example.body}"

}
}

这是 ruby 函数

 #!/usr/bin/env ruby
require 'json'
data = JSON.parse(STDIN.read)
results = data.to_json
STDOUT.write results

所有这些都有效。 external 数据输出如下(它看起来与 http 输出相同)但根据 terraform 文档,这应该是一个 map

external1 = {
data = [{"componentID":"k8QEbeuHdDnU","name":"Jenkins","description":"","status":"Partial Outage","order":1553796836},{"componentID":"ui","name":"ui","description":"","status":"Operational","order":1554483781},{"componentID":"auth","name":"auth","description":"","status":"Operational","order":1554483781},{"componentID":"elig","name":"elig","description":"","status":"Operational","order":1554483781},{"componentID":"kong","name":"kong","description":"","status":"Operational","order":1554483781}]
}

我原以为我现在可以访问 external 数据源中的数据。我做不到。

最终我想要做的是创建一个 listcomponentID 变量,这些变量位于 external 数据源中。

我尝试过的一些东西

* output.external: key "0" does not exist in map data.external.component_ids.result in:

${data.external.component_ids.result[0]}

* output.external: At column 3, line 1: element: argument 1 should be type list, got type string in:

${element(data.external.component_ids.result["componentID"],0)}
* output.external: key "componentID" does not exist in map data.external.component_ids.result in:

${data.external.component_ids.result["componentID"]}
ternal: lookup: lookup failed to find 'componentID' in:

${lookup(data.external.component_ids.*.result[0], "componentID")}

感谢您的帮助。

最佳答案

无法使用变量cloudwatch_endpoint 进行测试,因此我必须考虑解决方案。

Terraform 在 0.11.x 之前不能直接解码 json。但是有一种解决方法可以处理嵌套列表。

您的 ruby​​ 需要调整以将输出作为变量 http 下面,那么您应该可以得到您需要的东西。

$ cat main.tf 
variable "http" {
type = "list"
default = [{componentID = "k8QEbeuHdDnU", name = "Jenkins"}]
}

output "http" {
value = "${lookup(var.http[0], "componentID")}"
}

$ terraform apply

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

http = k8QEbeuHdDnU

关于ruby - 如何从 Terraform 中的外部数据源访问 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55543861/

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