diff --git a/bigquery/cloud-client/snippets/pom.xml b/bigquery/cloud-client/snippets/pom.xml new file mode 100644 index 00000000000..4acbac77b79 --- /dev/null +++ b/bigquery/cloud-client/snippets/pom.xml @@ -0,0 +1,70 @@ + + + + 4.0.0 + com.example.bigquery + cloud-client-snippets + jar + Google Cloud BigQuery Cloud Client Snippets + + + + com.google.cloud.samples + shared-configuration + 1.2.0 + + + + 21 + 21 + UTF-8 + + + + + + com.google.cloud + libraries-bom + 26.32.0 + pom + import + + + + + + + com.google.cloud + google-cloud-bigquery + + + junit + junit + 4.13.2 + test + + + com.google.truth + truth + 1.4.4 + test + + + diff --git a/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/CreateDataset.java b/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/CreateDataset.java new file mode 100644 index 00000000000..ed572bd107f --- /dev/null +++ b/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/CreateDataset.java @@ -0,0 +1,55 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.Dataset; +import com.google.cloud.bigquery.DatasetId; +import com.google.cloud.bigquery.DatasetInfo; + +public class CreateDataset { + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + // Project where to create the dataset. + String projectId = "MY_PROJECT_ID"; + String datasetName = "MY_DATASET_NAME"; + createDataset(projectId, datasetName); + } + + public static void createDataset(String projectId, String datasetName) { + try { + // Initialize client that will be used to send requests. This client only needs + // to be created once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + String location = "US"; + + // Create datasetId with the projectId and the datasetName, and set it into the datasetInfo. + DatasetId datasetId = DatasetId.of(projectId, datasetName); + DatasetInfo datasetInfo = DatasetInfo.newBuilder(datasetId).setLocation(location).build(); + + // Create Dataset. + Dataset dataset = bigquery.create(datasetInfo); + System.out.println( + "Dataset \"" + dataset.getDatasetId().getDataset() + "\" created successfully"); + } catch (BigQueryException e) { + System.out.println("Dataset was not created. \n" + e.toString()); + } + } +} diff --git a/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/CreateTable.java b/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/CreateTable.java new file mode 100644 index 00000000000..673815a6e6b --- /dev/null +++ b/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/CreateTable.java @@ -0,0 +1,68 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.Field; +import com.google.cloud.bigquery.Schema; +import com.google.cloud.bigquery.StandardSQLTypeName; +import com.google.cloud.bigquery.StandardTableDefinition; +import com.google.cloud.bigquery.Table; +import com.google.cloud.bigquery.TableDefinition; +import com.google.cloud.bigquery.TableId; +import com.google.cloud.bigquery.TableInfo; + +public class CreateTable { + + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + // Project and dataset name to create a new table + String projectId = "MY_PROJECT_ID"; + String datasetName = "MY_DATASET_NAME"; + String tableName = "MY_TABLE_NAME"; + + // Schema for a Google BigQuery Table. + Schema schema = + Schema.of( + Field.of("stringField", StandardSQLTypeName.STRING), + Field.of("isBooleanField", StandardSQLTypeName.BOOL)); + createTable(projectId, datasetName, tableName, schema); + } + + public static void createTable( + String projectId, String datasetName, String tableName, Schema schema) { + try { + // Initialize client that will be used to send requests. This client only needs + // to be created once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + // Create table identity given the projectId, the datasetName and the tableName. + TableId tableId = TableId.of(projectId, datasetName, tableName); + // Create table definition to build the table information + TableDefinition tableDefinition = StandardTableDefinition.of(schema); + TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build(); + + // Create table + Table table = bigquery.create(tableInfo); + System.out.println("Table \"" + table.getTableId().getTable() + "\" created successfully"); + } catch (BigQueryException e) { + System.out.println("Table was not created. \n" + e.toString()); + } + } +} diff --git a/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/CreateView.java b/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/CreateView.java new file mode 100644 index 00000000000..e1ca6083886 --- /dev/null +++ b/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/CreateView.java @@ -0,0 +1,64 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.Table; +import com.google.cloud.bigquery.TableId; +import com.google.cloud.bigquery.TableInfo; +import com.google.cloud.bigquery.ViewDefinition; + +// Sample to create a view +public class CreateView { + + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + // Project, dataset and table name to create a new view + String projectId = "MY_PROJECT_ID"; + String datasetName = "MY_DATASET_NAME"; + String tableName = "MY_TABLE_NAME"; + String viewName = "MY_VIEW_NAME"; + String query = + String.format("SELECT StringField, BooleanField FROM %s.%s", datasetName, tableName); + createView(projectId, datasetName, viewName, query); + } + + public static void createView( + String projectId, String datasetName, String viewName, String query) { + try { + // Initialize client that will be used to send requests. This client only needs + // to be created once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + // Create table identity given the projectId, the datasetName and the viewName. + TableId tableId = TableId.of(projectId, datasetName, viewName); + + // Create view definition to generate the table information. + ViewDefinition viewDefinition = + ViewDefinition.newBuilder(query).setUseLegacySql(false).build(); + TableInfo tableInfo = TableInfo.of(tableId, viewDefinition); + + // Create view. + Table view = bigquery.create(tableInfo); + System.out.println("View \"" + view.getTableId().getTable() + "\" created successfully"); + } catch (BigQueryException e) { + System.out.println("View was not created. \n" + e.toString()); + } + } +} diff --git a/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/DeleteDataset.java b/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/DeleteDataset.java new file mode 100644 index 00000000000..d89889e1b2f --- /dev/null +++ b/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/DeleteDataset.java @@ -0,0 +1,55 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.DatasetId; + +public class DeleteDataset { + + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + // Project from which to delete the dataset + String projectId = "MY_PROJECT_ID"; + String datasetName = "MY_DATASET_NAME"; + deleteDataset(projectId, datasetName); + } + + public static void deleteDataset(String projectId, String datasetName) { + try { + // Initialize client that will be used to send requests. This client only needs + // to be created once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + // Create datasetId with the projectId and the datasetName. + DatasetId datasetId = DatasetId.of(projectId, datasetName); + + // Delete dataset. + boolean success = bigquery.delete(datasetId, DatasetDeleteOption.deleteContents()); + if (success) { + System.out.println("Dataset \"" + datasetName + "\" deleted successfully"); + } else { + System.out.println("Dataset was not found"); + } + } catch (BigQueryException e) { + System.out.println("Dataset was not deleted. \n" + e.toString()); + } + } +} diff --git a/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/DeleteTable.java b/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/DeleteTable.java new file mode 100644 index 00000000000..4fa7d98721a --- /dev/null +++ b/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/DeleteTable.java @@ -0,0 +1,55 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.TableId; + +public class DeleteTable { + + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + // Project, dataset and table name to create a new table + String projectId = "MY_PROJECT_ID"; + String datasetName = "MY_DATASET_NAME"; + String tableName = "MY_TABLE_NAME"; + deleteTable(projectId, datasetName, tableName); + } + + public static void deleteTable(String projectId, String datasetName, String tableName) { + try { + // Initialize client that will be used to send requests. This client only needs + // to be created once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + // Create table identity given the projectId, the datasetName and the tableName. + TableId tableId = TableId.of(projectId, datasetName, tableName); + + // Delete the table. + boolean success = bigquery.delete(tableId); + if (success) { + System.out.println("Table \"" + tableName + "\" deleted successfully"); + } else { + System.out.println("Table was not found"); + } + } catch (BigQueryException e) { + System.out.println("Table was not deleted. \n" + e.toString()); + } + } +} diff --git a/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/GetDatasetAccessPolicy.java b/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/GetDatasetAccessPolicy.java new file mode 100644 index 00000000000..52f28b2e772 --- /dev/null +++ b/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/GetDatasetAccessPolicy.java @@ -0,0 +1,66 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +// [START bigquery_view_dataset_access_policy] + +import com.google.cloud.bigquery.Acl; +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.Dataset; +import com.google.cloud.bigquery.DatasetId; +import java.util.List; + +public class GetDatasetAccessPolicy { + + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + // Project and dataset from which to get the access policy. + String projectId = "MY_PROJECT_ID"; + String datasetName = "MY_DATASET_NAME"; + getDatasetAccessPolicy(projectId, datasetName); + } + + public static void getDatasetAccessPolicy(String projectId, String datasetName) { + try { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + // Create datasetId with the projectId and the datasetName. + DatasetId datasetId = DatasetId.of(projectId, datasetName); + Dataset dataset = bigquery.getDataset(datasetId); + + // Show ACL details. + // Find more information about ACL and the Acl Class here: + // https://cloud.google.com/storage/docs/access-control/lists + // https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Acl + List acls = dataset.getAcl(); + System.out.println("ACLs in dataset \"" + dataset.getDatasetId().getDataset() + "\":"); + System.out.println(acls.toString()); + for (Acl acl : acls) { + System.out.println(); + System.out.println("Role: " + acl.getRole()); + System.out.println("Entity: " + acl.getEntity()); + } + } catch (BigQueryException e) { + System.out.println("ACLs info not retrieved. \n" + e.toString()); + } + } +} +// [END bigquery_view_dataset_access_policy] diff --git a/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/GetTableOrViewAccessPolicy.java b/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/GetTableOrViewAccessPolicy.java new file mode 100644 index 00000000000..9a3db080d9a --- /dev/null +++ b/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/GetTableOrViewAccessPolicy.java @@ -0,0 +1,63 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +// [START bigquery_view_table_or_view_access_policy] + +import com.google.cloud.Policy; +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.TableId; + +public class GetTableOrViewAccessPolicy { + + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + // Project, dataset and resource (table or view) from which to get the access policy. + String projectId = "MY_PROJECT_ID"; + String datasetName = "MY_DATASET_NAME"; + String resourceName = "MY_RESOURCE_NAME"; + getTableOrViewAccessPolicy(projectId, datasetName, resourceName); + } + + public static void getTableOrViewAccessPolicy( + String projectId, String datasetName, String resourceName) { + try { + // Initialize client that will be used to send requests. This client only needs + // to be created once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + // Create table identity given the projectId, the datasetName and the resourceName. + TableId tableId = TableId.of(projectId, datasetName, resourceName); + + // Get the table IAM policy. + Policy policy = bigquery.getIamPolicy(tableId); + + // Show policy details. + // Find more information about the Policy Class here: + // https://cloud.google.com/java/docs/reference/google-cloud-core/latest/com.google.cloud.Policy + System.out.println( + "IAM policy info of resource \"" + resourceName + "\" retrieved succesfully"); + System.out.println(); + System.out.println("IAM policy info: " + policy.toString()); + } catch (BigQueryException e) { + System.out.println("IAM policy info not retrieved. \n" + e.toString()); + } + } +} +// [END bigquery_view_table_or_view_access_policy] diff --git a/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/GrantAccessToDataset.java b/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/GrantAccessToDataset.java new file mode 100644 index 00000000000..2a48a0ad251 --- /dev/null +++ b/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/GrantAccessToDataset.java @@ -0,0 +1,71 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +// [START bigquery_grant_access_to_dataset] +import com.google.cloud.bigquery.Acl; +import com.google.cloud.bigquery.Acl.Group; +import com.google.cloud.bigquery.Acl.Role; +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.Dataset; +import com.google.cloud.bigquery.DatasetId; +import java.util.ArrayList; +import java.util.List; + +public class GrantAccessToDataset { + + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + // Project and dataset from which to get the access policy + String projectId = "MY_PROJECT_ID"; + String datasetName = "MY_DATASET_NAME"; + // Create a new ACL granting the READER role to the group "user-or-group-to-add@example.com" + // For more information on the types of ACLs available see: + // https://cloud.google.com/storage/docs/access-control/lists + Acl newEntry = Acl.of(new Group("group-to-add@example.com"), Role.READER); + + grantAccessToDataset(projectId, datasetName, newEntry); + } + + public static void grantAccessToDataset(String projectId, String datasetName, Acl newEntry) { + try { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + // Create datasetId with the projectId and the datasetName. + DatasetId datasetId = DatasetId.of(projectId, datasetName); + Dataset dataset = bigquery.getDataset(datasetId); + + // Get a copy of the ACLs list from the dataset and append the new entry. + List acls = new ArrayList<>(dataset.getAcl()); + acls.add(newEntry); + + // Update the ACLs by setting the new list. + Dataset updatedDataset = bigquery.update(dataset.toBuilder().setAcl(acls).build()); + System.out.println( + "ACLs of dataset \"" + + updatedDataset.getDatasetId().getDataset() + + "\" updated successfully"); + } catch (BigQueryException e) { + System.out.println("ACLs were not updated \n" + e.toString()); + } + } +} +// [END bigquery_grant_access_to_dataset] diff --git a/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/GrantAccessToTableOrView.java b/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/GrantAccessToTableOrView.java new file mode 100644 index 00000000000..0e7f048aaf7 --- /dev/null +++ b/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/GrantAccessToTableOrView.java @@ -0,0 +1,66 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +// [START bigquery_grant_access_to_table_or_view] +import com.google.cloud.Identity; +import com.google.cloud.Policy; +import com.google.cloud.Role; +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.TableId; + +public class GrantAccessToTableOrView { + + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + // Project, dataset and resource (table or view) from which to get the access policy. + String projectId = "MY_PROJECT_ID"; + String datasetName = "MY_DATASET_NAME"; + String resourceName = "MY_TABLE_NAME"; + // Role to add to the policy access + Role role = Role.of("roles/bigquery.dataViewer"); + // Identity to add to the policy access + Identity identity = Identity.user("user-add@example.com"); + grantAccessToTableOrView(projectId, datasetName, resourceName, role, identity); + } + + public static void grantAccessToTableOrView( + String projectId, String datasetName, String resourceName, Role role, Identity identity) { + try { + // Initialize client that will be used to send requests. This client only needs + // to be created once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + // Create table identity given the projectId, the datasetName and the resourceName. + TableId tableId = TableId.of(projectId, datasetName, resourceName); + + // Add new user identity to current IAM policy. + Policy policy = bigquery.getIamPolicy(tableId); + policy = policy.toBuilder().addIdentity(role, identity).build(); + + // Update the IAM policy by setting the new one. + bigquery.setIamPolicy(tableId, policy); + + System.out.println("IAM policy of resource \"" + resourceName + "\" updated successfully"); + } catch (BigQueryException e) { + System.out.println("IAM policy was not updated. \n" + e.toString()); + } + } +} +// [END bigquery_grant_access_to_table_or_view] diff --git a/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/RevokeAccessToTableOrView.java b/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/RevokeAccessToTableOrView.java new file mode 100644 index 00000000000..96db8514384 --- /dev/null +++ b/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/RevokeAccessToTableOrView.java @@ -0,0 +1,94 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +// [START bigquery_revoke_access_to_table_or_view] +import com.google.cloud.Identity; +import com.google.cloud.Policy; +import com.google.cloud.Role; +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.TableId; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class RevokeAccessToTableOrView { + + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + // Project, dataset and resource (table or view) from which to get the access policy + String projectId = "MY_PROJECT_ID"; + String datasetName = "MY_DATASET_NAME"; + String resourceName = "MY_RESOURCE_NAME"; + // Role to remove from the access policy + Role role = Role.of("roles/bigquery.dataViewer"); + // Identity to remove from the access policy + Identity user = Identity.user("user-add@example.com"); + revokeAccessToTableOrView(projectId, datasetName, resourceName, role, user); + } + + public static void revokeAccessToTableOrView( + String projectId, String datasetName, String resourceName, Role role, Identity identity) { + try { + // Initialize client that will be used to send requests. This client only needs + // to be created once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + // Create table identity given the projectId, the datasetName and the resourceName. + TableId tableId = TableId.of(projectId, datasetName, resourceName); + + // Remove either identities or roles, or both from bindings and replace it in + // the current IAM policy. + Policy policy = bigquery.getIamPolicy(tableId); + // Create a copy of a inmutable map. + Map> bindings = new HashMap<>(policy.getBindings()); + + // Remove all identities with a specific role. + bindings.remove(role); + // Update bindings. + policy = policy.toBuilder().setBindings(bindings).build(); + + // Remove one identity in all the existing roles. + for (Role roleKey : bindings.keySet()) { + if (bindings.get(roleKey).contains(identity)) { + // Create a copy of a inmutable set if the identity is present in the role. + Set identities = new HashSet<>(bindings.get(roleKey)); + // Remove identity. + identities.remove(identity); + bindings.put(roleKey, identities); + if (bindings.get(roleKey).isEmpty()) { + // Remove the role if it has no identities. + bindings.remove(roleKey); + } + } + } + // Update bindings. + policy = policy.toBuilder().setBindings(bindings).build(); + + // Update the IAM policy by setting the new one. + bigquery.setIamPolicy(tableId, policy); + + System.out.println("IAM policy of resource \"" + resourceName + "\" updated successfully"); + } catch (BigQueryException e) { + System.out.println("IAM policy was not updated. \n" + e.toString()); + } + } +} +// [END bigquery_revoke_access_to_table_or_view] diff --git a/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/RevokeDatasetAccess.java b/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/RevokeDatasetAccess.java new file mode 100644 index 00000000000..cbfc3019253 --- /dev/null +++ b/bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/RevokeDatasetAccess.java @@ -0,0 +1,76 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +// [START bigquery_revoke_dataset_access] + +import com.google.cloud.bigquery.Acl; +import com.google.cloud.bigquery.Acl.Entity; +import com.google.cloud.bigquery.Acl.Group; +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.Dataset; +import com.google.cloud.bigquery.DatasetId; +import java.util.List; + +public class RevokeDatasetAccess { + + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + // Project and dataset from which to get the access policy. + String projectId = "MY_PROJECT_ID"; + String datasetName = "MY_DATASET_NAME"; + + // Create a new Entity with the corresponding type and email + // "user-or-group-to-remove@example.com" + // For more information on the types of Entities available see: + // https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Acl.Entity + // and + // https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Acl.Entity.Type + Entity entity = new Group("group-to-remove@example.com"); + + revokeDatasetAccess(projectId, datasetName, entity); + } + + public static void revokeDatasetAccess(String projectId, String datasetName, Entity entity) { + try { + // Initialize client that will be used to send requests. This client only needs + // to be created once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + // Create datasetId with the projectId and the datasetName. + DatasetId datasetId = DatasetId.of(projectId, datasetName); + Dataset dataset = bigquery.getDataset(datasetId); + + // To revoke access to a dataset, remove elements from the Acl list. + // Find more information about ACL and the Acl Class here: + // https://cloud.google.com/storage/docs/access-control/lists + // https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Acl + // Remove the entity from the ACLs list. + List acls = + dataset.getAcl().stream().filter(acl -> !acl.getEntity().equals(entity)).toList(); + + // Update the ACLs by setting the new list. + bigquery.update(dataset.toBuilder().setAcl(acls).build()); + System.out.println("ACLs of \"" + datasetName + "\" updated successfully"); + } catch (BigQueryException e) { + System.out.println("ACLs were not updated \n" + e.toString()); + } + } +} +// [END bigquery_revoke_dataset_access] diff --git a/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/CreateDatasetIT.java b/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/CreateDatasetIT.java new file mode 100644 index 00000000000..8af32250a37 --- /dev/null +++ b/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/CreateDatasetIT.java @@ -0,0 +1,80 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.bigquery.testing.RemoteBigQueryHelper; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class CreateDatasetIT { + + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String datasetName; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String GOOGLE_CLOUD_PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + + // Generate dataset name. + datasetName = RemoteBigQueryHelper.generateDatasetName(); + } + + @After + public void tearDown() { + // Clean up. + DeleteDataset.deleteDataset(GOOGLE_CLOUD_PROJECT, datasetName); + + // Restores print statements to the original output stream. + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, "\n" + bout.toString()); + } + + @Test + public void testCreateDataset() { + CreateDataset.createDataset(GOOGLE_CLOUD_PROJECT, datasetName); + assertThat(bout.toString()).contains(datasetName + "\" created successfully"); + } +} diff --git a/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/CreateTableIT.java b/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/CreateTableIT.java new file mode 100644 index 00000000000..371dc18a9e1 --- /dev/null +++ b/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/CreateTableIT.java @@ -0,0 +1,94 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.bigquery.Field; +import com.google.cloud.bigquery.Schema; +import com.google.cloud.bigquery.StandardSQLTypeName; +import com.google.cloud.bigquery.testing.RemoteBigQueryHelper; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class CreateTableIT { + + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String datasetName; + private String tableName; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String GOOGLE_CLOUD_PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + + // Create temporary dataset. + datasetName = RemoteBigQueryHelper.generateDatasetName(); + CreateDataset.createDataset(GOOGLE_CLOUD_PROJECT, datasetName); + + // Generate table name. + tableName = "table_test" + UUID.randomUUID().toString().substring(0, 8); + } + + @After + public void tearDown() { + // Clean up + DeleteTable.deleteTable(GOOGLE_CLOUD_PROJECT, datasetName, tableName); + DeleteDataset.deleteDataset(GOOGLE_CLOUD_PROJECT, datasetName); + + // Restores print statements to the original output stream. + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, "\n" + bout.toString()); + } + + @Test + public void testCreateTable() { + Schema schema = + Schema.of( + Field.of("stringField", StandardSQLTypeName.STRING), + Field.of("booleanField", StandardSQLTypeName.BOOL)); + CreateTable.createTable(GOOGLE_CLOUD_PROJECT, datasetName, tableName, schema); + assertThat(bout.toString()).contains(tableName + "\" created successfully"); + } +} diff --git a/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/CreateViewIT.java b/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/CreateViewIT.java new file mode 100644 index 00000000000..f935ed3c173 --- /dev/null +++ b/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/CreateViewIT.java @@ -0,0 +1,105 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.bigquery.Field; +import com.google.cloud.bigquery.Schema; +import com.google.cloud.bigquery.StandardSQLTypeName; +import com.google.cloud.bigquery.testing.RemoteBigQueryHelper; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class CreateViewIT { + + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String datasetName; + private String tableName; + private String viewName; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String GOOGLE_CLOUD_PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + + // Create temporary dataset. + datasetName = RemoteBigQueryHelper.generateDatasetName(); + CreateDataset.createDataset(GOOGLE_CLOUD_PROJECT, datasetName); + + // Create temporary table. + tableName = "table_test_" + UUID.randomUUID().toString().substring(0, 8); + Schema schema = + Schema.of( + Field.of("timestampField", StandardSQLTypeName.TIMESTAMP), + Field.of("stringField", StandardSQLTypeName.STRING), + Field.of("booleanField", StandardSQLTypeName.BOOL)); + CreateTable.createTable(GOOGLE_CLOUD_PROJECT, datasetName, tableName, schema); + + // Generate view name. + viewName = "view_test_" + UUID.randomUUID().toString().substring(0, 8); + } + + @After + public void tearDown() { + // Clean up. + DeleteTable.deleteTable(GOOGLE_CLOUD_PROJECT, datasetName, viewName); + DeleteTable.deleteTable(GOOGLE_CLOUD_PROJECT, datasetName, tableName); + DeleteDataset.deleteDataset(GOOGLE_CLOUD_PROJECT, datasetName); + + // Restores print statements to the original output stream. + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, "\n" + bout.toString()); + } + + @Test + public void testCreateView() { + String query = + String.format("SELECT stringField, booleanField FROM %s.%s", datasetName, tableName); + CreateView.createView(GOOGLE_CLOUD_PROJECT, datasetName, viewName, query); + assertThat(bout.toString()).contains(viewName + "\" created successfully"); + } +} diff --git a/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/DeleteDatasetIT.java b/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/DeleteDatasetIT.java new file mode 100644 index 00000000000..7bbf2059f93 --- /dev/null +++ b/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/DeleteDatasetIT.java @@ -0,0 +1,79 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.bigquery.testing.RemoteBigQueryHelper; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class DeleteDatasetIT { + + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String datasetName; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String GOOGLE_CLOUD_PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + + // Create temporary dataset. + datasetName = RemoteBigQueryHelper.generateDatasetName(); + CreateDataset.createDataset(GOOGLE_CLOUD_PROJECT, datasetName); + } + + @After + public void tearDown() { + // Restores print statements to the original output stream. + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, "\n" + bout.toString()); + } + + @Test + public void deleteDataset() { + DeleteDataset.deleteDataset(GOOGLE_CLOUD_PROJECT, datasetName); + + assertThat(bout.toString()).contains(datasetName + "\" deleted successfully"); + } +} diff --git a/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/DeleteTableIT.java b/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/DeleteTableIT.java new file mode 100644 index 00000000000..2c726972141 --- /dev/null +++ b/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/DeleteTableIT.java @@ -0,0 +1,89 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.bigquery.Schema; +import com.google.cloud.bigquery.testing.RemoteBigQueryHelper; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class DeleteTableIT { + + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String datasetName; + private String tableName; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String GOOGLE_CLOUD_PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + + // Create temporary dataset. + datasetName = RemoteBigQueryHelper.generateDatasetName(); + CreateDataset.createDataset(GOOGLE_CLOUD_PROJECT, datasetName); + + // Create temporary table to be deleted. + tableName = "table_test_" + UUID.randomUUID().toString().substring(0, 8); + CreateTable.createTable(GOOGLE_CLOUD_PROJECT, datasetName, tableName, Schema.of()); + } + + @After + public void tearDown() { + // Clean up. + DeleteDataset.deleteDataset(GOOGLE_CLOUD_PROJECT, datasetName); + + // Restores print statements to the original output stream. + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, "\n" + bout.toString()); + } + + @Test + public void testDeleteTable() { + // Delete the table that was just created. + DeleteTable.deleteTable(GOOGLE_CLOUD_PROJECT, datasetName, tableName); + assertThat(bout.toString()).contains(tableName + "\" deleted successfully"); + } +} diff --git a/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/GetDatasetAccessPolicyIT.java b/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/GetDatasetAccessPolicyIT.java new file mode 100644 index 00000000000..73925fdec08 --- /dev/null +++ b/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/GetDatasetAccessPolicyIT.java @@ -0,0 +1,82 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.bigquery.testing.RemoteBigQueryHelper; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class GetDatasetAccessPolicyIT { + + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String datasetName; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String GOOGLE_CLOUD_PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws Exception { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + datasetName = RemoteBigQueryHelper.generateDatasetName(); + + // Create a dataset in order to get its ACL policy. + CreateDataset.createDataset(GOOGLE_CLOUD_PROJECT, datasetName); + } + + @After + public void tearDown() { + // Clean up. + DeleteDataset.deleteDataset(GOOGLE_CLOUD_PROJECT, datasetName); + + // Restores print statements to the original output stream. + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, "\n" + bout.toString()); + } + + @Test + public void getDatasetAccessPolicy() { + // Get dataset ACLs + GetDatasetAccessPolicy.getDatasetAccessPolicy(GOOGLE_CLOUD_PROJECT, datasetName); + assertThat(bout.toString()).contains("ACLs in dataset \"" + datasetName); + } +} diff --git a/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/GetTableOrViewAccessPolicyIT.java b/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/GetTableOrViewAccessPolicyIT.java new file mode 100644 index 00000000000..c952a458f72 --- /dev/null +++ b/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/GetTableOrViewAccessPolicyIT.java @@ -0,0 +1,115 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.bigquery.Field; +import com.google.cloud.bigquery.Schema; +import com.google.cloud.bigquery.StandardSQLTypeName; +import com.google.cloud.bigquery.testing.RemoteBigQueryHelper; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class GetTableOrViewAccessPolicyIT { + + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String datasetName; + private String tableName; + private String viewName; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String GOOGLE_CLOUD_PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + + // Create temporary dataset. + datasetName = RemoteBigQueryHelper.generateDatasetName(); + CreateDataset.createDataset(GOOGLE_CLOUD_PROJECT, datasetName); + + // Create temporary table. + tableName = "get_access_policy_table_test_" + UUID.randomUUID().toString().substring(0, 8); + Schema schema = + Schema.of( + Field.of("stringField", StandardSQLTypeName.STRING), + Field.of("isBooleanField", StandardSQLTypeName.BOOL)); + CreateTable.createTable(GOOGLE_CLOUD_PROJECT, datasetName, tableName, schema); + + // Create a temporary view. + viewName = "get_access_policy_view_test_" + UUID.randomUUID().toString().substring(0, 8); + String query = + String.format("SELECT stringField, isBooleanField FROM %s.%s", datasetName, tableName); + CreateView.createView(GOOGLE_CLOUD_PROJECT, datasetName, viewName, query); + } + + @After + public void tearDown() { + // Clean up. + DeleteTable.deleteTable(GOOGLE_CLOUD_PROJECT, datasetName, viewName); + DeleteTable.deleteTable(GOOGLE_CLOUD_PROJECT, datasetName, tableName); + DeleteDataset.deleteDataset(GOOGLE_CLOUD_PROJECT, datasetName); + + // Restores print statements to the original output stream. + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testGetTableOrViewAccessPolicy_getTableAccessPolicy() { + GetTableOrViewAccessPolicy.getTableOrViewAccessPolicy( + GOOGLE_CLOUD_PROJECT, datasetName, tableName); + assertThat(bout.toString()) + .contains("IAM policy info of resource \"" + tableName + "\" retrieved succesfully"); + } + + @Test + public void testGetTableOrViewAccessPolicy_getViewAccessPolicy() { + GetTableOrViewAccessPolicy.getTableOrViewAccessPolicy( + GOOGLE_CLOUD_PROJECT, datasetName, viewName); + assertThat(bout.toString()) + .contains("IAM policy info of resource \"" + viewName + "\" retrieved succesfully"); + } +} diff --git a/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/GrantAccessToDatasetIT.java b/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/GrantAccessToDatasetIT.java new file mode 100644 index 00000000000..544e6a5237a --- /dev/null +++ b/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/GrantAccessToDatasetIT.java @@ -0,0 +1,87 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.bigquery.Acl; +import com.google.cloud.bigquery.Acl.Group; +import com.google.cloud.bigquery.Acl.Role; +import com.google.cloud.bigquery.testing.RemoteBigQueryHelper; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class GrantAccessToDatasetIT { + + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String datasetName; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String GOOGLE_CLOUD_PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws Exception { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + datasetName = RemoteBigQueryHelper.generateDatasetName(); + + // Create a dataset in order to modify its ACL policy. + CreateDataset.createDataset(GOOGLE_CLOUD_PROJECT, datasetName); + } + + @After + public void tearDown() { + // Clean up. + DeleteDataset.deleteDataset(GOOGLE_CLOUD_PROJECT, datasetName); + + // Restores print statements to the original output stream. + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, "\n" + bout.toString()); + } + + @Test + public void grantAccessToDataset() { + Acl newAclEntry = Acl.of(new Group("cloud-developer-relations@google.com"), Role.READER); + // Modify dataset's ACL + GrantAccessToDataset.grantAccessToDataset(GOOGLE_CLOUD_PROJECT, datasetName, newAclEntry); + assertThat(bout.toString()) + .contains("ACLs of dataset \"" + datasetName + "\" updated successfully"); + } +} diff --git a/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/GrantAccessToTableOrViewIT.java b/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/GrantAccessToTableOrViewIT.java new file mode 100644 index 00000000000..12b8c488652 --- /dev/null +++ b/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/GrantAccessToTableOrViewIT.java @@ -0,0 +1,123 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.Identity; +import com.google.cloud.Role; +import com.google.cloud.bigquery.Field; +import com.google.cloud.bigquery.Schema; +import com.google.cloud.bigquery.StandardSQLTypeName; +import com.google.cloud.bigquery.testing.RemoteBigQueryHelper; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class GrantAccessToTableOrViewIT { + + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String datasetName; + private String tableName; + private String viewName; + private Role role; + private Identity identity; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String GOOGLE_CLOUD_PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + + // Create temporary dataset. + datasetName = RemoteBigQueryHelper.generateDatasetName(); + CreateDataset.createDataset(GOOGLE_CLOUD_PROJECT, datasetName); + + // Create temporary table. + tableName = "grant_access_to_table_test_" + UUID.randomUUID().toString().substring(0, 8); + Schema schema = + Schema.of( + Field.of("stringField", StandardSQLTypeName.STRING), + Field.of("isBooleanField", StandardSQLTypeName.BOOL)); + CreateTable.createTable(GOOGLE_CLOUD_PROJECT, datasetName, tableName, schema); + + // Create a temporary view. + viewName = "grant_access_to_view_test_" + UUID.randomUUID().toString().substring(0, 8); + String query = + String.format("SELECT stringField, isBooleanField FROM %s.%s", datasetName, tableName); + CreateView.createView(GOOGLE_CLOUD_PROJECT, datasetName, viewName, query); + + // Role and identity to add to policy. + role = Role.of("roles/bigquery.dataViewer"); + identity = Identity.group("cloud-developer-relations@google.com"); + } + + @After + public void tearDown() { + // Clean up. + DeleteTable.deleteTable(GOOGLE_CLOUD_PROJECT, datasetName, viewName); + DeleteTable.deleteTable(GOOGLE_CLOUD_PROJECT, datasetName, tableName); + DeleteDataset.deleteDataset(GOOGLE_CLOUD_PROJECT, datasetName); + + // Restores print statements to the original output stream. + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testGrantAccessToTableOrView_grantAccessToTable() { + GrantAccessToTableOrView.grantAccessToTableOrView( + GOOGLE_CLOUD_PROJECT, datasetName, tableName, role, identity); + assertThat(bout.toString()) + .contains("IAM policy of resource \"" + tableName + "\" updated successfully"); + } + + @Test + public void testGrantAccessToTableOrView_grantAccessToView() { + GrantAccessToTableOrView.grantAccessToTableOrView( + GOOGLE_CLOUD_PROJECT, datasetName, viewName, role, identity); + assertThat(bout.toString()) + .contains("IAM policy of resource \"" + viewName + "\" updated successfully"); + } +} diff --git a/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/RevokeAccessToTableOrViewIT.java b/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/RevokeAccessToTableOrViewIT.java new file mode 100644 index 00000000000..cfa00896c84 --- /dev/null +++ b/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/RevokeAccessToTableOrViewIT.java @@ -0,0 +1,136 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.Identity; +import com.google.cloud.Role; +import com.google.cloud.bigquery.Field; +import com.google.cloud.bigquery.Schema; +import com.google.cloud.bigquery.StandardSQLTypeName; +import com.google.cloud.bigquery.testing.RemoteBigQueryHelper; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class RevokeAccessToTableOrViewIT { + + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String datasetName; + private String tableName; + private String viewName; + private Role firstRole; + private Role secondRole; + private Identity identity; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String GOOGLE_CLOUD_PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + + // Create temporary dataset. + datasetName = RemoteBigQueryHelper.generateDatasetName(); + CreateDataset.createDataset(GOOGLE_CLOUD_PROJECT, datasetName); + + // Create temporary table and view. + tableName = "revoke_access_to_table_test_" + UUID.randomUUID().toString().substring(0, 8); + Schema schema = + Schema.of( + Field.of("stringField", StandardSQLTypeName.STRING), + Field.of("isBooleanField", StandardSQLTypeName.BOOL)); + CreateTable.createTable(GOOGLE_CLOUD_PROJECT, datasetName, tableName, schema); + viewName = "revoke_access_to_view_test_" + UUID.randomUUID().toString().substring(0, 8); + String query = + String.format("SELECT stringField, isBooleanField FROM %s.%s", datasetName, tableName); + CreateView.createView(GOOGLE_CLOUD_PROJECT, datasetName, viewName, query); + + // Role and identity to add to policy. + firstRole = Role.of("roles/bigquery.dataViewer"); + identity = Identity.group("cloud-developer-relations@google.com"); + + // Grant access to table and view. + GrantAccessToTableOrView.grantAccessToTableOrView( + GOOGLE_CLOUD_PROJECT, datasetName, tableName, firstRole, identity); + GrantAccessToTableOrView.grantAccessToTableOrView( + GOOGLE_CLOUD_PROJECT, datasetName, viewName, firstRole, identity); + + // Add a second role for identity. + secondRole = Role.of("roles/bigquery.dataEditor"); + + // Grant access to table and view. + GrantAccessToTableOrView.grantAccessToTableOrView( + GOOGLE_CLOUD_PROJECT, datasetName, tableName, secondRole, identity); + GrantAccessToTableOrView.grantAccessToTableOrView( + GOOGLE_CLOUD_PROJECT, datasetName, viewName, secondRole, identity); + } + + @After + public void tearDown() { + // Clean up. + DeleteTable.deleteTable(GOOGLE_CLOUD_PROJECT, datasetName, tableName); + DeleteDataset.deleteDataset(GOOGLE_CLOUD_PROJECT, datasetName); + + // Restores print statements to the original output stream. + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, bout.toString()); + } + + @Test + public void testRevokeAccessToTableOrView_revokeAccessToTable() { + RevokeAccessToTableOrView.revokeAccessToTableOrView( + GOOGLE_CLOUD_PROJECT, datasetName, tableName, firstRole, identity); + assertThat(bout.toString()) + .contains("IAM policy of resource \"" + tableName + "\" updated successfully"); + } + + @Test + public void testRevokeAccessToTableOrView_revokeAccessToView() { + RevokeAccessToTableOrView.revokeAccessToTableOrView( + GOOGLE_CLOUD_PROJECT, datasetName, viewName, firstRole, identity); + assertThat(bout.toString()) + .contains("IAM policy of resource \"" + viewName + "\" updated successfully"); + } +} diff --git a/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/RevokeDatasetAccessIT.java b/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/RevokeDatasetAccessIT.java new file mode 100644 index 00000000000..59bd862d2eb --- /dev/null +++ b/bigquery/cloud-client/snippets/src/test/java/com/example/bigquery/RevokeDatasetAccessIT.java @@ -0,0 +1,89 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.bigquery.Acl; +import com.google.cloud.bigquery.Acl.Group; +import com.google.cloud.bigquery.Acl.Role; +import com.google.cloud.bigquery.testing.RemoteBigQueryHelper; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class RevokeDatasetAccessIT { + + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String datasetName; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static final String GOOGLE_CLOUD_PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() throws Exception { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + datasetName = RemoteBigQueryHelper.generateDatasetName(); + + // Create a dataset. + CreateDataset.createDataset(GOOGLE_CLOUD_PROJECT, datasetName); + Acl newAclEntry = Acl.of(new Group("cloud-developer-relations@google.com"), Role.READER); + + // Add new ACL entry in order to remove it. + GrantAccessToDataset.grantAccessToDataset(GOOGLE_CLOUD_PROJECT, datasetName, newAclEntry); + } + + @After + public void tearDown() { + // Clean up. + DeleteDataset.deleteDataset(GOOGLE_CLOUD_PROJECT, datasetName); + + // Restores print statements to the original output stream. + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, "\n" + bout.toString()); + } + + @Test + public void revokeDatasetAccess() { + RevokeDatasetAccess.revokeDatasetAccess( + GOOGLE_CLOUD_PROJECT, datasetName, new Group("cloud-developer-relations@google.com")); + assertThat(bout.toString()).contains("ACLs of \"" + datasetName + "\" updated successfully"); + } +}