|
| 1 | +/* |
| 2 | + * Copyright 2019 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.bigquery; |
| 18 | + |
| 19 | +// [START bigquery_update_dataset_access] |
| 20 | +import com.google.cloud.bigquery.Acl; |
| 21 | +import com.google.cloud.bigquery.Acl.Role; |
| 22 | +import com.google.cloud.bigquery.Acl.User; |
| 23 | +import com.google.cloud.bigquery.BigQuery; |
| 24 | +import com.google.cloud.bigquery.BigQueryException; |
| 25 | +import com.google.cloud.bigquery.BigQueryOptions; |
| 26 | +import com.google.cloud.bigquery.Dataset; |
| 27 | +import java.util.ArrayList; |
| 28 | + |
| 29 | +public class UpdateDatasetAccess { |
| 30 | + |
| 31 | + public static void runUpdateDatasetAccess() { |
| 32 | + // TODO(developer): Replace these variables before running the sample. |
| 33 | + String datasetName = "my-dataset-name"; |
| 34 | + updateDatasetAccess(datasetName); |
| 35 | + } |
| 36 | + |
| 37 | + public static void updateDatasetAccess(String datasetName) { |
| 38 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 39 | + // once, and can be reused for multiple requests. |
| 40 | + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); |
| 41 | + |
| 42 | + Dataset dataset = bigquery.getDataset(datasetName); |
| 43 | + |
| 44 | + // Create a new ACL granting the READER role to "sample.bigquery.dev@gmail.com" |
| 45 | + // For more information on the types of ACLs available see: |
| 46 | + // https://cloud.google.com/storage/docs/access-control/lists |
| 47 | + Acl newEntry = Acl.of(new User("sample.bigquery.dev@gmail.com"), Role.READER); |
| 48 | + |
| 49 | + // Get a copy of the ACLs list from the dataset and append the new entry |
| 50 | + ArrayList<Acl> acls = new ArrayList<>(dataset.getAcl()); |
| 51 | + acls.add(newEntry); |
| 52 | + |
| 53 | + //Update the dataset to use the new ACLs |
| 54 | + try { |
| 55 | + bigquery.update(dataset.toBuilder().setAcl(acls).build()); |
| 56 | + System.out.println("Dataset Access Control updated successfully"); |
| 57 | + } catch (BigQueryException e) { |
| 58 | + System.out.println("Dataset Access control was not updated \n" + e.toString()); |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | +// [END bigquery_update_dataset_access] |
0 commit comments