Skip to content
This repository was archived by the owner on Mar 4, 2026. It is now read-only.

Commit f12e295

Browse files
docs: update all public documents to use auto-generated admin clients. (#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>
1 parent afce362 commit f12e295

112 files changed

Lines changed: 6163 additions & 4717 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

samples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/sa
663663
__Usage:__
664664

665665

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

668668

669669
-----

samples/add-and-drop-new-database-role.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2022 Google LLC
1+
// Copyright 2024 Google LLC
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -30,19 +30,18 @@ function main(
3030
// const instanceId = 'my-instance';
3131
// const databaseId = 'my-database';
3232
// const projectId = 'my-project-id';
33-
// Imports the Google Cloud Spanner client library
33+
34+
// Imports the Google Cloud client library
3435
const {Spanner} = require('@google-cloud/spanner');
3536

36-
// Instantiates a client
37+
// creates a client
3738
const spanner = new Spanner({
3839
projectId: projectId,
3940
});
4041

41-
async function addAndDropNewDatabaseRole() {
42-
// Gets a reference to a Cloud Spanner instance and database.
43-
const instance = spanner.instance(instanceId);
44-
const database = instance.database(databaseId);
42+
const databaseAdminClient = spanner.getDatabaseAdminClient();
4543

44+
async function addAndDropNewDatabaseRole() {
4645
// Creates a new user defined role and grant permissions
4746
try {
4847
const request = [
@@ -51,7 +50,14 @@ function main(
5150
'CREATE ROLE child',
5251
'GRANT ROLE parent TO ROLE child',
5352
];
54-
const [operation] = await database.updateSchema(request);
53+
const [operation] = await databaseAdminClient.updateDatabaseDdl({
54+
database: databaseAdminClient.databasePath(
55+
projectId,
56+
instanceId,
57+
databaseId
58+
),
59+
statements: request,
60+
});
5561

5662
console.log('Waiting for operation to complete...');
5763
await operation.promise();
@@ -65,7 +71,14 @@ function main(
6571
// A role can't be dropped until all its permissions are revoked.
6672
try {
6773
const request = ['REVOKE ROLE parent FROM ROLE child', 'DROP ROLE child'];
68-
const [operation] = await database.updateSchema(request);
74+
const [operation] = await databaseAdminClient.updateDatabaseDdl({
75+
database: databaseAdminClient.databasePath(
76+
projectId,
77+
instanceId,
78+
databaseId
79+
),
80+
statements: request,
81+
});
6982

7083
console.log('Waiting for operation to complete...');
7184
await operation.promise();
@@ -74,8 +87,9 @@ function main(
7487
} catch (err) {
7588
console.error('ERROR:', err);
7689
} finally {
77-
// Close the database when finished.
78-
await database.close();
90+
// Close the spanner client when finished.
91+
// The databaseAdminClient does not require explicit closure. The closure of the Spanner client will automatically close the databaseAdminClient.
92+
spanner.close();
7993
}
8094
}
8195
addAndDropNewDatabaseRole();

samples/v2/add-and-drop-new-database-role.js renamed to samples/archived/add-and-drop-new-database-role.js

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 Google LLC
1+
// Copyright 2022 Google LLC
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -30,18 +30,19 @@ function main(
3030
// const instanceId = 'my-instance';
3131
// const databaseId = 'my-database';
3232
// const projectId = 'my-project-id';
33-
34-
// Imports the Google Cloud client library
33+
// Imports the Google Cloud Spanner client library
3534
const {Spanner} = require('@google-cloud/spanner');
3635

37-
// creates a client
36+
// Instantiates a client
3837
const spanner = new Spanner({
3938
projectId: projectId,
4039
});
4140

42-
const databaseAdminClient = spanner.getDatabaseAdminClient();
43-
4441
async function addAndDropNewDatabaseRole() {
42+
// Gets a reference to a Cloud Spanner instance and database.
43+
const instance = spanner.instance(instanceId);
44+
const database = instance.database(databaseId);
45+
4546
// Creates a new user defined role and grant permissions
4647
try {
4748
const request = [
@@ -50,14 +51,7 @@ function main(
5051
'CREATE ROLE child',
5152
'GRANT ROLE parent TO ROLE child',
5253
];
53-
const [operation] = await databaseAdminClient.updateDatabaseDdl({
54-
database: databaseAdminClient.databasePath(
55-
projectId,
56-
instanceId,
57-
databaseId
58-
),
59-
statements: request,
60-
});
54+
const [operation] = await database.updateSchema(request);
6155

6256
console.log('Waiting for operation to complete...');
6357
await operation.promise();
@@ -71,14 +65,7 @@ function main(
7165
// A role can't be dropped until all its permissions are revoked.
7266
try {
7367
const request = ['REVOKE ROLE parent FROM ROLE child', 'DROP ROLE child'];
74-
const [operation] = await databaseAdminClient.updateDatabaseDdl({
75-
database: databaseAdminClient.databasePath(
76-
projectId,
77-
instanceId,
78-
databaseId
79-
),
80-
statements: request,
81-
});
68+
const [operation] = await database.updateSchema(request);
8269

8370
console.log('Waiting for operation to complete...');
8471
await operation.promise();
@@ -87,9 +74,8 @@ function main(
8774
} catch (err) {
8875
console.error('ERROR:', err);
8976
} finally {
90-
// Close the spanner client when finished.
91-
// The databaseAdminClient does not require explicit closure. The closure of the Spanner client will automatically close the databaseAdminClient.
92-
spanner.close();
77+
// Close the database when finished.
78+
await database.close();
9379
}
9480
}
9581
addAndDropNewDatabaseRole();
Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2024 Google LLC
2+
* Copyright 2020 Google LLC
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
55
* You may obtain a copy of the License at
@@ -17,9 +17,9 @@
1717

1818
async function cancelBackup(instanceId, databaseId, backupId, projectId) {
1919
// [START spanner_cancel_backup_create]
20-
2120
// Imports the Google Cloud client library and precise date library
22-
const {Spanner, protos} = require('@google-cloud/spanner');
21+
const {Spanner} = require('@google-cloud/spanner');
22+
2323
/**
2424
* TODO(developer): Uncomment the following lines before running the sample.
2525
*/
@@ -33,33 +33,21 @@ async function cancelBackup(instanceId, databaseId, backupId, projectId) {
3333
projectId: projectId,
3434
});
3535

36-
// Gets a reference to a Cloud Spanner Database Admin Client object
37-
const databaseAdminClient = spanner.getDatabaseAdminClient();
36+
// Gets a reference to a Cloud Spanner instance and database
37+
const instance = spanner.instance(instanceId);
38+
const database = instance.database(databaseId);
39+
40+
const backup = instance.backup(backupId);
3841

3942
// Creates a new backup of the database
4043
try {
41-
console.log(
42-
`Creating backup of database ${databaseAdminClient.databasePath(
43-
projectId,
44-
instanceId,
45-
databaseId
46-
)}.`
47-
);
48-
44+
console.log(`Creating backup of database ${database.formattedName_}.`);
45+
const databasePath = database.formattedName_;
4946
// Expire backup one day in the future
5047
const expireTime = Date.now() + 1000 * 60 * 60 * 24;
51-
const [operation] = await databaseAdminClient.createBackup({
52-
parent: databaseAdminClient.instancePath(projectId, instanceId),
53-
backupId: backupId,
54-
backup: (protos.google.spanner.admin.database.v1.Backup = {
55-
database: databaseAdminClient.databasePath(
56-
projectId,
57-
instanceId,
58-
databaseId
59-
),
60-
expireTime: Spanner.timestamp(expireTime).toStruct(),
61-
name: databaseAdminClient.backupPath(projectId, instanceId, backupId),
62-
}),
48+
const [, operation] = await backup.create({
49+
databasePath: databasePath,
50+
expireTime: expireTime,
6351
});
6452

6553
// Cancel the backup
@@ -70,12 +58,10 @@ async function cancelBackup(instanceId, databaseId, backupId, projectId) {
7058
console.error('ERROR:', err);
7159
} finally {
7260
// Delete backup in case it got created before the cancel operation
73-
await databaseAdminClient.deleteBackup({
74-
name: databaseAdminClient.backupPath(projectId, instanceId, backupId),
75-
});
76-
// Close the spanner client when finished.
77-
// The databaseAdminClient does not require explicit closure. The closure of the Spanner client will automatically close the databaseAdminClient.
78-
spanner.close();
61+
await backup.delete();
62+
63+
// Close the database when finished.
64+
await database.close();
7965
}
8066
// [END spanner_cancel_backup_create]
8167
}
Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 Google LLC
1+
// Copyright 2022 Google LLC
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -37,15 +37,15 @@ function main(
3737
const {Spanner} = require('@google-cloud/spanner');
3838
const {PreciseDate} = require('@google-cloud/precise-date');
3939

40-
// Creates a client
40+
// Instantiates a client
4141
const spanner = new Spanner({
4242
projectId: projectId,
4343
});
4444

45-
// Gets a reference to a Cloud Spanner Database Admin Client object
46-
const databaseAdminClient = spanner.getDatabaseAdminClient();
47-
4845
async function spannerCopyBackup() {
46+
// Gets a reference to a Cloud Spanner instance and backup
47+
const instance = spanner.instance(instanceId);
48+
4949
// Expire copy backup 14 days in the future
5050
const expireTime = Spanner.timestamp(
5151
Date.now() + 1000 * 60 * 60 * 24 * 14
@@ -54,34 +54,31 @@ function main(
5454
// Copy the source backup
5555
try {
5656
console.log(`Creating copy of the source backup ${sourceBackupPath}.`);
57-
const [operation] = await databaseAdminClient.copyBackup({
58-
parent: databaseAdminClient.instancePath(projectId, instanceId),
59-
sourceBackup: sourceBackupPath,
60-
backupId: backupId,
61-
expireTime: expireTime,
62-
});
57+
const [, operation] = await instance.copyBackup(
58+
sourceBackupPath,
59+
backupId,
60+
{
61+
expireTime: expireTime,
62+
}
63+
);
6364

6465
console.log(
65-
`Waiting for backup copy ${databaseAdminClient.backupPath(
66-
projectId,
67-
instanceId,
68-
backupId
69-
)} to complete...`
66+
`Waiting for backup copy ${
67+
instance.backup(backupId).formattedName_
68+
} to complete...`
7069
);
7170
await operation.promise();
7271

7372
// Verify the copy backup is ready
74-
const [copyBackup] = await databaseAdminClient.getBackup({
75-
name: databaseAdminClient.backupPath(projectId, instanceId, backupId),
76-
});
77-
78-
if (copyBackup.state === 'READY') {
73+
const copyBackup = instance.backup(backupId);
74+
const [copyBackupInfo] = await copyBackup.getMetadata();
75+
if (copyBackupInfo.state === 'READY') {
7976
console.log(
80-
`Backup copy ${copyBackup.name} of size ` +
81-
`${copyBackup.sizeBytes} bytes was created at ` +
82-
`${new PreciseDate(copyBackup.createTime).toISOString()} ` +
77+
`Backup copy ${copyBackupInfo.name} of size ` +
78+
`${copyBackupInfo.sizeBytes} bytes was created at ` +
79+
`${new PreciseDate(copyBackupInfo.createTime).toISOString()} ` +
8380
'with version time ' +
84-
`${new PreciseDate(copyBackup.versionTime).toISOString()}`
81+
`${new PreciseDate(copyBackupInfo.versionTime).toISOString()}`
8582
);
8683
} else {
8784
console.error('ERROR: Copy of backup is not ready.');

0 commit comments

Comments
 (0)