Hi, how are you?
I would like some help from the community please.
I will try to detail as much as possible.
I will create users through resource "keycloak_user" "user"
After creating one or more users and adding them to the group resource "keycloak_user_groups" "user_groups"
.
This point is working perfectly.
main.tf
resource "keycloak_user" "user" {
for_each = var.keycloak_users
realm_id = local.keycloak_realm_data
username = each.key
enabled = true
email = each.value
initial_password {
value = ""
temporary = true
}
}
resource "keycloak_user_groups" "user_groups" {
for_each = keycloak_user.user
realm_id = local.keycloak_realm_data
user_id = each.value.id
group_ids = [
keycloak_group.group.id
]
}
variable.tf
variable “keycloak_users” {
type = map(any)
}
main.tf - root module
keycloak_users = {
"user1" = "user1@domain.com"
"user2" = "user2@domain.com"
"user3" = "user3@domain.com"
"user4" = "user4@domain.com"
}
However, there is a new demand in which in addition to adding the users created in the first keycloak_user resource within the keycloak_user_groups resource I also need to add other users.
With that decided to try to use the concat function as evidenced below:
main.tf
resource "keycloak_user" "user" {
for_each = var.keycloak_users
realm_id = local.keycloak_realm_data
username = each.key
enabled = true
email = each.value
initial_password {
value = ""
temporary = true
}
}
resource "keycloak_user_groups" "user_groups" {
for_each = var.keycloak_user_ext == null ? keycloak_user.user : concat(keycloak_user.user, var.keycloak_user_ext)
realm_id = local.keycloak_realm_data
user_id = each.value.id
group_ids = [
keycloak_group.group.id
]
}
I created a new variable that is not mandatory and created an if function, if it is filled in it will concatenate the value obtained from keycloak_user.user and the value of the variable var.keycloak_user_ext
The var.keycloak_user_ext variable can be 1 or more users.
variable.tf
variable "keycloak_user_ext" {
default = ""
}
When trying to run this run, I ended up getting the following error:
╷
│ Error: Invalid function argument
│
│ on ../../modules/squad/main.tf line 77, in resource "keycloak_user_groups" "user_groups":
│ 77: for_each = var.keycloak_user_ext == null ? keycloak_user.user : concat(keycloak_user.user, var.keycloak_user_ext)
│ ├────────────────
│ │ while calling concat(seqs...)
│ │ keycloak_user.user is object with 4 attributes
│
│ Invalid value for "seqs" parameter: all arguments must be lists or tuples; got object.
Could you please help solve this problem?
Grateful