-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathinstances.js
More file actions
277 lines (262 loc) · 9.5 KB
/
instances.js
File metadata and controls
277 lines (262 loc) · 9.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright 2017 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.
'use strict';
async function runInstanceOperations(instanceID, clusterID) {
console.log('Check Instance Exists');
// [START bigtable_check_instance_exists]
let instanceExists = true;
try {
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
const instanceAdminClient = new BigtableInstanceAdminClient();
const projectId = await instanceAdminClient.getProjectId();
await instanceAdminClient.getInstance({
name: `projects/${projectId}/instances/${instanceID}`,
});
} catch (e) {
if (e.code === 5) {
instanceExists = false;
}
}
// [END bigtable_check_instance_exists]
// Create instance if does not exists
if (!instanceExists) {
console.log('Creating a PRODUCTION Instance');
// [START bigtable_create_prod_instance]
// Creates a Production Instance with the ID "ssd-instance"
// with cluster id "ssd-cluster", 3 nodes and location us-central1-f
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
const instanceAdminClient = new BigtableInstanceAdminClient();
const projectId = await instanceAdminClient.getProjectId();
const instanceOptions = {
parent: `projects/${projectId}`,
instanceId: instanceID,
instance: {
displayName: instanceID,
labels: {'prod-label': 'prod-label'},
type: 'PRODUCTION',
},
clusters: {
[clusterID]: {
location: `projects/${projectId}/locations/us-central1-f`,
serveNodes: 3,
defaultStorageType: 'SSD',
},
},
};
// Create production instance with given options
const [prodInstance, operation] =
await instanceAdminClient.createInstance(instanceOptions);
await operation.promise();
console.log(`Created Instance: ${prodInstance.name}`);
// [END bigtable_create_prod_instance]
} else {
console.log(`Instance ${instanceID} exists`);
}
console.log(); //for just a new-line
console.log('Listing Instances:');
{
// [START bigtable_list_instances]
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
const instanceAdminClient = new BigtableInstanceAdminClient();
const projectId = await instanceAdminClient.getProjectId();
const [instances] = await instanceAdminClient.listInstances({
parent: `projects/${projectId}`,
});
instances.instances.forEach(instance => {
console.log(instance.name);
});
// [END bigtable_list_instances]
}
console.log(); //for just a new-line
console.log('Get Instance');
{
// [START bigtable_get_instance]
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
const instanceAdminClient = new BigtableInstanceAdminClient();
const projectId = await instanceAdminClient.getProjectId();
const [instance2] = await instanceAdminClient.getInstance({
name: `projects/${projectId}/instances/${instanceID}`,
});
console.log(`Instance ID: ${instance2.name}`);
console.log(`Instance Meta: ${JSON.stringify(instance2.labels)}`);
// [END bigtable_get_instance]
}
console.log(); //for just a new-line
console.log('Listing Clusters...');
{
// [START bigtable_get_clusters]
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
const instanceAdminClient = new BigtableInstanceAdminClient();
const projectId = await instanceAdminClient.getProjectId();
const [clusters] = await instanceAdminClient.listClusters({
parent: `projects/${projectId}/instances/${instanceID}`,
});
clusters.clusters.forEach(cluster => {
console.log(cluster.name);
});
// [END bigtable_get_clusters]
}
}
// Creates a Development instance with the ID "hdd-instance"
// with cluster ID "hdd-cluster" and location us-central1-f
// Cluster nodes should not be set while creating Development Instance
async function createDevInstance(instanceID, clusterID) {
// [START bigtable_create_dev_instance]
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
const instanceAdminClient = new BigtableInstanceAdminClient();
const projectId = await instanceAdminClient.getProjectId();
console.log(); //for just a new-line
console.log('Creating a DEVELOPMENT Instance');
// Set options to create an Instance
const options = {
parent: `projects/${projectId}`,
instanceId: instanceID,
instance: {
displayName: instanceID,
labels: {'dev-label': 'dev-label'},
type: 'DEVELOPMENT',
},
clusters: [
{
id: clusterID,
location: `projects/${projectId}/locations/us-central1-f`,
serveNodes: 1,
defaultStorageType: 'HDD',
},
],
};
// Create development instance with given options
const [instance, operation] =
await instanceAdminClient.createInstance(options);
await operation.promise();
console.log(`Created development instance: ${instance.name}`);
// [END bigtable_create_dev_instance]
}
// Delete the Instance
async function deleteInstance(instanceID) {
console.log(); //for just a new-line
// [START bigtable_delete_instance]
console.log('Deleting Instance');
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
const instanceAdminClient = new BigtableInstanceAdminClient();
const projectId = await instanceAdminClient.getProjectId();
await instanceAdminClient.deleteInstance({
name: `projects/${projectId}/instances/${instanceID}`,
});
console.log(`Instance deleted: ${instanceID}`);
// [END bigtable_delete_instance]
}
// Add Cluster
async function addCluster(instanceID, clusterID) {
let instanceExists = true;
try {
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
const instanceAdminClient = new BigtableInstanceAdminClient();
const projectId = await instanceAdminClient.getProjectId();
await instanceAdminClient.getInstance({
name: `projects/${projectId}/instances/${instanceID}`,
});
} catch (e) {
if (e.code === 5) {
instanceExists = false;
}
}
if (!instanceExists) {
console.log('Instance does not exists');
} else {
console.log(); //for just a new-line
console.log(`Adding Cluster to Instance ${instanceID}`);
// [START bigtable_create_cluster]
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
const instanceAdminClient = new BigtableInstanceAdminClient();
const projectId = await instanceAdminClient.getProjectId();
const clusterOptions = {
parent: `projects/${projectId}/instances/${instanceID}`,
clusterId: clusterID,
cluster: {
location: `projects/${projectId}/locations/us-central1-c`,
serveNodes: 3,
defaultStorageType: 'SSD',
},
};
const [cluster, operation] =
await instanceAdminClient.createCluster(clusterOptions);
await operation.promise();
console.log(`Cluster created: ${cluster.name}`);
// [END bigtable_create_cluster]
}
}
// Delete the Cluster
async function deleteCluster(instanceID, clusterID) {
// [START bigtable_delete_cluster]
const {BigtableInstanceAdminClient} = require('@google-cloud/bigtable').v2;
const instanceAdminClient = new BigtableInstanceAdminClient();
const projectId = await instanceAdminClient.getProjectId();
console.log(); //for just a new-line
console.log('Deleting Cluster');
await instanceAdminClient.deleteCluster({
name: `projects/${projectId}/instances/${instanceID}/clusters/${clusterID}`,
});
console.log(`Cluster deleted: ${clusterID}`);
// [END bigtable_delete_cluster]
}
require('yargs')
.demand(1)
.command(
'run',
'Creates an Instance(type: PRODUCTION) and run basic instance-operations',
{},
argv => runInstanceOperations(argv.instance, argv.cluster),
)
.example(
'node $0 run --instance [instanceID] --cluster [clusterID]',
'Run instance operations',
)
.command('dev-instance', 'Create Development Instance', {}, argv =>
createDevInstance(argv.instance, argv.cluster),
)
.example(
'node $0 dev-instance --instance [instanceID]',
'Create Development Instance',
)
.command('del-instance', 'Delete the Instance', {}, argv =>
deleteInstance(argv.instance),
)
.example(
'node $0 del-instance --instance [instanceID]',
'Delete the Instance.',
)
.command('add-cluster', 'Add Cluster', {}, argv =>
addCluster(argv.instance, argv.cluster),
)
.example(
'node $0 add-cluster --instance [instanceID] --cluster [clusterID]',
'Add Cluster',
)
.command('del-cluster', 'Delete the Cluster', {}, argv =>
deleteCluster(argv.instance, argv.cluster),
)
.example(
'node $0 del-cluster --instance [instanceID] --cluster [clusterID]',
'Delete the Cluster',
)
.wrap(120)
.nargs('instance', 1)
.nargs('cluster', 1)
.describe('instance', 'Cloud Bigtable Instance ID')
.describe('cluster', 'Cloud Bigtable Cluster ID')
.recommendCommands()
.epilogue('For more information, see https://cloud.google.com/bigtable/docs')
.help()
.strict().argv;