-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(bigquery): Add samples for control access 3/3 #4025
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5013dfc
feat(bigquery): Add samples for control access 3/3
hivanalejandro a49dfdd
Merge branch 'main' into hivanalejandro-features-bigquery-3
hivanalejandro 250ed79
chore(bigquery): testing, stylistic update
hivanalejandro 6a26991
fix(bigquery): update test from viewTableOrViewAccessPolicy
hivanalejandro 476a840
fix(bigquery): fix linting issue
hivanalejandro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
bigquery/cloud-client/test/viewDatasetAccessPolicy.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| const assert = require('assert'); | ||
| const {getDataset, setupBeforeAll, teardownAfterAll} = require('./config'); | ||
| const {viewDatasetAccessPolicy} = require('../viewDatasetAccessPolicy'); | ||
|
|
||
| describe('viewDatasetAccessPolicy', () => { | ||
| before(async () => { | ||
| await setupBeforeAll(); | ||
| }); | ||
|
|
||
| after(async () => { | ||
| await teardownAfterAll(); | ||
| }); | ||
|
|
||
| it('should view dataset access policies', async () => { | ||
| const dataset = await getDataset(); | ||
| const accessPolicy = await viewDatasetAccessPolicy(dataset.id); | ||
|
|
||
| assert.ok(accessPolicy, 'Access policy should be defined'); | ||
| assert.ok(Array.isArray(accessPolicy), 'Access policy should be an array'); | ||
| }); | ||
| }); |
90 changes: 90 additions & 0 deletions
90
bigquery/cloud-client/test/viewTableOrViewAccessPolicy.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| const assert = require('assert'); | ||
| const { | ||
| getProjectId, | ||
| getDataset, | ||
| getTable, | ||
| getView, | ||
| setupBeforeAll, | ||
| teardownAfterAll, | ||
| } = require('./config.js'); | ||
| const viewTableOrViewAccessPolicy = require('../viewTableOrViewAccessPolicy.js'); | ||
|
|
||
| describe('viewTableOrViewAccessPolicy', () => { | ||
| before(async () => { | ||
| await setupBeforeAll(); | ||
| }); | ||
|
|
||
| after(async () => { | ||
| await teardownAfterAll(); | ||
| }); | ||
|
|
||
| it('should view table access policies', async () => { | ||
| const projectId = await getProjectId(); | ||
| const dataset = await getDataset(); | ||
| const table = await getTable(); | ||
|
|
||
| const policy = await viewTableOrViewAccessPolicy( | ||
| projectId, | ||
| dataset.id, | ||
| table.id | ||
| ); | ||
|
|
||
| // Verify that the policy exists. | ||
| assert.ok(policy, 'Policy should be defined'); | ||
|
|
||
| // Verify that bindings exists and is an array. | ||
| assert.ok(Array.isArray(policy.bindings), 'Bindings should be an array'); | ||
|
|
||
| // In a new policy, bindings should be empty. | ||
| assert.strictEqual( | ||
| policy.bindings.length, | ||
| 0, | ||
| 'Bindings list should be empty' | ||
| ); | ||
|
|
||
| // Verify that etag exists, but do not validate its exact value. | ||
| assert.ok(policy.etag, 'Etag should be defined'); | ||
| }); | ||
|
|
||
| it('should view view access policies', async () => { | ||
| const projectId = await getProjectId(); | ||
| const dataset = await getDataset(); | ||
| const view = await getView(); | ||
|
|
||
| const policy = await viewTableOrViewAccessPolicy( | ||
| projectId, | ||
| dataset.id, | ||
| view.id | ||
| ); | ||
|
|
||
| // Verify that the policy exists. | ||
| assert.ok(policy, 'Policy should be defined'); | ||
|
|
||
| // Verify that bindings exists and is an array. | ||
| assert.ok(Array.isArray(policy.bindings), 'Bindings should be an array'); | ||
|
|
||
| // In a new policy, bindings should be empty. | ||
| assert.strictEqual( | ||
| policy.bindings.length, | ||
| 0, | ||
| 'Bindings list should be empty' | ||
| ); | ||
|
|
||
| // Verify that etag exists, but do not validate its exact value. | ||
| assert.ok(policy.etag, 'Etag should be defined'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| /** | ||
| * View access policies for a BigQuery dataset. | ||
| * @param {string} datasetId Dataset ID to view access policies for. | ||
| * @returns {Array} Array of access entries. | ||
| */ | ||
| function viewDatasetAccessPolicy(datasetId) { | ||
| // [START bigquery_view_dataset_access_policy] | ||
| const {BigQuery} = require('@google-cloud/bigquery'); | ||
|
|
||
| // Instantiate a client. | ||
| const bigquery = new BigQuery(); | ||
|
|
||
| // TODO (developer): Update and un-comment below lines. | ||
|
|
||
| // Dataset from which to get the access policy. | ||
| // datasetId = "my_dataset_id"; | ||
|
|
||
| // Get a reference to the dataset. | ||
| const dataset = bigquery.dataset(datasetId); | ||
|
|
||
| return dataset.getMetadata().then(([metadata]) => { | ||
| const accessEntries = metadata.access || []; | ||
|
|
||
| // Show the array of AccessEntry objects. | ||
| // More details about the AccessEntry object in the BigQuery documentation: | ||
| // https://cloud.google.com/nodejs/docs/reference/bigquery/latest | ||
| console.log( | ||
| `${accessEntries.length} Access entries in dataset '${datasetId}':` | ||
| ); | ||
| for (const accessEntry of accessEntries) { | ||
| console.log(`Role: ${accessEntry.role || 'null'}`); | ||
| console.log(`Special group: ${accessEntry.specialGroup || 'null'}`); | ||
| console.log(`User by Email: ${accessEntry.userByEmail || 'null'}`); | ||
| } | ||
|
|
||
| return accessEntries; | ||
| }); | ||
hivanalejandro marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // [END bigquery_view_dataset_access_policy] | ||
| } | ||
|
|
||
| module.exports = { | ||
| viewDatasetAccessPolicy, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| /** | ||
| * View access policy for a BigQuery table or view. | ||
| * | ||
| * @param {string} projectId Google Cloud Platform project. | ||
| * @param {string} datasetId Dataset where the table or view is. | ||
| * @param {string} resourceName Table or view name to get the access policy. | ||
| * @returns {Promise<object>} The IAM policy object. | ||
| */ | ||
| async function viewTableOrViewAccessPolicy(projectId, datasetId, resourceName) { | ||
| // [START bigquery_view_table_or_view_access_policy] | ||
| const {BigQuery} = require('@google-cloud/bigquery'); | ||
|
|
||
| // TODO(developer): Update and un-comment below lines. | ||
|
|
||
| // Google Cloud Platform project. | ||
| // projectId = "my_project_id"; | ||
|
|
||
| // Dataset where the table or view is. | ||
| // datasetId = "my_dataset_id"; | ||
|
|
||
| // Table or view name to get the access policy. | ||
| // resourceName = "my_table_name_id"; | ||
|
|
||
briandorsey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Instantiate a client. | ||
| const client = new BigQuery(); | ||
|
|
||
| // Get a reference to the dataset by datasetId. | ||
| const dataset = client.dataset(datasetId); | ||
| // Get a reference to the table by tableName. | ||
| const table = dataset.table(resourceName); | ||
|
|
||
| // Get the IAM access policy for the table or view. | ||
| const [policy] = await table.getIamPolicy(); | ||
|
|
||
| // Initialize bindings array. | ||
| if (!policy.bindings) { | ||
| policy.bindings = []; | ||
| } | ||
|
|
||
| // Show policy details | ||
| // Find more details for the Policy object here: | ||
| // https://cloud.google.com/bigquery/docs/reference/rest/v2/Policy | ||
| console.log(`Access Policy details for table or view '${resourceName}'.`); | ||
| console.log(`Bindings: ${JSON.stringify(policy.bindings, null, 2)}`); | ||
| console.log(`etag: ${policy.etag}`); | ||
| console.log(`Version: ${policy.version}`); | ||
|
|
||
hivanalejandro marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // [END bigquery_view_table_or_view_access_policy] | ||
| return policy; | ||
| } | ||
|
|
||
| module.exports = viewTableOrViewAccessPolicy; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.