【问题标题】:How to pass array and maps using terraform cloud API calls如何使用 terraform 云 API 调用传递数组和地图
【发布时间】:2021-04-03 15:23:11
【问题描述】:

我需要使用 terraform api 将数组和地图值传递给 terraform 工作区

尝试调用

{
    "data": {
      "id":"",
      "attributes": {
        "key":"PREFIXES",
        "value":'{a="b"}',
        "description":"some description",
        "category":"terraform",
        "hcl": false,
        "sensitive": false
      },
      "type":"vars"
    }
  }

curl 调用是

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 实现相同的功能。我的 terraform 如何给出错误:

错误:for_each 参数无效

在 main.tf 第 18 行,资源“aws_s3_bucket_object”“obj”中: 18: for_each = var.prefixes

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"]}}

还有我的 terraform 代码:​​


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 terraform python-3.8 terraform-enterprise


【解决方案1】:

您的 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。

【讨论】:

    【解决方案2】:

    我创建了一个名为 local_prefix 的局部变量并将 var.prefixes 传递给 jsoncode。

    这行得通。

    locals{
     local_prefix = jsoncode(var.prefix)
    }
    
    

    在 local_prefix 上应用 for_each

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-23
      • 2020-11-10
      • 2016-12-02
      • 1970-01-01
      • 1970-01-01
      • 2020-04-13
      • 2017-04-27
      相关资源
      最近更新 更多