Skip to content

Commit 509628f

Browse files
committed
feat(bigquery): Add example for creating an authorized view
1 parent 6dba0df commit 509628f

File tree

1 file changed

+33
-46
lines changed
  • bigquery/bigquery_authorized_view_tutorial

1 file changed

+33
-46
lines changed

bigquery/bigquery_authorized_view_tutorial/main.tf

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,73 +16,60 @@
1616

1717

1818
# [START bigquery_authorized_view_tutorial]
19-
/*
20-
Creates an authorized view.
21-
*/
22-
19+
# Creates an authorized view.
2320

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"
3025
location = "us-west1"
3126
}
3227

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"
4234

4335
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;"
4537
use_legacy_sql = false
4638
}
4739
}
4840

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"
5647

5748
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
6152
}
6253
}
6354

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" {
7159
binding {
7260
role = "roles/bigquery.dataViewer"
7361
members = [
74-
62+
63+
7564
]
7665
}
7766
}
7867

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
8774
}
8875
# [END bigquery_authorized_view_tutorial]

0 commit comments

Comments
 (0)