Skip to content

Commit 7c45228

Browse files
fix(bigquery): Update samples and tests format issues
1 parent 232b73b commit 7c45228

File tree

6 files changed

+80
-75
lines changed

6 files changed

+80
-75
lines changed

bigquery/cloud-client/grantAccessToDataset.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,63 +31,63 @@ async function grantAccessToDataset(datasetId, entityId, role) {
3131
PRECONDITION_FAILED: 412,
3232
};
3333

34-
// TODO(developer): Update and un-comment below lines
34+
// TODO(developer): Update and un-comment below lines.
3535

36-
// ID of the dataset to revoke access to
36+
// ID of the dataset to revoke access to.
3737
// datasetId = "my_project_id.my_dataset_name";
3838

39-
// ID of the user or group from whom you are adding access
39+
// ID of the user or group from whom you are adding access.
4040
// Alternatively, the JSON REST API representation of the entity,
41-
// such as a view's table reference
41+
// such as a view's table reference.
4242
// entityId = "[email protected]";
4343

4444
// One of the "Basic roles for datasets" described here:
4545
// https://cloud.google.com/bigquery/docs/access-control-basic-roles#dataset-basic-roles
4646
// role = "READER";
4747

48-
// Type of entity you are granting access to
48+
// Type of entity you are granting access to.
4949
// Find allowed allowed entity type names here:
5050
// https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets#resource:-dataset
5151
// In this case, we're using groupByEmail
5252
const entityType = 'groupByEmail';
5353

54-
// Instantiate a client
54+
// Instantiate a client.
5555
const client = new BigQuery();
5656

5757
try {
58-
// Get a reference to the dataset
58+
// Get a reference to the dataset.
5959
const [dataset] = await client.dataset(datasetId).get();
6060

61-
// The 'access entries' list is immutable. Create a copy for modifications
61+
// The 'access entries' list is immutable. Create a copy for modifications.
6262
const entries = Array.isArray(dataset.metadata.access)
6363
? [...dataset.metadata.access]
6464
: [];
6565

66-
// Append an AccessEntry to grant the role to a dataset
66+
// Append an AccessEntry to grant the role to a dataset.
6767
// Find more details about the AccessEntry object in the BigQuery documentation:
6868
// https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.dataset.AccessEntry
6969
entries.push({
7070
role: role,
7171
[entityType]: entityId,
7272
});
7373

74-
// Assign the list of AccessEntries back to the dataset
74+
// Assign the list of AccessEntries back to the dataset.
7575
const metadata = {
7676
access: entries,
7777
};
7878

7979
// Update will only succeed if the dataset
80-
// has not been modified externally since retrieval
80+
// has not been modified externally since retrieval.
8181
//
8282
// See the BigQuery client library documentation for more details on metadata updates
83-
// https://cloud.google.com/bigquery/docs/updating-model-metadata
83+
// https://cloud.google.com/nodejs/docs/reference/bigquery/latest
8484

85-
// Update just the 'access entries' property of the dataset
85+
// Update just the 'access entries' property of the dataset.
8686
const [updatedDataset] = await client
8787
.dataset(datasetId)
8888
.setMetadata(metadata);
8989

90-
// Show a success message
90+
// Show a success message.
9191
console.log(
9292
`Role '${role}' granted for entity '${entityId}'` +
9393
` in dataset '${datasetId}'.`
@@ -96,7 +96,6 @@ async function grantAccessToDataset(datasetId, entityId, role) {
9696
return updatedDataset.access;
9797
} catch (error) {
9898
if (error.code === HTTP_STATUS.PRECONDITION_FAILED) {
99-
// A read-modify-write error (PreconditionFailed equivalent)
10099
console.error(
101100
`Dataset '${datasetId}' was modified remotely before this update. ` +
102101
'Fetch the latest version and retry.'

bigquery/cloud-client/grantAccessToTableOrView.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
const {BigQuery} = require('@google-cloud/bigquery');
15+
'use strict';
1616

1717
/**
1818
* Grants access to a BigQuery table or view for a specified principal
@@ -32,42 +32,44 @@ async function grantAccessToTableOrView(
3232
role
3333
) {
3434
// [START bigquery_grant_access_to_table_or_view]
35+
const {BigQuery} = require('@google-cloud/bigquery');
36+
3537
// TODO(developer): Update and un-comment below lines
3638

37-
// Google Cloud Platform project
39+
// Google Cloud Platform project.
3840
// projectId = "my_project_id"
3941

40-
// Dataset where the table or view is
42+
// Dataset where the table or view is.
4143
// datasetId = "my_dataset"
4244

43-
// Table or view name to get the access policy
45+
// Table or view name to get the access policy.
4446
// resourceName = "my_table"
4547

46-
// The principal requesting access to the table or view
48+
// The principal requesting access to the table or view.
4749
// Find more details about principal identifiers here:
4850
// https://cloud.google.com/iam/docs/principal-identifiers
4951
// principalId = "user:[email protected]"
5052

51-
// Role to assign to the member
53+
// Role to assign to the member.
5254
// role = "roles/bigquery.dataViewer"
5355

54-
// Instantiate a client
56+
// Instantiate a client.
5557
const client = new BigQuery();
5658

57-
// Get the table reference
59+
// Get the table reference.
5860
const dataset = client.dataset(datasetId);
5961
const table = dataset.table(resourceName);
6062

61-
// Get the IAM access policy for the table or view
63+
// Get the IAM access policy for the table or view.
6264
const [policy] = await table.getIamPolicy();
6365

64-
// Initialize bindings if they do not exist
66+
// Initialize bindings if they do not exist.
6567
if (!policy.bindings) {
6668
policy.bindings = [];
6769
}
6870

6971
// To grant access to a table or view
70-
// add bindings to the Table or View policy
72+
// add bindings to the Table or View policy.
7173
//
7274
// Find more details about Policy and Binding objects here:
7375
// https://cloud.google.com/security-command-center/docs/reference/rest/Shared.Types/Policy
@@ -78,10 +80,10 @@ async function grantAccessToTableOrView(
7880
};
7981
policy.bindings.push(binding);
8082

81-
// Set the IAM access policy with updated bindings
83+
// Set the IAM access policy with updated bindings.
8284
const [updatedPolicy] = await table.setIamPolicy(policy);
8385

84-
// Show a success message
86+
// Show a success message.
8587
console.log(
8688
`Role '${role}' granted for principal '${principalId}' on resource '${datasetId}.${resourceName}'.`
8789
);

bigquery/cloud-client/revokeDatasetAccess.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414

1515
'use strict';
1616

17-
// [START bigquery_revoke_dataset_access]
18-
19-
const {BigQuery} = require('@google-cloud/bigquery');
20-
2117
// Define enum for HTTP codes
2218
const HTTP_STATUS = {
2319
PRECONDITION_FAILED: 412,
@@ -33,67 +29,72 @@ const HTTP_STATUS = {
3329
* @returns {Promise<Array>} A promise that resolves to the updated access entries
3430
*/
3531
async function revokeDatasetAccess(datasetId, entityId) {
36-
// Create a client
37-
const bigquery = new BigQuery();
32+
// [START bigquery_revoke_dataset_access]
33+
// Imports the Google Cloud client library.
34+
const {BigQuery} = require('@google-cloud/bigquery');
3835

39-
// TODO (developer): Update and un-comment below lines
36+
// TODO (developer): Update and un-comment below lines.
4037

41-
// ID of the dataset to revoke access to
38+
// ID of the dataset to revoke access to.
4239
// datasetId = "your-project.your_dataset"
4340

44-
// ID of the user or group from whom you are revoking access
41+
// ID of the user or group from whom you are revoking access.
4542
// Alternatively, the JSON REST API representation of the entity,
46-
// such as a view's table reference
43+
// such as a view's table reference.
4744
// entityId = "[email protected]"
4845

46+
// Instantiate a client.
47+
const bigquery = new BigQuery();
48+
4949
// Get a reference to the dataset.
5050
const [dataset] = await bigquery.dataset(datasetId).get();
5151

5252
// To revoke access to a dataset, remove elements from the access list
5353
//
5454
// See the BigQuery client library documentation for more details on access entries
55+
// https://cloud.google.com/nodejs/docs/reference/secret-manager/4.1.4
5556

5657
// Filter access entries to exclude entries matching the specified entity_id
57-
// and assign a new list back to the access list
58+
// and assign a new list back to the access list.
5859
dataset.metadata.access = dataset.metadata.access.filter(entry => {
59-
// Check for entity_id (specific match)
60+
// Check for entity_id (specific match).
6061
if (entry.entity_id === entityId) {
6162
console.log(
6263
`Found matching entity_id: ${entry.entity_id}, removing entry`
6364
);
6465
return false;
6566
}
6667

67-
// Check for userByEmail field
68+
// Check for userByEmail field.
6869
if (entry.userByEmail === entityId) {
6970
console.log(
7071
`Found matching userByEmail: ${entry.userByEmail}, removing entry`
7172
);
7273
return false;
7374
}
7475

75-
// Check for groupByEmail field
76+
// Check for groupByEmail field.
7677
if (entry.groupByEmail === entityId) {
7778
console.log(
7879
`Found matching groupByEmail: ${entry.groupByEmail}, removing entry`
7980
);
8081
return false;
8182
}
8283

83-
// Keep all other entries
84+
// Keep all other entries.
8485
return true;
8586
});
8687

8788
// Update will only succeed if the dataset
88-
// has not been modified externally since retrieval
89+
// has not been modified externally since retrieval.
8990

9091
try {
91-
// Update just the access entries property of the dataset
92+
// Update just the access entries property of the dataset.
9293
const [updatedDataset] = await dataset.setMetadata(dataset.metadata);
9394

9495
return updatedDataset.access;
9596
} catch (error) {
96-
// Check if it's a precondition failed error (a read-modify-write error)
97+
// Check if it's a precondition failed error (a read-modify-write error).
9798
if (error.code === HTTP_STATUS.PRECONDITION_FAILED) {
9899
console.log(
99100
`Dataset '${dataset.id}' was modified remotely before this update. ` +

bigquery/cloud-client/revokeTableOrViewAccess.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
'use strict';
16+
1517
/**
1618
* Revokes access to a BigQuery table or view
1719
* @param {string} projectId The ID of the Google Cloud project
@@ -29,77 +31,77 @@ async function revokeAccessToTableOrView(
2931
principalToRemove = null
3032
) {
3133
// [START bigquery_revoke_access_to_table_or_view]
32-
// Imports the Google Cloud client library
34+
// Imports the Google Cloud client library.
3335
const {BigQuery} = require('@google-cloud/bigquery');
3436

3537
// TODO (developer): Update and un-comment below lines
3638
// Google Cloud Platform project.
3739
// projectId = "my_project_id"
3840

39-
// Dataset where the table or view is
41+
// Dataset where the table or view is.
4042
// datasetId = "my_dataset"
4143

42-
// Table or view name to get the access policy
44+
// Table or view name to get the access policy.
4345
// resourceName = "my_table"
4446

45-
// (Optional) Role to remove from the table or view
47+
// (Optional) Role to remove from the table or view.
4648
// roleToRemove = "roles/bigquery.dataViewer"
4749

48-
// (Optional) Principal to remove from the table or view
50+
// (Optional) Principal to remove from the table or view.
4951
// principalToRemove = "user:[email protected]"
5052

5153
// Find more information about roles and principals (refered as members) here:
5254
// https://cloud.google.com/security-command-center/docs/reference/rest/Shared.Types/Binding
5355

54-
// Instantiate a client
56+
// Instantiate a client.
5557
const client = new BigQuery();
5658

57-
// Get the table reference
59+
// Get the table reference.
5860
const dataset = client.dataset(datasetId);
5961
const table = dataset.table(resourceName);
6062

61-
// Get the IAM access policy for the table or view
63+
// Get the IAM access policy for the table or view.
6264
const [policy] = await table.getIamPolicy();
6365

64-
// Initialize bindings of they do not exist
66+
// Initialize bindings of they do not exist.
6567
if (!policy.bindings) {
6668
policy.bindings = [];
6769
}
6870

6971
// To revoke access to a table or view,
70-
// remove bindings from the Table or View policy
72+
// remove bindings from the Table or View policy.
7173
//
7274
// Find more details about Policy objects here:
7375
// https://cloud.google.com/security-command-center/docs/reference/rest/Shared.Types/Policy
7476

7577
if (roleToRemove) {
7678
// Filter out all bindings with the `roleToRemove`
77-
// and assign a new list back to the policy bindings
79+
// and assign a new list back to the policy bindings.
7880
policy.bindings = policy.bindings.filter(b => b.role !== roleToRemove);
7981
}
8082

8183
if (principalToRemove) {
82-
// Create a copy to match original code structure
84+
// The `bindings` list is immutable. Create a copy for modifications.
8385
const bindings = [...policy.bindings];
8486

85-
// Filter out the principal from each binding
87+
// Filter out the principal from each binding.
8688
for (const binding of bindings) {
8789
if (binding.members) {
8890
binding.members = binding.members.filter(m => m !== principalToRemove);
8991
}
9092
}
9193

92-
// Filter out bindings with empty members
94+
// Filter out bindings with empty members.
9395
policy.bindings = bindings.filter(
9496
binding => binding.members && binding.members.length > 0
9597
);
9698
}
9799

98100
try {
99-
// Set the IAM access policy with updated bindings
101+
// Set the IAM access policy with updated bindings.
100102
await table.setIamPolicy(policy);
101103

102-
// Get the policy again to confirm it's set correctly
104+
// Get the policy again to confirm it's set correctly.
103105
const [verifiedPolicy] = await table.getIamPolicy();
104106

105107
if (verifiedPolicy && verifiedPolicy.bindings) {

bigquery/cloud-client/viewDatasetAccessPolicy.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ function viewDatasetAccessPolicy(datasetId) {
2424
// Import the Google Cloud client library.
2525
const {BigQuery} = require('@google-cloud/bigquery');
2626

27-
// Create a client
27+
// Instantiate a client.
2828
const bigquery = new BigQuery();
2929

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

@@ -39,6 +39,7 @@ function viewDatasetAccessPolicy(datasetId) {
3939

4040
// Show the list of AccessEntry objects.
4141
// More details about the AccessEntry object in the BigQuery documentation
42+
// https://cloud.google.com/nodejs/docs/reference/bigquery/latest
4243
console.log(
4344
`${accessEntries.length} Access entries in dataset '${datasetId}':`
4445
);

0 commit comments

Comments
 (0)