Files
hunfabric/modules/project/main.tf
Joshua Wright 325a997d79 Add Alerts, Logging, Channels Factories (#2758)
* WIP: Logging Alerts Factory

* Implement Logging Alerts on Remaining Modules

* Documentation & FMT

* Convert To Multiple Factories

* Correct Project

* Update Documentation

* Update modules/project/alerts-factory.tf

Co-authored-by: Julio Castillo <jccb@google.com>

* Update fast/stages/0-bootstrap/data/logging-metrics/compliance.yaml

Co-authored-by: Julio Castillo <jccb@google.com>

* Update Tests, Resources

* tests

* Fix Tests

* Fix formatting

* Reformat metric filters

* Formatting, reordering, and small fixes

* Bring back alerts and metrics documentation

* Revert change bootstrap outputs.tf

* Fix project notification channel vars and factories

* Fix vars and factory for logging alerts

* Complete alert variable and factory

* Reorder fields

* Update readme

* Reorder variables

* Add schemas, update README, and fix some types

* Remove default alerts email from project and project-factory

* Move observability factory to a single file

* Add outputs to project module

* Add factories_config to PF data_defaults and data_overrides

* Reorder PF field processing

* Revert fast/ to master.

We'll do observability stuff in a separate PR

* Remove observability from FAST

* Remove new FAST tests

* Remove unused local

* Fix tests

---------

Co-authored-by: Julio Castillo <jccb@google.com>
Co-authored-by: Ludovico Magnocavallo <ludomagno@google.com>
2025-01-05 19:49:20 +00:00

114 lines
4.0 KiB
HCL

/**
* Copyright 2025 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.
*/
locals {
descriptive_name = (
var.descriptive_name != null ? var.descriptive_name : "${local.prefix}${var.name}"
)
observability_factory_data_raw = [
for f in try(fileset(var.factories_config.observability, "*.yaml"), []) :
yamldecode(file("${var.factories_config.observability}/${f}"))
]
parent_type = var.parent == null ? null : split("/", var.parent)[0]
parent_id = var.parent == null ? null : split("/", var.parent)[1]
prefix = var.prefix == null ? "" : "${var.prefix}-"
project = (
var.project_create ?
{
project_id = try(google_project.project[0].project_id, null)
number = try(google_project.project[0].number, null)
name = try(google_project.project[0].name, null)
}
: {
project_id = "${local.prefix}${var.name}"
number = try(data.google_project.project[0].number, null)
name = try(data.google_project.project[0].name, null)
}
)
}
data "google_project" "project" {
count = var.project_create ? 0 : 1
project_id = "${local.prefix}${var.name}"
}
resource "google_project" "project" {
count = var.project_create ? 1 : 0
org_id = local.parent_type == "organizations" ? local.parent_id : null
folder_id = local.parent_type == "folders" ? local.parent_id : null
project_id = "${local.prefix}${var.name}"
name = local.descriptive_name
billing_account = var.billing_account
auto_create_network = var.auto_create_network
labels = var.labels
deletion_policy = var.deletion_policy
lifecycle {
precondition {
condition = var.skip_delete == null
error_message = "skip_delete is deprecated. Use deletion_policy."
}
}
}
resource "google_project_service" "project_services" {
for_each = toset(var.services)
project = local.project.project_id
service = each.value
disable_on_destroy = var.service_config.disable_on_destroy
disable_dependent_services = var.service_config.disable_dependent_services
depends_on = [google_org_policy_policy.default]
}
resource "google_compute_project_metadata_item" "default" {
for_each = (
contains(var.services, "compute.googleapis.com") ? var.compute_metadata : {}
)
project = local.project.project_id
key = each.key
value = each.value
depends_on = [google_project_service.project_services]
}
resource "google_resource_manager_lien" "lien" {
count = var.lien_reason != null ? 1 : 0
parent = "projects/${local.project.number}"
restrictions = ["resourcemanager.projects.delete"]
origin = "created-by-terraform"
reason = var.lien_reason
}
resource "google_essential_contacts_contact" "contact" {
provider = google-beta
for_each = var.contacts
parent = "projects/${local.project.project_id}"
email = each.key
language_tag = "en"
notification_category_subscriptions = each.value
depends_on = [
google_project_iam_binding.authoritative,
google_project_iam_binding.bindings,
google_project_iam_member.bindings
]
}
resource "google_monitoring_monitored_project" "primary" {
provider = google-beta
for_each = toset(var.metric_scopes)
metrics_scope = each.value
name = local.project.project_id
}