Skip to content

Commit 0cfc98b

Browse files
author
Praful Makani
authored
docs(samples): add create iam policy for table (#975)
1 parent 07ff507 commit 0cfc98b

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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_create_iam_policy]
20+
import com.google.cloud.Identity;
21+
import com.google.cloud.Policy;
22+
import com.google.cloud.Role;
23+
import com.google.cloud.bigquery.BigQuery;
24+
import com.google.cloud.bigquery.BigQueryException;
25+
import com.google.cloud.bigquery.BigQueryOptions;
26+
import com.google.cloud.bigquery.TableId;
27+
28+
// Sample to create iam policy for table
29+
public class CreateIamPolicy {
30+
31+
public static void main(String[] args) {
32+
// TODO(developer): Replace these variables before running the sample.
33+
String datasetName = "MY_DATASET_NAME";
34+
String tableName = "MY_TABLE_NAME";
35+
createIamPolicy(datasetName, tableName);
36+
}
37+
38+
public static void createIamPolicy(String datasetName, String tableName) {
39+
try {
40+
// Initialize client that will be used to send requests. This client only needs to be created
41+
// once, and can be reused for multiple requests.
42+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
43+
44+
TableId tableId = TableId.of(datasetName, tableName);
45+
46+
Policy policy = bigquery.getIamPolicy(tableId);
47+
policy
48+
.toBuilder()
49+
.addIdentity(Role.of("roles/bigquery.dataViewer"), Identity.allUsers())
50+
.build();
51+
bigquery.setIamPolicy(tableId, policy);
52+
System.out.println("Iam policy created successfully");
53+
} catch (BigQueryException e) {
54+
System.out.println("Iam policy was not created. \n" + e.toString());
55+
}
56+
}
57+
}
58+
// [END bigquery_create_iam_policy]
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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.Schema;
23+
import java.io.ByteArrayOutputStream;
24+
import java.io.PrintStream;
25+
import java.util.UUID;
26+
import java.util.logging.Level;
27+
import java.util.logging.Logger;
28+
import org.junit.After;
29+
import org.junit.Before;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
32+
33+
public class CreateIamPolicyIT {
34+
35+
private final Logger log = Logger.getLogger(this.getClass().getName());
36+
private String tableName;
37+
private ByteArrayOutputStream bout;
38+
private PrintStream out;
39+
private PrintStream originalPrintStream;
40+
41+
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");
42+
43+
private static String requireEnvVar(String varName) {
44+
String value = System.getenv(varName);
45+
assertNotNull(
46+
"Environment variable " + varName + " is required to perform these tests.",
47+
System.getenv(varName));
48+
return value;
49+
}
50+
51+
@BeforeClass
52+
public static void checkRequirements() {
53+
requireEnvVar("BIGQUERY_DATASET_NAME");
54+
}
55+
56+
@Before
57+
public void setUp() {
58+
bout = new ByteArrayOutputStream();
59+
out = new PrintStream(bout);
60+
originalPrintStream = System.out;
61+
System.setOut(out);
62+
63+
// create a temporary table
64+
tableName = "CREATE_POLICY_TABLE_TEST_" + UUID.randomUUID().toString().substring(0, 8);
65+
CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, Schema.of());
66+
}
67+
68+
@After
69+
public void tearDown() {
70+
// Clean up
71+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
72+
// restores print statements in the original method
73+
System.out.flush();
74+
System.setOut(originalPrintStream);
75+
log.log(Level.INFO, bout.toString());
76+
}
77+
78+
@Test
79+
public void testCreateIamPolicy() {
80+
CreateIamPolicy.createIamPolicy(BIGQUERY_DATASET_NAME, tableName);
81+
assertThat(bout.toString()).contains("Iam policy created successfully");
82+
}
83+
}

0 commit comments

Comments
 (0)