Files
hunfabric/modules/iam-service-account/iam.tf
Simon Roberts 26dbaa2d6e Enable terraform_naming_convention in tflint (#3930)
* Draft terraform_naming_convention

* Two fast/stages fixes for terraform_naming_convention

* Disable terraform_naming_convention for resources for now

* module fixes for terraform_naming_convention

* tfdoc

* Remove "moved" from recipe and needs-fixing

* Fix moved for spoke_ra

* fix tests

* Use default (snake_case) for resources

* factory.terraform_data.project-preconditions

* First-pass migration of resources + tests

* Fix tests/modules/organization

* Require snake_case for variables; Add annotations for _testing

* permit _fast_debug variable

* Fix net_vpc_factory and net_vpc_firewall tests

* tfdoc addons and recipe

* Fix more tests

* Fix some net-global -> net_global tests

---------

Co-authored-by: Ludovico Magnocavallo <ludomagno@google.com>
2026-05-06 06:06:26 +00:00

249 lines
6.6 KiB
HCL

/**
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
# tfdoc:file:description IAM bindings.
locals {
_iam_principal_roles = distinct(flatten(values(var.iam_by_principals)))
_iam_principals = {
for r in local._iam_principal_roles : r => [
for k, v in var.iam_by_principals :
k if try(index(v, r), null) != null
]
}
iam = {
for role in distinct(concat(keys(var.iam), keys(local._iam_principals))) :
role => concat(
try(var.iam[role], []),
try(local._iam_principals[role], [])
)
}
iam_billing_pairs = flatten([
for entity, roles in var.iam_billing_roles : [
for role in roles : [
{ entity = entity, role = role }
]
]
])
iam_bindings_additive = merge(
var.iam_bindings_additive,
[
for principal, roles in var.iam_by_principals_additive : {
for role in roles :
"iam-bpa:${principal}-${role}" => {
member = principal
role = role
condition = null
}
}
]...
)
iam_folder_pairs = flatten([
for entity, roles in var.iam_folder_roles : [
for role in roles : [
{ entity = entity, role = role }
]
]
])
iam_organization_pairs = flatten([
for entity, roles in var.iam_organization_roles : [
for role in roles : [
{ entity = entity, role = role }
]
]
])
iam_project_pairs = flatten([
for entity, roles in var.iam_project_roles : [
for role in roles : [
{ entity = entity, role = role }
]
]
])
iam_sa_pairs = flatten([
for entity, roles in var.iam_sa_roles : [
for role in roles : [
{ entity = entity, role = role }
]
]
])
iam_storage_pairs = flatten([
for entity, roles in var.iam_storage_roles : [
for role in roles : [
{ entity = entity, role = role }
]
]
])
}
resource "google_service_account_iam_binding" "authoritative" {
for_each = local.iam
service_account_id = try(
local.service_account.name, local.static_id
)
role = lookup(local.ctx.custom_roles, each.key, each.key)
members = [
for v in each.value :
lookup(local.ctx.iam_principals, v, v)
]
}
resource "google_service_account_iam_binding" "bindings" {
for_each = var.iam_bindings
service_account_id = try(
local.service_account.name, local.static_id
)
role = lookup(
local.ctx.custom_roles, each.value.role, each.value.role
)
members = [
for v in each.value.members : lookup(local.ctx.iam_principals, v, v)
]
dynamic "condition" {
for_each = each.value.condition == null ? [] : [""]
content {
expression = templatestring(
each.value.condition.expression, var.context.condition_vars
)
title = each.value.condition.title
description = each.value.condition.description
}
}
}
resource "google_service_account_iam_member" "bindings" {
for_each = local.iam_bindings_additive
service_account_id = try(
local.service_account.name, local.static_id
)
role = lookup(
local.ctx.custom_roles, each.value.role, each.value.role
)
member = lookup(
local.ctx.iam_principals, each.value.member, each.value.member
)
dynamic "condition" {
for_each = each.value.condition == null ? [] : [""]
content {
expression = templatestring(
each.value.condition.expression, var.context.condition_vars
)
title = each.value.condition.title
description = each.value.condition.description
}
}
}
moved {
from = google_billing_account_iam_member.billing-roles
to = google_billing_account_iam_member.billing_roles
}
resource "google_billing_account_iam_member" "billing_roles" {
for_each = {
for pair in local.iam_billing_pairs :
"${pair.entity}-${pair.role}" => pair
}
billing_account_id = each.value.entity
role = lookup(
local.ctx.custom_roles, each.value.role, each.value.role
)
member = local.iam_email
}
moved {
from = google_folder_iam_member.folder-roles
to = google_folder_iam_member.folder_roles
}
resource "google_folder_iam_member" "folder_roles" {
for_each = {
for pair in local.iam_folder_pairs :
"${pair.entity}-${pair.role}" => pair
}
folder = lookup(local.ctx.folder_ids, each.value.entity, each.value.entity)
role = lookup(
local.ctx.custom_roles, each.value.role, each.value.role
)
member = local.iam_email
}
moved {
from = google_organization_iam_member.organization-roles
to = google_organization_iam_member.organization_roles
}
resource "google_organization_iam_member" "organization_roles" {
for_each = {
for pair in local.iam_organization_pairs :
"${pair.entity}-${pair.role}" => pair
}
org_id = each.value.entity
role = lookup(
local.ctx.custom_roles, each.value.role, each.value.role
)
member = local.iam_email
}
moved {
from = google_project_iam_member.project-roles
to = google_project_iam_member.project_roles
}
resource "google_project_iam_member" "project_roles" {
for_each = {
for pair in local.iam_project_pairs :
"${pair.entity}-${pair.role}" => pair
}
project = lookup(local.ctx.project_ids, each.value.entity, each.value.entity)
role = lookup(
local.ctx.custom_roles, each.value.role, each.value.role
)
member = local.iam_email
}
resource "google_service_account_iam_member" "additive" {
for_each = {
for pair in local.iam_sa_pairs :
"${pair.entity}-${pair.role}" => pair
}
service_account_id = lookup(
local.ctx.service_account_ids, each.value.entity, each.value.entity
)
role = lookup(
local.ctx.custom_roles, each.value.role, each.value.role
)
member = local.iam_email
}
moved {
from = google_storage_bucket_iam_member.bucket-roles
to = google_storage_bucket_iam_member.bucket_roles
}
resource "google_storage_bucket_iam_member" "bucket_roles" {
for_each = {
for pair in local.iam_storage_pairs :
"${pair.entity}-${pair.role}" => pair
}
bucket = lookup(
local.ctx.storage_buckets, each.value.entity, each.value.entity
)
role = lookup(
local.ctx.custom_roles, each.value.role, each.value.role
)
member = local.iam_email
}