michal
michal

Reputation: 428

Add to list if not empty

I have defined variable users:

users = [
  {
    userName    = "john.doe"
    roles       = ["ORG_ADMIN"]
    profile_attributes = null
    appId = null
  },
  {
    userName    = "doe.john"
    roles       = ["ORG_ADMIN"]
    profile_attributes = <<EOT
{
  "testParameter":"value"
}
EOT
    appId = "test123"
  }
]

and now I want to create okta_app_user resource:

resource "okta_app_user" "app_users" {
  count    = length(var.users)
  app_id   = var.users[count.index].appId
  user_id  = okta_user.users[count.index].id
  username = "${var.users[count.index].userName}@example.com"
  profile = var.users[count.index].profile_attributes
}

but app_id can't be empty in this resource but may be empty in my configuration. Is it possible to skip that user in okta_app_user when var.users[count.index].appId is empty ?

so something similar to what can be achieved:

foreach($users in $user) {
  if (!$user.app_id) {
    continue;
  }
}

Upvotes: 1

Views: 3310

Answers (1)

Marcin
Marcin

Reputation: 238209

Yes, you can filter out users without the appId. For example, by creating helper local variable users_with_appId:

locals {
  
  users_with_appId = [
    for user in var.users: user if user.appId != null
  ]

}

And then:

resource "okta_app_user" "app_users" {
  count    = length(local.users_with_appId)
  app_id   = local.users_with_appId[count.index].appId
  user_id  = okta_user.users[count.index].id
  username = "${local.users_with_appId[count.index].userName}@example.com"
  profile  = local.users_with_appId[count.index].profile_attributes
}

In the above its not clear what okta_user.users[count.index].id is? Thus further adjustments may be needed.

Upvotes: 2

Related Questions