如何将一个值列表传递给资源内的一个块

huangapple go评论68阅读模式
英文:

How can I pass a list of values to a block within a resource

问题

我正在尝试使用Terraform从MySQL设置与BigQuery的Datastream连接(文档在此)。文档显示了用于指定要包括哪些表的结构如下:

resource "google_datastream_stream" "default" {
    depends_on = [
        google_kms_crypto_key_iam_member.key_user
    ]
    stream_id = "my-stream"
    desired_state = "NOT_STARTED"
    location = "us-central1"
    display_name = "my stream"
    labels = {
        key = "value"
    }
    source_config {
        source_connection_profile = google_datastream_connection_profile.source_connection_profile.id
        mysql_source_config {
            include_objects {
                mysql_databases {
                    database = "my-database"
                    mysql_tables {
                        table = "includedTable"
                        mysql_columns {
                            column = "includedColumn"
                            data_type = "VARCHAR"
                            collation = "utf8mb4"
                            primary_key = false
                            nullable = false
                            ordinal_position = 0
                        }
                    }
                }
            }
        }
    }
}

但我想要指定多个要包括的表,而不仅仅是一个。文档中的更多信息如下:

mysql_databases块支持:

database -(必需)数据库名称。

mysql_tables -(可选)数据库中的表。结构如下所示。

mysql_tables块支持:

table -(必需)表名称。

mysql_columns -(可选)模式中的MySQL列。如果未指定为包含/排除对象的一部分,将包含/排除所有内容。结构如下所示。

因此,它说mysql_databases块是我传递要包括的所有表的地方,但示例语法仅显示如何传递一个表,如table = "myTable"。我如何在这里传递一个值列表或类似的东西?我对Terraform还很新,所以也许我错过了一些标准功能。先感谢您的帮助。

英文:

I am trying to set up a Datastream connection from MySQL to BigQuery using Terraform (docs here). The docs show this structure for specifying which tables to include:

resource "google_datastream_stream" "default" {
    depends_on = [
        google_kms_crypto_key_iam_member.key_user
    ]
    stream_id = "my-stream"
    desired_state = "NOT_STARTED"
    location = "us-central1"
    display_name = "my stream"
    labels = {
        key = "value"
    }
    source_config {
        source_connection_profile = google_datastream_connection_profile.source_connection_profile.id
        mysql_source_config {
            include_objects {
                mysql_databases {
                    database = "my-database"
                    mysql_tables {
                        table = "includedTable"
                        mysql_columns {
                            column = "includedColumn"
                            data_type = "VARCHAR"
                            collation = "utf8mb4"
                            primary_key = false
                            nullable = false
                            ordinal_position = 0
                        }
                    }
                }
            }

But I want to specify several tables to include not just one. Further info in the docs say:

The mysql_databases block supports:

database - (Required) Database name.

mysql_tables - (Optional) Tables in the database. Structure is documented below.

The mysql_tables block supports:

table - (Required) Table name.

mysql_columns - (Optional) MySQL columns in the schema. When unspecified as part of include/exclude objects, includes/excludes everything. Structure is documented below.

So it says the mysql_databases block is where I pass in the all the tables I want to include but the example syntax only shows how to pass one table like table = "myTable". How can I pass a list of values here or something like that? I'm quite new to Terraform so maybe I'm missing something standard functionality. Thanks in advance.

答案1

得分: 1

这是动态创建嵌套块的解决方案:

dynamic "<block_name>" {
  for_each = toset(var.<your-var>)
  content {
    <thing> = <block_name>.value
  }
}
英文:

This is the solution to dynamically create nested blocks:

  dynamic &quot;&lt;block_name&gt;&quot; {
    for_each = toset(var.&lt;your-var&gt;)
    content {
      &lt;thing&gt; = &lt;block_name&gt;.value
    }
  }

huangapple
  • 本文由 发表于 2023年7月12日 23:46:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672367.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定