-
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 all commits
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
70 changes: 70 additions & 0 deletions
70
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,70 @@ | ||
| // 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'; | ||
|
|
||
| const {beforeEach, afterEach, it, describe} = require('mocha'); | ||
| const assert = require('assert'); | ||
| const sinon = require('sinon'); | ||
|
|
||
| const {setupBeforeAll, cleanupResources} = require('./config'); | ||
| const {viewDatasetAccessPolicy} = require('../viewDatasetAccessPolicy'); | ||
|
|
||
| describe('viewDatasetAccessPolicy', () => { | ||
| let datasetId = null; | ||
|
|
||
| beforeEach(async () => { | ||
| const response = await setupBeforeAll(); | ||
| datasetId = response.datasetId; | ||
|
|
||
| sinon.stub(console, 'log'); | ||
| sinon.stub(console, 'error'); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await cleanupResources(datasetId); | ||
| console.log.restore(); | ||
| console.error.restore(); | ||
| }); | ||
|
|
||
| it('should view dataset access policies', async () => { | ||
| // Act: View the dataset access policy | ||
| await viewDatasetAccessPolicy(datasetId); | ||
|
|
||
| // Assert: Check that the initial message was logged | ||
| assert.strictEqual( | ||
| console.log.calledWith( | ||
| sinon.match(`Access entries in dataset '${datasetId}':`) | ||
| ), | ||
| true | ||
| ); | ||
|
|
||
| // We're not checking the exact number of entries since that might vary, | ||
| // but we're making sure the appropriate logging format was followed | ||
| assert.ok( | ||
| console.log.calledWith(sinon.match(/Role:/)), | ||
| 'Should log role information' | ||
| ); | ||
|
|
||
| assert.ok( | ||
| console.log.calledWith(sinon.match(/Special group:/)), | ||
| 'Should log special group information' | ||
| ); | ||
|
|
||
| assert.ok( | ||
| console.log.calledWith(sinon.match(/User by Email:/)), | ||
| 'Should log user by email information' | ||
| ); | ||
| }); | ||
| }); | ||
71 changes: 71 additions & 0 deletions
71
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,71 @@ | ||
| // 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. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const {describe, it, beforeEach, afterEach} = require('mocha'); | ||
| const assert = require('assert'); | ||
| const sinon = require('sinon'); | ||
|
|
||
| const {setupBeforeAll, cleanupResources} = require('./config'); | ||
| const {viewTableOrViewAccessPolicy} = require('../viewTableOrViewAccessPolicy'); | ||
|
|
||
| describe('viewTableOrViewAccessPolicy', () => { | ||
| let datasetId = null; | ||
| let tableId = null; | ||
| const projectId = process.env.GCLOUD_PROJECT; | ||
|
|
||
| beforeEach(async () => { | ||
| const response = await setupBeforeAll(); | ||
| datasetId = response.datasetId; | ||
| tableId = response.tableId; | ||
|
|
||
| sinon.stub(console, 'log'); | ||
| sinon.stub(console, 'error'); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await cleanupResources(datasetId); | ||
| console.log.restore(); | ||
| console.error.restore(); | ||
| }); | ||
|
|
||
| it('should view table access policies', async () => { | ||
| // View the table access policy | ||
| await viewTableOrViewAccessPolicy(projectId, datasetId, tableId); | ||
|
|
||
| // Check that the right messages were logged | ||
| assert.strictEqual( | ||
| console.log.calledWith( | ||
| `Access Policy details for table or view '${tableId}'.` | ||
| ), | ||
| true | ||
| ); | ||
|
|
||
| assert.ok( | ||
| console.log.calledWith(sinon.match('Bindings:')), | ||
| 'Should log bindings information' | ||
| ); | ||
|
|
||
| assert.ok( | ||
| console.log.calledWith(sinon.match('etag:')), | ||
| 'Should log etag information' | ||
| ); | ||
|
|
||
| assert.ok( | ||
| console.log.calledWith(sinon.match('Version:')), | ||
| 'Should log version information' | ||
| ); | ||
| }); | ||
| }); |
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,52 @@ | ||
| // 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'; | ||
|
|
||
| async function main(datasetId) { | ||
| // [START bigquery_view_dataset_access_policy] | ||
|
|
||
| /** | ||
| * TODO(developer): Update and un-comment below lines | ||
| */ | ||
| // const datasetId = "my_project_id.my_dataset"; | ||
|
|
||
| const {BigQuery} = require('@google-cloud/bigquery'); | ||
|
|
||
| // Instantiate a client. | ||
| const bigquery = new BigQuery(); | ||
|
|
||
| async function viewDatasetAccessPolicy() { | ||
| const dataset = bigquery.dataset(datasetId); | ||
|
|
||
| const [metadata] = await dataset.getMetadata(); | ||
| const accessEntries = metadata.access || []; | ||
|
|
||
| // Show the list 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'}`); | ||
| } | ||
| } | ||
| // [END bigquery_view_dataset_access_policy] | ||
| await viewDatasetAccessPolicy(); | ||
| } | ||
|
|
||
| exports.viewDatasetAccessPolicy = main; |
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,56 @@ | ||
| // 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'; | ||
|
|
||
| async function main(projectId, datasetId, resourceName) { | ||
| // [START bigquery_view_table_or_view_access_policy] | ||
|
|
||
| /** | ||
| * TODO(developer): Update and un-comment below lines | ||
| */ | ||
| // const projectId = "YOUR_PROJECT_ID" | ||
| // const datasetId = "YOUR_DATASET_ID" | ||
| // const resourceName = "YOUR_RESOURCE_NAME"; | ||
|
|
||
| const {BigQuery} = require('@google-cloud/bigquery'); | ||
|
|
||
| // Instantiate a client. | ||
| const client = new BigQuery(); | ||
|
|
||
| async function viewTableOrViewAccessPolicy() { | ||
| const dataset = client.dataset(datasetId); | ||
| const table = dataset.table(resourceName); | ||
|
|
||
| // Get the IAM access policy for the table or view. | ||
| const [policy] = await table.getIamPolicy(); | ||
|
|
||
| // Initialize bindings if they don't exist | ||
| 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}`); | ||
| } | ||
| // [END bigquery_view_table_or_view_access_policy] | ||
| await viewTableOrViewAccessPolicy(); | ||
| } | ||
|
|
||
| exports.viewTableOrViewAccessPolicy = main; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
praise: nice use of
match()here.