-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(sample): Added AuthorizeDataset Sample (#1909)
* 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
1 parent
b23fc36
commit a7a196b
Showing
3 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
samples/snippets/src/main/java/com/example/bigquery/AuthorizeDataset.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
85 changes: 85 additions & 0 deletions
85
samples/snippets/src/test/java/com/example/bigquery/AuthorizeDatasetIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} |