简体   繁体   中英

Terraform HTTP Request body manipulation

I am using the data source http to perform GET requests on the FreshService API in Terraform

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:

{
"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.

I tried toset(data.http.example.body.tasks) and tolist(data.http.example.body.tasks) but it says attribute doesn't exists.

Can someone please help?

The docs say that the body is raw response. I So I think you should use jsondecode , not 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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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