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

Commit bb389dc

Browse files
docs: Migrate the remainder of the samples to use the admin client (#1717)
* Export the GCRuleMaker class * Use the admin client for the first sample * feat: updated samples to use GCRuleMaker * feat: updated samples to use GCRuleMaker * feat: Update restoreTable sample to use generated client This commit updates the `backups.restore.js` sample to use the `BigtableTableAdminClient` directly for restoring a table from a backup, instead of going through the handwritten `bigtable.instance().createTableFromBackup()` method. This change is based on the design document for the Node.js Bigtable Admin API autogeneration. The corresponding test for the sample in `samples/test/backups.js` has also been updated to pass the correct arguments to the updated sample script. * table id to table name * The rule should be a union - not nested instance of the admin client should be used * Use instance of admin client - not admin client itself * Update the rules so they won’t throw errors * change family.id to family.name * max age has to be at least 1 millisecond * Run linter * Refresh project id token * Get rid of projectId token * revert change to backups method called * Use the admin client for createTable instead * snippets should include admin client * This code snippet should include project fetch * Comment out options * Use the admin client to create a table * Use the generated layer for maxVersions * Don’t use instance.name in parent path * Update the family snippets * Update createTable in the Row samples * Eliminate the need for any default projectId fetch * Don’t hardcode the project id * don’t hardcode project id * commit examples * Don’t create table twice * options is not used. remove it * table admin * Hardcode instance name * delete the examples folder * remove options * add header * The rest of the samples should use BigtableTableAdminClient instread * Update more tables code * commit the api-reference-doc-snippets * correct the backups samples * Revert "correct the backups samples" This reverts commit 7241dbe. * Modify the reads samples * Update the functions script * Make separate column family creation calls This makes the test pass * Fix the deletes sample changes * change the fixture don’t wrap the return value in brackets * return value should be false literal * Correct the fixture for the deletes samples * Reintroduce the backup samples * Replace write.js code with calls to generated layr * removed unused table * backups from instance as well * Fix the index.js sample * Fixed a bunch of syntax highlighting errors * run the linter * Changes to pass the linting check * Eliminate unused cluster * Add comment for restore table LRO * Add a script for the consistency token * Use instance admin client and eliminate unused variables * Make a separate modifyColumnFamilies call * Use instance admin client for the clusters samples * use admin client in family.js * Use the instance admin client for creating instanc * reintroduce necessary variables * Fix the runtime issues from the before hook * Fix bugs due to switch to admin layer * Use the instance admin client in instance.js * changes to instances.test.js file * Get rid of calls to locationPath * define instancePath directly * instancePath should be fixed string * operation is actually first argument * Use instance admin client in row.js * Run linter and fix table.js # Conflicts: # samples/test/cluster.js # samples/test/family.js # samples/test/instance.js # samples/test/instances.test.js * Fix some of the linting errors # Conflicts: # samples/test/util.js * Use instanceAdminClient in instances.js * Update the appProfile instance admin samples * Revert "Update the appProfile instance admin samples" This reverts commit ada69c8. * Must iterate through subobject * Revert "Revert "Update the appProfile instance admin samples"" This reverts commit c71b50c. * Make updates to the cluster.js file * instance.js use the instanceAdminClient * refactor: update instance.js to use generated client * replace project path * Update the location paths * Fix the backup id paths * Don’t rely on admin client for path building * replace all instance paths * Use the instance id path * Replace table path builders * Replace appProfileId path * Fix the linting errors * fix linting issues --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent b28b638 commit bb389dc

29 files changed

Lines changed: 783 additions & 578 deletions

samples/api-reference-doc-snippets/app-profile.js

Lines changed: 80 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,25 @@
1313
// limitations under the License.
1414

1515
const snippets = {
16-
create: (instanceId, appProfileId) => {
16+
create: async (instanceId, appProfileId) => {
1717
// [START bigtable_api_create_app_profile]
18-
const {Bigtable} = require('@google-cloud/bigtable');
19-
const bigtable = new Bigtable();
20-
const instance = bigtable.instance(instanceId);
21-
const appProfile = instance.appProfile(appProfileId);
22-
// set routing policy, required for creating an app-profile
23-
const options = {
24-
routing: 'any',
18+
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
19+
const instanceAdminClient = new BigtableInstanceAdminClient();
20+
const projectId = await instanceAdminClient.getProjectId();
21+
22+
const appProfile = {
23+
name: `projects/${projectId}/instances/${instanceId}/appProfiles/${appProfileId}`,
24+
multiClusterRoutingUseAny: {},
25+
};
26+
27+
const request = {
28+
parent: `projects/${projectId}/instances/${instanceId}`,
29+
appProfileId: appProfileId,
30+
appProfile: appProfile,
2531
};
2632

27-
appProfile
28-
.create(options)
33+
instanceAdminClient
34+
.createAppProfile(request)
2935
.then(result => {
3036
const appProfile = result[0];
3137
const apiResponse = result[1];
@@ -36,15 +42,18 @@ const snippets = {
3642
// [END bigtable_api_create_app_profile]
3743
},
3844

39-
delete: (instanceId, appProfileId) => {
45+
delete: async (instanceId, appProfileId) => {
4046
// [START bigtable_api_delete_app_profile]
41-
const {Bigtable} = require('@google-cloud/bigtable');
42-
const bigtable = new Bigtable();
43-
const instance = bigtable.instance(instanceId);
44-
const appProfile = instance.appProfile(appProfileId);
47+
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
48+
const instanceAdminClient = new BigtableInstanceAdminClient();
49+
const projectId = await instanceAdminClient.getProjectId();
50+
51+
const request = {
52+
name: `projects/${projectId}/instances/${instanceId}/appProfiles/${appProfileId}`,
53+
};
4554

46-
appProfile
47-
.delete()
55+
instanceAdminClient
56+
.deleteAppProfile(request)
4857
.then(result => {
4958
const apiResponse = result[0];
5059
})
@@ -54,33 +63,42 @@ const snippets = {
5463
// [END bigtable_api_delete_app_profile]
5564
},
5665

57-
exists: (instanceId, appProfileId) => {
66+
exists: async (instanceId, appProfileId) => {
5867
// [START bigtable_api_exists_app_profile]
59-
const {Bigtable} = require('@google-cloud/bigtable');
60-
const bigtable = new Bigtable();
61-
const instance = bigtable.instance(instanceId);
62-
const appProfile = instance.appProfile(appProfileId);
68+
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
69+
const instanceAdminClient = new BigtableInstanceAdminClient();
70+
const projectId = await instanceAdminClient.getProjectId();
6371

64-
appProfile
65-
.exists()
66-
.then(result => {
67-
const exists = result[0];
68-
})
69-
.catch(err => {
72+
const request = {
73+
name: `projects/${projectId}/instances/${instanceId}/appProfiles/${appProfileId}`,
74+
};
75+
76+
try {
77+
await instanceAdminClient.getAppProfile(request);
78+
console.log('App profile exists.');
79+
} catch (err) {
80+
if (err.code === 5) {
81+
console.log('App profile does not exist.');
82+
} else {
7083
// Handle the error.
71-
});
84+
console.error(err);
85+
}
86+
}
7287
// [END bigtable_api_exists_app_profile]
7388
},
7489

75-
get: (instanceId, appProfileId) => {
90+
get: async (instanceId, appProfileId) => {
7691
// [START bigtable_api_get_app_profile]
77-
const {Bigtable} = require('@google-cloud/bigtable');
78-
const bigtable = new Bigtable();
79-
const instance = bigtable.instance(instanceId);
80-
const appProfile = instance.appProfile(appProfileId);
92+
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
93+
const instanceAdminClient = new BigtableInstanceAdminClient();
94+
const projectId = await instanceAdminClient.getProjectId();
95+
96+
const request = {
97+
name: `projects/${projectId}/instances/${instanceId}/appProfiles/${appProfileId}`,
98+
};
8199

82-
appProfile
83-
.get()
100+
instanceAdminClient
101+
.getAppProfile(request)
84102
.then(result => {
85103
const appProfile = result[0];
86104
const apiResponse = result[1];
@@ -91,15 +109,18 @@ const snippets = {
91109
// [END bigtable_api_get_app_profile]
92110
},
93111

94-
getMeta: (instanceId, appProfileId) => {
112+
getMeta: async (instanceId, appProfileId) => {
95113
// [START bigtable_api_app_profile_get_meta]
96-
const {Bigtable} = require('@google-cloud/bigtable');
97-
const bigtable = new Bigtable();
98-
const instance = bigtable.instance(instanceId);
99-
const appProfile = instance.appProfile(appProfileId);
114+
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
115+
const instanceAdminClient = new BigtableInstanceAdminClient();
116+
const projectId = await instanceAdminClient.getProjectId();
100117

101-
appProfile
102-
.getMetadata()
118+
const request = {
119+
name: `projects/${projectId}/instances/${instanceId}/appProfiles/${appProfileId}`,
120+
};
121+
122+
instanceAdminClient
123+
.getAppProfile(request)
103124
.then(result => {
104125
const metadata = result[0];
105126
const apiResponse = result[1];
@@ -110,22 +131,27 @@ const snippets = {
110131
// [END bigtable_api_app_profile_get_meta]
111132
},
112133

113-
setMeta: (instanceId, appProfileId, clusterId) => {
134+
setMeta: async (instanceId, appProfileId, clusterId) => {
114135
// [START bigtable_api_app_profile_set_meta]
115-
const {Bigtable} = require('@google-cloud/bigtable');
116-
const bigtable = new Bigtable();
117-
const instance = bigtable.instance(instanceId);
118-
const cluster = instance.cluster(clusterId);
119-
const appProfile = instance.appProfile(appProfileId);
136+
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
137+
const instanceAdminClient = new BigtableInstanceAdminClient();
138+
const projectId = await instanceAdminClient.getProjectId();
120139

121-
const metadata = {
140+
const appProfile = {
141+
name: `projects/${projectId}/instances/${instanceId}/appProfiles/${appProfileId}`,
122142
description: 'My Updated App Profile',
123-
routing: cluster,
124-
allowTransactionalWrites: true,
143+
multiClusterRoutingUseAny: {},
144+
};
145+
146+
const request = {
147+
appProfile: appProfile,
148+
updateMask: {
149+
paths: ['description', 'multi_cluster_routing_use_any'],
150+
},
125151
};
126152

127-
appProfile
128-
.setMetadata(metadata)
153+
instanceAdminClient
154+
.updateAppProfile(request)
129155
.then(result => {
130156
const apiResponse = result[0];
131157
})

samples/api-reference-doc-snippets/backups.create.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ async function main(
3333
const projectId = await tableAdminClient.getProjectId();
3434

3535
const request = {
36-
parent: tableAdminClient.clusterPath(projectId, instanceId, clusterId),
36+
parent: `projects/${projectId}/instances/${instanceId}/clusters/${clusterId}`,
3737
backupId: backupId,
3838
backup: {
39-
sourceTable: tableAdminClient.tablePath(projectId, instanceId, tableId),
39+
sourceTable: `projects/${projectId}/instances/${instanceId}/tables/${tableId}`,
4040
expireTime: new Date(Date.now() + 7 * 60 * 60 * 1000), // 7 hours from now
4141
},
4242
};

samples/api-reference-doc-snippets/backups.delete.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,7 @@ async function main(
3030
// const backupId = 'YOUR_BACKUP_ID';
3131
const projectId = await tableAdminClient.getProjectId();
3232
const request = {
33-
name: tableAdminClient.backupPath(
34-
projectId,
35-
instanceId,
36-
clusterId,
37-
backupId,
38-
),
33+
name: `projects/${projectId}/instances/${instanceId}/clusters/${clusterId}/backups/${backupId}`,
3934
};
4035
await tableAdminClient.deleteBackup(request);
4136
console.log(`Backup ${backupId} was deleted successfully.`);

samples/api-reference-doc-snippets/backups.get.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,7 @@ async function main(
3131
const projectId = await tableAdminClient.getProjectId();
3232

3333
const request = {
34-
name: tableAdminClient.backupPath(
35-
projectId,
36-
instanceId,
37-
clusterId,
38-
backupId,
39-
),
34+
name: `projects/${projectId}/instances/${instanceId}/clusters/${clusterId}/backups/${backupId}`,
4035
};
4136

4237
const [metadata] = await tableAdminClient.getBackup(request);

samples/api-reference-doc-snippets/backups.list.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ async function main(
2929
const projectId = await tableAdminClient.getProjectId();
3030

3131
const request = {
32-
parent: tableAdminClient.clusterPath(projectId, instanceId, clusterId),
32+
parent: `projects/${projectId}/instances/${instanceId}/clusters/${clusterId}`,
3333
};
3434

3535
const [backupsFromCluster] = await tableAdminClient.listBackups(request);

samples/api-reference-doc-snippets/backups.restore.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,9 @@ async function main(
3737

3838
// Restore a table to an instance.
3939
const [restoreLRO] = await adminClient.restoreTable({
40-
parent: adminClient.instancePath(projectId, instanceId),
40+
parent: `projects/${projectId}/instances/${instanceId}`,
4141
tableId,
42-
backup: adminClient.backupPath(
43-
projectId,
44-
instanceId,
45-
clusterId,
46-
backupId,
47-
),
42+
backup: `projects/${projectId}/instances/${instanceId}/clusters/${clusterId}/backups/${backupId}`,
4843
});
4944
console.log('Waiting for restoreTable operation to complete...');
5045
const [table, metadata] = await restoreLRO.promise();

samples/api-reference-doc-snippets/backups.update.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,7 @@ async function main(
3232

3333
const request = {
3434
backup: {
35-
name: tableAdminClient.backupPath(
36-
projectId,
37-
instanceId,
38-
clusterId,
39-
backupId,
40-
),
35+
name: `projects/${projectId}/instances/${instanceId}/clusters/${clusterId}/backups/${backupId}`,
4136
expireTime: new Date(Date.now() + 24 * 60 * 60 * 1000), // 24 hours
4237
},
4338
updateMask: {

0 commit comments

Comments
 (0)