Skip to content

Commit b013b3c

Browse files
fix(bigquery): Update samples and tests related to Dataset
1 parent db956a7 commit b013b3c

File tree

7 files changed

+531
-372
lines changed

7 files changed

+531
-372
lines changed

bigquery/cloud-client/grantAccessToDataset.js

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,61 +14,99 @@
1414

1515
'use strict';
1616

17-
const {BigQuery} = require('@google-cloud/bigquery');
18-
1917
/**
2018
* Grants access to a BigQuery dataset for a specified entity
2119
*
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
2623
* @returns {Promise<Array>} Array of access entries
2724
*/
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";
3242

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();
3451

3552
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();
3955

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+
: [];
4260

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
4463
entries.push({
4564
role: role,
46-
groupByEmail: entityId, // For group access. Use userByEmail for user access
65+
[entityType]: entityId,
4766
});
4867

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 = {
5270
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;
5490

5591
console.log(
56-
`Role '${role}' granted for entity '${entityId}' in dataset '${datasetId}'.`
92+
`Role '${role}' granted for entity '${entityId}'` +
93+
` in dataset '${fullDatasetId}'.`
5794
);
5895

59-
return updatedMetadata.access;
96+
return updatedDataset.access;
6097
} catch (error) {
6198
if (error.code === 412) {
62-
// 412 Precondition Failed - Dataset was modified between get and update
99+
// A read-modify-write error (PreconditionFailed equivalent)
63100
console.error(
64101
`Dataset '${datasetId}' was modified remotely before this update. ` +
65102
'Fetch the latest version and retry.'
66103
);
104+
} else {
105+
throw error;
67106
}
68-
throw error;
69107
}
108+
// [END bigquery_grant_access_to_dataset]
70109
}
71-
// [END bigquery_grant_access_to_dataset]
72110

73111
module.exports = {
74112
grantAccessToDataset,

bigquery/cloud-client/revokeDatasetAccess.js

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,89 @@ const {BigQuery} = require('@google-cloud/bigquery');
1818

1919
// [START bigquery_revoke_dataset_access]
2020
/**
21-
* Revokes access to a BigQuery dataset for a specified entity.
21+
* Revokes access to a dataset for a specified entity.
2222
*
23-
* @param {Object} params The parameters for revoking dataset access
24-
* @param {string} params.datasetId The ID of the dataset to revoke access from
25-
* @param {string} params.entityId The ID of the user or group to revoke access from
23+
* @param {string} datasetId - ID of the dataset to revoke access to.
24+
* @param {string} entityId - ID of the user or group from whom you are revoking access.
25+
* Alternatively, the JSON REST API representation of the entity,
26+
* such as a view's table reference.
2627
* @returns {Promise<Array>} A promise that resolves to the updated access entries
2728
*/
28-
async function revokeDatasetAccess({datasetId, entityId}) {
29-
// Instantiate a client
29+
async function revokeDatasetAccess(datasetId, entityId) {
30+
// Import the Google Cloud client library.
3031
const bigquery = new BigQuery();
3132

32-
try {
33-
// Get a reference to the dataset
34-
const [dataset] = await bigquery.dataset(datasetId).get();
33+
// TODO (developer): Update and un-comment below lines
3534

36-
// Filter out the access entry for the specified entity
37-
dataset.metadata.access = dataset.metadata.access.filter(
38-
entry => entry.userByEmail !== entityId && entry.groupByEmail !== entityId
39-
);
35+
// ID of the dataset to revoke access to.
36+
// datasetId = "your-project.your_dataset"
37+
38+
// ID of the user or group from whom you are revoking access.
39+
// Alternatively, the JSON REST API representation of the entity,
40+
// such as a view's table reference.
41+
// entityId = "[email protected]"
42+
43+
// Get a reference to the dataset.
44+
const [dataset] = await bigquery.dataset(datasetId).get();
45+
46+
// To revoke access to a dataset, remove elements from the access list.
47+
//
48+
// See the BigQuery client library documentation for more details on access entries
4049

41-
// Update the dataset with the new access entries
50+
// Filter access entries to exclude entries matching the specified entity_id
51+
// and assign a new list back to the access list.
52+
dataset.metadata.access = dataset.metadata.access.filter(entry => {
53+
// Check for entity_id (specific match)
54+
if (entry.entity_id === entityId) {
55+
console.log(
56+
`Found matching entity_id: ${entry.entity_id}, removing entry`
57+
);
58+
return false;
59+
}
60+
61+
// Check for userByEmail field
62+
if (entry.userByEmail === entityId) {
63+
console.log(
64+
`Found matching userByEmail: ${entry.userByEmail}, removing entry`
65+
);
66+
return false;
67+
}
68+
69+
// Check for groupByEmail field
70+
if (entry.groupByEmail === entityId) {
71+
console.log(
72+
`Found matching groupByEmail: ${entry.groupByEmail}, removing entry`
73+
);
74+
return false;
75+
}
76+
77+
// Keep all other entries
78+
return true;
79+
});
80+
81+
// Update will only succeed if the dataset
82+
// has not been modified externally since retrieval.
83+
84+
try {
85+
// Update just the access entries property of the dataset.
4286
const [updatedDataset] = await dataset.setMetadata(dataset.metadata);
4387

88+
const fullDatasetId = `${dataset.parent.projectId}.${dataset.id}`;
4489
console.log(
45-
`Revoked dataset access for '${entityId}' to dataset '${dataset.id}'.`
90+
`Revoked dataset access for '${entityId}' to dataset '${fullDatasetId}'.`
4691
);
4792

48-
return updatedDataset.metadata.access;
93+
return updatedDataset.access;
4994
} catch (error) {
95+
// Check if it's a precondition failed error (a read-modify-write error)
5096
if (error.code === 412) {
51-
// Handle precondition failed error (dataset modified externally)
52-
console.error(
53-
`Dataset '${datasetId}' was modified remotely before this update. ` +
97+
console.log(
98+
`Dataset '${dataset.id}' was modified remotely before this update. ` +
5499
'Fetch the latest version and retry.'
55100
);
101+
} else {
102+
throw error;
56103
}
57-
throw error;
58104
}
59105
}
60106
// [END bigquery_revoke_dataset_access]

0 commit comments

Comments
 (0)