Skip to content

Commit

Permalink
feat: changes as per comments
Browse files Browse the repository at this point in the history
  • Loading branch information
asthamohta committed Feb 1, 2022
1 parent a747b7f commit 568d62b
Show file tree
Hide file tree
Showing 10 changed files with 3,965 additions and 2,777 deletions.
918 changes: 628 additions & 290 deletions protos/protos.d.ts

Large diffs are not rendered by default.

5,253 changes: 3,012 additions & 2,241 deletions protos/protos.js

Large diffs are not rendered by default.

363 changes: 238 additions & 125 deletions protos/protos.json

Large diffs are not rendered by default.

37 changes: 19 additions & 18 deletions samples/backups-copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,23 @@

// sample-metadata:
// title: Copies a source backup
// usage: node spannerCopyBackup <INSTANCE_ID> <DATABASE_ID> <COPY_BACKUP_ID> <SOURCE_BACKUP_ID> <PROJECT_ID>
// usage: node spannerCopyBackup <INSTANCE_ID> <COPY_BACKUP_ID> <SOURCE_BACKUP_ID> <PROJECT_ID>

'use strict';

function main(
instanceId = 'my-instance',
databaseId = 'my-database',
backupId = 'my-backup',
sourceBackupId = 'my-source-backup',
sourceBackupPath = 'projects/my-project-id/instances/my-source-instance/backups/my-source-backup',
projectId = 'my-project-id'
) {
// [START spanner_copy_backup]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const instanceId = 'my-instance';
// const databaseId = 'my-database';
// const backupId = 'my-backup',
// const sourceBackupId = 'my-source-backup',
// const sourceBackupPath = 'projects/my-project-id/instances/my-source-instance/backups/my-source-backup',
// const projectId = 'my-project-id';

// Imports the Google Cloud Spanner client library
Expand All @@ -45,31 +43,29 @@ function main(
});

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

// Expire copy backup 14 days in the future
const expireTime = Spanner.timestamp(
Date.now() + 1000 * 60 * 60 * 24 * 14
).toStruct();

// Copy the backup of the database
// Copy the backup of the backup
try {
console.log(
`Creating copy of the source backup ${sourceBackup.formattedName_}.`
);
console.log(`Creating copy of the source backup ${sourceBackupPath}.`);
const [, operation] = await instance.copyBackup(
sourceBackup.formattedName_,
sourceBackupPath,
backupId,
{
expireTime: expireTime,
}
);

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

Expand All @@ -80,16 +76,15 @@ function main(
console.log(
`Backup copy ${copyBackupInfo.name} of size ` +
`${copyBackupInfo.sizeBytes} bytes was created at ` +
`${new PreciseDate(copyBackupInfo.createTime).toISOString()}`
`${new PreciseDate(copyBackupInfo.createTime).toISOString()} ` +
'with version time' +
`${new PreciseDate(copyBackupInfo.versionTime).toISOString()}`
);
} else {
console.error('ERROR: Copy of backup is not ready.');
}
} catch (err) {
console.error('ERROR:', err);
} finally {
// Close the database when finished.
await database.close();
}
}
spannerCopyBackup();
Expand All @@ -100,3 +95,9 @@ process.on('unhandledRejection', err => {
process.exitCode = 1;
});
main(...process.argv.slice(2));
// main(
// 'test-cross-region',
// 'backup007',
// 'projects/appdev-soda-spanner-staging/instances/test-cross-region/backups/backup123',
// 'appdev-soda-spanner-staging'
// );
2 changes: 1 addition & 1 deletion samples/backups-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function updateBackup(instanceId, backupId, projectId) {
// Read backup metadata and update expiry time
try {
const currentExpireTime = await backup.getExpireTime();
const maxExpireTime = await backup.getMaxExpireTime();
const maxExpireTime = backup.metadata.maxExpireTime;
const wantExpireTime = new PreciseDate(currentExpireTime);
wantExpireTime.setDate(wantExpireTime.getDate() + 1);
// New expire time should be less than the max expire time
Expand Down
4 changes: 4 additions & 0 deletions samples/index-read-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// sample-metadata:
// title: Read data using an existing index.
// usage: node readDataWithIndex <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>

'use strict';

function main(
Expand Down
3 changes: 2 additions & 1 deletion samples/system-test/spanner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1026,8 +1026,9 @@ describe('Spanner', () => {

// copy_backup
it('should create a copy of a backup', async () => {
const sourceBackupPath = `projects/${PROJECT_ID}/instances/${INSTANCE_ID}/backups/${BACKUP_ID}`;
const output = execSync(
`node backups-copy.js ${INSTANCE_ID} ${DATABASE_ID} ${COPY_BACKUP_ID} ${BACKUP_ID} ${PROJECT_ID}`
`node backups-copy.js ${INSTANCE_ID} ${COPY_BACKUP_ID} ${sourceBackupPath} ${PROJECT_ID}`
);
assert.match(
output,
Expand Down
101 changes: 11 additions & 90 deletions src/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export interface CreateBackupOptions {
}

export interface CopyBackupGaxOperation extends GaxOperation {
// Overridden with more specific type for CreateBackup operation
// Overridden with more specific type for CopyBackup operation
metadata: Metadata &
databaseAdmin.spanner.admin.database.v1.ICopyBackupMetadata;
}
Expand All @@ -70,6 +70,7 @@ export interface CopyBackupOptions
gaxOptions?: CallOptions;
}


/**
* IBackup structure with backup state enum translated to string form.
*/
Expand Down Expand Up @@ -98,7 +99,6 @@ export type GetStateCallback = NormalCallback<
EnumKey<typeof databaseAdmin.spanner.admin.database.v1.Backup.State>
>;
export type GetExpireTimeCallback = NormalCallback<PreciseDate>;
export type GetMaxExpireTimeCallback = NormalCallback<PreciseDate>;
export type ExistsCallback = NormalCallback<boolean>;
/**
* The {@link Backup} class represents a Cloud Spanner backup.
Expand All @@ -113,6 +113,14 @@ export type ExistsCallback = NormalCallback<boolean>;
* const spanner = new Spanner();
* const instance = spanner.instance('my-instance');
* const backup = instance.backup('my-backup');
* ```
*
* ```
* * @example
* const {Spanner} = require('@google-cloud/spanner');
* const spanner = new Spanner();
* const instance = spanner.instance('my-instance');
* const sourceBackup = instance.backup('my-source-backup');
* const copyBackup = instance.copyBackup('my-copy-backup', 'my-source-backup');
* ```
*/
Expand All @@ -135,7 +143,6 @@ class Backup {
};
}


/**
* @typedef {object} CreateBackupOptions
* @property {string} databasePath The database path.
Expand All @@ -151,34 +158,13 @@ class Backup {
* See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions}
* for more details.
*/
/**
* @typedef {object} CopyBackupOptions
* @property {string|null}
* sourceBackup The full path of the backup to be copied
* @property {string|number|google.protobuf.Timestamp|external:PreciseDate}
* expireTime The expire time of the backup.
* @property {google.spanner.admin.database.v1.ICopyBackupEncryptionConfig}
* encryptionConfig An encryption configuration describing the
* encryption type and key resources in Cloud KMS to be used to encrypt
* the copy backup.
* @property {object} [gaxOptions] The request configuration options,
* See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions}
* for more details.
*/
/**
* @typedef {array} CreateBackupResponse
* @property {Backup} 0 The new {@link Backup}.
* @property {google.longrunning.Operation} 1 An {@link Operation} object that can be used to check
* the status of the request.
* @property {object} 2 The full API response.
*/
/**
* @typedef {array} CopyBackupResponse
* @property {Backup} 0 The new {@link Backup}.
* @property {google.longrunning.Operation} 1 An {@link Operation} object that can be used to check
* the status of the request.
* @property {object} 2 The full API response.
*/
/**
* @callback CreateBackupCallback
* @param {?Error} err Request error, if any.
Expand All @@ -187,14 +173,6 @@ class Backup {
* check the status of the request.
* @param {object} apiResponse The full API response.
*/
/**
* @callback CopyBackupCallback
* @param {?Error} err Request error, if any.
* @param {Backup} backup The new {@link Backup}.
* @param {google.longrunning.Operation} operation An {@link Operation} object that can be used to
* check the status of the request.
* @param {object} apiResponse The full API response.
*/
/**
* Create a backup.
*
Expand Down Expand Up @@ -229,35 +207,6 @@ class Backup {
* await backupOperation.promise();
* ```
*/
/**
* Copy a backup.
*
* @method Backup#create
* @param {CopyBackupOptions} options Parameters for copying a backup.
* @param {CallOptions} [options.gaxOptions] The request configuration
* options, See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions}
* for more details.
* @param {CopyBackupCallback} [callback] Callback function.
* @returns {Promise<CopyBackupResponse>} When resolved, the copy backup
* operation will have started, but will not have necessarily completed.
*
* @example
* const {Spanner} = require('@google-cloud/spanner');
* const spanner = new Spanner();
* const instance = spanner.instance('my-instance');
* const oneDay = 1000 * 60 * 60 * 24;
* const expireTime = Date.now() + oneDay;
* const versionTime = Date.now() - oneDay;
* const sourceBackup = instance.backup('my-source-backup');
* const [, copyBackupOperation] = instance.copyBackup(sourceBackup.formattedName_, 'my-copy-backup', {
* expireTime: expireTime,
* encryptionConfig: {
* encryptionType: 'CUSTOMER_MANAGED_ENCRYPTION',
* kmsKeyName: 'projects/my-project-id/my-region/keyRings/my-key-ring/cryptoKeys/my-key',
* },);
* // Await completion of the copy backup operation.
* await copybBackupOperation.promise();
*/
create(
options: CreateBackupOptions | CopyBackupOptions
): Promise<CreateBackupResponse> | Promise<CopyBackupResponse>;
Expand Down Expand Up @@ -351,7 +300,6 @@ class Backup {
*
* @see {@link #getState}
* @see {@link #getExpireTime}
* @see {@link #getMaxExpireTime}
*
* @method Backup#getMetadata
* @param {object} [gaxOptions] Request configuration options,
Expand Down Expand Up @@ -469,33 +417,6 @@ class Backup {
return new PreciseDate(backupInfo.expireTime as DateStruct);
}

getMaxExpireTime(): Promise<PreciseDate | undefined>;
getMaxExpireTime(callback: GetExpireTimeCallback): void;
/**
* Retrieves the max expiry time of the backup.
*
* @see {@link #updateExpireTime}
* @see {@link #getMetadata}
*
* @method Backup#getMaxExpireTime
* @returns {Promise<external:PreciseDate>} When resolved, contains the
* max expire time of the backup if it exists.
*
* @example
* const {Spanner} = require('@google-cloud/spanner');
* const spanner = new Spanner();
* const instance = spanner.instance('my-instance');
* const backup = instance.backup('my-backup');
* const maxExpireTime = await backup.getMaxExpireTime();
* console.log(`Backup max expires on ${maxExpireTime.toISOString()}`);
*/
async getMaxExpireTime(): Promise<PreciseDate | undefined> {
const [backupInfo] = await this.getMetadata();
return new PreciseDate(backupInfo.maxExpireTime as DateStruct);
}

exists(): Promise<boolean>;
exists(callback: ExistsCallback): void;
/**
* Checks whether the backup exists.
*
Expand Down Expand Up @@ -702,7 +623,7 @@ class Backup {
* that a callback is omitted.
*/
promisifyAll(Backup, {
exclude: ['getState', 'getExpireTime', 'getMaxExpireTime', 'exists'],
exclude: ['getState', 'getExpireTime', 'exists'],
});

/*! Developer Documentation
Expand Down
Loading

0 comments on commit 568d62b

Please sign in to comment.