Bala Murali
Bala Murali

Reputation: 125

Terraform Combine tuple elements as list

Can someone suggest how to combine terraform tuple with elements to list as shown below.

Scenario: I have bunch of yaml files structure as below, I flatten them with local values and assign to the resource attributes.

On my resource i need to assign roles values as a list , example below

roles = local.acs_tup  #["READER,OWNERSHIP","OWNERSHIP1","READER1,DEVELOPER1"]

yaml files content format

schemagrants :
  - schema: BDV    
    privilege: USAGE
    roles:
      - READER      
      - OWNERSHIP
    environment: DEV 
  - schema: BDV    
    privilege: USAGE    
    roles:
      - OWNERSHIP1
      - DEVELOPER1  
    environment: DEV
- schema: CDV    
    privilege: USAGE    
    roles:
      - DEVELOPER2  
    environment: DEV

locals {
  acs_files = fileset("./objects/schema-accessgrant", "*.yml")
  acs_data  = [for f in local.acs_files : yamldecode(file("./objects/schema-accessgrant/${f}"))]
  acs_flatten = flatten([for access in local.acs_data :
    [for sgr in access.schemagrants : {
      id          = "${sch.schema}.${sch.privilege}.${sch.environment}"
      roles       = sgr.roles
      environment = sgr.environment
      }
    ]]
  )
  acs_tup = [for roles in local.acs_flatten :
    {
      role = join(",", roles.roles)

    }
  ]
}
    resource "snowflake_database_grant" "database-access" {    
    database_name     = "database"
    privilege         = "USAGE"
    roles             = local.acs_tup  #["READER,OWNERSHIP","OWNERSHIP1","DEVELOPER1","DEVELOPER2"]
    shares            = []
    with_grant_option = false
  }
# Current Output
[      + {
          + role = "READER,OWNERSHIP"
        },
      + {
          + role = "ONWERSHIP1,DEVELOPER1"
        },
      + {
          + role = "DEVELOPER2"
        },    
    ]
# Expected Output
["READER,OWNERSHIP","OWNERSHIP1","DEVELOPER1","DEVELOPER2"]

Upvotes: 3

Views: 8616

Answers (1)

Marcin
Marcin

Reputation: 238967

Assuming that your expected output should be ["READER","OWNERSHIP","OWNERSHIP1","DEVELOPER1","DEVELOPER2"], your acs_tup must be:

acs_tup = flatten([for roles in local.acs_flatten: roles.roles])

Upvotes: 2

Related Questions