Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit aeecac9

Browse files
author
Praful Makani
authoredDec 1, 2020
docs(samples): add update iam policy for table (#976)
* docs(samples): add update iam policy for table * docs(samples): fix format
1 parent 8be3b77 commit aeecac9

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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_update_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+
import java.util.HashMap;
28+
import java.util.Map;
29+
import java.util.Set;
30+
31+
// Sample to update iam policy in table
32+
public class UpdateIamPolicy {
33+
34+
public static void main(String[] args) {
35+
// TODO(developer): Replace these variables before running the sample.
36+
String datasetName = "MY_DATASET_NAME";
37+
String tableName = "MY_TABLE_NAME";
38+
updateIamPolicy(datasetName, tableName);
39+
}
40+
41+
public static void updateIamPolicy(String datasetName, String tableName) {
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+
TableId tableId = TableId.of(datasetName, tableName);
48+
49+
Policy policy = bigquery.getIamPolicy(tableId);
50+
Map<Role, Set<Identity>> binding = new HashMap<>(policy.getBindings());
51+
binding.remove(Role.of("roles/bigquery.dataViewer"));
52+
53+
policy.toBuilder().setBindings(binding).build();
54+
bigquery.setIamPolicy(tableId, policy);
55+
56+
System.out.println("Iam policy updated successfully");
57+
} catch (BigQueryException e) {
58+
System.out.println("Iam policy was not updated. \n" + e.toString());
59+
}
60+
}
61+
}
62+
// [END bigquery_update_iam_policy]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 UpdateIamPolicyIT {
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 = "UPDATE_POLICY_TABLE_TEST_" + UUID.randomUUID().toString().substring(0, 8);
65+
CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, Schema.of());
66+
CreateIamPolicy.createIamPolicy(BIGQUERY_DATASET_NAME, tableName);
67+
}
68+
69+
@After
70+
public void tearDown() {
71+
// Clean up
72+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
73+
// restores print statements in the original method
74+
System.out.flush();
75+
System.setOut(originalPrintStream);
76+
log.log(Level.INFO, bout.toString());
77+
}
78+
79+
@Test
80+
public void testUpdateIamPolicy() {
81+
UpdateIamPolicy.updateIamPolicy(BIGQUERY_DATASET_NAME, tableName);
82+
assertThat(bout.toString()).contains("Iam policy updated successfully");
83+
}
84+
}

0 commit comments

Comments
 (0)
Failed to load comments.