-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path04-publisher.tf
191 lines (177 loc) · 6.32 KB
/
04-publisher.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# Samuel Berthollier - 2024
#
# Unless required by applicable law or agreed to in writing, software
# distributed is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
# OR CONDITIONS OF ANY KIND, either express or implied.
# Setting local variables for IAM
locals {
prc_services = [
"bigquerydatatransfer.googleapis.com",
"bigquery.googleapis.com",
"bigquerystorage.googleapis.com",
"iam.googleapis.com",
"analyticshub.googleapis.com",
"storage.googleapis.com",
"storage-component.googleapis.com",
]
iam_prc = {
"roles/bigquery.admin" = [
module.prc-sa-0.iam_email
]
"roles/analyticshub.viewer" = [
module.prc-sa-0.iam_email
]
"roles/analyticshub.subscriber" = [
module.prc-sa-0.iam_email
]
"roles/analyticshub.publisher" = [
module.prc-sa-0.iam_email
]
"roles/storage.admin" = [
module.prc-sa-0.iam_email
]
"roles/iam.serviceAccountTokenCreator" = [
module.prc-sa-0.iam_email
]
}
publisher_schema_users = jsonencode([
{ name = "id", type = "INT64" },
{ name = "hashed_email", type = "STRING" },
{ name = "age", type = "INT64" },
{ name = "gender", type = "STRING" },
{ name = "city", type = "STRING" },
{ name = "country", type = "STRING" },
{ name = "traffic_source", type = "STRING" },
])
}
# Defining the project for a publisher to the clean room
module "prc-project" {
source = "../modules/project"
parent = var.project_config.parent
billing_account = var.project_config.billing_account_id
project_create = var.project_config.billing_account_id != null
prefix = (
var.project_config.billing_account_id == null ? null : var.prefix
)
name = (
var.project_config.billing_account_id == null
? var.project_config.project_ids.processing
: "${var.project_config.project_ids.processing}${local.project_suffix}"
)
iam = (
var.project_config.billing_account_id != null ? {} : local.iam_prc
)
services = local.prc_services
}
module "prc-sa-0" {
source = "../modules/iam-service-account"
project_id = module.prc-project.project_id
prefix = var.prefix
name = "prc-sa-0"
display_name = "Publisher zone service account."
}
resource "google_project_iam_member" "iam-prc" {
depends_on = [module.prc-sa-0]
project = module.prc-project.project_id
for_each = { for role, members in local.iam_prc : role => members }
role = each.key
member = each.value[0]
}
# Creating a Cloud Storage bucket to host the data from the publisher
module "prc-cs-0" {
source = "../modules/gcs"
project_id = module.prc-project.project_id
prefix = var.prefix
name = "prc-cs-0"
location = var.location
storage_class = "MULTI_REGIONAL"
objects_to_upload = {
sample-data = {
name = "users.csv"
source = "assets/users.csv"
content_type = "text/csv"
}
}
}
# Creating a dataset to host the data from the publisher and transfer the data from the Cloud Storage
module "publisher-dataset" {
source = "../modules/bigquery-dataset"
project_id = module.prc-project.project_id
id = "publisher_dataset"
location = var.location
options = {
default_table_expiration_ms = null
default_partition_expiration_ms = null
delete_contents_on_destroy = var.delete_contents_on_destroy
max_time_travel_hours = null
}
tables = {
users = {
friendly_name = "users"
schema = local.publisher_schema_users
deletion_protection = var.deletion_protection
}
}
}
resource "google_bigquery_data_transfer_config" "publisher-transfer" {
depends_on = [module.prc-project, module.publisher-dataset]
project = module.prc-project.project_id
display_name = "publisher-transfer"
location = var.location
schedule = "every 1 hours"
data_source_id = "google_cloud_storage"
destination_dataset_id = module.publisher-dataset.dataset_id
service_account_name = module.prc-sa-0.email
params = {
destination_table_name_template = "users"
data_path_template = "gs://${module.prc-cs-0.name}/users.csv"
write_disposition = "APPEND"
file_format = "CSV"
field_delimiter = ","
skip_leading_rows = 1
encoding = "UTF8"
}
}
# Create a dataset and a view for the Data Clean Room
module "dcr-publisher-dataset" {
source = "../modules/bigquery-dataset"
project_id = module.prc-project.project_id
id = "dcr_publisher_dataset"
location = var.location
options = {
default_table_expiration_ms = null
default_partition_expiration_ms = null
delete_contents_on_destroy = var.delete_contents_on_destroy
max_time_travel_hours = null
}
}
resource "null_resource" "dcr-publisher-view" {
triggers = {
always_run = "${timestamp()}"
}
provisioner "local-exec" {
command = "bq query --project_id ${module.prc-project.project_id} --nouse_legacy_sql 'CREATE OR REPLACE VIEW `${module.prc-project.project_id}.${module.dcr-publisher-dataset.dataset_id}.dcr_view` OPTIONS (privacy_policy= \"{\\\"aggregation_threshold_policy\\\": {\\\"threshold\\\" : 1, \\\"privacy_unit_columns\\\": \\\"hashed_email\\\"}}\") AS ( SELECT id, hashed_email, age, gender, city, country, traffic_source FROM `${module.prc-project.project_id}.${module.publisher-dataset.dataset_id}.users` )';"
}
}
# Create a listing for the Data Clean Room
resource "google_bigquery_analytics_hub_listing" "publisher-listing" {
project = module.land-project.project_id
location = var.location
data_exchange_id = google_bigquery_analytics_hub_data_exchange.data-exchange.data_exchange_id
listing_id = "publisher_listing"
display_name = "publisher_listing"
description = "Publisher listing for the ${var.data_exchange} clean room"
bigquery_dataset {
dataset = module.dcr-publisher-dataset.id
}
}
resource "google_bigquery_analytics_hub_listing_iam_binding" "publisher-binding" {
project = module.prc-project.project_id
location = var.location
data_exchange_id = var.data_exchange
listing_id = google_bigquery_analytics_hub_listing.publisher-listing.id
role = "roles/viewer"
members = [
"user:${var.super_admin}",
]
}