-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathaws_client.ts
More file actions
195 lines (159 loc) · 7.43 KB
/
aws_client.ts
File metadata and controls
195 lines (159 loc) · 7.43 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
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
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 { PluginServiceManagerContainer } from "./plugin_service_manager_container";
import { PluginService, PluginServiceImpl } from "./plugin_service";
import { DatabaseDialect, DatabaseType } from "./database_dialect/database_dialect";
import { ConnectionUrlParser } from "./utils/connection_url_parser";
import { HostListProvider } from "./host_list_provider/host_list_provider";
import { PluginManager } from "./plugin_manager";
import pkgStream from "stream";
import { ClientWrapper } from "./client_wrapper";
import { ConnectionProviderManager } from "./connection_provider_manager";
import { DefaultTelemetryFactory } from "./utils/telemetry/default_telemetry_factory";
import { TelemetryFactory } from "./utils/telemetry/telemetry_factory";
import { DriverDialect } from "./driver_dialect/driver_dialect";
import { WrapperProperties } from "./wrapper_property";
import { DriverConfigurationProfiles } from "./profile/driver_configuration_profiles";
import { ConfigurationProfile } from "./profile/configuration_profile";
import { AwsWrapperError, TransactionIsolationLevel, ConnectionProvider } from "./";
import { Messages } from "./utils/messages";
import { HostListProviderService } from "./host_list_provider_service";
import { SessionStateClient } from "./session_state_client";
import { DriverConnectionProvider } from "./driver_connection_provider";
const { EventEmitter } = pkgStream;
export abstract class AwsClient extends EventEmitter implements SessionStateClient {
private _defaultPort: number = -1;
protected telemetryFactory: TelemetryFactory;
protected pluginManager: PluginManager;
protected pluginService: PluginService;
protected isConnected: boolean = false;
protected _connectionUrlParser: ConnectionUrlParser;
protected _configurationProfile: ConfigurationProfile | null = null;
readonly properties: Map<string, any>;
config: any;
targetClient?: ClientWrapper;
protected constructor(
config: any,
dbType: DatabaseType,
knownDialectsByCode: Map<string, DatabaseDialect>,
parser: ConnectionUrlParser,
driverDialect: DriverDialect,
connectionProvider?: ConnectionProvider
) {
super();
this.config = config;
this._connectionUrlParser = parser;
this.properties = new Map<string, any>(Object.entries(config));
const profileName = WrapperProperties.PROFILE_NAME.get(this.properties);
if (profileName && profileName.length > 0) {
this._configurationProfile = DriverConfigurationProfiles.getProfileConfiguration(profileName);
if (this._configurationProfile) {
const profileProperties = this._configurationProfile.getProperties();
if (profileProperties) {
for (const key of profileProperties.keys()) {
if (this.properties.has(key)) {
// Setting defined by a user has priority over property in configuration profile.
continue;
}
this.properties.set(key, profileProperties.get(key));
}
const connectionProvider = WrapperProperties.CONNECTION_PROVIDER.get(this.properties);
if (!connectionProvider) {
WrapperProperties.CONNECTION_PROVIDER.set(this.properties, this._configurationProfile.getAwsCredentialProvider());
}
const customAwsCredentialProvider = WrapperProperties.CUSTOM_AWS_CREDENTIAL_PROVIDER_HANDLER.get(this.properties);
if (!customAwsCredentialProvider) {
WrapperProperties.CUSTOM_AWS_CREDENTIAL_PROVIDER_HANDLER.set(this.properties, this._configurationProfile.getAwsCredentialProvider());
}
const customDatabaseDialect = WrapperProperties.CUSTOM_DATABASE_DIALECT.get(this.properties);
if (!customDatabaseDialect) {
WrapperProperties.CUSTOM_DATABASE_DIALECT.set(this.properties, this._configurationProfile.getDatabaseDialect());
}
}
} else {
throw new AwsWrapperError(Messages.get("AwsClient.configurationProfileNotFound", profileName));
}
}
this.telemetryFactory = new DefaultTelemetryFactory(this.properties);
const container = new PluginServiceManagerContainer();
this.pluginService = new PluginServiceImpl(
container,
this,
dbType,
knownDialectsByCode,
this.properties,
this._configurationProfile?.getDriverDialect() ?? driverDialect
);
this.pluginManager = new PluginManager(
container,
this.properties,
new ConnectionProviderManager(connectionProvider ?? new DriverConnectionProvider(), WrapperProperties.CONNECTION_PROVIDER.get(this.properties)),
this.telemetryFactory
);
}
private async setup() {
await this.telemetryFactory.init();
await this.pluginManager.init(this._configurationProfile);
}
protected async internalConnect() {
await this.setup();
const hostListProvider: HostListProvider = this.pluginService
.getDialect()
.getHostListProvider(this.properties, this.properties.get("host"), <HostListProviderService>(<unknown>this.pluginService));
this.pluginService.setHostListProvider(hostListProvider);
await this.pluginService.refreshHostList();
const initialHostInfo = this.pluginService.getInitialConnectionHostInfo();
if (initialHostInfo != null) {
await this.pluginManager.initHostProvider(initialHostInfo, this.properties, <HostListProviderService>(<unknown>this.pluginService));
await this.pluginService.refreshHostList();
}
}
protected async internalPostConnect() {
const info = this.pluginService.getCurrentHostInfo();
if (info != null) {
await this.pluginService.refreshHostList();
}
this.isConnected = true;
}
get defaultPort(): number {
return this._defaultPort;
}
get connectionUrlParser(): ConnectionUrlParser {
return this._connectionUrlParser;
}
abstract setReadOnly(readOnly: boolean): Promise<any | void>;
abstract isReadOnly(): boolean | undefined;
abstract setAutoCommit(autoCommit: boolean): Promise<any | void>;
abstract getAutoCommit(): boolean | undefined;
abstract setTransactionIsolation(level: TransactionIsolationLevel): Promise<any | void>;
abstract getTransactionIsolation(): TransactionIsolationLevel | undefined;
abstract setSchema(schema: any): Promise<any | void>;
abstract getSchema(): string | undefined;
abstract setCatalog(catalog: string): Promise<any | void>;
abstract getCatalog(): string | undefined;
abstract end(): Promise<any>;
abstract connect(): Promise<any>;
abstract rollback(): Promise<any>;
unwrapPlugin<T>(iface: new (...args: any[]) => T): T | null {
return this.pluginManager.unwrapPlugin(iface);
}
async isValid(): Promise<boolean> {
if (!this.targetClient) {
return Promise.resolve(false);
}
return await this.pluginService.isClientValid(this.targetClient);
}
getPluginInstance<T>(iface: any): T {
return this.pluginManager.getPluginInstance(iface);
}
}