Introduce connection API (#598)

Including getCurrentConnection, getActiveConnections, and getCredentials
This commit is contained in:
Matt Irvine
2018-02-06 15:56:49 -08:00
committed by GitHub
parent f5aa49ebb9
commit 3df522536f
15 changed files with 264 additions and 3 deletions

View File

@@ -0,0 +1,33 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { IThreadService } from 'vs/workbench/services/thread/common/threadService';
import { ExtHostConnectionManagementShape, SqlMainContext, MainThreadConnectionManagementShape } from 'sql/workbench/api/node/sqlExtHost.protocol';
import * as data from 'data';
export class ExtHostConnectionManagement extends ExtHostConnectionManagementShape {
private _proxy: MainThreadConnectionManagementShape;
constructor(
threadService: IThreadService
) {
super();
this._proxy = threadService.get(SqlMainContext.MainThreadConnectionManagement);
}
public $getActiveConnections(): Thenable<data.connection.Connection[]> {
return this._proxy.$getActiveConnections();
}
public $getCurrentConnection(): Thenable<data.connection.Connection> {
return this._proxy.$getCurrentConnection();
}
public $getCredentials(connectionId: string): Thenable<{ [name: string]: string}> {
return this._proxy.$getCredentials(connectionId);
}
}

View File

@@ -0,0 +1,64 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { SqlExtHostContext, SqlMainContext, ExtHostConnectionManagementShape, MainThreadConnectionManagementShape } from 'sql/workbench/api/node/sqlExtHost.protocol';
import * as data from 'data';
import { IExtHostContext } from 'vs/workbench/api/node/extHost.protocol';
import { extHostNamedCustomer } from 'vs/workbench/api/electron-browser/extHostCustomers';
import { IConnectionManagementService } from 'sql/parts/connection/common/connectionManagement';
import { IObjectExplorerService } from 'sql/parts/registeredServer/common/objectExplorerService';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
import * as TaskUtilities from 'sql/workbench/common/taskUtilities';
import { IConnectionProfile } from 'sql/parts/connection/common/interfaces';
import { dispose, IDisposable } from 'vs/base/common/lifecycle';
@extHostNamedCustomer(SqlMainContext.MainThreadConnectionManagement)
export class MainThreadConnectionManagement implements MainThreadConnectionManagementShape {
private _proxy: ExtHostConnectionManagementShape;
private _toDispose: IDisposable[];
constructor(
extHostContext: IExtHostContext,
@IConnectionManagementService private _connectionManagementService: IConnectionManagementService,
@IObjectExplorerService private _objectExplorerService: IObjectExplorerService,
@IWorkbenchEditorService private _workbenchEditorService: IWorkbenchEditorService
) {
if (extHostContext) {
this._proxy = extHostContext.get(SqlExtHostContext.ExtHostConnectionManagement);
}
this._toDispose = [];
}
public dispose(): void {
this._toDispose = dispose(this._toDispose);
}
public $getActiveConnections(): Thenable<data.connection.Connection[]> {
return Promise.resolve(this._connectionManagementService.getActiveConnections().map(profile => this.convertConnection(profile)));
}
public $getCurrentConnection(): Thenable<data.connection.Connection> {
return Promise.resolve(this.convertConnection(TaskUtilities.getCurrentGlobalConnection(this._objectExplorerService, this._connectionManagementService, this._workbenchEditorService, true)));
}
public $getCredentials(connectionId: string): Thenable<{ [name: string]: string }> {
return Promise.resolve(this._connectionManagementService.getActiveConnectionCredentials(connectionId));
}
private convertConnection(profile: IConnectionProfile): data.connection.Connection {
if (!profile) {
return undefined;
}
profile = this._connectionManagementService.removeConnectionProfileCredentials(profile);
let connection: data.connection.Connection = {
providerName: profile.providerName,
connectionId: profile.id,
options: profile.options
};
return connection;
}
}

View File

@@ -28,6 +28,7 @@ import { ExtHostConfiguration } from 'vs/workbench/api/node/extHostConfiguration
import { ExtHostModalDialogs } from 'sql/workbench/api/node/extHostModalDialog';
import { ILogService } from 'vs/platform/log/common/log';
import { IExtensionApiFactory } from 'vs/workbench/api/node/extHost.api.impl';
import { ExtHostConnectionManagement } from 'sql/workbench/api/node/extHostConnectionManagement';
export interface ISqlExtensionApiFactory {
vsCodeFactory(extension: IExtensionDescription): typeof vscode;
@@ -49,6 +50,7 @@ export function createApiFactory(
// Addressable instances
const extHostAccountManagement = threadService.set(SqlExtHostContext.ExtHostAccountManagement, new ExtHostAccountManagement(threadService));
const extHostConnectionManagement = threadService.set(SqlExtHostContext.ExtHostConnectionManagement, new ExtHostConnectionManagement(threadService));
const extHostCredentialManagement = threadService.set(SqlExtHostContext.ExtHostCredentialManagement, new ExtHostCredentialManagement(threadService));
const extHostDataProvider = threadService.set(SqlExtHostContext.ExtHostDataProtocol, new ExtHostDataProtocol(threadService));
const extHostSerializationProvider = threadService.set(SqlExtHostContext.ExtHostSerializationProvider, new ExtHostSerializationProvider(threadService));
@@ -74,6 +76,19 @@ export function createApiFactory(
}
};
// namespace: connection
const connection: typeof data.connection = {
getActiveConnections(): Thenable<data.connection.Connection[]> {
return extHostConnectionManagement.$getActiveConnections();
},
getCurrentConnection(): Thenable<data.connection.Connection> {
return extHostConnectionManagement.$getCurrentConnection();
},
getCredentials(connectionId: string): Thenable<{ [name: string]: string }> {
return extHostConnectionManagement.$getCredentials(connectionId);
}
};
// namespace: credentials
const credentials: typeof data.credentials = {
registerProvider(provider: data.CredentialProvider): vscode.Disposable {
@@ -246,6 +261,7 @@ export function createApiFactory(
return {
accounts,
connection,
credentials,
resources,
serialization,

View File

@@ -10,6 +10,7 @@ import { Registry } from 'vs/platform/registry/common/platform';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
// --- SQL contributions
import 'sql/workbench/api/node/mainThreadConnectionManagement';
import 'sql/workbench/api/node/mainThreadCredentialManagement';
import 'sql/workbench/api/node/mainThreadDataProtocol';
import 'sql/workbench/api/node/mainThreadSerializationProvider';

View File

@@ -23,6 +23,8 @@ export abstract class ExtHostAccountManagementShape {
$refresh(handle: number, account: data.Account): Thenable<data.Account> { throw ni(); }
}
export abstract class ExtHostConnectionManagementShape { }
export abstract class ExtHostDataProtocolShape {
/**
@@ -389,6 +391,12 @@ export interface MainThreadDataProtocolShape extends IDisposable {
$onEditSessionReady(handle: number, ownerUri: string, success: boolean, message: string);
}
export interface MainThreadConnectionManagementShape extends IDisposable {
$getActiveConnections(): Thenable<data.connection.Connection[]>;
$getCurrentConnection(): Thenable<data.connection.Connection>;
$getCredentials(connectionId: string): Thenable<{ [name: string]: string }>;
}
export interface MainThreadCredentialManagementShape extends IDisposable {
$registerCredentialProvider(handle: number): TPromise<any>;
$unregisterCredentialProvider(handle: number): TPromise<any>;
@@ -406,6 +414,7 @@ function ni() { return new Error('Not implemented'); }
export const SqlMainContext = {
// SQL entries
MainThreadAccountManagement: createMainId<MainThreadAccountManagementShape>('MainThreadAccountManagement'),
MainThreadConnectionManagement: createMainId<MainThreadConnectionManagementShape>('MainThreadConnectionManagement'),
MainThreadCredentialManagement: createMainId<MainThreadCredentialManagementShape>('MainThreadCredentialManagement'),
MainThreadDataProtocol: createMainId<MainThreadDataProtocolShape>('MainThreadDataProtocol'),
MainThreadSerializationProvider: createMainId<MainThreadSerializationProviderShape>('MainThreadSerializationProvider'),
@@ -415,6 +424,7 @@ export const SqlMainContext = {
export const SqlExtHostContext = {
ExtHostAccountManagement: createExtId<ExtHostAccountManagementShape>('ExtHostAccountManagement'),
ExtHostConnectionManagement: createExtId<ExtHostConnectionManagementShape>('ExtHostConnectionManagement'),
ExtHostCredentialManagement: createExtId<ExtHostCredentialManagementShape>('ExtHostCredentialManagement'),
ExtHostDataProtocol: createExtId<ExtHostDataProtocolShape>('ExtHostDataProtocol'),
ExtHostSerializationProvider: createExtId<ExtHostSerializationProviderShape>('ExtHostSerializationProvider'),