1

i have this yaml file

team1:
   test@gmail,com: ${live_user_role}
   [email protected]: ${live_user_role} 
   [email protected]: ${live_admin_role}
 

terraform code

locals {
  render_membership = templatefile("${path.module}/teammembers.yaml",
    {
      live_user_role      = var.liveteam_user_role_id
      live_admin_role     = var.liveteam_admin_role_id
    }
  )

  membership_nested = yamldecode(local.render_membership)
  team_names        = keys(local.membership_nested)

  membership_flat = flatten(
    [
      for team_name in local.team_names : [
        for user_email, roles in local.membership_nested[team_name] : {
          team_name = team_name
          user_name = user_email
          roles     = [roles]
        }
      ]
    ]
  )
}


resource "squadcast_team_member" "membership" {
  for_each = { for i, v in local.membership_flat : i => v }
  team_id  = data.squadcast_team.teams[each.key].id
  user_id  = data.squadcast_user.users[each.key].id
  role_ids = each.value.roles
  lifecycle {
    create_before_destroy = true
  }
}

data "squadcast_team" "teams" {
  for_each = { for i, v in local.membership_flat : i => v }
  name     = each.value.team_name
}

data "squadcast_user" "users" {
  for_each = { for i, v in local.membership_flat : i => v }
  email    = each.value.user_name
}

Now when i add the new member in the list let's say like this:

team1:
   test@gmail,com: ${live_user_role}
   [email protected]: ${live_user_role} 
   [email protected]: ${live_admin_role}
   [email protected]:  ${live_admin_role}

terraform is deleting the previous user and recreating all the users again

    squadcast_team_member.membership["1"] must be replaced
+/- resource "squadcast_team_member" "membership" {
      ~ id       = "62ed115ab4b4017fa2a4b786" -> (known after apply)
      ~ role_ids = [
          - "61b08676e4466d68c4866db0",
          + "61b08676e4466d68c4866db1",
        ]
      ~ user_id  = "62ed115ab4b4017fa2a4b786" -> "61b7d915a14c2569ea9edde6" # forces replacement
        # (1 unchanged attribute hidden)
    }

how to modify the code that will create a new member only and not change the other members during its creation

0

1 Answer 1

1

This happens because membership_flat results in a list of maps. In a list, order is important. Thus its better to flatten your data, to get a map instead:

  membership_flat = merge([
    for team_name in local.team_names : {
      for user_email, roles in local.membership_nested[team_name] :
        "${team_name}-${user_email}" => {
          team_name = team_name
          user_name = user_email
          roles     = [roles]        
        }
    }
  ]...) # dots are important. Do not Remove them.

then

data "squadcast_team" "teams" {
  for_each = local.membership_flat 
  name     = each.value.team_name
}

data "squadcast_user" "users" {
  for_each = local.membership_flat 
  email    = each.value.user_name
}


resource "squadcast_team_member" "membership" {
  for_each = local.membership_flat 
  team_id  = data.squadcast_team.teams[each.key].id
  user_id  = data.squadcast_user.users[each.key].id
  role_ids = each.value.roles
  lifecycle {
    create_before_destroy = true
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.