15
15
*/
16
16
17
17
import { promisifyAll } from '@google-cloud/promisify' ;
18
- import * as is from 'is' ;
18
+ import { CallOptions , Operation as GaxOperation } from 'google-gax' ;
19
+ import { ServiceError } from 'grpc' ;
20
+
21
+ import { google as btTypes } from '../proto/bigtable' ;
22
+ import { Bigtable } from '.' ;
23
+ import { Instance } from './instance' ;
24
+
25
+ export interface GenericCallback < T > {
26
+ ( err ?: ServiceError | null , apiResponse ?: T | null ) : void ;
27
+ }
28
+ export interface GenericClusterCallback < T > {
29
+ ( err ?: ServiceError | null , cluster ?: Cluster | null , apiResponse ?: T | null ) ;
30
+ }
31
+ export interface GenericOperationCallback < T > {
32
+ ( err ?: ServiceError | null , operation ?: GaxOperation | null ,
33
+ apiResponse ?: T | null ) : void ;
34
+ }
35
+
36
+ export type IEmpty = btTypes . protobuf . IEmpty ;
37
+ export type ICluster = btTypes . bigtable . admin . v2 . ICluster ;
38
+ export type IOperation = btTypes . longrunning . IOperation ;
39
+
40
+ export type ApiResponse = [ IOperation ] ;
41
+ export type CreateClusterResponse = [ ICluster , GaxOperation , IOperation ] ;
42
+ export type BooleanResponse = [ boolean ] ;
43
+ export type GetClusterResponse = [ ICluster , IOperation ] ;
44
+ export type MetadataResponse = [ Metadata , IOperation ] ;
45
+
46
+ export type CreateClusterCallback = GenericCallback < IOperation > ;
47
+ export type DeleteClusterCallback = GenericCallback < IOperation > ;
48
+ export type ExistsClusterCallback = GenericCallback < boolean > ;
49
+ export type GetClusterCallback = GenericClusterCallback < ICluster > ;
50
+ export type SetClusterMetadataCallback = GenericOperationCallback < IOperation > ;
51
+
52
+ export interface CreateClusterOptions {
53
+ gaxOptions ?: CallOptions ;
54
+ location : string ;
55
+ nodes : number ;
56
+ storage ?: string ;
57
+ }
58
+ export interface GetClusterMetadataCallback {
59
+ // tslint:disable-next-line no-any
60
+ ( ...args : any [ ] ) : void ;
61
+ ( err : ServiceError | null , metadata ?: ICluster | null ,
62
+ apiResponse ?: IOperation | null ) : void ;
63
+ }
64
+ export interface Metadata extends CreateClusterOptions {
65
+ displayName ?: string ;
66
+ }
19
67
20
68
/**
21
69
* Create a cluster object to interact with your cluster.
@@ -31,16 +79,16 @@ import * as is from 'is';
31
79
* const cluster = instance.cluster('my-cluster');
32
80
*/
33
81
export class Cluster {
34
- bigtable ;
35
- instance ;
36
- id ;
37
- name ;
38
- metadata ;
39
- constructor ( instance , id ) {
82
+ bigtable : Bigtable ;
83
+ instance : Instance ;
84
+ id : string ;
85
+ name : string ;
86
+ metadata ?: Metadata ;
87
+ constructor ( instance : Instance , id : string ) {
40
88
this . bigtable = instance . bigtable ;
41
89
this . instance = instance ;
42
90
43
- let name ;
91
+ let name : string ;
44
92
45
93
if ( id . includes ( '/' ) ) {
46
94
if ( id . startsWith ( `${ instance . name } /clusters/` ) ) {
@@ -52,7 +100,7 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`);
52
100
} else {
53
101
name = `${ instance . name } /clusters/${ id } ` ;
54
102
}
55
- this . id = name . split ( '/' ) . pop ( ) ;
103
+ this . id = name . split ( '/' ) . pop ( ) ! ;
56
104
this . name = name ;
57
105
}
58
106
@@ -69,14 +117,14 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`);
69
117
* Cluster.getLocation_('my-project', 'us-central1-b');
70
118
* // 'projects/my-project/locations/us-central1-b'
71
119
*/
72
- static getLocation_ ( project , location ) {
120
+ static getLocation_ ( project : string , location : string ) : string {
73
121
if ( location . includes ( '/' ) ) {
74
122
return location ;
75
123
}
76
124
77
125
// in-case project has '/', split and pick last component
78
126
if ( project . includes ( '/' ) ) {
79
- project = project . split ( '/' ) . pop ( ) ;
127
+ project = project . split ( '/' ) . pop ( ) ! ;
80
128
}
81
129
82
130
return `projects/${ project } /locations/${ location } ` ;
@@ -94,20 +142,24 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`);
94
142
* Cluster.getStorageType_('ssd');
95
143
* // 1
96
144
*/
97
- static getStorageType_ ( type ) {
98
- const storageTypes = {
145
+ static getStorageType_ ( type : string ) : number {
146
+ const storageTypes : { [ k : string ] : number } = {
99
147
unspecified : 0 ,
100
148
ssd : 1 ,
101
149
hdd : 2 ,
102
150
} ;
103
151
104
- if ( is . string ( type ) ) {
152
+ if ( typeof type === 'string' ) {
105
153
type = type . toLowerCase ( ) ;
106
154
}
107
155
108
156
return storageTypes [ type ] || storageTypes . unspecified ;
109
157
}
110
158
159
+ create ( ) : Promise < CreateClusterResponse > ;
160
+ create ( options : CreateClusterOptions ) : Promise < CreateClusterResponse > ;
161
+ create ( callback : CreateClusterCallback ) : void ;
162
+ create ( options : CreateClusterOptions , callback : CreateClusterCallback ) : void ;
111
163
/**
112
164
* Create a cluster.
113
165
*
@@ -120,15 +172,22 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`);
120
172
* @example <caption>include:samples/document-snippets/cluster.js</caption>
121
173
* region_tag:bigtable_create_cluster
122
174
*/
123
- create ( options , callback ) {
124
- if ( is . fn ( options ) ) {
125
- callback = options ;
126
- options = { } ;
127
- }
175
+ create (
176
+ optionsOrCallback ?: CreateClusterOptions | CreateClusterCallback ,
177
+ cb ?: CreateClusterCallback ) : void | Promise < CreateClusterResponse > {
178
+ const callback =
179
+ typeof optionsOrCallback === 'function' ? optionsOrCallback : cb ! ;
180
+ const options = typeof optionsOrCallback === 'object' && optionsOrCallback ?
181
+ optionsOrCallback :
182
+ { } as CreateClusterOptions ;
128
183
129
184
this . instance . createCluster ( this . id , options , callback ) ;
130
185
}
131
186
187
+ delete ( ) : Promise < ApiResponse > ;
188
+ delete ( gaxOptions : CallOptions ) : Promise < ApiResponse > ;
189
+ delete ( callback : DeleteClusterCallback ) : void ;
190
+ delete ( gaxOptions : CallOptions , callback : DeleteClusterCallback ) : void ;
132
191
/**
133
192
* Delete the cluster.
134
193
*
@@ -142,11 +201,15 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`);
142
201
* @example <caption>include:samples/document-snippets/cluster.js</caption>
143
202
* region_tag:bigtable_delete_cluster
144
203
*/
145
- delete ( gaxOptions , callback ) {
146
- if ( is . fn ( gaxOptions ) ) {
147
- callback = gaxOptions ;
148
- gaxOptions = { } ;
149
- }
204
+ delete (
205
+ gaxOptionsOrCallback ?: CallOptions | DeleteClusterCallback ,
206
+ cb ?: DeleteClusterCallback ) : void | Promise < ApiResponse > {
207
+ const callback =
208
+ typeof gaxOptionsOrCallback === 'function' ? gaxOptionsOrCallback : cb ! ;
209
+ const gaxOptions =
210
+ typeof gaxOptionsOrCallback === 'object' && gaxOptionsOrCallback ?
211
+ gaxOptionsOrCallback :
212
+ { } as CallOptions ;
150
213
151
214
this . bigtable . request (
152
215
{
@@ -160,6 +223,10 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`);
160
223
callback ) ;
161
224
}
162
225
226
+ exists ( ) : Promise < BooleanResponse > ;
227
+ exists ( gaxOptions : CallOptions ) : Promise < BooleanResponse > ;
228
+ exists ( callback : ExistsClusterCallback ) : void ;
229
+ exists ( gaxOptions : CallOptions , callback : ExistsClusterCallback ) : void ;
163
230
/**
164
231
* Check if a cluster exists.
165
232
*
@@ -173,13 +240,17 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`);
173
240
* @example <caption>include:samples/document-snippets/cluster.js</caption>
174
241
* region_tag:bigtable_exists_cluster
175
242
*/
176
- exists ( gaxOptions , callback ) {
177
- if ( is . fn ( gaxOptions ) ) {
178
- callback = gaxOptions ;
179
- gaxOptions = { } ;
180
- }
243
+ exists (
244
+ gaxOptionsOrCallback ?: CallOptions | ExistsClusterCallback ,
245
+ cb ?: ExistsClusterCallback ) : void | Promise < BooleanResponse > {
246
+ const callback =
247
+ typeof gaxOptionsOrCallback === 'function' ? gaxOptionsOrCallback : cb ! ;
248
+ const gaxOptions =
249
+ typeof gaxOptionsOrCallback === 'object' && gaxOptionsOrCallback ?
250
+ gaxOptionsOrCallback :
251
+ { } as CallOptions ;
181
252
182
- this . getMetadata ( gaxOptions , err => {
253
+ this . getMetadata ( gaxOptions , ( err ?: ServiceError | null ) => {
183
254
if ( err ) {
184
255
if ( err . code === 5 ) {
185
256
callback ( null , false ) ;
@@ -194,6 +265,10 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`);
194
265
} ) ;
195
266
}
196
267
268
+ get ( ) : Promise < GetClusterResponse > ;
269
+ get ( gaxOptions : CallOptions ) : Promise < GetClusterResponse > ;
270
+ get ( callback : GetClusterCallback ) : void ;
271
+ get ( gaxOptions : CallOptions , callback : GetClusterCallback ) : void ;
197
272
/**
198
273
* Get a cluster if it exists.
199
274
*
@@ -207,17 +282,26 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`);
207
282
* @example <caption>include:samples/document-snippets/cluster.js</caption>
208
283
* region_tag:bigtable_get_cluster
209
284
*/
210
- get ( gaxOptions , callback ) {
211
- if ( is . fn ( gaxOptions ) ) {
212
- callback = gaxOptions ;
213
- gaxOptions = { } ;
214
- }
285
+ get ( gaxOptionsOrCallback ?: CallOptions | GetClusterCallback ,
286
+ cb ?: GetClusterCallback ) : void | Promise < GetClusterResponse > {
287
+ const callback =
288
+ typeof gaxOptionsOrCallback === 'function' ? gaxOptionsOrCallback : cb ! ;
289
+ const gaxOptions =
290
+ typeof gaxOptionsOrCallback === 'object' && gaxOptionsOrCallback ?
291
+ gaxOptionsOrCallback :
292
+ { } as CallOptions ;
215
293
216
- this . getMetadata ( gaxOptions , ( err , metadata ) => {
217
- callback ( err , err ? null : this , metadata ) ;
218
- } ) ;
294
+ this . getMetadata (
295
+ gaxOptions , ( err ?: ServiceError | null , metadata ?: ICluster | null ) => {
296
+ callback ( err , err ? null : this , metadata ) ;
297
+ } ) ;
219
298
}
220
299
300
+ getMetadata ( ) : Promise < MetadataResponse > ;
301
+ getMetadata ( gaxOptions : CallOptions ) : Promise < MetadataResponse > ;
302
+ getMetadata ( callback : GetClusterMetadataCallback ) : void ;
303
+ getMetadata ( gaxOptions : CallOptions , callback : GetClusterMetadataCallback ) :
304
+ void ;
221
305
/**
222
306
* Get the cluster metadata.
223
307
*
@@ -232,11 +316,15 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`);
232
316
* @example <caption>include:samples/document-snippets/cluster.js</caption>
233
317
* region_tag:bigtable_cluster_get_meta
234
318
*/
235
- getMetadata ( gaxOptions , callback ) {
236
- if ( is . fn ( gaxOptions ) ) {
237
- callback = gaxOptions ;
238
- gaxOptions = { } ;
239
- }
319
+ getMetadata (
320
+ gaxOptionsOrCallback ?: CallOptions | GetClusterMetadataCallback ,
321
+ cb ?: GetClusterMetadataCallback ) : void | Promise < MetadataResponse > {
322
+ const callback =
323
+ typeof gaxOptionsOrCallback === 'function' ? gaxOptionsOrCallback : cb ! ;
324
+ const gaxOptions =
325
+ typeof gaxOptionsOrCallback === 'object' && gaxOptionsOrCallback ?
326
+ gaxOptionsOrCallback :
327
+ { } as CallOptions ;
240
328
241
329
this . bigtable . request (
242
330
{
@@ -247,7 +335,8 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`);
247
335
} ,
248
336
gaxOpts : gaxOptions ,
249
337
} ,
250
- ( ...args ) => {
338
+ // tslint:disable-next-line no-any
339
+ ( ...args : any [ ] ) => {
251
340
if ( args [ 1 ] ) {
252
341
this . metadata = args [ 1 ] ;
253
342
}
@@ -256,6 +345,15 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`);
256
345
} ) ;
257
346
}
258
347
348
+ setMetadata ( metadata : CreateClusterOptions ) : Promise < MetadataResponse > ;
349
+ setMetadata ( metadata : CreateClusterOptions , gaxOptions : CallOptions ) :
350
+ Promise < MetadataResponse > ;
351
+ setMetadata (
352
+ metadata : CreateClusterOptions ,
353
+ callback : SetClusterMetadataCallback ) : void ;
354
+ setMetadata (
355
+ metadata : CreateClusterOptions , gaxOptions : CallOptions ,
356
+ callback : SetClusterMetadataCallback ) : void ;
259
357
/**
260
358
* Set the cluster metadata.
261
359
*
@@ -272,12 +370,18 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`);
272
370
* @example <caption>include:samples/document-snippets/cluster.js</caption>
273
371
* region_tag:bigtable_cluster_set_meta
274
372
*/
275
- setMetadata ( metadata , gaxOptions , callback ) {
276
- if ( is . fn ( gaxOptions ) ) {
277
- callback = gaxOptions ;
278
- gaxOptions = { } ;
279
- }
373
+ setMetadata (
374
+ metadata : CreateClusterOptions ,
375
+ gaxOptionsOrCallback ?: CallOptions | SetClusterMetadataCallback ,
376
+ cb ?: SetClusterMetadataCallback ) : void | Promise < MetadataResponse > {
377
+ const callback =
378
+ typeof gaxOptionsOrCallback === 'function' ? gaxOptionsOrCallback : cb ! ;
379
+ const gaxOptions =
380
+ typeof gaxOptionsOrCallback === 'object' && gaxOptionsOrCallback ?
381
+ gaxOptionsOrCallback :
382
+ { } as CallOptions ;
280
383
384
+ // tslint:disable-next-line no-any
281
385
const reqOpts : any = {
282
386
name : this . name ,
283
387
} ;
0 commit comments