简体   繁体   English

Terraform HTTP 请求体操作

[英]Terraform HTTP Request body manipulation

I am using the data source http to perform GET requests on the FreshService API in Terraform我正在使用数据源http对 Terraform 中的 FreshService API 执行 GET 请求

My code is as follows:我的代码如下:

# Query FreshService
data "http" "example" {
  url = ".../api/v2/changes/7195/tasks"

  # Optional request headers
  request_headers = {
    Accept        = "application/json"
    Authorization = "XXXXXXXX"
  }
}

output "freshservice_task_title" {
  # Gets the root block volume id
  value = "Targeting Volume ID: ${jsonencode(data.http.example.body)}"
}

The output is returning something like this: output 返回如下内容:

{
"tasks": [
    {
        "id": XXXX,
        "agent_id": XXXX,
        "status": 3,
        "due_date": "2021-04-07T09:30:00Z",
        "notify_before": 0,
        "title": "XXXXX",
        "description": "",
        "created_at": "2021-03-31T12:28:43Z",
        "updated_at": "2021-04-07T12:43:55Z",
        "closed_at": "2021-04-07T12:43:55Z",
        "group_id": XXXXX
    }
   ]
}

I need to iterate the tasks in Terraform to do some manipulation but i am not able to do so.我需要迭代 Terraform 中的任务来做一些操作,但我不能这样做。

I tried toset(data.http.example.body.tasks) and tolist(data.http.example.body.tasks) but it says attribute doesn't exists.我尝试toset(data.http.example.body.tasks) and tolist(data.http.example.body.tasks)但它说属性不存在。

Can someone please help?有人可以帮忙吗?

The docs say that the body is raw response.文档说身体raw反应。 I So I think you should use jsondecode , not jsonencode .我所以我认为你应该使用jsondecode ,而不是jsonencode Then you access tasks as follows:然后按如下方式访问任务:

jsondecode(data.http.example.body).tasks

or iterate (example):或迭代(示例):

[for task in jsondecode(data.http.example.body).tasks: task.status]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM