diff --git a/README.md b/README.md index 420476374..ae6e80dbe 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-bigquery/tree | Auth Snippets | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/AuthSnippets.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/AuthSnippets.java) | | Auth User Flow | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java) | | Auth User Query | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java) | +| Authorize Dataset | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/AuthorizeDataset.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/AuthorizeDataset.java) | | Authorized View Tutorial | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/AuthorizedViewTutorial.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/AuthorizedViewTutorial.java) | | Browse Table | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/BrowseTable.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/BrowseTable.java) | | Cancel Job | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/CancelJob.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/CancelJob.java) | diff --git a/samples/snippets/src/main/java/com/example/bigquery/AuthorizeDataset.java b/samples/snippets/src/main/java/com/example/bigquery/AuthorizeDataset.java new file mode 100644 index 000000000..f7f28c218 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigquery/AuthorizeDataset.java @@ -0,0 +1,73 @@ +/* + * Copyright 2022 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_authorized_dataset] +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 com.google.common.collect.ImmutableList; +import java.util.ArrayList; +import java.util.List; + +public class AuthorizeDataset { + + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + String projectId = "PROJECT_ID"; + String sourceDatasetName = "BIGQUERY_SOURCE_DATASET_NAME"; + String userDatasetName = "BIGQUERY_USER_DATASET_NAME"; + authorizeDataset( + DatasetId.of(projectId, sourceDatasetName), DatasetId.of(projectId, userDatasetName)); + } + + // This method will update userDataset's ACL with sourceDataset's ACL + public static void authorizeDataset(DatasetId sourceDatasetId, DatasetId userDatasetId) { + 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(); + + // Get both source and user dataset's references + Dataset sourceDataset = bigquery.getDataset(sourceDatasetId); + Dataset userDataset = bigquery.getDataset(userDatasetId); + + // Get the source dataset's ACL + List sourceDatasetAcl = new ArrayList<>(sourceDataset.getAcl()); + + // Add the user dataset's DatasetAccessEntry object to the existing sourceDatasetAcl + List targetTypes = ImmutableList.of("VIEWS"); + Acl.DatasetAclEntity userDatasetAclEntity = + new Acl.DatasetAclEntity(userDatasetId, targetTypes); + sourceDatasetAcl.add(Acl.of(userDatasetAclEntity)); + + // update the user dataset with source dataset's ACL + Dataset updatedUserDataset = + userDataset.toBuilder().setAcl(sourceDatasetAcl).build().update(); + + System.out.printf( + "Dataset %s updated with the added authorization\n", updatedUserDataset.getDatasetId()); + + } catch (BigQueryException e) { + System.out.println("Dataset Authorization failed due to error: \n" + e); + } + } +} +// [END bigquery_authorized_dataset] diff --git a/samples/snippets/src/test/java/com/example/bigquery/AuthorizeDatasetIT.java b/samples/snippets/src/test/java/com/example/bigquery/AuthorizeDatasetIT.java new file mode 100644 index 000000000..b13c6bb73 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigquery/AuthorizeDatasetIT.java @@ -0,0 +1,85 @@ +/* + * Copyright 2022 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.DatasetId; +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 AuthorizeDatasetIT { + private final Logger log = Logger.getLogger(this.getClass().getName()); + private String userDatasetName; + private String srcDatasetName; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + private static final String GOOGLE_CLOUD_PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT"); + private DatasetId sourceDatasetId; + private DatasetId userDatasetId; + + 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() { + userDatasetName = RemoteBigQueryHelper.generateDatasetName(); + srcDatasetName = RemoteBigQueryHelper.generateDatasetName(); + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + CreateDataset.createDataset(userDatasetName); + CreateDataset.createDataset(srcDatasetName); + userDatasetId = DatasetId.of(GOOGLE_CLOUD_PROJECT, userDatasetName); + sourceDatasetId = DatasetId.of(GOOGLE_CLOUD_PROJECT, srcDatasetName); + } + + @After + public void tearDown() { + // Clean up + DeleteDataset.deleteDataset(GOOGLE_CLOUD_PROJECT, userDatasetName); + DeleteDataset.deleteDataset(GOOGLE_CLOUD_PROJECT, srcDatasetName); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + log.log(Level.INFO, "\n" + bout.toString()); + } + + @Test + public void testCreateDataset() { + AuthorizeDataset.authorizeDataset(sourceDatasetId, userDatasetId); + assertThat(bout.toString()).contains(userDatasetId + " updated with the added authorization"); + } +}