forked from GoogleCloudPlatform/cloud-foundation-fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
109 lines (100 loc) · 3.84 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* Copyright 2024 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}"
)
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
}