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

如何在Grafana的CDKTF(Python)中为资源提供编码JSON数据

Python版CDKTF配置Grafana数据源:编码类参数配置补充

我是较早使用Python版CDKTF配置Grafana的用户,以下InfluxDB数据源配置已成功被Grafana接收:

DataSource(self, "xxx",
    uid = "datasource_influxdb",
    type = "influxdb",
    name = "...",
    url  = "...",
    json_data= [DataSourceJsonData(
        default_bucket    = "xxx",
        http_method       = "POST",
        organization      = "xxx",
        version           = "Flux",
        tls_skip_verify   = "true"
    )]
)

目前官方文档未明确说明json_data_encoded和secure_json_data_encoded这两个参数的配置方法,不过我有一套可正常运行的Terraform配置(用于解决InfluxDB 2.x数据源的token配置问题),示例如下:

resource "grafana_data_source" "influxdb" {
  type = "influxdb"
  name = "InfluxDB"
  url  = "http://influxdb:8086"

  json_data_encoded = jsonencode({
    version           = "Flux"
    organization      = "my-org"
    default_bucket    = "my-bucket"
    http_method       = "POST"
    tls_skip_verify   = true
  })

  secure_json_data_encoded = jsonencode({
    token = "my-influxdb-token"
  })
}

对应到Python版CDKTF中,可参考这个逻辑,将需要编码的JSON数据通过json.dumps()处理后传入对应参数:

import json

DataSource(self, "xxx",
    uid = "datasource_influxdb",
    type = "influxdb",
    name = "...",
    url  = "...",
    json_data_encoded = json.dumps({
        "default_bucket": "xxx",
        "http_method": "POST",
        "organization": "xxx",
        "version": "Flux",
        "tls_skip_verify": True
    }),
    secure_json_data_encoded = json.dumps({
        "token": "your-influxdb-token"
    })
)

核心注意点:

  • json_data_encoded用于传递无需加密的结构化配置,需先序列化为JSON字符串
  • secure_json_data_encoded用于传递敏感信息(如token),同样需序列化为JSON字符串后传入

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 02:35:33