You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Terraform集成Mastercard REST API Provider大JSON数据字段配置问题

解决Terraform中REST API Provider多线JSON数据字段的问题

方法1:用jsonencode()(推荐)

把原本的JSON结构转成Terraform原生的HCL映射和列表,再通过jsonencode()自动生成合规的JSON字符串,彻底避开引号转义和多行字符串的坑。

修改后的代码示例:

resource "restapi_object" "terraformauto" {
  provider = restapi.restapi_headers
  path     = "/api/rest/v1/clusters"

  data = jsonencode({
    replication_factor          = 2
    prism_central_configuration = {
      pc_deployment_config = {
        pc_spec = {
          version            = "pc.2022.9"
          capacity_settings = {
            vm_count = 3
          }
        }
        network_config = {
          vpc_cloud_id          = "TME-terra-PC"
          subnet_cloud_id       = "Terra-PC"
          resource_group_name   = "tme-terraform-auto"
          ntp_server_ip_list    = ["216.239.35.0"]
          fgw = {
            ssh_key = {
              resource_group = "TMEENGKEY"
              key_name       = "multi-tme-eng"
            }
            number_of_fgw_machines = 1
            instance_type          = "Standard_D4_v4"
            ifs = {
              cloud_id = "Terra-FlowInteral"
            }
            efs = {
              cloud_id = "Terra-FlowExternal"
            }
          }
        }
      }
    }
    organization_id = "644d8115-e2f5-4411-b802-d6136c156c2a"
  })
}

这种写法的优势是Terraform会自动处理所有JSON格式细节,还能在计划阶段检查语法错误,比直接写JSON字符串更可靠。

方法2:用Heredoc多行字符串语法

如果要保留原始JSON结构,直接用Terraform的heredoc语法包裹即可,内部双引号无需转义,注意结尾的分隔符要和代码块缩进对齐。

示例代码:

resource "restapi_object" "terraformauto" {
  provider = restapi.restapi_headers
  path     = "/api/rest/v1/clusters"

  data = <<EOF
{
  "replication_factor": 2,
  "prism_central_configuration": {
    "pc_deployment_config": {
      "pc_spec": {
        "version": "pc.2022.9",
        "capacity_settings": {
          "vm_count": 3
        }
      },
      "network_config": {
        "vpc_cloud_id": "TME-terra-PC",
        "subnet_cloud_id": "Terra-PC",
        "resource_group_name": "tme-terraform-auto",
        "ntp_server_ip_list": [
          "216.239.35.0"
        ],
        "fgw": {
          "ssh_key": {
            "resource_group": "TMEENGKEY",
            "key_name": "multi-tme-eng"
          },
          "number_of_fgw_machines": 1,
          "instance_type": "Standard_D4_v4",
          "ifs": {
            "cloud_id": "Terra-FlowInteral"
          },
          "efs": {
            "cloud_id": "Terra-FlowExternal"
          }
        }
      }
    }
  },
  "organization_id": "644d8115-e2f5-4411-b802-d6136c156c2a"
}
EOF
}

这种方法适合直接复用现有JSON,但要自己保证JSON语法正确,Terraform不会帮你检查JSON格式错误。

内容的提问来源于stack exchange,提问作者Dwayne Lessner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 17:08:32