我需要获得现有项目的存储库id,以便在该repo上工作。除了使用Azure DevOps REST API,似乎没有其他方法。
我试图利用REST API在我的地形代码中获取repo id:
data "http" "example" {
url = "https://dev.azure.com/{organization}/{project}/_apis/git/repositories?api-version=6.0"
request_headers = {
"Authorization" = "Basic ${base64encode("PAT:${var.personal_access_token}")}"
}
}
output "repository_id" {
value = data.http.example.json.value[0].id
}
当我运行地形计划时,它产生错误:
Error: Unsupported attribute
line 29, in output "repository_id":
29: value = data.http.example.json.value[0].id
我也尝试了jsondecode (jq已经安装):
resource "null_resource" "example" {
provisioner "local-exec" {
command = "curl -s -H 'Authorization: Bearer ${var.pat}' https://dev.azure.com/{organization}/{project}/_apis/git/repositories?api-version=6.0 | jq '.value[0].id'"
interpreter = ["bash", "-c"]
}
}
output "repo_id" {
value = "${jsondecode(null_resource.example.stdout).id}"
}
那也不工作!!
Azure DevOps REST API工作得很好,我只是无法从响应中获取值到地形!什么是正确的代码,或者它可以不使用REST API完成!
谢谢!
与外部API交互并将其输出返回给TF的正确方法是通过外部数据源。链接的TF文档提供了如何使用和创建这种数据源的示例。