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