Reputation: 45
Trying to convert list to strings by iterating resource with variable using if condition
How to use if condition for resource to iterate on variable(list) in for_each
locals {
new_out = flatten([
for opi, clsan in var.module_cl_list : {
opic_R_P = reverse(split("@", "${opi}"))[1]
#op_R = ["WRITE"]
op_R = ["READ","DESCRIBE"]
}
])
}
for "Write" it is working, because resource takes one value per variable at a time
resource "something" "some" {
for_each = {for opi1, clsa1 in local.new_out: opi1 => clsa1}
name = local.new_out_opic_R_P
op_R = join(", ", each.value.op_R)
permission = "ALLOW"
}
for resource if we want to iterate the op_R variable, ["READ","DESCRIBE"], How to convert list to string iteratively?
Input
if input variable is write only, variable should be assigned as write if input variable is Read only, variable should be assigned itereatively with "READ" and "Describe"
note: WO = WRITE, RO = READ and DESCRIBE Example :-
west = {
name = "random1"
operation = "WRITE"
permission = "ALLOW"
},
west2 = {
name = "random2"
operation = "READ"
permission = "ALLOW"
},
west2 = {
name = "random2"
operation = "DESCRIBE"
permission = "ALLOW"
}
Current code works with this variable op_R = ["WRITE"]
If given below variable, It fails to process i need this to do in iterative manner
op_R = ["READ","DESCRIBE"]
Input
module_cl_list = {
"west@WO" = {appid = "456"},
"west2@RO" = {appid = "123"}
}
Error
Current code taking value as "Read, Describe" , resource takes only one value at a time for each variable
Upvotes: 1
Views: 256
Reputation: 238081
You need to use double for loop:
new_out = flatten([
for opi, clsan in var.module_cl_list : [
for op in (split("@", "${opi}")[1] == "RO" ? ["READ","DESCRIBE"] : ["WRITE"]): {
opic_R_P = reverse(split("@", "${opi}"))[1]
op_R = op
}
]
])
which will produce:
[
{
"op_R" = "READ"
"opic_R_P" = "west2"
},
{
"op_R" = "DESCRIBE"
"opic_R_P" = "west2"
},
{
"op_R" = "WRITE"
"opic_R_P" = "west"
},
]
Upvotes: 1