|
14 | 14 |
|
15 | 15 | 'use strict'; |
16 | 16 |
|
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) { |
34 | 18 | // [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" |
47 | 19 |
|
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"; |
52 | 28 |
|
53 | | - // Role to assign to the member. |
54 | | - // role = "roles/bigquery.dataViewer" |
| 29 | + const {BigQuery} = require('@google-cloud/bigquery'); |
55 | 30 |
|
56 | 31 | // Instantiate a client. |
57 | 32 | const client = new BigQuery(); |
58 | 33 |
|
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 | + ); |
70 | 65 | } |
71 | 66 |
|
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(); |
91 | 68 | // [END bigquery_grant_access_to_table_or_view] |
92 | | - return updatedPolicy.bindings; |
93 | 69 | } |
94 | 70 |
|
95 | | -module.exports = {grantAccessToTableOrView}; |
| 71 | +exports.grantAccessToTableOrView = main; |
0 commit comments