2020-08-29 02:29:46 -07:00
|
|
|
/**
|
2022-01-01 06:52:31 -08:00
|
|
|
* Copyright 2022 Google LLC
|
2020-08-29 02:29:46 -07:00
|
|
|
*
|
|
|
|
* 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 {
|
|
|
|
projects = (
|
|
|
|
var.quota_config.projects == null
|
|
|
|
? [var.project_id]
|
|
|
|
: var.quota_config.projects
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
module "project" {
|
2022-01-10 06:09:38 -08:00
|
|
|
source = "../../../modules/project"
|
2020-08-29 02:29:46 -07:00
|
|
|
name = var.project_id
|
|
|
|
project_create = var.project_create
|
|
|
|
services = [
|
|
|
|
"compute.googleapis.com",
|
|
|
|
"cloudfunctions.googleapis.com"
|
|
|
|
]
|
|
|
|
service_config = {
|
|
|
|
disable_on_destroy = false,
|
|
|
|
disable_dependent_services = false
|
|
|
|
}
|
2020-11-04 07:49:55 -08:00
|
|
|
iam = {
|
2020-08-29 02:29:46 -07:00
|
|
|
"roles/monitoring.metricWriter" = [module.cf.service_account_iam_email]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module "pubsub" {
|
2022-01-10 06:09:38 -08:00
|
|
|
source = "../../../modules/pubsub"
|
2020-08-29 02:29:46 -07:00
|
|
|
project_id = module.project.project_id
|
|
|
|
name = var.name
|
|
|
|
subscriptions = {
|
|
|
|
"${var.name}-default" = null
|
|
|
|
}
|
|
|
|
# the Cloud Scheduler robot service account already has pubsub.topics.publish
|
|
|
|
# at the project level via roles/cloudscheduler.serviceAgent
|
|
|
|
}
|
|
|
|
|
|
|
|
module "cf" {
|
2022-01-10 06:09:38 -08:00
|
|
|
source = "../../../modules/cloud-function"
|
2020-08-29 02:29:46 -07:00
|
|
|
project_id = module.project.project_id
|
|
|
|
name = var.name
|
|
|
|
bucket_name = "${var.name}-${random_pet.random.id}"
|
|
|
|
bucket_config = {
|
|
|
|
location = var.region
|
|
|
|
lifecycle_delete_age = null
|
|
|
|
}
|
|
|
|
bundle_config = {
|
2021-11-07 01:46:22 -08:00
|
|
|
source_dir = "cf"
|
|
|
|
output_path = var.bundle_path
|
|
|
|
excludes = null
|
2020-08-29 02:29:46 -07:00
|
|
|
}
|
2020-08-31 22:52:48 -07:00
|
|
|
# https://github.com/hashicorp/terraform-provider-archive/issues/40
|
|
|
|
# https://issuetracker.google.com/issues/155215191
|
2020-08-31 22:50:04 -07:00
|
|
|
environment_variables = {
|
|
|
|
USE_WORKER_V2 = "true"
|
|
|
|
PYTHON37_DRAIN_LOGS_ON_CRASH_WAIT_SEC = "5"
|
|
|
|
}
|
2020-08-29 02:29:46 -07:00
|
|
|
service_account_create = true
|
|
|
|
trigger_config = {
|
|
|
|
event = "google.pubsub.topic.publish"
|
|
|
|
resource = module.pubsub.topic.id
|
|
|
|
retry = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_cloud_scheduler_job" "job" {
|
|
|
|
project = var.project_id
|
|
|
|
region = var.region
|
|
|
|
name = var.name
|
|
|
|
schedule = var.schedule_config
|
|
|
|
time_zone = "UTC"
|
|
|
|
|
|
|
|
pubsub_target {
|
|
|
|
attributes = {}
|
|
|
|
topic_name = module.pubsub.topic.id
|
|
|
|
data = base64encode(jsonencode({
|
|
|
|
gce_project = var.quota_config.projects
|
|
|
|
gce_region = var.quota_config.regions
|
|
|
|
keywords = var.quota_config.filters
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_project_iam_member" "network_viewer" {
|
|
|
|
for_each = toset(local.projects)
|
|
|
|
project = each.key
|
|
|
|
role = "roles/compute.networkViewer"
|
|
|
|
member = module.cf.service_account_iam_email
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_project_iam_member" "quota_viewer" {
|
|
|
|
for_each = toset(local.projects)
|
|
|
|
project = each.key
|
|
|
|
role = "roles/servicemanagement.quotaViewer"
|
|
|
|
member = module.cf.service_account_iam_email
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_monitoring_alert_policy" "alert_policy" {
|
|
|
|
project = module.project.project_id
|
|
|
|
display_name = "Quota monitor"
|
|
|
|
combiner = "OR"
|
|
|
|
conditions {
|
|
|
|
display_name = "simple quota threshold"
|
|
|
|
condition_threshold {
|
|
|
|
filter = "metric.type=\"custom.googleapis.com/quota/gce\" resource.type=\"global\""
|
|
|
|
threshold_value = 0.75
|
|
|
|
comparison = "COMPARISON_GT"
|
|
|
|
duration = "0s"
|
|
|
|
aggregations {
|
|
|
|
alignment_period = "60s"
|
|
|
|
group_by_fields = []
|
|
|
|
per_series_aligner = "ALIGN_MEAN"
|
|
|
|
}
|
|
|
|
trigger {
|
|
|
|
count = 1
|
|
|
|
percent = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enabled = false
|
|
|
|
user_labels = {
|
|
|
|
name = var.name
|
|
|
|
}
|
|
|
|
documentation {
|
|
|
|
content = "GCE quota over threshold."
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "random_pet" "random" {
|
|
|
|
length = 1
|
|
|
|
}
|