Provisioning Teams in GitHub using Terraform - team hierarchy

Viewed 109

I am attempting to use the github terraform provider to simplify how we manage teams in our GitHub Organization. To reduce the amount of changes to the Terraform code I wanted to use a solution like https://learn.hashicorp.com/tutorials/terraform/github-user-teams where contributors would only need to change values in a csv file.

The example does not include a solution for having teams be members of teams. Looking at the provider documentation https://registry.terraform.io/providers/integrations/github/latest/docs/resources/team, it requires parent_team_id be provided.

My current approach is to have two CSV files, one with parent team name and one without, and build a local mapping. The issue is that when I apply I get the error

The "for_each" value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be created. To work around this, use the -target argument to first apply only the resources that the for_each depends on.

Can someone point out where I am going wrong, it will be something simple but I cannot see what.

NOTE: I am aware (after posting) that I am not actually using the parent_team_id in the second team resource, thats a simple fix once I get the local working.

Resources

resource "github_team" "l0" {
  for_each = {
    for team in csvdecode(file("teams.csv")) :
    team.name => team
  }

  name                      = each.value.name
  description               = each.value.description
  privacy                   = each.value.privacy
  create_default_maintainer = true
}

resource "github_team" "l1" {
  for_each = {
    for team in local.l1_mapping : team.name => team
  }

  name                      = each.value.name
  description               = each.value.description
  privacy                   = each.value.privacy
  create_default_maintainer = true
  depends_on = [
    github_team.l0
  ]
}

Locals

# Create local values to retrieve items from CSVs
locals {

  l1_team_plus = {
      for file2 in csvdecode(file("l1_teams.csv")) :
      file2.name => file2
  }

  l1_mapping = flatten([
    for team2 in local.l1_team_plus : [
      for t in github_team.l0 : {
        id      = t.id
        slug    = t.slug
        team2 = team2
      } if t.slug == team2.name
    ]
  ])

}
0 Answers
Related