Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bigquery/cloud-client/snippets/pom.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?xml version='1.0' encoding='UTF-8'?>
<!--
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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Acl> 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]
Original file line number Diff line number Diff line change
@@ -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 "[email protected]"
// 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("[email protected]"), Role.READER);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: should the initialization of newEntry be moved into the body of the sample? I think, for testing purposes, we will want to avoid passing around non-primitives for methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, so I did some changes


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<Acl> 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]
Original file line number Diff line number Diff line change
@@ -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
// "[email protected]"
// 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("[email protected]");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: same as previous, should we pass in the group name as a string to revokeDatasetAccess()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, so I did some changes


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<Acl> 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]
Original file line number Diff line number Diff line change
@@ -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.
Util.setUpTest_createDataset(GOOGLE_CLOUD_PROJECT, datasetName);
}

@After
public void tearDown() {
// Clean up.
Util.tearDownTest_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);
}
}
Loading