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