Skip to content

Commit 8682184

Browse files
Joe CervinoJustinBeckwith
Joe Cervino
authored andcommitted
refactor(ts): cluster.ts (OPR 1) (#452)
1 parent e29f9aa commit 8682184

File tree

1 file changed

+153
-49
lines changed

1 file changed

+153
-49
lines changed

src/cluster.ts

+153-49
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,55 @@
1515
*/
1616

1717
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+
}
1967

2068
/**
2169
* Create a cluster object to interact with your cluster.
@@ -31,16 +79,16 @@ import * as is from 'is';
3179
* const cluster = instance.cluster('my-cluster');
3280
*/
3381
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) {
4088
this.bigtable = instance.bigtable;
4189
this.instance = instance;
4290

43-
let name;
91+
let name: string;
4492

4593
if (id.includes('/')) {
4694
if (id.startsWith(`${instance.name}/clusters/`)) {
@@ -52,7 +100,7 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`);
52100
} else {
53101
name = `${instance.name}/clusters/${id}`;
54102
}
55-
this.id = name.split('/').pop();
103+
this.id = name.split('/').pop()!;
56104
this.name = name;
57105
}
58106

@@ -69,14 +117,14 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`);
69117
* Cluster.getLocation_('my-project', 'us-central1-b');
70118
* // 'projects/my-project/locations/us-central1-b'
71119
*/
72-
static getLocation_(project, location) {
120+
static getLocation_(project: string, location: string): string {
73121
if (location.includes('/')) {
74122
return location;
75123
}
76124

77125
// in-case project has '/', split and pick last component
78126
if (project.includes('/')) {
79-
project = project.split('/').pop();
127+
project = project.split('/').pop()!;
80128
}
81129

82130
return `projects/${project}/locations/${location}`;
@@ -94,20 +142,24 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`);
94142
* Cluster.getStorageType_('ssd');
95143
* // 1
96144
*/
97-
static getStorageType_(type) {
98-
const storageTypes = {
145+
static getStorageType_(type: string): number {
146+
const storageTypes: {[k: string]: number} = {
99147
unspecified: 0,
100148
ssd: 1,
101149
hdd: 2,
102150
};
103151

104-
if (is.string(type)) {
152+
if (typeof type === 'string') {
105153
type = type.toLowerCase();
106154
}
107155

108156
return storageTypes[type] || storageTypes.unspecified;
109157
}
110158

159+
create(): Promise<CreateClusterResponse>;
160+
create(options: CreateClusterOptions): Promise<CreateClusterResponse>;
161+
create(callback: CreateClusterCallback): void;
162+
create(options: CreateClusterOptions, callback: CreateClusterCallback): void;
111163
/**
112164
* Create a cluster.
113165
*
@@ -120,15 +172,22 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`);
120172
* @example <caption>include:samples/document-snippets/cluster.js</caption>
121173
* region_tag:bigtable_create_cluster
122174
*/
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;
128183

129184
this.instance.createCluster(this.id, options, callback);
130185
}
131186

187+
delete(): Promise<ApiResponse>;
188+
delete(gaxOptions: CallOptions): Promise<ApiResponse>;
189+
delete(callback: DeleteClusterCallback): void;
190+
delete(gaxOptions: CallOptions, callback: DeleteClusterCallback): void;
132191
/**
133192
* Delete the cluster.
134193
*
@@ -142,11 +201,15 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`);
142201
* @example <caption>include:samples/document-snippets/cluster.js</caption>
143202
* region_tag:bigtable_delete_cluster
144203
*/
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;
150213

151214
this.bigtable.request(
152215
{
@@ -160,6 +223,10 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`);
160223
callback);
161224
}
162225

226+
exists(): Promise<BooleanResponse>;
227+
exists(gaxOptions: CallOptions): Promise<BooleanResponse>;
228+
exists(callback: ExistsClusterCallback): void;
229+
exists(gaxOptions: CallOptions, callback: ExistsClusterCallback): void;
163230
/**
164231
* Check if a cluster exists.
165232
*
@@ -173,13 +240,17 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`);
173240
* @example <caption>include:samples/document-snippets/cluster.js</caption>
174241
* region_tag:bigtable_exists_cluster
175242
*/
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;
181252

182-
this.getMetadata(gaxOptions, err => {
253+
this.getMetadata(gaxOptions, (err?: ServiceError|null) => {
183254
if (err) {
184255
if (err.code === 5) {
185256
callback(null, false);
@@ -194,6 +265,10 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`);
194265
});
195266
}
196267

268+
get(): Promise<GetClusterResponse>;
269+
get(gaxOptions: CallOptions): Promise<GetClusterResponse>;
270+
get(callback: GetClusterCallback): void;
271+
get(gaxOptions: CallOptions, callback: GetClusterCallback): void;
197272
/**
198273
* Get a cluster if it exists.
199274
*
@@ -207,17 +282,26 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`);
207282
* @example <caption>include:samples/document-snippets/cluster.js</caption>
208283
* region_tag:bigtable_get_cluster
209284
*/
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;
215293

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+
});
219298
}
220299

300+
getMetadata(): Promise<MetadataResponse>;
301+
getMetadata(gaxOptions: CallOptions): Promise<MetadataResponse>;
302+
getMetadata(callback: GetClusterMetadataCallback): void;
303+
getMetadata(gaxOptions: CallOptions, callback: GetClusterMetadataCallback):
304+
void;
221305
/**
222306
* Get the cluster metadata.
223307
*
@@ -232,11 +316,15 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`);
232316
* @example <caption>include:samples/document-snippets/cluster.js</caption>
233317
* region_tag:bigtable_cluster_get_meta
234318
*/
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;
240328

241329
this.bigtable.request(
242330
{
@@ -247,7 +335,8 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`);
247335
},
248336
gaxOpts: gaxOptions,
249337
},
250-
(...args) => {
338+
// tslint:disable-next-line no-any
339+
(...args: any[]) => {
251340
if (args[1]) {
252341
this.metadata = args[1];
253342
}
@@ -256,6 +345,15 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`);
256345
});
257346
}
258347

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;
259357
/**
260358
* Set the cluster metadata.
261359
*
@@ -272,12 +370,18 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`);
272370
* @example <caption>include:samples/document-snippets/cluster.js</caption>
273371
* region_tag:bigtable_cluster_set_meta
274372
*/
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;
280383

384+
// tslint:disable-next-line no-any
281385
const reqOpts: any = {
282386
name: this.name,
283387
};

0 commit comments

Comments
 (0)