Json 如何使用terraform云API调用传递数组和映射
我需要使用TerraformAPI将数组和映射值传递给terraform workspace 试着打电话Json 如何使用terraform云API调用传递数组和映射,json,terraform,python-3.8,terraform-enterprise,Json,Terraform,Python 3.8,Terraform Enterprise,我需要使用TerraformAPI将数组和映射值传递给terraform workspace 试着打电话 { "data": { "id":"", "attributes": { "key":"PREFIXES", "value":'{a="b"}', &qu
{
"data": {
"id":"",
"attributes": {
"key":"PREFIXES",
"value":'{a="b"}',
"description":"some description",
"category":"terraform",
"hcl": false,
"sensitive": false
},
"type":"vars"
}
}
而curl call是
curl \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/vnd.api+json" \
--request PATCH \
--data @payload.json \
https://app.terraform.io/api/v2/workspaces/$WORKSPACE_ID/vars/$PREFIXES_ID
以错误告终
{"errors":[{"status":"400","title":"JSON body is invalid","detail":"784: unexpected token at '{ \"data\": { \"id\":\"\", \"attributes\": { \"key\":\"PREFIXES\", \"value\":'{a=\"b\"}', \"description\":\"some description\", \"category\":\"terraform\", \"hcl\": false, \"sensitive\": false }, \"type\":\"vars\" } }'"}]}
我试着用python实现同样的功能。我的地形是如何给出错误的:
错误:对每个参数无效
在main.tf第18行的资源“aws_s3_bucket_object”“obj”中:
18:for_each=变量前缀
python3
def update_workspace_vars(workspace_vars, var_values, params):
headers = {"Authorization": "Bearer " + params["TOKEN"],
"Content-Type": "application/vnd.api+json"}
for k in var_values:
payload = {
"data": {
"id": workspace_vars[k],
"attributes": {
"key": k,
"value": var_values[k],
"category": "terraform"
},
"type": "vars"
}
}
patch_params = dict((k, params[k]) for k in ("workspace_id", "tfe_host"))
patch_params.update({"var_id": workspace_vars[k]})
url = "https://{tfe_host}/api/v2/workspaces/{workspace_id}/vars/{var_id}".format(**patch_params)
response = http.request("PATCH", url, headers=headers, body=json.dumps(payload)).data
var_variables = {"prefixes": {"a": ["a1", "a2", "a3"], "b": ["b1", "b2", "b3"]}}
还有我的地形代码:
resource "aws_s3_bucket" "b" {
bucket = "my-tf-test-bucket-pinnaka"
acl = "private"
}
resource "aws_s3_bucket_object" "obj" {
for_each = var.prefixes
bucket = aws_s3_bucket.b.id
key = each.key
content = each.value
}```
您的JSON似乎无效
{
"data": {
"id":"",
"attributes": {
"key":"PREFIXES",
"value":'{a="b"}',
"description":"some description",
"category":"terraform",
"hcl": false,
"sensitive": false
},
"type":"vars"
}
}
“value”:“{a=“b”}”
是无效的JSON语法
要么使用
“value”:{“a”:“b”}
作为JSON,要么使用“value”:“{a=\'b\}\”
对单引号进行转义,以防止{“a”=“b”}被解析为JSON。我创建了一个局部变量名称local\u前缀,并将var.prefixes传递给jsoncode
这起作用了
locals{
local_prefix = jsoncode(var.prefix)
}
应用于本地前缀“{p>”值“{a=“b”}”上的每个值,
JSON不使用单引号作为键或值。使用双引号并转义\“
双引号。“值”:“{a=\“b\”},