Skip to content

Commit 12dd1f6

Browse files
feat(bigquery): Add samples for control access 3/3 (#4025)
* feat(bigquery): Add samples for control access 3/3 * chore(bigquery): testing, stylistic update * fix(bigquery): update test from viewTableOrViewAccessPolicy * fix(bigquery): fix linting issue
1 parent 4c03eb2 commit 12dd1f6

File tree

4 files changed

+249
-0
lines changed

4 files changed

+249
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const {beforeEach, afterEach, it, describe} = require('mocha');
18+
const assert = require('assert');
19+
const sinon = require('sinon');
20+
21+
const {setupBeforeAll, cleanupResources} = require('./config');
22+
const {viewDatasetAccessPolicy} = require('../viewDatasetAccessPolicy');
23+
24+
describe('viewDatasetAccessPolicy', () => {
25+
let datasetId = null;
26+
27+
beforeEach(async () => {
28+
const response = await setupBeforeAll();
29+
datasetId = response.datasetId;
30+
31+
sinon.stub(console, 'log');
32+
sinon.stub(console, 'error');
33+
});
34+
35+
afterEach(async () => {
36+
await cleanupResources(datasetId);
37+
console.log.restore();
38+
console.error.restore();
39+
});
40+
41+
it('should view dataset access policies', async () => {
42+
// Act: View the dataset access policy
43+
await viewDatasetAccessPolicy(datasetId);
44+
45+
// Assert: Check that the initial message was logged
46+
assert.strictEqual(
47+
console.log.calledWith(
48+
sinon.match(`Access entries in dataset '${datasetId}':`)
49+
),
50+
true
51+
);
52+
53+
// We're not checking the exact number of entries since that might vary,
54+
// but we're making sure the appropriate logging format was followed
55+
assert.ok(
56+
console.log.calledWith(sinon.match(/Role:/)),
57+
'Should log role information'
58+
);
59+
60+
assert.ok(
61+
console.log.calledWith(sinon.match(/Special group:/)),
62+
'Should log special group information'
63+
);
64+
65+
assert.ok(
66+
console.log.calledWith(sinon.match(/User by Email:/)),
67+
'Should log user by email information'
68+
);
69+
});
70+
});
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const {describe, it, beforeEach, afterEach} = require('mocha');
18+
const assert = require('assert');
19+
const sinon = require('sinon');
20+
21+
const {setupBeforeAll, cleanupResources} = require('./config');
22+
const {viewTableOrViewAccessPolicy} = require('../viewTableOrViewAccessPolicy');
23+
24+
describe('viewTableOrViewAccessPolicy', () => {
25+
let datasetId = null;
26+
let tableId = null;
27+
const projectId = process.env.GCLOUD_PROJECT;
28+
29+
beforeEach(async () => {
30+
const response = await setupBeforeAll();
31+
datasetId = response.datasetId;
32+
tableId = response.tableId;
33+
34+
sinon.stub(console, 'log');
35+
sinon.stub(console, 'error');
36+
});
37+
38+
afterEach(async () => {
39+
await cleanupResources(datasetId);
40+
console.log.restore();
41+
console.error.restore();
42+
});
43+
44+
it('should view table access policies', async () => {
45+
// View the table access policy
46+
await viewTableOrViewAccessPolicy(projectId, datasetId, tableId);
47+
48+
// Check that the right messages were logged
49+
assert.strictEqual(
50+
console.log.calledWith(
51+
`Access Policy details for table or view '${tableId}'.`
52+
),
53+
true
54+
);
55+
56+
assert.ok(
57+
console.log.calledWith(sinon.match('Bindings:')),
58+
'Should log bindings information'
59+
);
60+
61+
assert.ok(
62+
console.log.calledWith(sinon.match('etag:')),
63+
'Should log etag information'
64+
);
65+
66+
assert.ok(
67+
console.log.calledWith(sinon.match('Version:')),
68+
'Should log version information'
69+
);
70+
});
71+
});
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main(datasetId) {
18+
// [START bigquery_view_dataset_access_policy]
19+
20+
/**
21+
* TODO(developer): Update and un-comment below lines
22+
*/
23+
// const datasetId = "my_project_id.my_dataset";
24+
25+
const {BigQuery} = require('@google-cloud/bigquery');
26+
27+
// Instantiate a client.
28+
const bigquery = new BigQuery();
29+
30+
async function viewDatasetAccessPolicy() {
31+
const dataset = bigquery.dataset(datasetId);
32+
33+
const [metadata] = await dataset.getMetadata();
34+
const accessEntries = metadata.access || [];
35+
36+
// Show the list of AccessEntry objects.
37+
// More details about the AccessEntry object in the BigQuery documentation:
38+
// https://cloud.google.com/nodejs/docs/reference/bigquery/latest
39+
console.log(
40+
`${accessEntries.length} Access entries in dataset '${datasetId}':`
41+
);
42+
for (const accessEntry of accessEntries) {
43+
console.log(`Role: ${accessEntry.role || 'null'}`);
44+
console.log(`Special group: ${accessEntry.specialGroup || 'null'}`);
45+
console.log(`User by Email: ${accessEntry.userByEmail || 'null'}`);
46+
}
47+
}
48+
// [END bigquery_view_dataset_access_policy]
49+
await viewDatasetAccessPolicy();
50+
}
51+
52+
exports.viewDatasetAccessPolicy = main;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main(projectId, datasetId, resourceName) {
18+
// [START bigquery_view_table_or_view_access_policy]
19+
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 resourceName = "YOUR_RESOURCE_NAME";
26+
27+
const {BigQuery} = require('@google-cloud/bigquery');
28+
29+
// Instantiate a client.
30+
const client = new BigQuery();
31+
32+
async function viewTableOrViewAccessPolicy() {
33+
const dataset = client.dataset(datasetId);
34+
const table = dataset.table(resourceName);
35+
36+
// Get the IAM access policy for the table or view.
37+
const [policy] = await table.getIamPolicy();
38+
39+
// Initialize bindings if they don't exist
40+
if (!policy.bindings) {
41+
policy.bindings = [];
42+
}
43+
44+
// Show policy details.
45+
// Find more details for the Policy object here:
46+
// https://cloud.google.com/bigquery/docs/reference/rest/v2/Policy
47+
console.log(`Access Policy details for table or view '${resourceName}'.`);
48+
console.log(`Bindings: ${JSON.stringify(policy.bindings, null, 2)}`);
49+
console.log(`etag: ${policy.etag}`);
50+
console.log(`Version: ${policy.version}`);
51+
}
52+
// [END bigquery_view_table_or_view_access_policy]
53+
await viewTableOrViewAccessPolicy();
54+
}
55+
56+
exports.viewTableOrViewAccessPolicy = main;

0 commit comments

Comments
 (0)