we created okta_group_memberships using terraform. we are creating multple users but we want only selected user

Viewed 76

We created okta users, and okta groups using terraform

This is variables.tf used variable list object for users and groups

variable "users" {
  type = list(object
    ({
      first_name   = string
      last_name    = string
      email        = string
      organization = string
      role         = string
      team         = string
  }))
}

variable "groups" {
  type = list(object
    ({
      name        = string
      description = string
  }))
}

This is terraform.tfvars

groups = [
  { name = "dev", description = "This group for Devlopers" },
  { name = "qa", description = "This group for QA" },
  { name = "per", description = "This group for Per" }
  { name = "devops", description = "This group for Devops" }
]

users = [
  { first_name = "a", last_name = "a", email = "a@gmail.com", role = "Engineer", organization = "organization", team = ["dev"] },
  { first_name = "b", last_name = "b", email = "b@gmail.com", role = "Lead", organization = "organization", team = ["qa" },
  { first_name = "c", last_name = "c", email = "c@gmail.com", role = "Devloper", organization = "organization", team = ["devops" },
  { first_name = "d", last_name = "d", email = "d@gmail.com", role = "Engineer", organization = "organization", team = ["dev",},
  { first_name = "e", last_name = "e", email = "e@gmail.com", role = "Lead", organization = "organization", team = ["per"] },
  { first_name = "f", last_name = "f", email = "f@gmail.com", role = "Devloper", organization = "organization", team = ["dev" },
],

this is main.tf

locals {
  #groups
  dev_group         = [for dev in var.groups : dev if apollo.name == "dev"][0]
  devops_group       = [for devops in var.groups : devops if devops.name == "devops"][0]
  per_group         = [for per in var.groups : per if per.name == "per"][0]
  qa_group.         = [for qa in var.groups : qa if qa.name == "qa"][0]
 # users
  dev_user         = [for dev in var.users : dev if dev.team == "dev"][0]
  qa_user          = [for qa var.users : qa if qa.team == "qa"][0]
  per_user         = [for per var.users : per if per.team == "per"][0]
  devops_user.     = [for devops var.users : devops if devops.team == "devops"][0]
}

to get the values used for_each

resource "okta_group" "groups" {
  for_each    = { for group in var.groups : join("-", [group.name, group.description]) => group }
  name        = each.value.name
  description = each.value.description
}



resource "okta_user" "okta_user_add" {
  for_each = { for user in var.users : join("-", [user.first_name, user.last_name]) => user }

  title        = each.value.role
  email        = each.value.email
  first_name   = each.value.first_name
  last_name    = each.value.last_name
  login        = each.value.email
  organization = each.value.organization
}

when we are trying to get id we tried multiple things but it didn't work for us. unable to get the group id and user id's

resource "okta_group_memberships" "b_member_group" {

  group_id = okta_group.groups[join("-", [local.dev.name, local.dev.description])].id
  users = [ okta_user.okta_user_add[join("-", [local.b_user.first_name, local.b_user.last_name])].id ]
}

my question is

When we are creating okta_group_memberships we are getting only one user from it. in locals i selected b_user select b_user = [for bin var.users : b if b.team == "b"][0] This is working but it selecting only one user from it.

if i insert [*] i am getting error from it.

Error: Unsupported attribute
│
│   on main.tf line 55, in resource "okta_group_memberships" "b_member_group":
│   55:     okta_user.okta_user_add[join("-", [local.b_user.first_name, local.b_user.last_name])].id    
│     ├────────────────
│     │ local.b_user is tuple with 4 elements
│
│ This value does not have any attributes.

1 Answers

Your expression [for bin var.users : b if b.team == "b"] constructs the following:

[
  { first_name = "c", last_name = "c", email = "c@gmail.com", role = "Devloper", organization = "organization", okta_admin = false, team ="b" },
  { first_name = "d", last_name = "d", email = "d@gmail.com", role = "Engineer", organization = "organization", okta_admin = true, team = "b" },
  { first_name = "e", last_name = "e", email = "e@gmail.com", role = "Lead", organization = "organization", okta_admin = true, team ="b" },
  { first_name = "f", last_name = "f", email = "f@gmail.com", role = "Devloper", organization = "organization", okta_admin = false, team ="b" },
]

You then access the zeroth element of this list with [0], which would return:

{ first_name = "c", last_name = "c", email = "c@gmail.com", role = "Devloper", organization = "organization", okta_admin = false, team ="b" }

This is why there is only one user returned from the value. You need to remove the [0] element accessor:

b_user = [for bin var.users : b if b.team == "b"]

You would also need to update your argument value correspondingly:

users = [ for b_user in local.b_user : okta_user.okta_user_add[join("-", [b_user.first_name, b_user.last_name])].id]
Related