Skip to content

Commit 456c3d5

Browse files
authored
docs: samples for instances should include all necessary code (#1720)
## Description The samples for instances do not contain code for fetching the projectId and the admin client, but they should because this isn't a trivial step. This PR ensures the samples include all the code necessary in order for them to be able to work. ## Impact Improves the documentation and makes it more usable ## Testing These are changes to samples tests ## Additional Information Next steps: - We should do this for all our samples - make sure the samples don't require any assumptions
1 parent bb389dc commit 456c3d5

File tree

1 file changed

+55
-40
lines changed

1 file changed

+55
-40
lines changed

samples/instances.js

Lines changed: 55 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@
1515
'use strict';
1616

1717
async function runInstanceOperations(instanceID, clusterID) {
18-
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
19-
const instanceAdminClient = new BigtableInstanceAdminClient();
20-
const projectId = await instanceAdminClient.getProjectId();
21-
2218
console.log('Check Instance Exists');
2319
// [START bigtable_check_instance_exists]
2420
let instanceExists = true;
2521
try {
22+
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
23+
const instanceAdminClient = new BigtableInstanceAdminClient();
24+
const projectId = await instanceAdminClient.getProjectId();
2625
await instanceAdminClient.getInstance({
2726
name: `projects/${projectId}/instances/${instanceID}`,
2827
});
@@ -39,6 +38,9 @@ async function runInstanceOperations(instanceID, clusterID) {
3938
// [START bigtable_create_prod_instance]
4039
// Creates a Production Instance with the ID "ssd-instance"
4140
// with cluster id "ssd-cluster", 3 nodes and location us-central1-f
41+
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
42+
const instanceAdminClient = new BigtableInstanceAdminClient();
43+
const projectId = await instanceAdminClient.getProjectId();
4244

4345
const instanceOptions = {
4446
parent: `projects/${projectId}`,
@@ -69,46 +71,60 @@ async function runInstanceOperations(instanceID, clusterID) {
6971

7072
console.log(); //for just a new-line
7173
console.log('Listing Instances:');
72-
// [START bigtable_list_instances]
73-
const [instances] = await instanceAdminClient.listInstances({
74-
parent: `projects/${projectId}`,
75-
});
76-
instances.instances.forEach(instance => {
77-
console.log(instance.name);
78-
});
79-
// [END bigtable_list_instances]
74+
{
75+
// [START bigtable_list_instances]
76+
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
77+
const instanceAdminClient = new BigtableInstanceAdminClient();
78+
const projectId = await instanceAdminClient.getProjectId();
79+
const [instances] = await instanceAdminClient.listInstances({
80+
parent: `projects/${projectId}`,
81+
});
82+
instances.instances.forEach(instance => {
83+
console.log(instance.name);
84+
});
85+
// [END bigtable_list_instances]
86+
}
8087

8188
console.log(); //for just a new-line
8289
console.log('Get Instance');
83-
// [START bigtable_get_instance]
84-
const [instance2] = await instanceAdminClient.getInstance({
85-
name: `projects/${projectId}/instances/${instanceID}`,
86-
});
87-
console.log(`Instance ID: ${instance2.name}`);
88-
console.log(`Instance Meta: ${JSON.stringify(instance2.labels)}`);
89-
// [END bigtable_get_instance]
90+
{
91+
// [START bigtable_get_instance]
92+
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
93+
const instanceAdminClient = new BigtableInstanceAdminClient();
94+
const projectId = await instanceAdminClient.getProjectId();
95+
const [instance2] = await instanceAdminClient.getInstance({
96+
name: `projects/${projectId}/instances/${instanceID}`,
97+
});
98+
console.log(`Instance ID: ${instance2.name}`);
99+
console.log(`Instance Meta: ${JSON.stringify(instance2.labels)}`);
100+
// [END bigtable_get_instance]
101+
}
90102

91103
console.log(); //for just a new-line
92104
console.log('Listing Clusters...');
93-
// [START bigtable_get_clusters]
94-
const [clusters] = await instanceAdminClient.listClusters({
95-
parent: `projects/${projectId}/instances/${instanceID}`,
96-
});
97-
clusters.clusters.forEach(cluster => {
98-
console.log(cluster.name);
99-
});
100-
// [END bigtable_get_clusters]
105+
{
106+
// [START bigtable_get_clusters]
107+
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
108+
const instanceAdminClient = new BigtableInstanceAdminClient();
109+
const projectId = await instanceAdminClient.getProjectId();
110+
const [clusters] = await instanceAdminClient.listClusters({
111+
parent: `projects/${projectId}/instances/${instanceID}`,
112+
});
113+
clusters.clusters.forEach(cluster => {
114+
console.log(cluster.name);
115+
});
116+
// [END bigtable_get_clusters]
117+
}
101118
}
102119

103120
// Creates a Development instance with the ID "hdd-instance"
104121
// with cluster ID "hdd-cluster" and location us-central1-f
105122
// Cluster nodes should not be set while creating Development Instance
106123
async function createDevInstance(instanceID, clusterID) {
124+
// [START bigtable_create_dev_instance]
107125
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
108126
const instanceAdminClient = new BigtableInstanceAdminClient();
109127
const projectId = await instanceAdminClient.getProjectId();
110-
111-
// [START bigtable_create_dev_instance]
112128
console.log(); //for just a new-line
113129
console.log('Creating a DEVELOPMENT Instance');
114130
// Set options to create an Instance
@@ -140,14 +156,12 @@ async function createDevInstance(instanceID, clusterID) {
140156

141157
// Delete the Instance
142158
async function deleteInstance(instanceID) {
143-
// Creates a client
144-
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
145-
const instanceAdminClient = new BigtableInstanceAdminClient();
146-
const projectId = await instanceAdminClient.getProjectId();
147-
148159
console.log(); //for just a new-line
149160
// [START bigtable_delete_instance]
150161
console.log('Deleting Instance');
162+
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
163+
const instanceAdminClient = new BigtableInstanceAdminClient();
164+
const projectId = await instanceAdminClient.getProjectId();
151165
await instanceAdminClient.deleteInstance({
152166
name: `projects/${projectId}/instances/${instanceID}`,
153167
});
@@ -157,12 +171,11 @@ async function deleteInstance(instanceID) {
157171

158172
// Add Cluster
159173
async function addCluster(instanceID, clusterID) {
160-
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
161-
const instanceAdminClient = new BigtableInstanceAdminClient();
162-
const projectId = await instanceAdminClient.getProjectId();
163-
164174
let instanceExists = true;
165175
try {
176+
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
177+
const instanceAdminClient = new BigtableInstanceAdminClient();
178+
const projectId = await instanceAdminClient.getProjectId();
166179
await instanceAdminClient.getInstance({
167180
name: `projects/${projectId}/instances/${instanceID}`,
168181
});
@@ -177,6 +190,9 @@ async function addCluster(instanceID, clusterID) {
177190
console.log(); //for just a new-line
178191
console.log(`Adding Cluster to Instance ${instanceID}`);
179192
// [START bigtable_create_cluster]
193+
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
194+
const instanceAdminClient = new BigtableInstanceAdminClient();
195+
const projectId = await instanceAdminClient.getProjectId();
180196
const clusterOptions = {
181197
parent: `projects/${projectId}/instances/${instanceID}`,
182198
clusterId: clusterID,
@@ -197,11 +213,10 @@ async function addCluster(instanceID, clusterID) {
197213

198214
// Delete the Cluster
199215
async function deleteCluster(instanceID, clusterID) {
216+
// [START bigtable_delete_cluster]
200217
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
201218
const instanceAdminClient = new BigtableInstanceAdminClient();
202219
const projectId = await instanceAdminClient.getProjectId();
203-
204-
// [START bigtable_delete_cluster]
205220
console.log(); //for just a new-line
206221
console.log('Deleting Cluster');
207222
await instanceAdminClient.deleteCluster({

0 commit comments

Comments
 (0)