Skip to content

Commit dbb3a4d

Browse files
fix(bigquery): Update samples and tests according to PR comments
1 parent f6a685d commit dbb3a4d

File tree

7 files changed

+32
-60
lines changed

7 files changed

+32
-60
lines changed

bigquery/cloud-client/grantAccessToTableOrView.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ async function grantAccessToTableOrView(
4040
// projectId = "my_project_id"
4141

4242
// Dataset where the table or view is.
43-
// datasetId = "my_dataset"
43+
// datasetId = "my_dataset_id"
4444

4545
// Table or view name to get the access policy.
46-
// resourceName = "my_table"
46+
// resourceName = "my_table_id"
4747

4848
// The principal requesting access to the table or view.
4949
// Find more details about principal identifiers here:
@@ -88,7 +88,6 @@ async function grantAccessToTableOrView(
8888
`Role '${role}' granted for principal '${principalId}' on resource '${datasetId}.${resourceName}'.`
8989
);
9090
// [END bigquery_grant_access_to_table_or_view]
91-
9291
return updatedPolicy.bindings;
9392
}
9493

bigquery/cloud-client/revokeDatasetAccess.js

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ async function revokeDatasetAccess(datasetId, entityId) {
3636
// TODO (developer): Update and un-comment below lines.
3737

3838
// ID of the dataset to revoke access to.
39-
// datasetId = "your-project.your_dataset"
39+
// datasetId = "my_project.my_dataset"
4040

4141
// ID of the user or group from whom you are revoking access.
4242
// Alternatively, the JSON REST API representation of the entity,
@@ -57,32 +57,12 @@ async function revokeDatasetAccess(datasetId, entityId) {
5757
// Filter access entries to exclude entries matching the specified entity_id
5858
// and assign a new list back to the access list.
5959
dataset.metadata.access = dataset.metadata.access.filter(entry => {
60-
// Check for entity_id (specific match).
61-
if (entry.entity_id === entityId) {
62-
console.log(
63-
`Found matching entity_id: ${entry.entity_id}, removing entry`
64-
);
65-
return false;
66-
}
67-
68-
// Check for userByEmail field.
69-
if (entry.userByEmail === entityId) {
70-
console.log(
71-
`Found matching userByEmail: ${entry.userByEmail}, removing entry`
72-
);
73-
return false;
74-
}
75-
76-
// Check for groupByEmail field.
77-
if (entry.groupByEmail === entityId) {
78-
console.log(
79-
`Found matching groupByEmail: ${entry.groupByEmail}, removing entry`
80-
);
81-
return false;
82-
}
83-
84-
// Keep all other entries.
85-
return true;
60+
// Return false (remove entry) if any of these fields match entityId
61+
return !(
62+
entry.entity_id === entityId ||
63+
entry.userByEmail === entityId ||
64+
entry.groupByEmail === entityId
65+
);
8666
});
8767

8868
// Update will only succeed if the dataset

bigquery/cloud-client/revokeTableOrViewAccess.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ async function revokeAccessToTableOrView(
3939
// projectId = "my_project_id"
4040

4141
// Dataset where the table or view is.
42-
// datasetId = "my_dataset"
42+
// datasetId = "my_dataset_id"
4343

4444
// Table or view name to get the access policy.
45-
// resourceName = "my_table"
45+
// resourceName = "my_table_id"
4646

4747
// (Optional) Role to remove from the table or view.
4848
// roleToRemove = "roles/bigquery.dataViewer"

bigquery/cloud-client/test/config.js

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -188,26 +188,19 @@ const setupBeforeAll = async () => {
188188
const cleanupResources = async () => {
189189
console.log('=== Cleaning up test resources ===');
190190

191-
if (sharedClient) {
191+
if (sharedClient && sharedDataset) {
192192
try {
193-
// Check if resources exist before attempting to delete
194-
if (sharedDataset) {
195-
try {
196-
console.log(
197-
`Deleting dataset: ${DATASET_ID} and all contained tables/views`
198-
);
199-
await sharedClient.dataset(DATASET_ID).delete({force: true});
200-
console.log(`Successfully deleted dataset: ${DATASET_ID}`);
201-
} catch (err) {
202-
if (err.code !== HTTP_STATUS.NOT_FOUND) {
203-
console.error(`Error deleting dataset: ${err.message}`);
204-
} else {
205-
console.log(`Dataset ${DATASET_ID} already deleted or not found`);
206-
}
207-
}
208-
}
193+
console.log(
194+
`Deleting dataset: ${DATASET_ID} and all contained tables/views`
195+
);
196+
await sharedClient.dataset(DATASET_ID).delete({force: true});
197+
console.log(`Successfully deleted dataset: ${DATASET_ID}`);
209198
} catch (err) {
210-
console.error(`Cleanup error: ${err.message}`);
199+
if (err.code !== HTTP_STATUS.NOT_FOUND) {
200+
console.error(`Error deleting dataset: ${err.message}`);
201+
} else {
202+
console.log(`Dataset ${DATASET_ID} already deleted or not found`);
203+
}
211204
}
212205
}
213206

bigquery/cloud-client/test/viewTableOrViewAccessPolicy.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,20 @@ describe('viewTableOrViewAccessPolicy', () => {
4343
table.id
4444
);
4545

46-
// Verificar que la política existe
46+
// Verify that the policy exists
4747
assert.ok(policy, 'Policy should be defined');
4848

49-
// Verificar que bindings existe y es un array
49+
// Verify that bindings exists and is an array
5050
assert.ok(Array.isArray(policy.bindings), 'Bindings should be an array');
5151

52-
// En una política nueva, bindings debería estar vacío
52+
// In a new policy, bindings should be empty
5353
assert.strictEqual(
5454
policy.bindings.length,
5555
0,
5656
'Bindings list should be empty'
5757
);
5858

59-
// Verificar que etag existe, pero no validar su valor exacto
59+
// Verify that etag exists, but do not validate its exact value
6060
assert.ok(policy.etag, 'Etag should be defined');
6161
});
6262

@@ -71,20 +71,20 @@ describe('viewTableOrViewAccessPolicy', () => {
7171
view.id
7272
);
7373

74-
// Verificar que la política existe
74+
// Verify that the policy exists
7575
assert.ok(policy, 'Policy should be defined');
7676

77-
// Verificar que bindings existe y es un array
77+
// Verify that bindings exists and is an array
7878
assert.ok(Array.isArray(policy.bindings), 'Bindings should be an array');
7979

80-
// En una política nueva, bindings debería estar vacío
80+
// In a new policy, bindings should be empty
8181
assert.strictEqual(
8282
policy.bindings.length,
8383
0,
8484
'Bindings list should be empty'
8585
);
8686

87-
// Verificar que etag existe, pero no validar su valor exacto
87+
// Verify that etag exists, but do not validate its exact value
8888
assert.ok(policy.etag, 'Etag should be defined');
8989
});
9090
});

bigquery/cloud-client/viewDatasetAccessPolicy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function viewDatasetAccessPolicy(datasetId) {
2929

3030
// TODO (developer): Update and un-comment below lines.
3131
// Dataset from which to get the access policy
32-
// datasetId = "my_dataset";
32+
// datasetId = "my_dataset_id";
3333

3434
// Get a reference to the dataset.
3535
const dataset = bigquery.dataset(datasetId);

bigquery/cloud-client/viewTableOrViewAccessPolicy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async function viewTableOrViewAccessPolicy(projectId, datasetId, resourceName) {
3333
// Dataset where the table or view is.
3434
// datasetId = "my_dataset_id";
3535
// Table or view name to get the access policy.
36-
// resourceName = "my_table_name";
36+
// resourceName = "my_table_name_id";
3737

3838
// Instantiate a client.
3939
const client = new BigQuery();

0 commit comments

Comments
 (0)