Skip to content

Commit 0041270

Browse files
committed
testing, stylistic update
1 parent f2b62df commit 0041270

File tree

5 files changed

+152
-451
lines changed

5 files changed

+152
-451
lines changed

bigquery/cloud-client/grantAccessToDataset.js

Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,60 +14,43 @@
1414

1515
'use strict';
1616

17-
/**
18-
* Grants access to a BigQuery dataset for a specified entity.
19-
*
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.
23-
* @returns {Promise<Array>} Array of access entries.
24-
*/
25-
async function grantAccessToDataset(datasetId, entityId, role) {
17+
async function main(datasetId, entityId, role) {
2618
// [START bigquery_grant_access_to_dataset]
27-
const {BigQuery} = require('@google-cloud/bigquery');
28-
29-
// Define enum for HTTP codes.
30-
const HTTP_STATUS = {
31-
PRECONDITION_FAILED: 412,
32-
};
3319

34-
// TODO(developer): Update and un-comment below lines.
20+
/**
21+
* TODO(developer): Update and un-comment below lines.
22+
*/
3523

36-
// ID of the dataset to revoke access to.
37-
// datasetId = "my_project_id.my_dataset_name";
24+
// const datasetId = "my_project_id.my_dataset_name";
3825

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

4429
// One of the "Basic roles for datasets" described here:
4530
// https://cloud.google.com/bigquery/docs/access-control-basic-roles#dataset-basic-roles
46-
// role = "READER";
31+
// const role = "READER";
32+
33+
const {BigQuery} = require('@google-cloud/bigquery');
34+
35+
// Instantiate a client.
36+
const client = new BigQuery();
4737

4838
// Type of entity you are granting access to.
4939
// Find allowed allowed entity type names here:
5040
// https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets#resource:-dataset
51-
// In this case, we're using groupByEmail
5241
const entityType = 'groupByEmail';
5342

54-
// Instantiate a client.
55-
const client = new BigQuery();
56-
57-
try {
58-
// Get a reference to the dataset.
43+
async function grantAccessToDataset() {
5944
const [dataset] = await client.dataset(datasetId).get();
6045

6146
// The 'access entries' array is immutable. Create a copy for modifications.
62-
const entries = Array.isArray(dataset.metadata.access)
63-
? [...dataset.metadata.access]
64-
: [];
47+
const entries = [...dataset.metadata.access];
6548

6649
// Append an AccessEntry to grant the role to a dataset.
6750
// Find more details about the AccessEntry object in the BigQuery documentation:
6851
// https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.dataset.AccessEntry
6952
entries.push({
70-
role: role,
53+
role,
7154
[entityType]: entityId,
7255
});
7356

@@ -83,29 +66,14 @@ async function grantAccessToDataset(datasetId, entityId, role) {
8366
// https://cloud.google.com/nodejs/docs/reference/bigquery/latest
8467

8568
// Update just the 'access entries' property of the dataset.
86-
const [updatedDataset] = await client
87-
.dataset(datasetId)
88-
.setMetadata(metadata);
69+
await client.dataset(datasetId).setMetadata(metadata);
8970

90-
// Show a success message.
9171
console.log(
92-
`Role '${role}' granted for entity '${entityId}' in dataset '${datasetId}'.`
72+
`Role '${role}' granted for entity '${entityId}' in '${datasetId}'.`
9373
);
94-
95-
return updatedDataset.access;
96-
} catch (error) {
97-
if (error.code === HTTP_STATUS.PRECONDITION_FAILED) {
98-
console.error(
99-
`Dataset '${datasetId}' was modified remotely before this update. ` +
100-
'Fetch the latest version and retry.'
101-
);
102-
} else {
103-
throw error;
104-
}
10574
}
10675
// [END bigquery_grant_access_to_dataset]
76+
await grantAccessToDataset();
10777
}
10878

109-
module.exports = {
110-
grantAccessToDataset,
111-
};
79+
exports.grantAccessToDataset = main;

bigquery/cloud-client/grantAccessToTableOrView.js

Lines changed: 43 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -14,82 +14,58 @@
1414

1515
'use strict';
1616

17-
/**
18-
* Grants access to a BigQuery table or view for a specified principal.
19-
*
20-
* @param {string} projectId Google Cloud Platform project ID.
21-
* @param {string} datasetId Dataset where the table or view is.
22-
* @param {string} resourceName Table or view name to get the access policy.
23-
* @param {string} principalId The principal requesting access to the table or view.
24-
* @param {string} role Role to assign to the member.
25-
* @returns {Promise<object[]>} The updated policy bindings.
26-
*/
27-
async function grantAccessToTableOrView(
28-
projectId,
29-
datasetId,
30-
resourceName,
31-
principalId,
32-
role
33-
) {
17+
async function main(projectId, datasetId, tableId, principalId, role) {
3418
// [START bigquery_grant_access_to_table_or_view]
35-
const {BigQuery} = require('@google-cloud/bigquery');
36-
37-
// TODO(developer): Update and un-comment below lines.
38-
39-
// Google Cloud Platform project.
40-
// projectId = "my_project_id"
41-
42-
// Dataset where the table or view is.
43-
// datasetId = "my_dataset_id"
44-
45-
// Table or view name to get the access policy.
46-
// resourceName = "my_table_id"
4719

48-
// The principal requesting access to the table or view.
49-
// Find more details about principal identifiers here:
50-
// https://cloud.google.com/iam/docs/principal-identifiers
51-
// principalId = "user:[email protected]"
20+
/**
21+
* TODO(developer): Update and un-comment below lines
22+
*/
23+
// const projectId = "YOUR_PROJECT_ID";
24+
// const datasetId = "YOUR_DATASET_ID";
25+
// const tableId = "YOUR_TABLE_ID";
26+
// const principalId = "YOUR_PRINCIPAL_ID";
27+
// const role = "YOUR_ROLE";
5228

53-
// Role to assign to the member.
54-
// role = "roles/bigquery.dataViewer"
29+
const {BigQuery} = require('@google-cloud/bigquery');
5530

5631
// Instantiate a client.
5732
const client = new BigQuery();
5833

59-
// Get a reference to the dataset by datasetId.
60-
const dataset = client.dataset(datasetId);
61-
// Get a reference to the table by tableName.
62-
const table = dataset.table(resourceName);
63-
64-
// Get the IAM access policy for the table or view.
65-
const [policy] = await table.getIamPolicy();
66-
67-
// Initialize bindings array.
68-
if (!policy.bindings) {
69-
policy.bindings = [];
34+
async function grantAccessToTableOrView() {
35+
const dataset = client.dataset(datasetId);
36+
const table = dataset.table(tableId);
37+
38+
// Get the IAM access policy for the table or view.
39+
const [policy] = await table.getIamPolicy();
40+
41+
// Initialize bindings array.
42+
if (!policy.bindings) {
43+
policy.bindings = [];
44+
}
45+
46+
// To grant access to a table or view
47+
// add bindings to the Table or View policy.
48+
//
49+
// Find more details about Policy and Binding objects here:
50+
// https://cloud.google.com/security-command-center/docs/reference/rest/Shared.Types/Policy
51+
// https://cloud.google.com/security-command-center/docs/reference/rest/Shared.Types/Binding
52+
const binding = {
53+
role,
54+
members: [principalId],
55+
};
56+
policy.bindings.push(binding);
57+
58+
// Set the IAM access policy with updated bindings.
59+
await table.setIamPolicy(policy);
60+
61+
// Show a success message.
62+
console.log(
63+
`Role '${role}' granted for principal '${principalId}' on resource '${datasetId}.${tableId}'.`
64+
);
7065
}
7166

72-
// To grant access to a table or view
73-
// add bindings to the Table or View policy.
74-
//
75-
// Find more details about Policy and Binding objects here:
76-
// https://cloud.google.com/security-command-center/docs/reference/rest/Shared.Types/Policy
77-
// https://cloud.google.com/security-command-center/docs/reference/rest/Shared.Types/Binding
78-
const binding = {
79-
role: role,
80-
members: [principalId],
81-
};
82-
policy.bindings.push(binding);
83-
84-
// Set the IAM access policy with updated bindings.
85-
const [updatedPolicy] = await table.setIamPolicy(policy);
86-
87-
// Show a success message.
88-
console.log(
89-
`Role '${role}' granted for principal '${principalId}' on resource '${datasetId}.${resourceName}'.`
90-
);
67+
await grantAccessToTableOrView();
9168
// [END bigquery_grant_access_to_table_or_view]
92-
return updatedPolicy.bindings;
9369
}
9470

95-
module.exports = {grantAccessToTableOrView};
71+
exports.grantAccessToTableOrView = main;

0 commit comments

Comments
 (0)