# Cloudflare 配置
# 使用 Consul 存储的 Cloudflare token 进行 API 调用

# 从 Consul 获取 Cloudflare 配置
data "consul_keys" "cloudflare_config" {
  key {
    name = "token"
    path = "config/dev/cloudflare/token"
  }
}

# Cloudflare Provider 配置
provider "cloudflare" {
  api_token = data.consul_keys.cloudflare_config.var.token
}

# 测试 Cloudflare API 连通性 - 获取可用区域
data "cloudflare_zones" "available" {
  filter {
    status = "active"
  }
}

# 测试 Cloudflare API 连通性 - 获取账户信息
data "cloudflare_accounts" "available" {}

# 输出 Cloudflare 连通性测试结果
output "cloudflare_connectivity_test" {
  description = "Cloudflare API 连通性测试结果"
  value = {
    zones_count = length(data.cloudflare_zones.available.zones)
    accounts_count = length(data.cloudflare_accounts.available.accounts)
    zones = [for zone in data.cloudflare_zones.available.zones : {
      name = zone.name
      id   = zone.id
    }]
    accounts = [for account in data.cloudflare_accounts.available.accounts : {
      name = account.name
      id   = account.id
    }]
  }
}
