Files
hunfabric/modules/project-factory/projects-defaults.tf
Julio Castillo 2eaa0d5e27 Add support for dynamic tags (#3897)
* Allow creation of dynamic tags

* Extend project factory and related modules to support dynamic values

* Extend folder and organization modules

* project and organization readme

* Simplify dynamic tag support and remove unnecessary restrictions

  • Schemas & Validations: Removed the restriction that forbade combining IAM fields with  allowed_values_regex  on tags. Updated validations in  project  and  organization  modules, and
  simplified all relevant JSON schemas.
  • Module Tag Bindings: Simplified the  tag_value  assignment in  folder ,  project ,  gcs ,  bigquery-dataset , and  kms  modules by removing the defensive  can(regex(...))  check and
  calling  templatestring  directly.
  • Outputs: Removed the  tags_dynamic  output from  project  and  organization  modules, as the same information is now available in  tag_keys .
  • Project Factory: Updated  tag_vars_projects  in  projects.tf  to use the native  namespaced_name  attribute and filtered manually for dynamic tags.

* fix(organization, project): fix linting and tests for dynamic tag support

- Align allowed_values_regex and description extraction in _tags_merged
  locals to use lookup() for consistency with other fields.
- Fix spacing in project context variable (alphabetical ordering).
- Update organization tags test to include the new cost_center tag key
  with allowed_values_regex.
- Update project tags test to include the new cost_center tag key and
  reflect the resolved allowed_values_regex on environment.

* refactor(gcs): refine tag bindings and fix context test

- Add _tag_bindings local to pre-resolve context references, enabling
  templatestring to receive a direct map reference (required by Terraform).
- Use var.context.tag_vars instead of the non-existent local.ctx.tag_vars.
- Fix HCL syntax in context.tfvars (escaped inner quotes).
- Update context test inventory to reflect 3 tag bindings including a
  dynamic value resolved via templatestring.

* refactor: align modules with tag binding context pattern

- Add _tag_bindings local + templatestring dance to cloud-run-v2,
  compute-vm, folder, kms modules (bigquery-dataset already had it)
- Exclude tag_vars from local.ctx in cloud-run-v2, compute-vm, folder,
  kms, project modules (bigquery-dataset already had it)
- Add tag_vars to context variable in cloud-run-v2, compute-vm modules
  (others already had it)
- Update all context tests with dynamic tag binding values using
  var.context.tag_vars

* docs: add module-level tftest.yaml test instructions to GEMINI.md

* docs: regenerate READMEs after tag-regex alignment

- Regenerate variable tables in 7 module READMEs to reflect
  line number shifts from prior tag-regex changes
- Add tag_vars exclusion to gcs ctx local
- Fix whitespace alignment in iam-service-account and
  project-factory tag_vars blocks
- Update tftest resource counts for organization and project
- Remove tags_dynamic from organization/project output tables

* fix(project-factory): update test inventory for tag_bindings module split

- Move tag binding address from folder-2 to folder-2-iam in test
  inventory (tag_bindings moved from creation to IAM modules)
- Update module instance count from 34 to 35
- Regenerate README tables after terraform fmt line shifts
- Apply terraform fmt to variables.tf

* refactor(project-factory): remove unnecessary depends_on from folder-iam modules

Folder IAM modules depend on their own folder creation modules, not
on module.projects. The explicit depends_on was leftover from an
earlier design.

* FAST stages

* Address review comments.

- FAST Stages:
  - Added tag_keys to output-files.tf in 0-org-setup to pass org tags via tfvars.
  - Sorted tag_keys and tag_values in output-files.tf.
  - Updated project-factory, networking, and security stages to use tag_keys.
  - Filtered tag_keys for dynamic tags only.
- Modules:
  - Excluded tag_vars from local.ctx in iam-service-account and organization.
  - Simplified tag_value in iam-service-account.
- Tests:
  - Updated test inventories for 0-org-setup and project-factory.

* Fix tf format

* Fix tfdoc

* docs: add ADR for templatestring vars convention and update status of base path ADR

* More tfdoc

* Update schemas

* Use endswith in context loop

* Address review

* Update FAST readmes

* Update last modules

* Terraform fmt

* Revert alloydb

* Fix whitespace

---------

Co-authored-by: Ludovico Magnocavallo <ludo@qix.it>
2026-04-24 20:45:45 +00:00

316 lines
12 KiB
HCL

/**
* Copyright 2026 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.
*/
# inputs:
# local._projects_input: raw projects data
# outputs:
# local.data_defaults: normalized defaults/overrides
# local._projects_output: normalized project data
locals {
_data_defaults = {
defaults = try(var.data_defaults, {})
overrides = try(var.data_overrides, {})
}
_projects_output = {
# Semantics of the merges are:
# - if data_overrides.<field> is not null, use this value
# - if _projects_inputs.<field> is not null, use this value
# - use data_default value, which if not set, will provide "empty" type
# This logic is easily implemented using coalesce, even on maps and list and allows to
# set data_overrides.<field> to "", [] or {} to ensure, that empty value is always passed, or do
# the same in _projects_input to prevent falling back to default value
for k, v in local._projects_input : k => merge(v, {
asset_feeds = try(v.asset_feeds, {})
billing_account = try(coalesce( # type: string
local.data_defaults.overrides.billing_account,
try(v.billing_account, null),
local.data_defaults.defaults.billing_account
), null)
deletion_policy = try(coalesce( # type: string
local.data_defaults.overrides.deletion_policy,
try(v.deletion_policy, null),
local.data_defaults.defaults.deletion_policy
), null)
contacts = coalesce( # type: map
local.data_defaults.overrides.contacts,
try(v.contacts, null),
local.data_defaults.defaults.contacts
)
factories_config = {
custom_roles = try(v.factories_config.custom_roles, null)
observability = try(v.factories_config.observability, null)
org_policies = try(v.factories_config.org_policies, null)
pam_entitlements = try(v.factories_config.pam_entitlements, null)
quotas = try(v.factories_config.quotas, null)
scc_mute_configs = try(v.factories_config.scc_mute_configs, null)
scc_sha_custom_modules = try(v.factories_config.scc_sha_custom_modules, null)
tags = try(v.factories_config.tags, null)
}
iam = try(v.iam, {}) # type: map(list(string))
iam_bindings = try(v.iam_bindings, {}) # type: map(object({...}))
iam_bindings_additive = try(v.iam_bindings_additive, {}) # type: map(object({...}))
iam_by_principals_additive = try(v.iam_by_principals_additive, {}) # type: map(list(string))
iam_by_principals = try(v.iam_by_principals, {}) # map(list(string))
iam_by_principals_conditional = try(v.iam_by_principals_conditional, {}) # map(object({...}))
kms = {
autokeys = try(v.kms.autokeys, {})
keyrings = try(v.kms.keyrings, {})
}
labels = coalesce( # type: map(string)
try(v.labels, null),
local.data_defaults.defaults.labels
)
logging_data_access = try(v.data_access_logs, {})
metric_scopes = coalesce( # type: list(string)
try(v.metric_scopes, null),
local.data_defaults.defaults.metric_scopes
)
descriptive_name = lookup(v, "descriptive_name", null)
name = lookup(v, "name", basename(k)) # type: string
org_policies = try(v.org_policies, {}) # type: map(object({...}))
parent = try( # type: string, nullable
coalesce(
local.data_defaults.overrides.parent,
try(v.parent, null),
local.data_defaults.defaults.parent
), null
)
prefix = try(
(
local.data_defaults.overrides.prefix != null
? local.data_defaults.overrides.prefix
: (
try(v.prefix, "-") == "-"
? local.data_defaults.defaults.prefix
: v.prefix
)
), null)
project_reuse = ( # type: object({...})
try(v.project_reuse, null) != null
? merge(
{
use_data_source = true
attributes = null
},
v.project_reuse
)
: local.data_defaults.defaults.project_reuse
)
quotas = try(v.quotas, {})
service_encryption_key_ids = coalesce( # type: map(list(string))
local.data_defaults.overrides.service_encryption_key_ids,
try(v.service_encryption_key_ids, null),
local.data_defaults.defaults.service_encryption_key_ids
)
services = coalesce( # type: list(string)
local.data_defaults.overrides.services,
try(v.services, null),
local.data_defaults.defaults.services
)
shared_vpc_host_config = ( # type: object({...})
try(v.shared_vpc_host_config, null) != null
? merge(
{ service_projects = [] },
v.shared_vpc_host_config
)
: null
)
shared_vpc_service_config = ( # type: object({...})
try(v.shared_vpc_service_config, null) != null
? {
host_project = try(v.shared_vpc_service_config.host_project, null)
iam_bindings_additive = try(v.shared_vpc_service_config.iam_bindings_additive, {})
network_users = try(v.shared_vpc_service_config.network_users, [])
service_agent_iam = try(v.shared_vpc_service_config.service_agent_iam, {})
service_agent_subnet_iam = try(v.shared_vpc_service_config.service_agent_subnet_iam, {})
service_iam_grants = try(v.shared_vpc_service_config.service_iam_grants, [])
network_subnet_users = try(v.shared_vpc_service_config.network_subnet_users, {})
}
: local.data_defaults.defaults.shared_vpc_service_config
)
tag_bindings = coalesce( # type: map(string)
local.data_defaults.overrides.tag_bindings,
try(v.tag_bindings, null),
local.data_defaults.defaults.tag_bindings
)
tags = {
for tag_name, tag_data in try(v.tags, {}) : tag_name => {
allowed_values_regex = try(tag_data.allowed_values_regex, null)
description = try(
tag_data.description,
"Managed by the Terraform project-factory module."
)
id = try(tag_data.id, null)
iam = try(tag_data.iam, {})
iam_bindings = try(tag_data.iam_bindings, {})
iam_bindings_additive = try(tag_data.iam_bindings_additive, {})
values = {
for value_name, value_data in try(tag_data.values, {}) :
value_name => {
description = try(
value_data.description,
"Managed by the Terraform project-factory module."
)
id = try(value_data.id, null)
iam = try(value_data.iam, {})
iam_bindings = try(value_data.iam_bindings, {})
iam_bindings_additive = try(value_data.iam_bindings_additive, {})
}
}
}
}
universe = (
local.data_defaults.overrides.universe != null
? local.data_defaults.overrides.universe
: (
try(v.universe, null) != null
? v.universe
: local.data_defaults.defaults.universe
)
)
vpc_sc = (
local.data_defaults.overrides.vpc_sc != null
? local.data_defaults.overrides.vpc_sc
: (
try(v.vpc_sc, null) != null
? merge(
{
perimeter_name = null
is_dry_run = false
},
v.vpc_sc
)
: local.data_defaults.defaults.vpc_sc
)
)
workload_identity_pools = {
for wk, wv in try(v.workload_identity_pools, {}) : wk => {
display_name = lookup(wv, "display_name", null)
description = lookup(wv, "description", null)
disabled = lookup(wv, "disabled", null)
providers = {
for pk, pv in try(wv.providers, {}) : pk => {
display_name = lookup(pv, "display_name", null)
description = lookup(pv, "description", null)
disabled = lookup(pv, "disabled", null)
attribute_condition = lookup(pv, "attribute_condition", null)
attribute_mapping = lookup(pv, "attribute_mapping", {})
identity_provider = lookup(pv, "identity_provider", {})
}
}
}
}
})
}
# tflint-ignore: terraform_unused_declarations
_projects_uniqueness_validation = {
# will raise error, if the same project (derived from file name, or provided in the YAML file)
# is used more than once
for k, v in local._projects_output :
"${v.prefix != null ? v.prefix : ""}-${v.name}" => k
}
data_defaults = {
defaults = merge(
{
billing_account = null
contacts = {}
deletion_policy = null
labels = {}
locations = {
bigquery = try(local._data_defaults.defaults.locations.bigquery, null)
logging = try(local._data_defaults.defaults.locations.logging, null)
storage = try(local._data_defaults.defaults.locations.storage, null)
}
metric_scopes = []
parent = null
prefix = null
project_reuse = merge(
{
use_data_source = true
attributes = null
},
try(local._data_defaults.defaults.project_reuse, {
use_data_source = true
attributes = null
}
)
)
service_encryption_key_ids = {}
services = []
shared_vpc_service_config = {
host_project = try(local._data_defaults.defaults.shared_vpc_service_config.host_project, null)
iam_bindings_additive = try(local._data_defaults.defaults.shared_vpc_service_config.iam_bindings_additive, {})
network_users = try(local._data_defaults.defaults.shared_vpc_service_config.network_users, [])
service_agent_iam = try(local._data_defaults.defaults.shared_vpc_service_config.service_agent_iam, {})
service_agent_subnet_iam = try(local._data_defaults.defaults.shared_vpc_service_config.service_agent_subnet_iam, {})
service_iam_grants = try(local._data_defaults.defaults.shared_vpc_service_config.service_iam_grants, [])
network_subnet_users = try(local._data_defaults.defaults.shared_vpc_service_config.network_subnet_users, {})
}
tag_bindings = {}
service_accounts = {}
universe = null
vpc_sc = merge(
{
perimeter_name = null
is_dry_run = false
},
try(local._data_defaults.defaults.vpc_sc, {
perimeter_name = null
is_dry_run = false
}
)
)
},
try(
local._data_defaults.defaults, {}
)
)
# data_overrides default to null's, to mark that they should not override
overrides = merge({
billing_account = null
contacts = null
deletion_policy = null
locations = {
bigquery = try(local._data_defaults.overrides.locations.bigquery, null)
logging = try(local._data_defaults.overrides.locations.logging, null)
storage = try(local._data_defaults.overrides.locations.storage, null)
}
parent = null
prefix = null
service_encryption_key_ids = null
tag_bindings = null
services = null
service_accounts = null
universe = null
vpc_sc = try(
merge(
{
perimeter_name = null
is_dry_run = false
},
local._data_defaults.overrides.vpc_sc
),
null
)
},
try(
local._data_defaults.overrides, {}
)
)
}
}