This is for setting up our users and teams in github with terraform.
(provider github 2.8.0)
Basically I want to define all users with one structure like:
gh_users = {
user_one = {
username = "user1",
main_role = "member",
teams = {
team1 = {
role = "member"
}
}
},
user_two = {
username = "user2",
main_role = "admin",
teams = {
team1 = {
role = "maintainer"
},
team2 = {
role = "maintainer"
}
}
}
}
Using maps to be able to reference all steps by names and use for_each.
Im now trying to create resources, and for creating users it works fine:
resource "github_membership" "users" {
for_each = var.gh_users
username = each.key
role = each.value.main_role
}
But when I want to assign the members to groups (resource github_team_membership) it is not as straightforward…
For this I need “team_ids”, “usernames”, and “user roles”, all which is defined in the structure above, but I just cant wrap my head around how.
Im trying to create another local map team_members with team-user-role information with a nested for loop, and then doing a for_each over that map, like this:
resource "github_team_membership" "members" {
for_each = local.team_members
team_id = each.value.team_name
username = each.value.username
role = each.value.role
}
locals {
team_members = {
for user, user_info in var.gh_users: [
for team, team_data in user_info.teams: {
"${user}-${team}" = {
"team_name" = team,
"username" = user_info.username,
"role" = team_data.role
}
}
]
}
}
Here I get: “Key expression is required when building an object.”
I have tried a few other ways but all fails so if somebody can show me here I would be glad! (I have full control over the data structure so changes there can be made as well…)