-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(bigquery): Add cloud-client samples for access policies #3975
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
5925b0f
e24c2e8
84ec0f6
21f786e
5d408c7
62e343a
c87d659
58ee72a
62ebc2c
9d0ce88
627536a
70cfb83
ff35988
b8ad0f4
7b38e5a
5cfa8dc
ccf30d6
eb32aa8
0a5f5dd
6c40940
b600729
c7a1821
db956a7
b013b3c
3f3d68a
12bc84b
b9e4bb1
e6e886f
ec3c990
232b73b
7c45228
f6a685d
dbb3a4d
5c04884
eed264a
20609bf
f721a80
158f0b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,61 +14,99 @@ | |
|
|
||
| 'use strict'; | ||
|
|
||
| const {BigQuery} = require('@google-cloud/bigquery'); | ||
|
|
||
| /** | ||
| * Grants access to a BigQuery dataset for a specified entity | ||
| * | ||
| * @param {object} options The configuration object | ||
| * @param {string} options.datasetId ID of the dataset to grant access to (e.g. "my_project_id.my_dataset") | ||
| * @param {string} options.entityId ID of the user or group to grant access to (e.g. "user-or-group-to-add@example.com") | ||
| * @param {string} options.role One of the basic roles for datasets (e.g. "READER") | ||
| * @param {string} datasetId ID of the dataset to grant access to | ||
| * @param {string} entityId ID of the entity to grant access to | ||
| * @param {string} role Role to grant | ||
| * @returns {Promise<Array>} Array of access entries | ||
| */ | ||
| // [START bigquery_grant_access_to_dataset] | ||
| async function grantAccessToDataset(options) { | ||
| // Create a BigQuery client | ||
| const bigquery = new BigQuery(); | ||
| async function grantAccessToDataset(datasetId, entityId, role) { | ||
| // [START bigquery_grant_access_to_dataset] | ||
| const {BigQuery} = require('@google-cloud/bigquery'); | ||
|
|
||
| // TODO(developer): Update and un-comment below lines | ||
|
|
||
| // ID of the dataset to revoke access to. | ||
| // datasetId = "my_project_id.my_dataset"; | ||
|
hivanalejandro marked this conversation as resolved.
Outdated
|
||
|
|
||
| // ID of the user or group from whom you are adding access. | ||
| // Alternatively, the JSON REST API representation of the entity, | ||
| // such as a view's table reference. | ||
| // entityId = "user-or-group-to-add@example.com"; | ||
|
|
||
| // One of the "Basic roles for datasets" described here: | ||
| // https://cloud.google.com/bigquery/docs/access-control-basic-roles#dataset-basic-roles | ||
| // role = "READER"; | ||
|
|
||
| const {datasetId, entityId, role} = options; | ||
| // Type of entity you are granting access to. | ||
| // Find allowed allowed entity type names here: | ||
| // https://cloud.google.com/python/docs/reference/bigquery/latest/enums#class-googlecloudbigqueryenumsentitytypesvalue | ||
| // In this case, we're using the equivalent of GROUP_BY_EMAIL | ||
|
hivanalejandro marked this conversation as resolved.
Outdated
|
||
| const entityType = 'groupByEmail'; | ||
|
|
||
| // Instantiate a client. | ||
| const client = new BigQuery(); | ||
|
|
||
| try { | ||
|
hivanalejandro marked this conversation as resolved.
|
||
| // Get a reference to the dataset | ||
| const dataset = bigquery.dataset(datasetId); | ||
| const [metadata] = await dataset.getMetadata(); | ||
| // Get a reference to the dataset. | ||
| const [dataset] = await client.dataset(datasetId).get(); | ||
|
|
||
| // The access entries list is immutable. Create a copy for modifications | ||
| const entries = [...(metadata.access || [])]; | ||
| // The 'access entries' list is immutable. Create a copy for modifications. | ||
| const entries = Array.isArray(dataset.metadata.access) | ||
| ? [...dataset.metadata.access] | ||
| : []; | ||
|
|
||
| // Add the new access entry | ||
| // Append an AccessEntry to grant the role to a dataset. | ||
| // Find more details about the AccessEntry object in the BigQuery documentation | ||
|
hivanalejandro marked this conversation as resolved.
Outdated
|
||
| entries.push({ | ||
| role: role, | ||
| groupByEmail: entityId, // For group access. Use userByEmail for user access | ||
|
hivanalejandro marked this conversation as resolved.
Outdated
|
||
| [entityType]: entityId, | ||
| }); | ||
|
|
||
| // Update the dataset's access entries | ||
| const [updatedMetadata] = await dataset.setMetadata({ | ||
| ...metadata, | ||
| // Assign the list of AccessEntries back to the dataset. | ||
| const metadata = { | ||
| access: entries, | ||
| }); | ||
| }; | ||
|
|
||
| // Update will only succeed if the dataset | ||
| // has not been modified externally since retrieval. | ||
| // | ||
| // See the BigQuery client library documentation for more details on metadata updates | ||
|
eapl-gemugami marked this conversation as resolved.
Outdated
|
||
|
|
||
| // Update just the 'access entries' property of the dataset. | ||
|
hivanalejandro marked this conversation as resolved.
|
||
| const [updatedDataset] = await client | ||
| .dataset(datasetId) | ||
| .setMetadata(metadata); | ||
|
|
||
| // Show a success message. | ||
|
hivanalejandro marked this conversation as resolved.
|
||
| const fullDatasetId = | ||
| updatedDataset && | ||
| updatedDataset.metadata && | ||
| updatedDataset.metadata.datasetReference | ||
| ? `${updatedDataset.metadata.datasetReference.projectId}.${updatedDataset.metadata.datasetReference.datasetId}` | ||
| : datasetId; | ||
|
|
||
| console.log( | ||
| `Role '${role}' granted for entity '${entityId}' in dataset '${datasetId}'.` | ||
| `Role '${role}' granted for entity '${entityId}'` + | ||
| ` in dataset '${fullDatasetId}'.` | ||
| ); | ||
|
|
||
| return updatedMetadata.access; | ||
| return updatedDataset.access; | ||
| } catch (error) { | ||
| if (error.code === 412) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest avoiding Magic numbers like 412, and using a const instead
Usually these constants are available in the client-library, although I can't find it for Node.JS. Could you replicate that error in your runs? |
||
| // 412 Precondition Failed - Dataset was modified between get and update | ||
| // A read-modify-write error (PreconditionFailed equivalent) | ||
| console.error( | ||
| `Dataset '${datasetId}' was modified remotely before this update. ` + | ||
| 'Fetch the latest version and retry.' | ||
| ); | ||
| } else { | ||
| throw error; | ||
| } | ||
| throw error; | ||
| } | ||
| // [END bigquery_grant_access_to_dataset] | ||
| } | ||
| // [END bigquery_grant_access_to_dataset] | ||
|
|
||
| module.exports = { | ||
| grantAccessToDataset, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.