-
Notifications
You must be signed in to change notification settings - Fork 770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
github_team.id can't be used in a for_each expression to create multiple github_team_repository resources #500
Comments
hi @jspiro, thank you for creating this issue! re:
using the example you provided, a map created with regarding the error log, however, a For 1 team: data "github_team" "writers" {
slug = "foo-bar"
}
locals {
teams = [data.github_team.writers.id]
}
resource "github_team_repository" "writers" {
count = length(local.teams)
team_id = local.teams[count.index]
repository = "repo"
permission = "push"
} For more than 1 team: data "github_team" "writers" {
slug = "foo-bar"
}
data "github_team" "writers_new" {
slug = "foo-bar-new"
}
locals {
teams = [data.github_team.writers.id, data.github_team.writers_new.id]
}
resource "github_team_repository" "writers" {
count = length(local.teams)
team_id = local.teams[count.index]
repository = "repo"
permission = "push"
} hope this helps! if any further questions arise, let me know 😄 |
@anGie44 Hey, thanks so much for the detailed reply! I had not thought to try it with It's possibly a bug in terraform, or possibly by design (hashicorp/terraform#23529 or hashicorp/terraform#4149)–unclear. I brought it here in case anyone else had tried it (I imagine many who use this module would have encountered the use case) and had suggestions. If there isn't anything that can be done in the Go for the provider, then perhaps it would be worth documenting the limitation with What do you think? |
Happy to add this limitation / workaround to our docs. Will put something up for review for the next release. |
To add some further validation to this general approach, here are some similar snippets from our module. Array of objects that represent a team and the permission to grant: # snip from permissions.tf in modules/repository/permissions.tf
resource "github_team_repository" "permission" {
for_each = { for team in var.additional_permissions : team.team_id => team }
# we need to use an ID but this is a poor user experience to have to look it up.
# we may want to suggest using local variables rather than the standard .tfvars
team_id = each.value.team_id
repository = github_repository.repo.name
permission = each.value.permission
} The caller then passes in a set of maps: # snip from modules/repository/variables.tf
variable "additional_permissions" {
description = <<DESCRIPTION
This is an array of mappings of teams with permissions for the repository
for example:
additional_users = [
{
"team_id" = data.github_team.one.id,
"permission" = "push"
},
{
"team_id" = data.github_team.two.id,
"permission" = "admin"
},
{
"team_id" = "1234567",
"permission = "pull"
}
]
DESCRIPTION
# we may wish to remove the default in the future
# we will make the breaking changes later
default = []
type = list(
object(
{
# TODO: keep an eye on new expiremental feature that would be nice here
# https://www.terraform.io/docs/configuration/functions/defaults.html
team_id = string,
permission = string
}
)
)
} Also another good note is to avoid (where possible) data sources within your modules if you need to instantiate it a bunch of times you will likely exhaust your github api rate limit budget. |
👋 Hey Friends, this issue has been automatically marked as |
👋 Hey Friends, this issue has been automatically marked as |
Terraform Version
0.12.28
Affected Resource(s)
Terraform Configuration Files
Not working:
Working:
Expected Behavior
I've been trying to do a simple thing: Provide a list (whether data or resource) of teams to github_team_repository using 0.12
for_each
syntax. I expect that thefor_each
syntax would work.Actual Behavior
Steps to Reproduce
terraform plan
Important Factoids
I built a team module that handles membership, and a repo module that hooks up those teams to admin/writer/reader roles. Passing one team into the other is a natural thing to do.
But TF cannot seem to get the dependencies right. It doesn't infer that the data needs to be looked up first, or that it's a computed value, or the team needs to be created first (if using github_team resources) when given in a
for_each
, but it works perfectly if I provide the team id directly.I've tried to provide the ID a dozen different ways, through different vars, locals, lists, sets, you name it. I've tried elaborate and explicit layers of
depends_on
in both variables, resources, and the like. The above is the simplest reproduction.Workaround
If I create the teams first, and then hook them up to the repo, no problem.
But it's not really possible to run this in a CI environment without creating the teams first in one PR, then the repos in another – that's two different PRs, two different applications–I can't get my team to accept the workaround, they'd rather dump terraform.
Ultimately, this is annoying and should work, but maybe I'm missing something obvious. At this point I've taken it personally and have spent hours trying to figure it out 😩
References
Are there any other GitHub issues (open or closed) or Pull Requests that should be linked here? For example:
The text was updated successfully, but these errors were encountered: