从flatten()生成一组字符串

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

Materialize a Set of String From flatten()

问题

这是尝试从对复杂对象调用flatten()的调用中返回一个集合(字符串)的挣扎。

我的尝试:

在 dev.tfvars 中

  1. source_endpoints = [
  2. {
  3. name = "DBSERVER01",
  4. ip_address = "xxx.xx.x.xxx"
  5. port = "1433",
  6. client_databases = [
  7. "DB01",
  8. "DB02"
  9. ]
  10. },
  11. {
  12. name = "DBSERVER02",
  13. ip_address = "xxx.xx.x.xxx"
  14. port = "1433",
  15. client_databases = [
  16. "DB03",
  17. "DB04"
  18. ]
  19. }
  20. ]

在 locals.tf 中

  1. locals {
  2. database_names = flatten([
  3. for x, source_endpoint in var.source_endpoints : [
  4. for y, client_database in source_endpoint.client_databases : [
  5. client_database
  6. ]
  7. ]
  8. ])
  9. }

应用于资源:

  1. resource "aws_s3_object" "bronze-cdc" {
  2. for_each = local.database_names
  3. bucket = module.s3_bronze.bucket.id
  4. key = "${each.value}/cdc-files/"
  5. content_type = "application/x-directory"
  6. }

Terraform Plan 错误:

  1. > 错误: 无效的 for_each 参数
  2. >
  3. > s3-folders.tf 的第 19 , 在资源 "aws_s3_object" "silver-dbo" :
  4. > 19: for_each = local.database_names
  5. > ├────────────────
  6. > local.database_names is tuple with 4 elements
  7. >
  8. > 所提供的 "for_each" 参数值不适用: "for_each" 参数必须是映射或字符串集并且您提供了
  9. > 类型为元组的值

local.database_names 的期望结果:

  1. ["DB01","DB02","DB03","DB04"]

我知道肯定有一种方法将结果投影到一个集合(字符串)中,我只是还没有碰到任何示例。欢迎任何建议。

英文:

It has been a struggle to attempt to return a set(string) from a call to flatten() against a complex object.

My Attempt:

In dev.tfvars

  1. source_endpoints = [
  2. {
  3. name = "DBSERVER01",
  4. ip_address = "xxx.xx.x.xxx"
  5. port = "1433",
  6. client_databases = [
  7. "DB01",
  8. "DB02"
  9. ]
  10. },
  11. {
  12. name = "DBSERVER02",
  13. ip_address = "xxx.xx.x.xxx"
  14. port = "1433",
  15. client_databases = [
  16. "DB03",
  17. "DB04"
  18. ]
  19. }
  20. ]

In locals.tf

  1. locals {
  2. database_names = flatten([
  3. for x, source_endpoint in var.source_endpoints : [
  4. for y, client_database in source_endpoint.client_databases : [
  5. client_database
  6. ]
  7. ]
  8. ])
  9. }

Applied To Resource:

  1. resource "aws_s3_object" "bronze-cdc" {
  2. for_each = local.database_names
  3. bucket = module.s3_bronze.bucket.id
  4. key = "${each.value}/cdc-files/"
  5. content_type = "application/x-directory"
  6. }

Terraform Plan Error :

  1. > Error: Invalid for_each argument
  2. >
  3. > on s3-folders.tf line 19, in resource "aws_s3_object" "silver-dbo":
  4. > 19: for_each = local.database_names
  5. > ├────────────────
  6. > local.database_names is tuple with 4 elements
  7. >
  8. > The given "for_each" argument value is unsuitable: the "for_each"
  9. > argument must be a map, or set of strings, and you have provided a
  10. > value of type tuple.

Desired Result of local.database_names:

  1. ["DB01","DB02","DB03","DB04"]

I know there must be a way to project the result to a set(string), I just have not stumbled across any example. Any Advice is welcomed.

答案1

得分: 1

基于错误输出:

  1. for_each”参数必须是一个映射或字符串集,而您提供的是元组类型的值。

本地变量的列表类型可以通过使用内置的 toset [1] 函数转换为集合(与 for_each 一起使用):

  1. resource "aws_s3_object" "bronze-cdc" {
  2. for_each = toset(local.database_names)
  3. bucket = module.s3_bronze.bucket.id
  4. key = "${each.value}/cdc-files/"
  5. content_type = "application/x-directory"
  6. }

[1] https://developer.hashicorp.com/terraform/language/functions/toset

英文:

Based on the error output:

  1. "for_each" argument must be a map, or set of strings, and you have provided a value of type tuple.

The list type of the local variable can be converted to a set (which works with for_each) by using the built-in toset [1] function:

  1. resource "aws_s3_object" "bronze-cdc" {
  2. for_each = toset(local.database_names)
  3. bucket = module.s3_bronze.bucket.id
  4. key = "${each.value}/cdc-files/"
  5. content_type = "application/x-directory"
  6. }

[1] https://developer.hashicorp.com/terraform/language/functions/toset

huangapple
  • 本文由 发表于 2023年3月9日 22:12:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685762.html
匿名

发表评论

匿名网友

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

确定