|
| 1 | +/** |
| 2 | +* Copyright 2025 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 | + |
| 18 | +# [START bigquery_authorized_view_tutorial] |
| 19 | +/* |
| 20 | +Creates an authorized view. |
| 21 | +*/ |
| 22 | + |
| 23 | + |
| 24 | +/* |
| 25 | +Create a dataset to contain the authorized view. |
| 26 | +*/ |
| 27 | +resource "google_bigquery_dataset" "default" { |
| 28 | + dataset_id = "authdataset" |
| 29 | + description = "Dataset for authorized view" |
| 30 | + location = "us-west1" |
| 31 | + |
| 32 | +} |
| 33 | + |
| 34 | +/* |
| 35 | +Create the view to authorize. |
| 36 | +*/ |
| 37 | +resource "google_bigquery_table" "default" { |
| 38 | + project = google_bigquery_dataset.default.project |
| 39 | + dataset_id = google_bigquery_dataset.default.dataset_id |
| 40 | + table_id = "authview" |
| 41 | + description = "View to authorize" |
| 42 | + deletion_protection = false # set to "true" in production |
| 43 | + |
| 44 | + view { |
| 45 | + query = "SELECT item_id, avg(rating) FROM `chriscar9.movielens.movielens_1m` GROUP BY item_id ORDER BY item_id;" |
| 46 | + use_legacy_sql = false |
| 47 | + } |
| 48 | + depends_on = [ |
| 49 | + google_bigquery_dataset.default |
| 50 | + ] |
| 51 | +} |
| 52 | + |
| 53 | +/* |
| 54 | +Authorize the view to access the dataset that |
| 55 | +the query data originates from. |
| 56 | +*/ |
| 57 | +resource "google_bigquery_dataset_access" "default" { |
| 58 | + project = "chriscar9" |
| 59 | + dataset_id = "movielens" |
| 60 | + |
| 61 | + view { |
| 62 | + project_id = google_bigquery_table.default.project |
| 63 | + dataset_id = google_bigquery_table.default.dataset_id |
| 64 | + table_id = google_bigquery_table.default.table_id |
| 65 | + } |
| 66 | + depends_on = [ |
| 67 | + google_bigquery_dataset.default |
| 68 | + ] |
| 69 | +} |
| 70 | + |
| 71 | +/* |
| 72 | +Set the IAM policy for principals that can access |
| 73 | +the authorized view. These users should already have the |
| 74 | +roles/bigqueryUser role at the project level. |
| 75 | +*/ |
| 76 | + |
| 77 | +data "google_iam_policy" "default" { |
| 78 | + binding { |
| 79 | + role = "roles/bigquery.dataViewer" |
| 80 | + members = [ |
| 81 | + |
| 82 | + ] |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/* |
| 87 | +Set the IAM policy on the authorized view. |
| 88 | +*/ |
| 89 | +resource "google_bigquery_table_iam_policy" "default" { |
| 90 | + project = google_bigquery_table.default.project |
| 91 | + dataset_id = google_bigquery_dataset.default.dataset_id |
| 92 | + table_id = "authview" |
| 93 | + policy_data = data.google_iam_policy.default.policy_data |
| 94 | +} |
| 95 | +# [END bigquery_authorized_view_tutorial] |
0 commit comments