|
| 1 | +// Copyright 2024 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 | +// sample-metadata: |
| 16 | +// title: Add and drop new database role |
| 17 | +// usage: node add-and-drop-new-database-role.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID> |
| 18 | + |
| 19 | +'use strict'; |
| 20 | + |
| 21 | +async function main( |
| 22 | + instanceId = 'my-instance', |
| 23 | + databaseId = 'my-database', |
| 24 | + projectId = 'my-project-id' |
| 25 | +) { |
| 26 | + // [START spanner_add_and_drop_database_role] |
| 27 | + /** |
| 28 | + * TODO(developer): Uncomment these variables before running the sample. |
| 29 | + */ |
| 30 | + // const instanceId = 'my-instance'; |
| 31 | + // const databaseId = 'my-database'; |
| 32 | + // const projectId = 'my-project-id'; |
| 33 | + |
| 34 | + // Imports the Google Cloud client library |
| 35 | + const {Spanner} = require('@google-cloud/spanner'); |
| 36 | + |
| 37 | + // creates a client |
| 38 | + const spanner = new Spanner({ |
| 39 | + projectId: projectId, |
| 40 | + }); |
| 41 | + |
| 42 | + const databaseAdminClient = spanner.getDatabaseAdminClient(); |
| 43 | + |
| 44 | + async function addAndDropNewDatabaseRole() { |
| 45 | + // Creates a new user defined role and grant permissions |
| 46 | + try { |
| 47 | + const createRequest = [ |
| 48 | + 'CREATE ROLE parent', |
| 49 | + 'GRANT SELECT ON TABLE Singers TO ROLE parent', |
| 50 | + 'CREATE ROLE child', |
| 51 | + 'GRANT ROLE parent TO ROLE child', |
| 52 | + ]; |
| 53 | + const [createOperation] = await databaseAdminClient.updateDatabaseDdl({ |
| 54 | + database: databaseAdminClient.databasePath( |
| 55 | + projectId, |
| 56 | + instanceId, |
| 57 | + databaseId |
| 58 | + ), |
| 59 | + statements: createRequest, |
| 60 | + }); |
| 61 | + |
| 62 | + console.log('Waiting for operation to complete...'); |
| 63 | + await createOperation.promise(); |
| 64 | + |
| 65 | + console.log('Created roles child and parent and granted privileges'); |
| 66 | + |
| 67 | + // Revoke permissions and drop child role. |
| 68 | + // A role can't be dropped until all its permissions are revoked. |
| 69 | + const dropRequest = [ |
| 70 | + 'REVOKE ROLE parent FROM ROLE child', |
| 71 | + 'DROP ROLE child', |
| 72 | + ]; |
| 73 | + const [dropOperation] = await databaseAdminClient.updateDatabaseDdl({ |
| 74 | + database: databaseAdminClient.databasePath( |
| 75 | + projectId, |
| 76 | + instanceId, |
| 77 | + databaseId |
| 78 | + ), |
| 79 | + statements: dropRequest, |
| 80 | + }); |
| 81 | + |
| 82 | + console.log('Waiting for operation to complete...'); |
| 83 | + await dropOperation.promise(); |
| 84 | + |
| 85 | + console.log('Revoked privileges and dropped role child'); |
| 86 | + } catch (err) { |
| 87 | + console.error('Error adding or dropping database roles:', err); |
| 88 | + } finally { |
| 89 | + // Close the spanner client when finished. |
| 90 | + // The databaseAdminClient does not require explicit closure. The closure of the Spanner client will automatically close the databaseAdminClient. |
| 91 | + spanner.close(); |
| 92 | + } |
| 93 | + } |
| 94 | + await addAndDropNewDatabaseRole(); |
| 95 | + // [END spanner_add_and_drop_database_role] |
| 96 | +} |
| 97 | + |
| 98 | +main(...process.argv.slice(2)); |
0 commit comments