Skip to content

Commit 5013dfc

Browse files
feat(bigquery): Add samples for control access 3/3
1 parent 4401f90 commit 5013dfc

File tree

4 files changed

+252
-0
lines changed

4 files changed

+252
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
const assert = require('assert');
16+
const {getDataset, setupBeforeAll, teardownAfterAll} = require('./config');
17+
const {viewDatasetAccessPolicy} = require('../viewDatasetAccessPolicy');
18+
19+
describe('viewDatasetAccessPolicy', () => {
20+
before(async () => {
21+
await setupBeforeAll();
22+
});
23+
24+
after(async () => {
25+
await teardownAfterAll();
26+
});
27+
28+
it('should view dataset access policies', async () => {
29+
const dataset = await getDataset();
30+
const accessPolicy = await viewDatasetAccessPolicy(dataset.id);
31+
32+
assert.ok(accessPolicy, 'Access policy should be defined');
33+
assert.ok(Array.isArray(accessPolicy), 'Access policy should be an array');
34+
});
35+
});
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
const assert = require('assert');
16+
const {
17+
getProjectId,
18+
getDataset,
19+
getTable,
20+
getView,
21+
setupBeforeAll,
22+
teardownAfterAll,
23+
} = require('./config.js');
24+
const viewTableOrViewAccessPolicy = require('../viewTableOrViewAccessPolicy.js');
25+
26+
describe('viewTableOrViewAccessPolicy', () => {
27+
before(async () => {
28+
await setupBeforeAll();
29+
});
30+
31+
after(async () => {
32+
await teardownAfterAll();
33+
});
34+
35+
it('should view table access policies', async () => {
36+
const projectId = await getProjectId();
37+
const dataset = await getDataset();
38+
const table = await getTable();
39+
40+
const policy = await viewTableOrViewAccessPolicy(
41+
projectId,
42+
dataset.id,
43+
table.id
44+
);
45+
46+
// Verify that the policy exists.
47+
assert.ok(policy, 'Policy should be defined');
48+
49+
// Verify that bindings exists and is an array.
50+
assert.ok(Array.isArray(policy.bindings), 'Bindings should be an array');
51+
52+
// In a new policy, bindings should be empty.
53+
assert.strictEqual(
54+
policy.bindings.length,
55+
0,
56+
'Bindings list should be empty'
57+
);
58+
59+
// Verify that etag exists, but do not validate its exact value.
60+
assert.ok(policy.etag, 'Etag should be defined');
61+
});
62+
63+
it('should view view access policies', async () => {
64+
const projectId = await getProjectId();
65+
const dataset = await getDataset();
66+
const view = await getView();
67+
68+
const policy = await viewTableOrViewAccessPolicy(
69+
projectId,
70+
dataset.id,
71+
view.id
72+
);
73+
74+
// Verify that the policy exists.
75+
assert.ok(policy, 'Policy should be defined');
76+
77+
// Verify that bindings exists and is an array.
78+
assert.ok(Array.isArray(policy.bindings), 'Bindings should be an array');
79+
80+
// In a new policy, bindings should be empty.
81+
assert.strictEqual(
82+
policy.bindings.length,
83+
0,
84+
'Bindings list should be empty'
85+
);
86+
87+
// Verify that etag exists, but do not validate its exact value.
88+
assert.ok(policy.etag, 'Etag should be defined');
89+
});
90+
});
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
/**
18+
* View access policies for a BigQuery dataset.
19+
* @param {string} datasetId Dataset ID to view access policies for.
20+
* @returns {Array} Array of access entries.
21+
*/
22+
function viewDatasetAccessPolicy(datasetId) {
23+
// [START bigquery_view_dataset_access_policy]
24+
const {BigQuery} = require('@google-cloud/bigquery');
25+
26+
// Instantiate a client.
27+
const bigquery = new BigQuery();
28+
29+
// TODO (developer): Update and un-comment below lines.
30+
31+
// Dataset from which to get the access policy.
32+
// datasetId = "my_dataset_id";
33+
34+
// Get a reference to the dataset.
35+
const dataset = bigquery.dataset(datasetId);
36+
37+
return dataset.getMetadata().then(([metadata]) => {
38+
const accessEntries = metadata.access || [];
39+
40+
// Show the array of AccessEntry objects.
41+
// More details about the AccessEntry object in the BigQuery documentation:
42+
// https://cloud.google.com/nodejs/docs/reference/bigquery/latest
43+
console.log(
44+
`${accessEntries.length} Access entries in dataset '${datasetId}':`
45+
);
46+
for (const accessEntry of accessEntries) {
47+
console.log(`Role: ${accessEntry.role || 'null'}`);
48+
console.log(`Special group: ${accessEntry.specialGroup || 'null'}`);
49+
console.log(`User by Email: ${accessEntry.userByEmail || 'null'}`);
50+
}
51+
52+
return accessEntries;
53+
});
54+
// [END bigquery_view_dataset_access_policy]
55+
}
56+
57+
module.exports = {
58+
viewDatasetAccessPolicy,
59+
};
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
/**
18+
* View access policy for a BigQuery table or view.
19+
*
20+
* @param {string} projectId Google Cloud Platform project.
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+
* @returns {Promise<object>} The IAM policy object.
24+
*/
25+
async function viewTableOrViewAccessPolicy(projectId, datasetId, resourceName) {
26+
// [START bigquery_view_table_or_view_access_policy]
27+
const {BigQuery} = require('@google-cloud/bigquery');
28+
29+
// TODO(developer): Update and un-comment below lines.
30+
31+
// Google Cloud Platform project.
32+
// projectId = "my_project_id";
33+
34+
// Dataset where the table or view is.
35+
// datasetId = "my_dataset_id";
36+
37+
// Table or view name to get the access policy.
38+
// resourceName = "my_table_name_id";
39+
40+
// Instantiate a client.
41+
const client = new BigQuery();
42+
43+
// Get a reference to the dataset by datasetId.
44+
const dataset = client.dataset(datasetId);
45+
// Get a reference to the table by tableName.
46+
const table = dataset.table(resourceName);
47+
48+
// Get the IAM access policy for the table or view.
49+
const [policy] = await table.getIamPolicy();
50+
51+
// Initialize bindings array.
52+
if (!policy.bindings) {
53+
policy.bindings = [];
54+
}
55+
56+
// Show policy details
57+
// Find more details for the Policy object here:
58+
// https://cloud.google.com/bigquery/docs/reference/rest/v2/Policy
59+
console.log(`Access Policy details for table or view '${resourceName}'.`);
60+
console.log(`Bindings: ${JSON.stringify(policy.bindings, null, 2)}`);
61+
console.log(`etag: ${policy.etag}`);
62+
console.log(`Version: ${policy.version}`);
63+
64+
// [END bigquery_view_table_or_view_access_policy]
65+
return policy;
66+
}
67+
68+
module.exports = viewTableOrViewAccessPolicy;

0 commit comments

Comments
 (0)