Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions spanner/add-and-drop-new-database-role.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright 2024 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.

// sample-metadata:
// title: Add and drop new database role
// usage: node add-and-drop-new-database-role.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>

'use strict';

async function main(
instanceId = 'my-instance',
databaseId = 'my-database',
projectId = 'my-project-id'
) {
// [START spanner_add_and_drop_database_role]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const instanceId = 'my-instance';
// const databaseId = 'my-database';
// const projectId = 'my-project-id';

// Imports the Google Cloud client library
const {Spanner} = require('@google-cloud/spanner');

// creates a client
const spanner = new Spanner({
projectId: projectId,
});

const databaseAdminClient = spanner.getDatabaseAdminClient();

async function addAndDropNewDatabaseRole() {
// Creates a new user defined role and grant permissions
try {
const createRequest = [
'CREATE ROLE parent',
'GRANT SELECT ON TABLE Singers TO ROLE parent',
'CREATE ROLE child',
'GRANT ROLE parent TO ROLE child',
];
const [createOperation] = await databaseAdminClient.updateDatabaseDdl({
database: databaseAdminClient.databasePath(
projectId,
instanceId,
databaseId
),
statements: createRequest,
});

console.log('Waiting for operation to complete...');
await createOperation.promise();

console.log('Created roles child and parent and granted privileges');

// Revoke permissions and drop child role.
// A role can't be dropped until all its permissions are revoked.
const dropRequest = [
'REVOKE ROLE parent FROM ROLE child',
'DROP ROLE child',
];
const [dropOperation] = await databaseAdminClient.updateDatabaseDdl({
database: databaseAdminClient.databasePath(
projectId,
instanceId,
databaseId
),
statements: dropRequest,
});

console.log('Waiting for operation to complete...');
await dropOperation.promise();

console.log('Revoked privileges and dropped role child');
} catch (err) {
console.error('Error adding or dropping database roles:', err);
} finally {
// Close the spanner client when finished.
// The databaseAdminClient does not require explicit closure. The closure of the Spanner client will automatically close the databaseAdminClient.
spanner.close();
}
}
await addAndDropNewDatabaseRole();
// [END spanner_add_and_drop_database_role]
}

main(...process.argv.slice(2));
75 changes: 75 additions & 0 deletions spanner/create-instance-without-default-backup-schedules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Copyright 2024 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';

function main(instanceId, projectId) {
async function createInstanceWithoutDefaultBackupSchedules() {
// [START spanner_create_instance_without_default_backup_schedule]
/**
* TODO(developer): Uncomment the following lines before running the sample.
**/
// const projectId = 'my-project-id';
// const instanceId = 'my-instance';

// Imports the Google Cloud client library
const {Spanner, protos} = require('@google-cloud/spanner');

// Creates a client
const spanner = new Spanner({
projectId: projectId,
});

const instanceAdminClient = spanner.getInstanceAdminClient();
// Creates a new instance
try {
const [operation] = await instanceAdminClient.createInstance({
instanceId: instanceId,
parent: instanceAdminClient.projectPath(projectId),
instance: {
config: instanceAdminClient.instanceConfigPath(
projectId,
'regional-me-central2'
),
nodeCount: 1,
displayName: 'Display name for the instance.',
labels: {
cloud_spanner_samples: 'true',
created: Math.round(Date.now() / 1000).toString(), // current time
},
defaultBackupScheduleType:
protos.google.spanner.admin.instance.v1.Instance
.DefaultBackupScheduleType.NONE,
},
});
await operation.promise();

console.log(
`Created instance ${instanceId} without default backup schedules.`
);
} catch (err) {
console.error(
'Error creating instance without default backup schedules:',
err
);
} finally {
spanner.close();
}
// [END spanner_create_instance_without_default_backup_schedule]
}
createInstanceWithoutDefaultBackupSchedules();
}

main(...process.argv.slice(2));
75 changes: 75 additions & 0 deletions spanner/get-database-roles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2024 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.

// sample-metadata:
// title: List database roles
// usage: node get-database-roles.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>

'use strict';

async function main(
instanceId = 'my-instance',
databaseId = 'my-database',
projectId = 'my-project-id'
) {
// [START spanner_list_database_roles]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const instanceId = 'my-instance';
// const databaseId = 'my-database';
// const projectId = 'my-project-id';

// Imports the Google Cloud client library
const {Spanner} = require('@google-cloud/spanner');

// creates a client
const spanner = new Spanner({
projectId: projectId,
});

const databaseAdminClient = spanner.getDatabaseAdminClient();

async function getDatabaseRoles() {
try {
const dbPath = databaseAdminClient.databasePath(
projectId,
instanceId,
databaseId
);

// Fetching database roles
const [databaseRoles] = await databaseAdminClient.listDatabaseRoles({
parent: dbPath,
});

console.log(`Roles for Database: ${dbPath}`);
databaseRoles.forEach(role => {
console.log(`Role: ${role.name}`);
});
} catch (err) {
console.error('Error listing database roles:', err);
} finally {
spanner.close();
}
}
await getDatabaseRoles();
// [END spanner_list_database_roles]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
87 changes: 87 additions & 0 deletions spanner/instance-update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* Copyright 2024 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.
*/

// sample-metadata:
// title: Updates an instance.
// usage: node instance-update.js <INSTANCE_ID> <PROJECT_ID>

'use strict';

function main(instanceId, projectId) {
async function updateInstance() {
// [START spanner_update_instance]

// Imports the Google Cloud client library
const {Spanner, protos} = require('@google-cloud/spanner');

/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'my-project-id';
// const instanceId = 'my-instance';

// Creates a client
const spanner = new Spanner({
projectId: projectId,
});

const instanceAdminClient = spanner.getInstanceAdminClient();

// Updates an instance
try {
const instancePath = instanceAdminClient.instancePath(
projectId,
instanceId
);

console.log(`Updating instance ${instancePath}.`);

const [operation] = await instanceAdminClient.updateInstance({
instance: {
name: instancePath,
labels: {
updated: 'true',
created: Math.round(Date.now() / 1000).toString(), // current time
},
edition:
protos.google.spanner.admin.instance.v1.Instance.Edition.ENTERPRISE, //optional
},
// Field mask specifying fields that should get updated in an Instance
fieldMask: {
paths: ['labels', 'edition'],
},
});

console.log(`Waiting for operation on ${instanceId} to complete...`);
await operation.promise();
console.log(`Updated instance ${instanceId}.`);
const [metadata] = await instanceAdminClient.getInstance({
name: instanceAdminClient.instancePath(projectId, instanceId),
});
console.log(
`Instance ${instanceId} has been updated with the ${metadata.edition} ` +
'edition.'
);
} catch (err) {
console.error('Error updating instance:', err);
} finally {
spanner.close();
}
// [END spanner_update_instance]
}
updateInstance();
}

main(...process.argv.slice(2));
Loading
Loading