Skip to content

Commit

Permalink
docs(sample): Added AuthorizeDataset Sample (#1909)
Browse files Browse the repository at this point in the history
* Added AuthorizeDataset sample

* Added AuthorizeDataset IT

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
prash-mi and gcf-owl-bot[bot] committed Mar 9, 2022
1 parent b23fc36 commit a7a196b
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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) |
Expand Down
@@ -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<Acl> sourceDatasetAcl = new ArrayList<>(sourceDataset.getAcl());

// Add the user dataset's DatasetAccessEntry object to the existing sourceDatasetAcl
List<String> 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]
@@ -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");
}
}

0 comments on commit a7a196b

Please sign in to comment.