Terraform - cosmosdb cassandra 模式块

Terraform - cosmosdb cassandra schema block

我正在尝试使用 cassandra 创建 cosmos 数据库 api。我想将 table - 架构作为变量传递。有人可以指导我如何做到这一点。我正在使用以下代码

resource "azurerm_cosmosdb_cassandra_keyspace" "ks" {
  name                = "${var.key_space_name}"
  resource_group_name = "${azurerm_cosmosdb_account.acc.resource_group_name}"
  account_name        = "${azurerm_cosmosdb_account.acc.name}"
  throughput          = 400
}

resource "azurerm_cosmosdb_cassandra_table" "ct" {
  name                  = "${var.table_name}"
  cassandra_keyspace_id = "${azurerm_cosmosdb_cassandra_keyspace.ks.id}"
  schema  = "${var.table_schema}"
}

variable.tf file
variable "table_schema" {
  description  = "table schema"
}
abc.tfvars
table_schema = " column {\nname = 'test1'\ntype = 'ascii'\n}\ncolumn {\nname = 'test2'\ntype = 'int'\n}\npartition_key {\nname = 'test1'\n}\n}"

错误:不支持的参数 │ │ 在 main.tf 第 68 行,在资源“azurerm_cosmosdb_cassandra_table”“ct”中: │ 68: 架构 = "${var.table_schema}" │ │ 这里不需要名为“schema”的参数。您是要定义一个“模式”类型的块吗? ╵

无法将整个模式块作为变量传递,因为Terraform当您将它作为参数传递时,期望它是一个块 .

如果您想将其作为变量传递,那么您可以使用动态块 如下所示:

Main.tf

resource "azurerm_cosmosdb_cassandra_table" "ct" {
  name                  = "tableans"
  cassandra_keyspace_id = azurerm_cosmosdb_cassandra_keyspace.ks.id
  schema  {
      dynamic "column"{
          for_each = var.table_schema.columns
          content{
              name = column.value.name
              type = column.value.type
          }
      }
      dynamic "partition_key"{
          for_each = var.table_schema.partition_keys
          content{
              name = partition_key.value.name
          }
      }
      dynamic "cluster_key"{
          for_each = var.table_schema.cluster_keys
          content{
              name = cluster_key.value.name
              order_by = cluster_key.value.order_by
          }
      }
  }
}

variable.tf

无变化

abc.tfvars

table_schema = {
columns = [{
    name = "test1"
    type = "ascii"},
    {
    name = "test2"
    type = "int"}]
partition_keys = [{
    name = "test1"
    }]
cluster_keys = [
]
}

输出:

注: Cluster Keys我在tfvars中留空了file 这就是为什么动态块 cluster_key 不执行。如果您还想包含集群键,那么下面将是您的 abc.tfvars 文件:

table_schema = {
columns = [{
    name = "test1"
    type = "ascii"},
    {
    name = "test2"
    type = "int"}]
partition_keys = [{
    name = "test1"
    }]
cluster_keys = [{
    name = "test2"
    order_by = "Asc"
    }
]
} 

输出: