Skip to content

Commit

Permalink
feat: updating samples
Browse files Browse the repository at this point in the history
  • Loading branch information
asthamohta committed Mar 22, 2022
1 parent 84544aa commit 2e489e3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
4 changes: 2 additions & 2 deletions samples/backups-copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function main(
Date.now() + 1000 * 60 * 60 * 24 * 14
).toStruct();

// Copy the backup of the backup
// Copy the source backup
try {
console.log(`Creating copy of the source backup ${sourceBackupPath}.`);
const [, operation] = await instance.copyBackup(
Expand All @@ -77,7 +77,7 @@ function main(
`Backup copy ${copyBackupInfo.name} of size ` +
`${copyBackupInfo.sizeBytes} bytes was created at ` +
`${new PreciseDate(copyBackupInfo.createTime).toISOString()} ` +
'with version time' +
'with version time ' +
`${new PreciseDate(copyBackupInfo.versionTime).toISOString()}`
);
} else {
Expand Down
36 changes: 34 additions & 2 deletions samples/backups-get-operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@

'use strict';

async function getBackupOperations(instanceId, databaseId, projectId) {
async function getBackupOperations(
instanceId,
databaseId,
backupId,
projectId
) {
// [START spanner_list_backup_operations]
// Imports the Google Cloud client library
const {Spanner, protos} = require('@google-cloud/spanner');
Expand All @@ -25,6 +30,7 @@ async function getBackupOperations(instanceId, databaseId, projectId) {
*/
// const projectId = 'my-project-id';
// const databaseId = 'my-database';
// const backupId = 'my-backup';
// const instanceId = 'my-instance';

// Creates a client
Expand All @@ -35,7 +41,7 @@ async function getBackupOperations(instanceId, databaseId, projectId) {
// Gets a reference to a Cloud Spanner instance
const instance = spanner.instance(instanceId);

// List backup operations
// List create backup operations
try {
const [backupOperations] = await instance.getBackupOperations({
filter:
Expand All @@ -56,6 +62,32 @@ async function getBackupOperations(instanceId, databaseId, projectId) {
} catch (err) {
console.error('ERROR:', err);
}

// List copy backup operations
try {
console.log(
'(metadata.@type:type.googleapis.com/google.spanner.admin.database.v1.CopyBackupMetadata) ' +
`AND (metadata.source_backup:${backupId})`
);
const [backupOperations] = await instance.getBackupOperations({
filter:
'(metadata.@type:type.googleapis.com/google.spanner.admin.database.v1.CopyBackupMetadata) ' +
`AND (metadata.source_backup:${backupId})`,
});
console.log('Copy Backup Operations:');
backupOperations.forEach(backupOperation => {
const metadata =
protos.google.spanner.admin.database.v1.CopyBackupMetadata.decode(
backupOperation.metadata.value
);
console.log(
`Backup ${metadata.name} copied from source backup ${metadata.sourceBackup} is ` +
`${metadata.progress.progressPercent}% complete.`
);
});
} catch (err) {
console.error('ERROR:', err);
}
// [END spanner_list_backup_operations]
}

Expand Down
9 changes: 7 additions & 2 deletions samples/backups.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,16 @@ require('yargs')
)
)
.command(
'getBackupOperations <instanceName> <databaseName> <projectId>',
'getBackupOperations <instanceName> <databaseName> <backupName> <projectId>',
'Lists all backup operations in the instance.',
{},
opts =>
getBackupOperations(opts.instanceName, opts.databaseName, opts.projectId)
getBackupOperations(
opts.instanceName,
opts.databaseName,
opts.backupName,
opts.projectId
)
)
.command(
'getDatabaseOperations <instanceName> <projectId>',
Expand Down
8 changes: 7 additions & 1 deletion samples/system-test/spanner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1063,13 +1063,19 @@ describe('Spanner', () => {
// list_backup_operations
it('should list backup operations in the instance', async () => {
const output = execSync(
`${backupsCmd} getBackupOperations ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`
`${backupsCmd} getBackupOperations ${INSTANCE_ID} ${DATABASE_ID} ${BACKUP_ID} ${PROJECT_ID}`
);
assert.match(output, /Create Backup Operations:/);
assert.match(
output,
new RegExp(`Backup (.+)${BACKUP_ID} (.+) is 100% complete`)
);
assert.match(output, /Copy Backup Operations:/);
assert.match(
output,
new RegExp(`Backup (.+)${COPY_BACKUP_ID} (.+) is 100% complete`)
);
console.log(output);
});

// update_backup_expire_time
Expand Down

0 comments on commit 2e489e3

Please sign in to comment.