|
| 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 | +/** |
| 18 | + * Grants access to a BigQuery dataset for a specified entity. |
| 19 | + * |
| 20 | + * @param {string} datasetId ID of the dataset to grant access to. |
| 21 | + * @param {string} entityId ID of the entity to grant access to. |
| 22 | + * @param {string} role Role to grant. |
| 23 | + * @returns {Promise<Array>} Array of access entries. |
| 24 | + */ |
| 25 | +async function grantAccessToDataset(datasetId, entityId, role) { |
| 26 | + // [START bigquery_grant_access_to_dataset] |
| 27 | + const {BigQuery} = require('@google-cloud/bigquery'); |
| 28 | + |
| 29 | + // Define enum for HTTP codes. |
| 30 | + const HTTP_STATUS = { |
| 31 | + PRECONDITION_FAILED: 412, |
| 32 | + }; |
| 33 | + |
| 34 | + // TODO(developer): Update and un-comment below lines. |
| 35 | + |
| 36 | + // ID of the dataset to revoke access to. |
| 37 | + // datasetId = "my_project_id.my_dataset_name"; |
| 38 | + |
| 39 | + // ID of the user or group from whom you are adding access. |
| 40 | + // Alternatively, the JSON REST API representation of the entity, |
| 41 | + // such as a view's table reference. |
| 42 | + // entityId = "[email protected]"; |
| 43 | + |
| 44 | + // One of the "Basic roles for datasets" described here: |
| 45 | + // https://cloud.google.com/bigquery/docs/access-control-basic-roles#dataset-basic-roles |
| 46 | + // role = "READER"; |
| 47 | + |
| 48 | + // Type of entity you are granting access to. |
| 49 | + // Find allowed allowed entity type names here: |
| 50 | + // https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets#resource:-dataset |
| 51 | + // In this case, we're using groupByEmail |
| 52 | + const entityType = 'groupByEmail'; |
| 53 | + |
| 54 | + // Instantiate a client. |
| 55 | + const client = new BigQuery(); |
| 56 | + |
| 57 | + try { |
| 58 | + // Get a reference to the dataset. |
| 59 | + const [dataset] = await client.dataset(datasetId).get(); |
| 60 | + |
| 61 | + // The 'access entries' array is immutable. Create a copy for modifications. |
| 62 | + const entries = Array.isArray(dataset.metadata.access) |
| 63 | + ? [...dataset.metadata.access] |
| 64 | + : []; |
| 65 | + |
| 66 | + // Append an AccessEntry to grant the role to a dataset. |
| 67 | + // Find more details about the AccessEntry object in the BigQuery documentation: |
| 68 | + // https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.dataset.AccessEntry |
| 69 | + entries.push({ |
| 70 | + role: role, |
| 71 | + [entityType]: entityId, |
| 72 | + }); |
| 73 | + |
| 74 | + // Assign the array of AccessEntries back to the dataset. |
| 75 | + const metadata = { |
| 76 | + access: entries, |
| 77 | + }; |
| 78 | + |
| 79 | + // Update will only succeed if the dataset |
| 80 | + // has not been modified externally since retrieval. |
| 81 | + // |
| 82 | + // See the BigQuery client library documentation for more details on metadata updates: |
| 83 | + // https://cloud.google.com/nodejs/docs/reference/bigquery/latest |
| 84 | + |
| 85 | + // Update just the 'access entries' property of the dataset. |
| 86 | + const [updatedDataset] = await client |
| 87 | + .dataset(datasetId) |
| 88 | + .setMetadata(metadata); |
| 89 | + |
| 90 | + // Show a success message. |
| 91 | + console.log( |
| 92 | + `Role '${role}' granted for entity '${entityId}' in dataset '${datasetId}'.` |
| 93 | + ); |
| 94 | + |
| 95 | + return updatedDataset.access; |
| 96 | + } catch (error) { |
| 97 | + if (error.code === HTTP_STATUS.PRECONDITION_FAILED) { |
| 98 | + console.error( |
| 99 | + `Dataset '${datasetId}' was modified remotely before this update. ` + |
| 100 | + 'Fetch the latest version and retry.' |
| 101 | + ); |
| 102 | + } else { |
| 103 | + throw error; |
| 104 | + } |
| 105 | + } |
| 106 | + // [END bigquery_grant_access_to_dataset] |
| 107 | +} |
| 108 | + |
| 109 | +module.exports = { |
| 110 | + grantAccessToDataset, |
| 111 | +}; |
0 commit comments