Skip to content

Commit

Permalink
Added integration tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
hwin16 committed Sep 8, 2024
1 parent b4c192d commit 74c9e98
Show file tree
Hide file tree
Showing 5 changed files with 299 additions and 83 deletions.
105 changes: 105 additions & 0 deletions samples/backups-copy-with-multiple-kms-keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// 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: Copies a source backup
// usage: node spannerCopyBackup <INSTANCE_ID> <COPY_BACKUP_ID> <SOURCE_BACKUP_ID> <PROJECT_ID>

'use strict';

function main(
instanceId = 'my-instance',
backupId = 'my-backup',
sourceBackupPath = 'projects/my-project-id/instances/my-source-instance/backups/my-source-backup',
projectId = 'my-project-id',
kmsKeyNames = 'key1,key2'
) {
// [START spanner_copy_backup_with_MR_CMEK]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const instanceId = 'my-instance';
// const backupId = 'my-backup',
// const sourceBackupPath = 'projects/my-project-id/instances/my-source-instance/backups/my-source-backup',
// const projectId = 'my-project-id';
// const kmsKeyNames =
// 'projects/my-project-id/my-region/keyRings/my-key-ring/cryptoKeys/my-key1,
// projects/my-project-id/my-region/keyRings/my-key-ring/cryptoKeys/my-key2';

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

// Creates 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 spannerCopyBackupWithMultipleKmsKeys() {
// Expire copy backup 14 days in the future
const expireTime = Spanner.timestamp(
Date.now() + 1000 * 60 * 60 * 24 * 14
).toStruct();

// 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,
kmsKeyNames: kmsKeyNames.split(','),
});

console.log(
`Waiting for backup copy ${databaseAdminClient.backupPath(
projectId,
instanceId,
backupId
)} 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') {
console.log(
`Backup copy ${copyBackup.name} of size ` +
`${copyBackup.sizeBytes} bytes was created at ` +
`${new PreciseDate(copyBackup.createTime).toISOString()} ` +
'with version time ' +
`${new PreciseDate(copyBackup.versionTime).toISOString()}`
);
} else {
console.error('ERROR: Copy of backup is not ready.');
}
} catch (err) {
console.error('ERROR:', err);
}
}
spannerCopyBackupWithMultipleKmsKeys();
// [END spanner_copy_backup_with_MR_CMEK]
}
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
51 changes: 25 additions & 26 deletions samples/backups-create-with-multiple-kms-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,36 @@
'use strict';

function main(
projectId = 'my-project-id',
instanceId = 'my-instance',
databaseId = 'my-database',
backupId = 'my-backup',
kmsKeyNames = []
projectId = 'my-project-id',
kmsKeyNames = 'key1,key2'
) {
async function createBackupWithMultipleKmsKeys() {
// [START spanner_create_backup_with_MR_CMEK]
// Imports the Google Cloud client library
const {Spanner, protos} = require('@google-cloud/spanner');
const {PreciseDate} = require('@google-cloud/precise-date');
// [START spanner_create_backup_with_MR_CMEK]
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'my-project-id';
// const instanceId = 'my-instance';
// const databaseId = 'my-database';
// const backupId = 'my-backup';
// const kmsKeyNames =
// 'projects/my-project-id/my-region/keyRings/my-key-ring/cryptoKeys/my-key1,
// 'projects/my-project-id/my-region/keyRings/my-key-ring/cryptoKeys/my-key2';

/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'my-project-id';
// const instanceId = 'my-instance';
// const databaseId = 'my-database';
// const backupId = 'my-backup';
// const kmsKeyNames = [
// 'projects/my-project-id/my-region/keyRings/my-key-ring/cryptoKeys/my-key1',
// 'projects/my-project-id/my-region/keyRings/my-key-ring/cryptoKeys/my-key2'];
// Imports the Google Cloud client library
const {Spanner, protos} = require('@google-cloud/spanner');
const {PreciseDate} = require('@google-cloud/precise-date');

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

// Gets a reference to a Cloud Spanner Database Admin Client object
const databaseAdminClient = spanner.getDatabaseAdminClient();
// Creates 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 createBackupWithMultipleKmsKeys() {
// Creates a new backup of the database
try {
console.log(
Expand Down Expand Up @@ -75,7 +74,7 @@ function main(
}),
encryptionConfig: {
encryptionType: 'CUSTOMER_MANAGED_ENCRYPTION',
kmsKeyNames: kmsKeyNames,
kmsKeyNames: kmsKeyNames.split(','),
},
});

Expand Down Expand Up @@ -114,9 +113,9 @@ function main(
// The databaseAdminClient does not require explicit closure. The closure of the Spanner client will automatically close the databaseAdminClient.
spanner.close();
}
// [END spanner_create_backup_with_MR_CMEK]
}
createBackupWithMultipleKmsKeys();
// [END spanner_create_backup_with_MR_CMEK]
}

process.on('unhandledRejection', err => {
Expand Down
46 changes: 23 additions & 23 deletions samples/backups-restore-with-multiple-kms-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,32 @@ function main(
databaseId = 'my-database',
backupId = 'my-backup',
projectId = 'my-project',
kmsKeyNames = []
kmsKeyNames = 'key1,key2'
) {
async function restoreBackupWithMultipleKmsKeys() {
// [START spanner_restore_backup_with_MR_CMEK]
// Imports the Google Cloud client library and precise date library
const {Spanner} = require('@google-cloud/spanner');
// [START spanner_restore_backup_with_MR_CMEK]
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'my-project-id';
// const instanceId = 'my-instance';
// const databaseId = 'my-database';
// const backupId = 'my-backup';
// const kmsKeyNames =
// 'projects/my-project-id/my-region/keyRings/my-key-ring/cryptoKeys/my-key1,
// projects/my-project-id/my-region/keyRings/my-key-ring/cryptoKeys/my-key2';

/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'my-project-id';
// const instanceId = 'my-instance';
// const databaseId = 'my-database';
// const backupId = 'my-backup';
// const kmsKeyNames = [
// 'projects/my-project-id/my-region/keyRings/my-key-ring/cryptoKeys/my-key1',
// 'projects/my-project-id/my-region/keyRings/my-key-ring/cryptoKeys/my-key2'];
// Imports the Google Cloud client library and precise date library
const {Spanner} = require('@google-cloud/spanner');

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

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

async function restoreBackupWithMultipleKmsKeys() {
// Restore the database
console.log(
`Restoring database ${databaseAdminClient.databasePath(
Expand All @@ -60,7 +60,7 @@ function main(
backup: databaseAdminClient.backupPath(projectId, instanceId, backupId),
encryptionConfig: {
encryptionType: 'CUSTOMER_MANAGED_ENCRYPTION',
kmsKeyNames: kmsKeyNames,
kmsKeyNames: kmsKeyNames.split(','),
},
});

Expand All @@ -77,9 +77,9 @@ function main(
`to ${databaseId} from backup ${metadata.restoreInfo.backupInfo.backup} ` +
`using encryption key ${metadata.encryptionConfig.kmsKeyNames}.`
);
// [END spanner_restore_backup_with_MR_CMEK]
}
restoreBackupWithMultipleKmsKeys();
// [END spanner_restore_backup_with_MR_CMEK]
}

process.on('unhandledRejection', err => {
Expand Down
45 changes: 22 additions & 23 deletions samples/database-create-with-multiple-kms-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,37 @@ function main(
instanceId = 'my-instance',
databaseId = 'my-database',
projectId = 'my-project',
kmsKeyNames = []
kmsKeyNames = 'key1,key2,key3'
) {
async function createDatabaseWithMultipleKmsKeys() {
// [START spanner_create_database_with_MR_CMEK]

// Imports the Google Cloud client library
const {Spanner, protos} = require('@google-cloud/spanner');
// [START spanner_create_database_with_MR_CMEK]
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'my-project-id';
// const instanceId = 'my-instance';
// const databaseId = 'my-database';
// const kmsKeyNames =
// 'projects/my-project-id/my-region/keyRings/my-key-ring/cryptoKeys/my-key1,projects/my-project-id/my-region/keyRings/my-key-ring/cryptoKeys/my-key2';

/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'my-project-id';
// const instanceId = 'my-instance';
// const databaseId = 'my-database';
// const keyName =
// 'projects/my-project-id/my-region/keyRings/my-key-ring/cryptoKeys/my-key';
// Imports the Google Cloud client library
const {Spanner, protos} = require('@google-cloud/spanner');

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

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

async function createDatabaseWithMultipleKmsKeys() {
// Creates a database
const [operation] = await databaseAdminClient.createDatabase({
createStatement: 'CREATE DATABASE `' + databaseId + '`',
parent: databaseAdminClient.instancePath(projectId, instanceId),
encryptionConfig:
(protos.google.spanner.admin.database.v1.EncryptionConfig = {
kmsKeyNames: kmsKeyNames,
kmsKeyNames: kmsKeyNames.split(','),
}),
});

Expand All @@ -64,11 +63,11 @@ function main(
});

console.log(
`Database encrypted with key ${metadata.encryptionConfig.kmsKeyNames}.`
`Database encrypted with keys ${metadata.encryptionConfig.kmsKeyNames}.`
);
// [END spanner_create_database_with_MR_CMEK]
}
createDatabaseWithMultipleKmsKeys();
// [END spanner_create_database_with_MR_CMEK]
}

process.on('unhandledRejection', err => {
Expand Down
Loading

0 comments on commit 74c9e98

Please sign in to comment.