Skip to content

Commit 62ebc2c

Browse files
feat(bigquery): Update app.js file to add revokeTableOrViewAccess & fix linting errors
1 parent 58ee72a commit 62ebc2c

File tree

3 files changed

+203
-193
lines changed

3 files changed

+203
-193
lines changed

bigquery/cloud-client/app.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,29 @@ const {viewDatasetAccessPolicy} = require('./src/viewDatasetAccessPolicy');
1818
const {
1919
viewTableOrViewAccessPolicy,
2020
} = require('./src/viewTableOrViewAccessPolicy');
21+
const {revokeTableOrViewAccess} = require('./src/revokeTableOrViewAccess');
2122

2223
async function main() {
2324
try {
25+
const projectId = process.env.GOOGLE_CLOUD_PROJECT;
26+
2427
// Example usage of dataset access policy viewer
2528
await viewDatasetAccessPolicy();
2629

2730
// Example usage of table/view access policy viewer
28-
const projectId = process.env.GOOGLE_CLOUD_PROJECT;
2931
await viewTableOrViewAccessPolicy({
3032
projectId,
3133
datasetId: 'my_new_dataset',
3234
resourceName: 'my_table',
3335
});
36+
37+
await revokeTableOrViewAccess({
38+
projectId,
39+
datasetId: 'my_new_dataset',
40+
resourceName: 'my_table',
41+
memberToRevoke: 'group:[email protected]',
42+
roleToRevoke: 'roles/bigquery.dataViewer',
43+
});
3444
} catch (error) {
3545
console.error('Error:', error);
3646
process.exitCode = 1;

bigquery/cloud-client/src/revokeTableOrViewAccess.js

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
const { BigQuery } = require("@google-cloud/bigquery");
15+
const {BigQuery} = require('@google-cloud/bigquery');
1616

1717
/**
1818
* Revokes access to a BigQuery table or view
@@ -25,69 +25,69 @@ const { BigQuery } = require("@google-cloud/bigquery");
2525
* @returns {Promise<void>}
2626
*/
2727
async function revokeTableOrViewAccess({
28-
projectId,
29-
datasetId,
30-
resourceId,
31-
memberToRevoke,
32-
roleToRevoke = "roles/bigquery.dataViewer",
28+
projectId,
29+
datasetId,
30+
resourceId,
31+
memberToRevoke,
32+
roleToRevoke = 'roles/bigquery.dataViewer',
3333
}) {
34-
try {
35-
// Create BigQuery client
36-
const bigquery = new BigQuery({
37-
projectId: projectId,
38-
});
34+
try {
35+
// Create BigQuery client
36+
const bigquery = new BigQuery({
37+
projectId: projectId,
38+
});
3939

40-
// Get reference to the table or view
41-
const dataset = bigquery.dataset(datasetId);
42-
const table = dataset.table(resourceId);
40+
// Get reference to the table or view
41+
const dataset = bigquery.dataset(datasetId);
42+
const table = dataset.table(resourceId);
4343

44-
// Get current IAM policy
45-
const [policy] = await table.iam.getPolicy();
46-
console.log(
47-
"Current IAM Policy:",
48-
JSON.stringify(policy.bindings, null, 2)
49-
);
44+
// Get current IAM policy
45+
const [policy] = await table.iam.getPolicy();
46+
console.log(
47+
'Current IAM Policy:',
48+
JSON.stringify(policy.bindings, null, 2)
49+
);
5050

51-
// Filter bindings based on parameters
52-
let newBindings = policy.bindings;
51+
// Filter bindings based on parameters
52+
let newBindings = policy.bindings;
5353

54-
if (memberToRevoke) {
55-
// Remove specific member from specific role
56-
newBindings = policy.bindings
57-
.map((binding) => ({
58-
...binding,
59-
members:
60-
binding.role === roleToRevoke
61-
? binding.members.filter((member) => member !== memberToRevoke)
62-
: binding.members,
63-
}))
64-
.filter((binding) => binding.members.length > 0);
65-
} else {
66-
// Remove all bindings for the specified role
67-
newBindings = policy.bindings.filter(
68-
(binding) => binding.role !== roleToRevoke
69-
);
70-
}
54+
if (memberToRevoke) {
55+
// Remove specific member from specific role
56+
newBindings = policy.bindings
57+
.map(binding => ({
58+
...binding,
59+
members:
60+
binding.role === roleToRevoke
61+
? binding.members.filter(member => member !== memberToRevoke)
62+
: binding.members,
63+
}))
64+
.filter(binding => binding.members.length > 0);
65+
} else {
66+
// Remove all bindings for the specified role
67+
newBindings = policy.bindings.filter(
68+
binding => binding.role !== roleToRevoke
69+
);
70+
}
7171

72-
// Create new policy with updated bindings
73-
const newPolicy = {
74-
bindings: newBindings,
75-
};
72+
// Create new policy with updated bindings
73+
const newPolicy = {
74+
bindings: newBindings,
75+
};
7676

77-
// Set the new IAM policy
78-
await table.iam.setPolicy(newPolicy);
79-
console.log(`Access revoked successfully for ${resourceId}`);
77+
// Set the new IAM policy
78+
await table.iam.setPolicy(newPolicy);
79+
console.log(`Access revoked successfully for ${resourceId}`);
8080

81-
// Verify the changes
82-
const [updatedPolicy] = await table.iam.getPolicy();
83-
console.log(
84-
"Updated IAM Policy:",
85-
JSON.stringify(updatedPolicy.bindings, null, 2)
86-
);
87-
} catch (error) {
88-
console.error("Error revoking access:", error);
89-
throw error;
90-
}
81+
// Verify the changes
82+
const [updatedPolicy] = await table.iam.getPolicy();
83+
console.log(
84+
'Updated IAM Policy:',
85+
JSON.stringify(updatedPolicy.bindings, null, 2)
86+
);
87+
} catch (error) {
88+
console.error('Error revoking access:', error);
89+
throw error;
90+
}
9191
}
9292

93-
module.exports = { revokeTableOrViewAccess };
93+
module.exports = {revokeTableOrViewAccess};

0 commit comments

Comments
 (0)