|
| 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 | +# Creates an authorized view. |
| 20 | + |
| 21 | +# Create a dataset to contain the view. |
| 22 | +resource "google_bigquery_dataset" "view_dataset" { |
| 23 | + dataset_id = "view_dataset" |
| 24 | + description = "Dataset that contains the view" |
| 25 | + location = "us-west1" |
| 26 | +} |
| 27 | + |
| 28 | +# Create the view to authorize. |
| 29 | +resource "google_bigquery_table" "movie_view" { |
| 30 | + project = google_bigquery_dataset.view_dataset.project |
| 31 | + dataset_id = google_bigquery_dataset.view_dataset.dataset_id |
| 32 | + table_id = "movie_view" |
| 33 | + description = "View to authorize" |
| 34 | + |
| 35 | + view { |
| 36 | + query = "SELECT item_id, avg(rating) FROM `movie_project.movie_dataset.movie_ratings` GROUP BY item_id ORDER BY item_id;" |
| 37 | + use_legacy_sql = false |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | +# Authorize the view to access the dataset |
| 43 | +# that the query data originates from. |
| 44 | +resource "google_bigquery_dataset_access" "view_authorization" { |
| 45 | + project = "movie_project" |
| 46 | + dataset_id = "movie_dataset" |
| 47 | + |
| 48 | + view { |
| 49 | + project_id = google_bigquery_table.movie_view.project |
| 50 | + dataset_id = google_bigquery_table.movie_view.dataset_id |
| 51 | + table_id = google_bigquery_table.movie_view.table_id |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +# Specify the IAM policy for principals that can access |
| 56 | +# the authorized view. These users should already |
| 57 | +# have the roles/bigqueryUser role at the project level. |
| 58 | +data "google_iam_policy" "principals_policy" { |
| 59 | + binding { |
| 60 | + role = "roles/bigquery.dataViewer" |
| 61 | + members = [ |
| 62 | + |
| 63 | + ] |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +# Set the IAM policy on the authorized view. |
| 68 | +resource "google_bigquery_table_iam_policy" "authorized_view_policy" { |
| 69 | + project = google_bigquery_table.movie_view.project |
| 70 | + dataset_id = google_bigquery_table.movie_view.dataset_id |
| 71 | + table_id = google_bigquery_table.movie_view.table_id |
| 72 | + policy_data = data.google_iam_policy.principals_policy.policy_data |
| 73 | +} |
| 74 | +# [END bigquery_authorized_view_tutorial] |
0 commit comments