-
Notifications
You must be signed in to change notification settings - Fork 1
/
workflows.tf
100 lines (89 loc) · 3.83 KB
/
workflows.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
/**
* Copyright 2023 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.
*/
# Set up the Workflow
## Create the Workflows service account to manage permissions
resource "google_service_account" "workflow_service_account" {
project = module.project-services.project_id
account_id = "cloud-workflow-sa-${random_id.id.hex}"
display_name = "Service Account for Cloud Workflows"
depends_on = [time_sleep.wait_after_apis]
}
## Grant the Workflow service account access needed to execute its tasks
resource "google_project_iam_member" "workflow_service_account_roles" {
for_each = toset([
"roles/workflows.admin",
"roles/run.invoker",
"roles/cloudfunctions.invoker",
"roles/iam.serviceAccountTokenCreator",
"roles/storage.objectAdmin",
"roles/bigquery.connectionAdmin",
"roles/bigquery.jobUser",
"roles/bigquery.dataEditor",
]
)
project = module.project-services.project_id
role = each.key
member = "serviceAccount:${google_service_account.workflow_service_account.email}"
}
## Create the workflow
resource "google_workflows_workflow" "workflow" {
name = "initial-workflow"
project = module.project-services.project_id
region = var.region
description = "Runs post Terraform setup steps for Solution in Console"
service_account = google_service_account.workflow_service_account.id
source_contents = templatefile("${path.module}/templates/workflow.tftpl", {
dataset_id = google_bigquery_dataset.demo_dataset.dataset_id
})
depends_on = [
google_service_account.workflow_service_account,
google_bigquery_connection.function_connection,
google_bigquery_routine.image_create_remote_function_sp,
google_bigquery_routine.image_query_remote_function_sp,
google_bigquery_routine.provision_text_sample_table_sp,
google_bigquery_routine.text_create_remote_function_sp,
google_bigquery_routine.text_query_remote_function_sp,
google_bigquery_table.object_table,
google_cloudfunctions2_function.image_remote_function,
google_cloudfunctions2_function.text_remote_function,
google_storage_bucket_object.image_upload,
google_project_iam_member.functions_invoke_roles,
time_sleep.wait_after_functions,
]
}
## Trigger the execution of the setup workflow
data "http" "call_workflows_setup" {
url = "https://workflowexecutions.googleapis.com/v1/projects/${module.project-services.project_id}/locations/${var.region}/workflows/${google_workflows_workflow.workflow.name}/executions"
method = "POST"
request_headers = {
Accept = "application/json"
Authorization = "Bearer ${data.google_client_config.current.access_token}" }
depends_on = [
google_workflows_workflow.workflow,
google_bigquery_connection.function_connection,
google_bigquery_routine.image_create_remote_function_sp,
google_bigquery_routine.image_query_remote_function_sp,
google_bigquery_routine.provision_text_sample_table_sp,
google_bigquery_routine.text_create_remote_function_sp,
google_bigquery_routine.text_query_remote_function_sp,
google_bigquery_table.object_table,
google_cloudfunctions2_function.image_remote_function,
google_cloudfunctions2_function.text_remote_function,
google_storage_bucket_object.image_upload,
google_project_iam_member.functions_invoke_roles,
time_sleep.wait_after_functions,
]
}