mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-08 17:24:01 -05:00
Add ADS Windows support extension with LaunchSsmsDialog command (#4248)
* ADS Windows support extension with LaunchSsmsDialog command * Update readme * Fix spacing * Update download with new file location and name * Update SsmsMin package with bits from latest RC build and addressed some comments. * Update extension name. Add Context menu extension for launching server properties dialog. Remove params interface from public API * Rename folder and update README * Correct README title * Fix a few issues and clean up some stuff. * Update to azdata namespace * Refactor to use async/await and add some more telemetry * Add .bat for running extension tests (currently only Notebook) and set up launch.json with 2 new launch configs for running & debugging extension tests. * Rename files to make it clear these aren't the integration tests * Update launch.config too * Fix spacing and missed file name update * Fix some bugs in buildSsmsMinCommandArgs and add unit tests
This commit is contained in:
12
extensions/admin-tool-ext-win/src/config.json
Normal file
12
extensions/admin-tool-ext-win/src/config.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"downloadUrl": "https://sqlopsextensions.blob.core.windows.net/tools/ssmsmin/{#version#}/{#fileName#}",
|
||||
"version": "15.0.18092.0",
|
||||
"downloadFileNames": {
|
||||
"Windows_64": "SsmsMin-15.0.18092.0-win-x64.zip",
|
||||
"Windows_86": "SsmsMin-15.0.18092.0-win-x86.zip"
|
||||
},
|
||||
"installDirectory": "ssmsmin/{#platform#}/{#version#}",
|
||||
"executableFiles": [
|
||||
"SsmsMin.exe"
|
||||
]
|
||||
}
|
||||
144
extensions/admin-tool-ext-win/src/main.ts
Normal file
144
extensions/admin-tool-ext-win/src/main.ts
Normal file
@@ -0,0 +1,144 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* 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 * as nls from 'vscode-nls';
|
||||
import * as path from 'path';
|
||||
import * as shelljs from 'shelljs';
|
||||
import * as azdata from 'azdata';
|
||||
import * as vscode from 'vscode';
|
||||
import { IConfig, ServerProvider } from 'service-downloader';
|
||||
import { Telemetry } from './telemetry';
|
||||
import * as utils from './utils';
|
||||
|
||||
const baseConfig = require('./config.json');
|
||||
const localize = nls.loadMessageBundle();
|
||||
let exePath:string;
|
||||
|
||||
// Params to pass to SsmsMin.exe, only an action and server are required - the rest are optional based on the
|
||||
// action used. Exported for use in testing.
|
||||
export interface LaunchSsmsDialogParams {
|
||||
action: string;
|
||||
server: string;
|
||||
database?: string;
|
||||
user?: string;
|
||||
password?: string;
|
||||
useAad?: boolean;
|
||||
urn?: string;
|
||||
}
|
||||
|
||||
export async function activate(context: vscode.ExtensionContext): Promise<void> {
|
||||
Telemetry.sendTelemetryEvent('startup/ExtensionActivated');
|
||||
|
||||
// Only supported on Win32 currently, display error message if not that until extensions are able to block install
|
||||
// based on conditions
|
||||
if(process.platform === 'win32') {
|
||||
let config: IConfig = JSON.parse(JSON.stringify(baseConfig));
|
||||
config.installDirectory = path.join(context.extensionPath, config.installDirectory);
|
||||
config.proxy = utils.getConfiguration('http').get('proxy');
|
||||
config.strictSSL = utils.getConfiguration('http').get('proxyStrictSSL') || true;
|
||||
|
||||
const serverdownloader = new ServerProvider(config);
|
||||
const installationStart = Date.now();
|
||||
|
||||
try {
|
||||
let downloadedExePath = await serverdownloader.getOrDownloadServer();
|
||||
const installationComplete = Date.now();
|
||||
|
||||
// Don't register the command if we couldn't find the EXE since it won't be able to do anything
|
||||
if(downloadedExePath) {
|
||||
exePath = downloadedExePath;
|
||||
} else {
|
||||
throw new Error('Could not find SsmsMin.exe after downloading');
|
||||
}
|
||||
// Add the command now that we have the exePath to run the tool with
|
||||
context.subscriptions.push(
|
||||
vscode.commands.registerCommand('adminToolExtWin.launchSsmsServerPropertiesDialog', handleLaunchSsmsServerPropertiesDialogCommand));
|
||||
|
||||
Telemetry.sendTelemetryEvent('startup/ExtensionStarted', {
|
||||
installationTime: String(installationComplete - installationStart),
|
||||
beginningTimestamp: String(installationStart)
|
||||
});
|
||||
}
|
||||
catch(err) {
|
||||
Telemetry.sendTelemetryEvent('startup/ExtensionInitializationFailed', {
|
||||
error: err
|
||||
});
|
||||
}
|
||||
} else {
|
||||
vscode.window.showErrorMessage(localize('adminToolExtWin.onlySupportedOnWindows', 'The Admin Tool Extension is only supported on Windows platforms.'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for command to launch SSMS Server Properties dialog
|
||||
* @param connectionId The connection context from the command
|
||||
*/
|
||||
function handleLaunchSsmsServerPropertiesDialogCommand(connectionContext?: azdata.ObjectExplorerContext) {
|
||||
if(connectionContext && connectionContext.connectionProfile) {
|
||||
launchSsmsDialog(
|
||||
/*action*/'sqla:Properties@Microsoft.SqlServer.Management.Smo.Server',
|
||||
/*connectionProfile*/connectionContext.connectionProfile);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Launches SsmsMin with parameters from the specified connection
|
||||
* @param action The action to launch
|
||||
* @param params The params used to construct the command
|
||||
* @param urn The URN to pass to SsmsMin
|
||||
*/
|
||||
function launchSsmsDialog(action:string, connectionProfile: azdata.IConnectionProfile, urn?:string) {
|
||||
if(!exePath) {
|
||||
vscode.window.showErrorMessage(localize('adminToolExtWin.noExeError', 'Unable to find SsmsMin.exe.'));
|
||||
return;
|
||||
}
|
||||
|
||||
Telemetry.sendTelemetryEvent('LaunchSsmsDialog', { 'action': action});
|
||||
|
||||
let params:LaunchSsmsDialogParams = {
|
||||
action:action,
|
||||
server:connectionProfile.serverName,
|
||||
database:connectionProfile.databaseName,
|
||||
password:connectionProfile.password,
|
||||
user:connectionProfile.userName,
|
||||
useAad:connectionProfile.authenticationType === 'AzureMFA',
|
||||
urn: urn};
|
||||
let args = buildSsmsMinCommandArgs(params);
|
||||
|
||||
// This will be an async call since we pass in the callback
|
||||
var proc = shelljs.exec(
|
||||
/*command*/`"${exePath}" ${args}`,
|
||||
/*options*/'',
|
||||
(code, stdout, stderr) => {
|
||||
Telemetry.sendTelemetryEvent('LaunchSsmsDialogResult', {
|
||||
'action': params.action,
|
||||
'returnCode': code,
|
||||
'error': stderr
|
||||
});
|
||||
|
||||
// If we're not using AAD the tool prompts for a password on stdin
|
||||
if(params.useAad !== true) {
|
||||
proc.stdin.end(params.password ? params.password : '');
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the command arguments to pass to SsmsMin.exe. Values are expected to be escaped correctly
|
||||
* already per their - they will be further escaped * for command-line usage but no additional
|
||||
* escaping will occur.
|
||||
* @param params The params used to build up the command parameter string
|
||||
*/
|
||||
export function buildSsmsMinCommandArgs(params:LaunchSsmsDialogParams): string {
|
||||
return `${params.action ? '-a "' + params.action.replace(/"/g, '\\"') + '"' : ''}\
|
||||
${params.server ? ' -S "' + params.server.replace(/"/g, '\\"') + '"' : ''}\
|
||||
${params.database ? ' -D "' + params.database.replace(/"/g, '\\"') + '"' : ''}\
|
||||
${params.useAad !== true && params.user ? ' -U "' + params.user.replace(/"/g, '\\"') + '"' : ''}\
|
||||
${params.useAad === true ? ' -G': ''}\
|
||||
${params.urn ? ' -u "' + params.urn.replace(/"/g, '\\"') + '"' : ''}`;
|
||||
}
|
||||
135
extensions/admin-tool-ext-win/src/telemetry.ts
Normal file
135
extensions/admin-tool-ext-win/src/telemetry.ts
Normal file
@@ -0,0 +1,135 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* 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 * as vscode from 'vscode';
|
||||
import TelemetryReporter from 'vscode-extension-telemetry';
|
||||
import { PlatformInformation } from 'service-downloader/out/platform';
|
||||
|
||||
import * as Utils from './utils';
|
||||
|
||||
const packageJson = require('../package.json');
|
||||
|
||||
export interface ITelemetryEventProperties {
|
||||
[key: string]: string;
|
||||
}
|
||||
|
||||
export interface ITelemetryEventMeasures {
|
||||
[key: string]: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters error paths to only include source files. Exported to support testing
|
||||
*/
|
||||
export function filterErrorPath(line: string): string {
|
||||
if (line) {
|
||||
let values: string[] = line.split('/out/');
|
||||
if (values.length <= 1) {
|
||||
// Didn't match expected format
|
||||
return line;
|
||||
} else {
|
||||
return values[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class Telemetry {
|
||||
private static reporter: TelemetryReporter;
|
||||
private static userId: string;
|
||||
private static platformInformation: PlatformInformation;
|
||||
private static disabled: boolean;
|
||||
|
||||
public static getPlatformInformation(): Promise<PlatformInformation> {
|
||||
if (this.platformInformation) {
|
||||
return Promise.resolve(this.platformInformation);
|
||||
} else {
|
||||
return new Promise<PlatformInformation>(resolve => {
|
||||
PlatformInformation.getCurrent().then(info => {
|
||||
this.platformInformation = info;
|
||||
resolve(this.platformInformation);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable telemetry reporting
|
||||
*/
|
||||
public static disable(): void {
|
||||
this.disabled = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the telemetry reporter for use.
|
||||
*/
|
||||
public static initialize(): void {
|
||||
if (typeof this.reporter === 'undefined') {
|
||||
// Check if the user has opted out of telemetry
|
||||
if (!vscode.workspace.getConfiguration('telemetry').get<boolean>('enableTelemetry', true)) {
|
||||
this.disable();
|
||||
return;
|
||||
}
|
||||
|
||||
let packageInfo = Utils.getPackageInfo(packageJson);
|
||||
this.reporter = new TelemetryReporter(packageInfo.name, packageInfo.version, packageInfo.aiKey);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a telemetry event for an exception
|
||||
*/
|
||||
public static sendTelemetryEventForException(
|
||||
err: any, methodName: string, extensionConfigName: string): void {
|
||||
try {
|
||||
let stackArray: string[];
|
||||
let firstLine: string = '';
|
||||
if (err !== undefined && err.stack !== undefined) {
|
||||
stackArray = err.stack.split('\n');
|
||||
if (stackArray !== undefined && stackArray.length >= 2) {
|
||||
firstLine = stackArray[1]; // The first line is the error message and we don't want to send that telemetry event
|
||||
firstLine = filterErrorPath(firstLine);
|
||||
}
|
||||
}
|
||||
|
||||
// Only adding the method name and the fist line of the stack trace. We don't add the error message because it might have PII
|
||||
this.sendTelemetryEvent('Exception', { methodName: methodName, errorLine: firstLine });
|
||||
} catch (telemetryErr) {
|
||||
// If sending telemetry event fails ignore it so it won't break the extension
|
||||
console.error('Failed to send telemetry event. error: ' + telemetryErr, extensionConfigName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a telemetry event using application insights
|
||||
*/
|
||||
public static sendTelemetryEvent(
|
||||
eventName: string,
|
||||
properties?: ITelemetryEventProperties,
|
||||
measures?: ITelemetryEventMeasures): void {
|
||||
|
||||
if (typeof this.disabled === 'undefined') {
|
||||
this.disabled = false;
|
||||
}
|
||||
|
||||
if (this.disabled || typeof (this.reporter) === 'undefined') {
|
||||
// Don't do anything if telemetry is disabled
|
||||
return;
|
||||
}
|
||||
|
||||
if (!properties || typeof properties === 'undefined') {
|
||||
properties = {};
|
||||
}
|
||||
|
||||
// Augment the properties structure with additional common properties before sending
|
||||
Promise.all([this.getPlatformInformation()]).then(() => {
|
||||
properties['distribution'] = (this.platformInformation && this.platformInformation.distribution) ?
|
||||
`${this.platformInformation.distribution.name}, ${this.platformInformation.distribution.version}` : '';
|
||||
|
||||
this.reporter.sendTelemetryEvent(eventName, properties, measures);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Telemetry.initialize();
|
||||
30
extensions/admin-tool-ext-win/src/test/index.ts
Normal file
30
extensions/admin-tool-ext-win/src/test/index.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
const path = require('path');
|
||||
const testRunner = require('vscode/lib/testrunner');
|
||||
|
||||
const suite = 'Database Admin Tool Extensions for Windows';
|
||||
|
||||
const options: any = {
|
||||
ui: 'bdd',
|
||||
useColors: true,
|
||||
timeout: 600000
|
||||
};
|
||||
|
||||
if (process.env.BUILD_ARTIFACTSTAGINGDIRECTORY) {
|
||||
options.reporter = 'mocha-multi-reporters';
|
||||
options.reporterOptions = {
|
||||
reporterEnabled: 'spec, mocha-junit-reporter',
|
||||
mochaJunitReporterReporterOptions: {
|
||||
testsuitesTitle: `${suite} ${process.platform}`,
|
||||
mochaFile: path.join(process.env.BUILD_ARTIFACTSTAGINGDIRECTORY, `test-results/${process.platform}-${suite.toLowerCase().replace(/[^\w]/g, '-')}-results.xml`)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
testRunner.configure(options);
|
||||
|
||||
export = testRunner;
|
||||
67
extensions/admin-tool-ext-win/src/test/utils.test.ts
Normal file
67
extensions/admin-tool-ext-win/src/test/utils.test.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* 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 * as should from 'should';
|
||||
import 'mocha';
|
||||
|
||||
import * as extensionMain from '../main';
|
||||
|
||||
describe('buildSsmsMinCommandArgs Method Tests', () => {
|
||||
it('Should be built correctly with all params and UseAAD as false', function (): void {
|
||||
let params: extensionMain.LaunchSsmsDialogParams = {
|
||||
action: 'myAction',
|
||||
server: 'myServer',
|
||||
database: 'myDatabase',
|
||||
user: 'user',
|
||||
password: 'password',
|
||||
useAad: false,
|
||||
urn: 'Server\\Database\\Table'
|
||||
};
|
||||
let args = extensionMain.buildSsmsMinCommandArgs(params);
|
||||
should(args).equal('-a "myAction" -S "myServer" -D "myDatabase" -U "user" -u "Server\\Database\\Table"');
|
||||
});
|
||||
|
||||
it('Should be built correctly with all params and UseAAD as true', function (): void {
|
||||
let params: extensionMain.LaunchSsmsDialogParams = {
|
||||
action: 'myAction',
|
||||
server: 'myServer',
|
||||
database: 'myDatabase',
|
||||
user: 'user',
|
||||
password: 'password',
|
||||
useAad: true,
|
||||
urn: 'Server\\Database\\Table'
|
||||
};
|
||||
let args = extensionMain.buildSsmsMinCommandArgs(params);
|
||||
// User is omitted since UseAAD is true
|
||||
should(args).equal('-a "myAction" -S "myServer" -D "myDatabase" -G -u "Server\\Database\\Table"');
|
||||
});
|
||||
|
||||
it('Should be built correctly and names escaped correctly', function (): void {
|
||||
let params: extensionMain.LaunchSsmsDialogParams = {
|
||||
action: 'myAction\'"/\\[]tricky',
|
||||
server: 'myServer\'"/\\[]tricky',
|
||||
database: 'myDatabase\'"/\\[]tricky',
|
||||
user: 'user\'"/\\[]tricky',
|
||||
password: 'password',
|
||||
useAad: true,
|
||||
urn: 'Server\\Database[\'myDatabase\'\'"/\\[]tricky\']\\Table["myTable\'""/\\[]tricky"]'
|
||||
};
|
||||
let args = extensionMain.buildSsmsMinCommandArgs(params);
|
||||
// User is omitted since UseAAD is true
|
||||
should(args).equal('-a "myAction\'\\"/\\[]tricky" -S "myServer\'\\"/\\[]tricky" -D "myDatabase\'\\"/\\[]tricky" -G -u "Server\\Database[\'myDatabase\'\'\\"/\\[]tricky\']\\Table[\\"myTable\'\\"\\"/\\[]tricky\\"]"');
|
||||
});
|
||||
|
||||
it('Should be built correctly with only action and server', function (): void {
|
||||
|
||||
let params: extensionMain.LaunchSsmsDialogParams = {
|
||||
action: 'myAction',
|
||||
server: 'myServer'
|
||||
};
|
||||
let args = extensionMain.buildSsmsMinCommandArgs(params);
|
||||
should(args).equal('-a "myAction" -S "myServer"');
|
||||
});
|
||||
});
|
||||
9
extensions/admin-tool-ext-win/src/typings/ref.d.ts
vendored
Normal file
9
extensions/admin-tool-ext-win/src/typings/ref.d.ts
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
/// <reference path='../../../../src/sql/azdata.d.ts'/>
|
||||
/// <reference path='../../../../src/sql/azdata.proposed.d.ts'/>
|
||||
/// <reference path='../../../../src/vs/vscode.d.ts'/>
|
||||
/// <reference types='@types/node'/>
|
||||
42
extensions/admin-tool-ext-win/src/utils.ts
Normal file
42
extensions/admin-tool-ext-win/src/utils.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* 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 * as vscode from 'vscode';
|
||||
|
||||
export interface IPackageInfo {
|
||||
name: string;
|
||||
version: string;
|
||||
aiKey: string;
|
||||
}
|
||||
|
||||
export function getPackageInfo(packageJson: any): IPackageInfo {
|
||||
if (packageJson) {
|
||||
return {
|
||||
name: packageJson.name,
|
||||
version: packageJson.version,
|
||||
aiKey: packageJson.aiKey
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the configuration for a extensionName
|
||||
* @param extensionName The string name of the extension to get the configuration for
|
||||
* @param resource The optional URI, as a URI object or a string, to use to get resource-scoped configurations
|
||||
*/
|
||||
export function getConfiguration(extensionName?: string, resource?: vscode.Uri | string): vscode.WorkspaceConfiguration {
|
||||
if (typeof resource === 'string') {
|
||||
try {
|
||||
resource = this.parseUri(resource);
|
||||
} catch (e) {
|
||||
resource = undefined;
|
||||
}
|
||||
} else if (!resource) {
|
||||
// Fix to avoid adding lots of errors to debug console. Expects a valid resource or null, not undefined
|
||||
resource = null;
|
||||
}
|
||||
return vscode.workspace.getConfiguration(extensionName, resource as vscode.Uri);
|
||||
}
|
||||
Reference in New Issue
Block a user