Skip to content

Commit

Permalink
docs: update all public documents to use auto-generated admin clients. (
Browse files Browse the repository at this point in the history
googleapis#2005)

* docs: swap existing and generated samples

* docs: swap existing and generated samples

* docs: swap existing and generated samples

* docs: swap existing and generated samples

* refactor: add missing samples

* fix: presubmit errors

* fix: presubmit errors

* refactor: integration test

* fix: presubmit errors

* fix: presubmit errors

* fix: header-checks

* fix: header-checks error

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* fix: presubmit error

* docs: update the readme

* docs: update the readme

* fix: presubmit error

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
alkatrivedi and gcf-owl-bot[bot] authored Mar 4, 2024
1 parent afce362 commit f12e295
Show file tree
Hide file tree
Showing 112 changed files with 6,163 additions and 4,717 deletions.
2 changes: 1 addition & 1 deletion samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/sa
__Usage:__


`node get-instance-config.js <PROJECT_ID>`
`node get-instance-config.js <PROJECT_ID> <INSTANCE_CONFIG_ID>`


-----
Expand Down
36 changes: 25 additions & 11 deletions samples/add-and-drop-new-database-role.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Google LLC
// 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.
Expand Down Expand Up @@ -30,19 +30,18 @@ function main(
// const instanceId = 'my-instance';
// const databaseId = 'my-database';
// const projectId = 'my-project-id';
// Imports the Google Cloud Spanner client library

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

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

async function addAndDropNewDatabaseRole() {
// Gets a reference to a Cloud Spanner instance and database.
const instance = spanner.instance(instanceId);
const database = instance.database(databaseId);
const databaseAdminClient = spanner.getDatabaseAdminClient();

async function addAndDropNewDatabaseRole() {
// Creates a new user defined role and grant permissions
try {
const request = [
Expand All @@ -51,7 +50,14 @@ function main(
'CREATE ROLE child',
'GRANT ROLE parent TO ROLE child',
];
const [operation] = await database.updateSchema(request);
const [operation] = await databaseAdminClient.updateDatabaseDdl({
database: databaseAdminClient.databasePath(
projectId,
instanceId,
databaseId
),
statements: request,
});

console.log('Waiting for operation to complete...');
await operation.promise();
Expand All @@ -65,7 +71,14 @@ function main(
// A role can't be dropped until all its permissions are revoked.
try {
const request = ['REVOKE ROLE parent FROM ROLE child', 'DROP ROLE child'];
const [operation] = await database.updateSchema(request);
const [operation] = await databaseAdminClient.updateDatabaseDdl({
database: databaseAdminClient.databasePath(
projectId,
instanceId,
databaseId
),
statements: request,
});

console.log('Waiting for operation to complete...');
await operation.promise();
Expand All @@ -74,8 +87,9 @@ function main(
} catch (err) {
console.error('ERROR:', err);
} finally {
// Close the database when finished.
await database.close();
// 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();
}
}
addAndDropNewDatabaseRole();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Google LLC
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,18 +30,19 @@ function main(
// const instanceId = 'my-instance';
// const databaseId = 'my-database';
// const projectId = 'my-project-id';

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

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

const databaseAdminClient = spanner.getDatabaseAdminClient();

async function addAndDropNewDatabaseRole() {
// Gets a reference to a Cloud Spanner instance and database.
const instance = spanner.instance(instanceId);
const database = instance.database(databaseId);

// Creates a new user defined role and grant permissions
try {
const request = [
Expand All @@ -50,14 +51,7 @@ function main(
'CREATE ROLE child',
'GRANT ROLE parent TO ROLE child',
];
const [operation] = await databaseAdminClient.updateDatabaseDdl({
database: databaseAdminClient.databasePath(
projectId,
instanceId,
databaseId
),
statements: request,
});
const [operation] = await database.updateSchema(request);

console.log('Waiting for operation to complete...');
await operation.promise();
Expand All @@ -71,14 +65,7 @@ function main(
// A role can't be dropped until all its permissions are revoked.
try {
const request = ['REVOKE ROLE parent FROM ROLE child', 'DROP ROLE child'];
const [operation] = await databaseAdminClient.updateDatabaseDdl({
database: databaseAdminClient.databasePath(
projectId,
instanceId,
databaseId
),
statements: request,
});
const [operation] = await database.updateSchema(request);

console.log('Waiting for operation to complete...');
await operation.promise();
Expand All @@ -87,9 +74,8 @@ function main(
} catch (err) {
console.error('ERROR:', 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();
// Close the database when finished.
await database.close();
}
}
addAndDropNewDatabaseRole();
Expand Down
48 changes: 17 additions & 31 deletions samples/v2/backups-cancel.js → samples/archived/backups-cancel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2024 Google LLC
* Copyright 2020 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
Expand All @@ -17,9 +17,9 @@

async function cancelBackup(instanceId, databaseId, backupId, projectId) {
// [START spanner_cancel_backup_create]

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

/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
Expand All @@ -33,33 +33,21 @@ async function cancelBackup(instanceId, databaseId, backupId, projectId) {
projectId: projectId,
});

// Gets a reference to a Cloud Spanner Database Admin Client object
const databaseAdminClient = spanner.getDatabaseAdminClient();
// Gets a reference to a Cloud Spanner instance and database
const instance = spanner.instance(instanceId);
const database = instance.database(databaseId);

const backup = instance.backup(backupId);

// Creates a new backup of the database
try {
console.log(
`Creating backup of database ${databaseAdminClient.databasePath(
projectId,
instanceId,
databaseId
)}.`
);

console.log(`Creating backup of database ${database.formattedName_}.`);
const databasePath = database.formattedName_;
// Expire backup one day in the future
const expireTime = Date.now() + 1000 * 60 * 60 * 24;
const [operation] = await databaseAdminClient.createBackup({
parent: databaseAdminClient.instancePath(projectId, instanceId),
backupId: backupId,
backup: (protos.google.spanner.admin.database.v1.Backup = {
database: databaseAdminClient.databasePath(
projectId,
instanceId,
databaseId
),
expireTime: Spanner.timestamp(expireTime).toStruct(),
name: databaseAdminClient.backupPath(projectId, instanceId, backupId),
}),
const [, operation] = await backup.create({
databasePath: databasePath,
expireTime: expireTime,
});

// Cancel the backup
Expand All @@ -70,12 +58,10 @@ async function cancelBackup(instanceId, databaseId, backupId, projectId) {
console.error('ERROR:', err);
} finally {
// Delete backup in case it got created before the cancel operation
await databaseAdminClient.deleteBackup({
name: databaseAdminClient.backupPath(projectId, instanceId, backupId),
});
// 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 backup.delete();

// Close the database when finished.
await database.close();
}
// [END spanner_cancel_backup_create]
}
Expand Down
47 changes: 22 additions & 25 deletions samples/v2/backups-copy.js → samples/archived/backups-copy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Google LLC
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,15 +37,15 @@ function main(
const {Spanner} = require('@google-cloud/spanner');
const {PreciseDate} = require('@google-cloud/precise-date');

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

// Gets a reference to a Cloud Spanner Database Admin Client object
const databaseAdminClient = spanner.getDatabaseAdminClient();

async function spannerCopyBackup() {
// Gets a reference to a Cloud Spanner instance and backup
const instance = spanner.instance(instanceId);

// Expire copy backup 14 days in the future
const expireTime = Spanner.timestamp(
Date.now() + 1000 * 60 * 60 * 24 * 14
Expand All @@ -54,34 +54,31 @@ function main(
// Copy the source backup
try {
console.log(`Creating copy of the source backup ${sourceBackupPath}.`);
const [operation] = await databaseAdminClient.copyBackup({
parent: databaseAdminClient.instancePath(projectId, instanceId),
sourceBackup: sourceBackupPath,
backupId: backupId,
expireTime: expireTime,
});
const [, operation] = await instance.copyBackup(
sourceBackupPath,
backupId,
{
expireTime: expireTime,
}
);

console.log(
`Waiting for backup copy ${databaseAdminClient.backupPath(
projectId,
instanceId,
backupId
)} to complete...`
`Waiting for backup copy ${
instance.backup(backupId).formattedName_
} to complete...`
);
await operation.promise();

// Verify the copy backup is ready
const [copyBackup] = await databaseAdminClient.getBackup({
name: databaseAdminClient.backupPath(projectId, instanceId, backupId),
});

if (copyBackup.state === 'READY') {
const copyBackup = instance.backup(backupId);
const [copyBackupInfo] = await copyBackup.getMetadata();
if (copyBackupInfo.state === 'READY') {
console.log(
`Backup copy ${copyBackup.name} of size ` +
`${copyBackup.sizeBytes} bytes was created at ` +
`${new PreciseDate(copyBackup.createTime).toISOString()} ` +
`Backup copy ${copyBackupInfo.name} of size ` +
`${copyBackupInfo.sizeBytes} bytes was created at ` +
`${new PreciseDate(copyBackupInfo.createTime).toISOString()} ` +
'with version time ' +
`${new PreciseDate(copyBackup.versionTime).toISOString()}`
`${new PreciseDate(copyBackupInfo.versionTime).toISOString()}`
);
} else {
console.error('ERROR: Copy of backup is not ready.');
Expand Down
Loading

0 comments on commit f12e295

Please sign in to comment.