...
1/**
2 * Copyright 2022 Google LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17```hcl
18resource "google_bigquery_table" "source" {
19 deletion_protection = false
20 count = length(google_bigquery_dataset.source)
21
22 dataset_id = google_bigquery_dataset.source[count.index].dataset_id
23 table_id = "job_copy_${count.index}_table"
24
25 schema = <<EOF
26[
27 {
28 "name": "name",
29 "type": "STRING",
30 "mode": "NULLABLE"
31 },
32 {
33 "name": "post_abbr",
34 "type": "STRING",
35 "mode": "NULLABLE"
36 },
37 {
38 "name": "date",
39 "type": "DATE",
40 "mode": "NULLABLE"
41 }
42]
43EOF
44}
45
46resource "google_bigquery_dataset" "source" {
47 count = 2
48
49 dataset_id = "job_copy_${count.index}_dataset"
50 friendly_name = "test"
51 description = "This is a test description"
52 location = "US"
53}
54
55resource "google_bigquery_table" "dest" {
56 deletion_protection = false
57 dataset_id = google_bigquery_dataset.dest.dataset_id
58 table_id = "job_copy_dest_table"
59
60 schema = <<EOF
61[
62 {
63 "name": "name",
64 "type": "STRING",
65 "mode": "NULLABLE"
66 },
67 {
68 "name": "post_abbr",
69 "type": "STRING",
70 "mode": "NULLABLE"
71 },
72 {
73 "name": "date",
74 "type": "DATE",
75 "mode": "NULLABLE"
76 }
77]
78EOF
79
80 encryption_configuration {
81 kms_key_name = google_kms_crypto_key.crypto_key.id
82 }
83
84 depends_on = ["google_project_iam_member.encrypt_role"]
85}
86
87resource "google_bigquery_dataset" "dest" {
88 dataset_id = "job_copy_dest_dataset"
89 friendly_name = "test"
90 description = "This is a test description"
91 location = "US"
92}
93
94resource "google_kms_crypto_key" "crypto_key" {
95 name = "example-key"
96 key_ring = google_kms_key_ring.key_ring.id
97}
98
99resource "google_kms_key_ring" "key_ring" {
100 name = "example-keyring"
101 location = "global"
102}
103
104data "google_project" "project" {
105 project_id = "my-project-name"
106}
107
108resource "google_project_iam_member" "encrypt_role" {
109 project = data.google_project.project.project_id
110 role = "roles/cloudkms.cryptoKeyEncrypterDecrypter"
111 member = "serviceAccount:bq-${data.google_project.project.number}@bigquery-encryption.iam.gserviceaccount.com"
112}
113
114resource "google_bigquery_job" "job" {
115 job_id = "job_copy"
116
117 copy {
118 source_tables {
119 project_id = google_bigquery_table.source.0.project
120 dataset_id = google_bigquery_table.source.0.dataset_id
121 table_id = google_bigquery_table.source.0.table_id
122 }
123
124 source_tables {
125 project_id = google_bigquery_table.source.1.project
126 dataset_id = google_bigquery_table.source.1.dataset_id
127 table_id = google_bigquery_table.source.1.table_id
128 }
129
130 destination_table {
131 project_id = google_bigquery_table.dest.project
132 dataset_id = google_bigquery_table.dest.dataset_id
133 table_id = google_bigquery_table.dest.table_id
134 }
135
136 destination_encryption_configuration {
137 kms_key_name = google_kms_crypto_key.crypto_key.id
138 }
139 }
140
141 depends_on = ["google_project_iam_member.encrypt_role"]
142}
143```
View as plain text