Skip to content
This repository was archived by the owner on Mar 23, 2026. It is now read-only.

Commit 0c092e0

Browse files
author
Praful Makani
authored
docs(samples): add grant view access (#563)
* docs(samples): add grant view access * docs(samples): add comment
1 parent a049d2b commit 0c092e0

2 files changed

Lines changed: 176 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2020 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+
package com.example.bigquery;
18+
19+
// [START bigquery_grant_view_access]
20+
import com.google.cloud.bigquery.Acl;
21+
import com.google.cloud.bigquery.BigQuery;
22+
import com.google.cloud.bigquery.BigQueryException;
23+
import com.google.cloud.bigquery.BigQueryOptions;
24+
import com.google.cloud.bigquery.Dataset;
25+
import com.google.cloud.bigquery.DatasetId;
26+
import com.google.cloud.bigquery.Table;
27+
import java.util.ArrayList;
28+
import java.util.List;
29+
30+
// Sample to grant view access on dataset
31+
public class GrantViewAccess {
32+
33+
public static void runGrantViewAccess() {
34+
// TODO(developer): Replace these variables before running the sample.
35+
String srcDatasetId = "MY_DATASET_ID";
36+
String viewDatasetId = "MY_VIEW_DATASET_ID";
37+
String viewId = "MY_VIEW_ID";
38+
grantViewAccess(srcDatasetId, viewDatasetId, viewId);
39+
}
40+
41+
public static void grantViewAccess(String srcDatasetId, String viewDatasetId, String viewId) {
42+
try {
43+
// Initialize client that will be used to send requests. This client only needs to be created
44+
// once, and can be reused for multiple requests.
45+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
46+
47+
Dataset srcDataset = bigquery.getDataset(DatasetId.of(srcDatasetId));
48+
Dataset viewDataset = bigquery.getDataset(DatasetId.of(viewDatasetId));
49+
Table view = viewDataset.get(viewId);
50+
51+
// First, we'll add a group to the ACL for the dataset containing the view. This will allow
52+
// users within that group to query the view, but they must have direct access to any tables
53+
// referenced by the view.
54+
List<Acl> viewAcl = new ArrayList<>();
55+
viewAcl.addAll(viewDataset.getAcl());
56+
viewAcl.add(Acl.of(new Acl.Group("example-analyst-group@google.com"), Acl.Role.READER));
57+
viewDataset.toBuilder().setAcl(viewAcl).build().update();
58+
59+
// Now, we'll authorize a specific view against a source dataset, delegating access
60+
// enforcement. Once this has been completed, members of the group previously added to the
61+
// view dataset's ACL no longer require access to the source dataset to successfully query the
62+
// view
63+
List<Acl> srcAcl = new ArrayList<>();
64+
srcAcl.addAll(srcDataset.getAcl());
65+
srcAcl.add(Acl.of(new Acl.View(view.getTableId())));
66+
srcDataset.toBuilder().setAcl(srcAcl).build().update();
67+
System.out.println("Grant view access successfully");
68+
} catch (BigQueryException e) {
69+
System.out.println("Grant view access was not success. \n" + e.toString());
70+
}
71+
}
72+
}
73+
// [END bigquery_grant_view_access]
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2020 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+
package com.example.bigquery;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import com.google.cloud.bigquery.Field;
23+
import com.google.cloud.bigquery.Schema;
24+
import com.google.cloud.bigquery.StandardSQLTypeName;
25+
import java.io.ByteArrayOutputStream;
26+
import java.io.PrintStream;
27+
import java.util.UUID;
28+
import org.junit.After;
29+
import org.junit.Before;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
32+
33+
public class GrantViewAccessIT {
34+
35+
private String datasetName;
36+
private String tableName;
37+
private String viewName;
38+
private ByteArrayOutputStream bout;
39+
private PrintStream out;
40+
41+
private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT");
42+
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");
43+
44+
private static String requireEnvVar(String varName) {
45+
String value = System.getenv(varName);
46+
assertNotNull(
47+
"Environment variable " + varName + " is required to perform these tests.",
48+
System.getenv(varName));
49+
return value;
50+
}
51+
52+
@BeforeClass
53+
public static void checkRequirements() {
54+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
55+
requireEnvVar("BIGQUERY_DATASET_NAME");
56+
}
57+
58+
@Before
59+
public void setUp() {
60+
bout = new ByteArrayOutputStream();
61+
out = new PrintStream(bout);
62+
System.setOut(out);
63+
64+
// create a temporary dataset, table and view to be deleted.
65+
datasetName = "MY_DATASET_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8);
66+
tableName = "MY_TABLE_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8);
67+
viewName = "MY_VIEW_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8);
68+
69+
CreateDataset.createDataset(datasetName);
70+
71+
Schema schema =
72+
Schema.of(
73+
Field.of("timestampField", StandardSQLTypeName.TIMESTAMP),
74+
Field.of("stringField", StandardSQLTypeName.STRING),
75+
Field.of("booleanField", StandardSQLTypeName.BOOL));
76+
CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, schema);
77+
78+
String query =
79+
String.format(
80+
"SELECT timestampField, stringField, booleanField FROM %s.%s",
81+
BIGQUERY_DATASET_NAME, tableName);
82+
CreateView.createView(BIGQUERY_DATASET_NAME, viewName, query);
83+
84+
bout = new ByteArrayOutputStream();
85+
out = new PrintStream(bout);
86+
System.setOut(out);
87+
}
88+
89+
@After
90+
public void tearDown() {
91+
// Clean up
92+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, viewName);
93+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
94+
DeleteDataset.deleteDataset(PROJECT_ID, datasetName);
95+
System.setOut(null);
96+
}
97+
98+
@Test
99+
public void testGrantViewAccess() {
100+
GrantViewAccess.grantViewAccess(datasetName, BIGQUERY_DATASET_NAME, viewName);
101+
assertThat(bout.toString()).contains("Grant view access successfully");
102+
}
103+
}

0 commit comments

Comments
 (0)