This repository has been archived by the owner on Nov 22, 2019. It is now read-only.
/
connection.service.ts
418 lines (377 loc) · 16.8 KB
/
connection.service.ts
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/**
* @license
* Copyright 2017 JBoss Inc
*
* 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.
*/
import { Injectable } from "@angular/core";
import { Http, RequestOptions } from "@angular/http";
import { ConnectionStatus } from "./connection-status";
import { ConnectionType } from "./connection-type.model";
import { ConnectionsConstants } from "./connections-constants";
import { NewConnection } from "./new-connection.model";
import { SchemaNode } from "./schema-node.model";
import { ServiceCatalogSource } from "./service-catalog-source.model";
import { ApiService } from "../../core/api.service";
import { AppSettingsService } from "../../core/app-settings.service";
import { LoggerService } from "../../core/logger.service";
import { ConnectionSummary } from "../../dataservices/shared/connection-summary.model";
import { NotifierService } from "../../dataservices/shared/notifier.service";
import { VdbService } from "../../dataservices/shared/vdb.service";
import { environment } from "../../environments/environment";
import { Observable } from "rxjs/Observable";
import { Subscription } from "rxjs/Subscription";
import { VdbsConstants } from "../../dataservices/shared/vdbs-constants";
import { Column } from "../../dataservices/shared/column.model";
import "rxjs/add/operator/map";
import { timer } from "rxjs/observable/timer";
import "rxjs/add/observable/of";
@Injectable()
export class ConnectionService extends ApiService {
private static readonly nameValidationUrl = environment.komodoWorkspaceUrl
+ ConnectionsConstants.connectionsRootPath
+ "/nameValidation/";
private static readonly refreshConnectionSchemaUrl = environment.komodoWorkspaceUrl
+ ConnectionsConstants.connectionsRootPath
+ "/refresh-schema/";
private http: Http;
private updatesSubscription: Subscription;
private notifierService: NotifierService;
private vdbService: VdbService;
private cachedConnectionStatuses: Map<string, ConnectionStatus> = new Map<string, ConnectionStatus>();
constructor( http: Http, vdbService: VdbService, notifierService: NotifierService,
appSettings: AppSettingsService, logger: LoggerService ) {
super( appSettings, logger );
this.http = http;
this.vdbService = vdbService;
this.notifierService = notifierService;
// Polls to fire Connection VDB state updates every 15 sec
this.pollConnectionSchemaStatus(15);
}
/**
* Validates the specified connection name. If the name contains valid characters and the name is unique, the
* service returns 'null'. Otherwise, a 'string' containing an error message is returned.
*
* @param {string} name the connection name
* @returns {Observable<String>}
*/
public isValidName( name: string ): Observable< any > {
if ( !name || name.length === 0 ) {
return Observable.of( "Connection name cannot be empty" );
}
const url = ConnectionService.nameValidationUrl + encodeURIComponent( name );
return this.http.get( url, this.getAuthRequestOptions() )
.map(( response ) => {
if ( response.ok ) {
if ( response.text() ) {
return response.text();
}
return "";
} } )
.catch( ( error ) => this.handleError( error ) );
}
/**
* Get the connection summaries from the komodo rest interface. The supplied parameters determine what portions
* of the ConnectionSummary are returned.
* - include-connection=true (include connection [default=true])
* - include-schema-status=true (include schema vdb status [default=false])
* @param {boolean} includeConnection 'true' to include connection
* @param {boolean} includeSchemaStatus 'true' to include connection schema status
* @returns {Observable<ConnectionSummary[]>}
*/
public getConnections(includeConnection: boolean, includeSchemaStatus: boolean): Observable<ConnectionSummary[]> {
// Build the url with parameters
const connectionsUrl = this.buildGetConnectionsUrl(includeConnection, includeSchemaStatus);
return this.http
.get(connectionsUrl, this.getAuthRequestOptions())
.map((response) => {
const connectionSummaries = response.json();
return connectionSummaries.map((connectionSummary) => ConnectionSummary.create( connectionSummary ));
})
.catch( ( error ) => this.handleError( error ) );
}
/**
* Deployes the connection server VDB if one does not already exists. If a server VDB has already been deployed, one
* can be redeployed. A schema can only be generated if it doesn't exist and there is already a deployed server VDB.
* @param {string} connectionName the name of the connection being refreshed
* @param {boolean} redeployServerVdb `true` if the server VDB should be deployed if one exists (defaults to `false`)
* @param {boolean} generateSchema `true` if the schema should be generated if one does not exist (defaults to `true`)
* @returns {Observable<boolean>}
*/
public refreshConnectionSchema(connectionName: string,
redeployServerVdb = false,
generateSchema = true): Observable<any> {
if ( !connectionName || connectionName.length === 0 ) {
return Observable.of( false );
}
const url = ConnectionService.refreshConnectionSchemaUrl + encodeURIComponent( connectionName );
// setup query parameters
const queryParams = {
params: {
"redeploy": redeployServerVdb,
"generate-schema": generateSchema
}
};
const options = new RequestOptions( queryParams );
return this.http
.post( url, this.getAuthRequestOptions().merge( options ) )
.map((response) => {
return response.ok;
})
.catch( ( error ) => this.handleError( error ) );
}
/**
* Bind a syndesis source via the komodo rest interface
* @param {string} syndesisSourceName
* @returns {Observable<boolean>}
*/
public bindSyndesisSource(syndesisSourceName: string): Observable<any> {
return this.http
.post(environment.komodoTeiidUrl + ConnectionsConstants.syndesisSourcesRootPath,
{ name: syndesisSourceName}, this.getAuthRequestOptions())
.map((response) => {
return response.ok;
})
.catch( ( error ) => this.handleError( error ) );
}
/**
* Delete a connection via the komodo rest interface
* @param {string} connectionId
* @returns {Observable<boolean>}
*/
public deleteConnection(connectionId: string): Observable<any> {
return this.http
.delete(environment.komodoWorkspaceUrl + ConnectionsConstants.connectionsRootPath + "/" + connectionId,
this.getAuthRequestOptions())
.map((response) => {
return response.ok;
})
.catch( ( error ) => this.handleError( error ) );
}
/**
* Get the connection types from the komodo rest interface
* @returns {ConnectionType[]}
*/
public getConnectionTypes(): ConnectionType[] {
const connectionTypes: ConnectionType[] = [];
const connType1: ConnectionType = new ConnectionType();
connType1.setName(ConnectionsConstants.connectionType_postgresql);
connType1.setDescription(ConnectionsConstants.connectionTypeDescription_postgresql);
const connType2: ConnectionType = new ConnectionType();
connType2.setName(ConnectionsConstants.connectionType_mysql);
connType2.setDescription(ConnectionsConstants.connectionTypeDescription_mysql);
const connType3: ConnectionType = new ConnectionType();
connType3.setName(ConnectionsConstants.connectionType_mongodb);
connType3.setDescription(ConnectionsConstants.connectionTypeDescription_mongodb);
const connType4: ConnectionType = new ConnectionType();
connType4.setName(ConnectionsConstants.connectionType_salesforce);
connType4.setDescription(ConnectionsConstants.connectionTypeDescription_salesforce);
// const connType4: ConnectionType = new ConnectionType();
// connType4.setName(ConnectionsConstants.connectionType_mariadb);
// connType4.setDescription(ConnectionsConstants.connectionTypeDescription_mariadb);
connectionTypes.push(connType1);
connectionTypes.push(connType2);
connectionTypes.push(connType3);
connectionTypes.push(connType4);
return connectionTypes;
}
/**
* Get the available SyndesisSources from the komodo rest interface
* @returns {Observable<ServiceCatalogSource[]>}
*/
public getAllSyndesisSources(): Observable<ServiceCatalogSource[]> {
return this.http
.get(environment.komodoTeiidUrl + ConnectionsConstants.syndesisSourcesRootPath, this.getAuthRequestOptions())
.map((response) => {
const catalogSources = response.json();
return catalogSources.map((catSource) => ServiceCatalogSource.create( catSource ));
})
.catch( ( error ) => this.handleError( error ) );
}
/**
* Get the schema for the specified connection. The connection must be ACTIVE, otherwise the schema
* will be empty.
* @param {string} connectionId the connection id
* @returns {Observable<SchemaNode[]>}
*/
public getConnectionSchema(connectionId: string): Observable<SchemaNode[]> {
return this.http
.get( environment.komodoWorkspaceUrl + ConnectionsConstants.connectionsRootPath
+ "/" + connectionId + "/schema", this.getAuthRequestOptions())
.map((response) => {
const schemaNodes = response.json();
return schemaNodes.map((schemaNode) => SchemaNode.create( schemaNode ));
})
.catch( ( error ) => this.handleError( error ) );
}
/**
* Get the columns for the specified connection and table. The connection must be ACTIVE, otherwise the schema
* will be empty.
* @param {string} connectionId the connection id
* @param {string} tableOption the table option (eg. schema=public/table=customer)
* @returns {Observable<Column[]>}
*/
public getConnectionSchemaColumns(connectionId: string, tableOption: string): Observable<Column[]> {
// setup query parameters
const tableParam = {
params: {
"tableOption": tableOption
}
};
const options = new RequestOptions( tableParam );
return this.http
.get( environment.komodoWorkspaceUrl + ConnectionsConstants.connectionsRootPath
+ "/" + connectionId + "/schema-columns", this.getAuthRequestOptions().merge(options))
.map((response) => {
const columns = response.json();
return columns.map((column) => Column.create( column ));
})
.catch( ( error ) => this.handleError( error ) );
}
/**
* Create a connection in the Komodo repo - also binds the specified syndesisSource
* @param {NewConnection} connection the connection object
* @returns {Observable<boolean>}
*/
public createAndBindConnection(connection: NewConnection): Observable<any> {
return this.http
.post(environment.komodoWorkspaceUrl + ConnectionsConstants.connectionsRootPath + "/" + connection.getName(),
connection, this.getAuthRequestOptions())
.map((response) => {
return response.ok;
})
.catch( ( error ) => this.handleError( error ) );
}
/**
* Update a connection in the Komodo repo - also binds the specified syndesisSource.
* @param {NewConnection} connection the connection object
* @returns {Observable<boolean>}
*/
public updateAndBindConnection(connection: NewConnection): Observable<any> {
return this.http
.put(environment.komodoWorkspaceUrl + ConnectionsConstants.connectionsRootPath + "/" + connection.getName(),
connection, this.getAuthRequestOptions())
.map((response) => {
return response.ok;
})
.catch( ( error ) => this.handleError( error ) );
}
/**
* Creates a workspace Connection, binds it to the specified syndesisSource, and initiates
* a refresh of the connection schema.
* @param {NewConnection} connection the connection object
* @returns {Observable<boolean>}
*/
public createDeployConnection(connection: NewConnection): Observable<boolean> {
return this.createAndBindConnection(connection)
.flatMap((res) => this.refreshConnectionSchema(connection.getName()))
.flatMap((res) => this.refreshPreviewVdb(VdbsConstants.PREVIEW_VDB_NAME));
}
/**
* Updates a workspace Connection, binds it to the specified syndesisSource, and initiates
* a refresh of the connection schema.
* @param {NewConnection} connection the connection object
* @returns {Observable<boolean>}
*/
public updateDeployConnection(connection: NewConnection): Observable<boolean> {
return this.updateAndBindConnection(connection)
.flatMap((res) => this.refreshConnectionSchema(connection.getName()))
.flatMap((res) => this.refreshPreviewVdb(VdbsConstants.PREVIEW_VDB_NAME));
}
/**
* Delete the repo Connection VDB (if it exists) and undeploy the Connection VDB
* (if exists)
* @param {string} connectionId
* @returns {Observable<boolean>}
*/
public deleteUndeployConnectionVdb(connectionId: string): Observable<boolean> {
const vdbName = connectionId + "BtlConn";
return this.vdbService.deleteVdbIfFound(vdbName)
.flatMap((res) => this.vdbService.undeployVdb(vdbName))
.flatMap((res) => this.refreshPreviewVdb(VdbsConstants.PREVIEW_VDB_NAME));
}
/**
* Update the preview VDB and re-deploy it if needed.
* @param {string} vdbName
* @returns {Observable<boolean>}
*/
public refreshPreviewVdb(vdbName: string): Observable<any> {
const url = environment.komodoTeiidUrl + "/refreshPreviewVdb/" + vdbName;
return this.http
.post(url, this.getAuthRequestOptions())
.map((response) => {
return response.ok;
})
.catch( ( error ) => this.handleError( error ) );
}
/**
* Updates the current Connecton VDB states - triggers update to be broadcast to interested components
*/
public updateConnectionSchemaStates(): void {
const self = this;
this.getConnections(false, true)
.subscribe(
(connectionSummaries) => {
self.cachedConnectionStatuses = self.createConnectionStatusMap(connectionSummaries);
this.notifierService.sendConnectionStatusMap(self.cachedConnectionStatuses);
},
(error) => {
// On error, broadcast the cached states
this.notifierService.sendConnectionStatusMap(self.cachedConnectionStatuses);
}
);
}
/**
* Polls the server and sends Connection schema state updates at the specified interval
* @param {number} pollIntervalSec the interval (sec) between polling attempts
*/
public pollConnectionSchemaStatus(pollIntervalSec: number): void {
const pollIntervalMillis = pollIntervalSec * 1000;
const self = this;
// start the timer
const stopWatch = timer(500, pollIntervalMillis);
this.updatesSubscription = stopWatch.subscribe((t: any) => {
self.updateConnectionSchemaStates();
});
}
/**
* Build the getConnection Url based on the supplied parameters.
* @param {boolean} includeConnection 'true' to include connection, 'false' to omit
* @param {boolean} includeSchemaStatus 'true' to include connection schema status, 'false' to omit
*/
private buildGetConnectionsUrl(includeConnection: boolean, includeSchemaStatus: boolean): string {
// Base getConnections service url
const connectionsUrl = environment.komodoWorkspaceUrl + ConnectionsConstants.connectionsRootPath;
// Additional parameters
const urlParams = "?" + ConnectionsConstants.includeConnectionParameter + "=" + String(includeConnection) +
"&" + ConnectionsConstants.includeSchemaStatusParameter + "=" + String(includeSchemaStatus);
return connectionsUrl + urlParams;
}
/*
* Creates a Map of connection name to ConnectionStatus
* @param {ConnectionSummary[]} connectionSummaries the array of ConnectionSummary objects
* @returns {Map<string,ConnectionStatus>} the map of connection name to ConnectionStatus
*/
private createConnectionStatusMap(connectionSummaries: ConnectionSummary[]): Map<string, ConnectionStatus> {
const connStatusMap: Map<string, ConnectionStatus> = new Map<string, ConnectionStatus>();
// For each connection, determine its status. Add the map entry
for ( const connectionSummary of connectionSummaries ) {
if (connectionSummary.hasStatus()) {
const status = connectionSummary.getStatus();
const connName = status.getConnectionName();
connStatusMap.set( connName, status );
}
}
return connStatusMap;
}
}