|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +async function main(datasetId, entityId, role) { |
| 18 | + // [START bigquery_grant_access_to_dataset] |
| 19 | + |
| 20 | + /** |
| 21 | + * TODO(developer): Update and un-comment below lines. |
| 22 | + */ |
| 23 | + |
| 24 | + // const datasetId = "my_project_id.my_dataset_name"; |
| 25 | + |
| 26 | + // ID of the user or group from whom you are adding access. |
| 27 | + // const entityId = "[email protected]"; |
| 28 | + |
| 29 | + // One of the "Basic roles for datasets" described here: |
| 30 | + // https://cloud.google.com/bigquery/docs/access-control-basic-roles#dataset-basic-roles |
| 31 | + // const role = "READER"; |
| 32 | + |
| 33 | + const {BigQuery} = require('@google-cloud/bigquery'); |
| 34 | + |
| 35 | + // Instantiate a client. |
| 36 | + const client = new BigQuery(); |
| 37 | + |
| 38 | + // Type of entity you are granting access to. |
| 39 | + // Find allowed allowed entity type names here: |
| 40 | + // https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets#resource:-dataset |
| 41 | + const entityType = 'groupByEmail'; |
| 42 | + |
| 43 | + async function grantAccessToDataset() { |
| 44 | + const [dataset] = await client.dataset(datasetId).get(); |
| 45 | + |
| 46 | + // The 'access entries' array is immutable. Create a copy for modifications. |
| 47 | + const entries = [...dataset.metadata.access]; |
| 48 | + |
| 49 | + // Append an AccessEntry to grant the role to a dataset. |
| 50 | + // Find more details about the AccessEntry object in the BigQuery documentation: |
| 51 | + // https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.dataset.AccessEntry |
| 52 | + entries.push({ |
| 53 | + role, |
| 54 | + [entityType]: entityId, |
| 55 | + }); |
| 56 | + |
| 57 | + // Assign the array of AccessEntries back to the dataset. |
| 58 | + const metadata = { |
| 59 | + access: entries, |
| 60 | + }; |
| 61 | + |
| 62 | + // Update will only succeed if the dataset |
| 63 | + // has not been modified externally since retrieval. |
| 64 | + // |
| 65 | + // See the BigQuery client library documentation for more details on metadata updates: |
| 66 | + // https://cloud.google.com/nodejs/docs/reference/bigquery/latest |
| 67 | + |
| 68 | + // Update just the 'access entries' property of the dataset. |
| 69 | + await client.dataset(datasetId).setMetadata(metadata); |
| 70 | + |
| 71 | + console.log( |
| 72 | + `Role '${role}' granted for entity '${entityId}' in '${datasetId}'.` |
| 73 | + ); |
| 74 | + } |
| 75 | + // [END bigquery_grant_access_to_dataset] |
| 76 | + await grantAccessToDataset(); |
| 77 | +} |
| 78 | + |
| 79 | +exports.grantAccessToDataset = main; |
0 commit comments