Hi @kpfleming,
It looks like your goal here is to retrieve some data about a set of users in your var.maintainers
and then make use of it in a team membership. That’s not a problem that templatefile
will help you solve, but here’s a different way to do it:
variable "maintainers" {
# It looks like you're only using this variable as
# a set, so might as well just declare it as one so
# the module caller can see that the ordering isn't
# important.
type = set(string)
}
data "github_user" "maintainer" {
for_each = var.maintainers
username = each.value
}
resource "github_team_membership" "team" {
for_each = data.github_user.membership
username = each.value.username
# etc, etc
}
Notice that the for_each
expression for the membership is data.github_user.membership
rather than var.maintainers
. That means that inside that block each.value
is an object representing a data "github_user"
result, and so you don’t need to do anything special to select the object by username.
This works because for any resource block where for_each
is set the resulting value is a map of objects, where the keys of that map are the keys in the for_each
expression. That means that your data source results will have addresses like data.github_user.maintainer["username"]
.
With that said, I assumed above that var.maintainers
is a set of GitHub usernames, in which case retreiving the github_user
objects this way would seem unnecessary: you already know the usernames and could just use them directly in github_team_membership
like this:
variable "maintainers" {
type = set(string)
}
resource "github_team_membership" "team" {
for_each = var.maintainers
username = each.key
# etc, etc
}
If your goal here was to connect some other identifier used within your organization with github usernames, there’s a slight variant of the above where you could set var.maintainers
to be a mapping from your internal identifiers to GitHub’s usernames. For the sake of example, I’m going to say that these “internal identifiers” are corporate email addresses under example.com
:
variable "maintainers" {
type = map(string)
description = "Mapping from corporate email address to GitHub username."
}
resource "github_team_membership" "team" {
for_each = var.maintainers
username = each.value.username
# etc, etc
}
Notice that only the type of var.maintainers
has changed above. Let’s say for example that the value of var.maintainers
is something like this:
{
"kevin.p.fleming@example.com" = "kpfleming"
"martin.atkins@example.com" = "apparentlymart"
}
In the above, we’d end up with resource addresses like github_team_membership.team["kevin.p.fleming@example.com"]
, but the username
inside would be kpfleming
, which thus allows the broader Terraform configuration to talk about the users in terms of their corporate email addresses while making sure that they are all mapped to the right GitHub users when interacting with GitHub’s API.