New UI for deploying SQL project to a new Azure server (#18833)

This commit is contained in:
Leila Lali
2022-04-29 15:39:21 -07:00
committed by GitHub
parent 14a63977c8
commit d95aff1d3c
17 changed files with 1453 additions and 367 deletions

View File

@@ -149,9 +149,16 @@ export const nameMustNotBeEmpty = localize('nameMustNotBeEmpty', "Name must not
// Deploy
export const SqlServerName = 'SQL server';
export const AzureSqlServerName = 'Azure SQL server';
export const AzureSqlLogicalServerName = 'Azure SQL logical server';
export const selectPublishOption = localize('selectPublishOption', "Select where to publish the project to");
export function publishToExistingServer(name: string) { return localize('publishToExistingServer', "Publish to an existing {0}", name); }
export function publishToDockerContainer(name: string) { return localize('publishToDockerContainer', "Publish to new {0} local development container", name); }
export const publishToNewAzureServer = localize('publishToNewAzureServer', "Publish to new Azure SQL logical server");
export const azureServerName = localize('azureServerName', "Azure SQL server name");
export const azureSubscription = localize('azureSubscription', "Azure subscription");
export const resourceGroup = localize('resourceGroup', "Resource group");
export const azureLocation = localize('location', "Location");
export const azureAccounts = localize('azureAccounts', "Azure accounts");
export function enterPortNumber(name: string) { return localize('enterPortNumber', "Enter {0} port number or press enter to use the default value", name); }
export function serverPortNumber(name: string) { return localize('serverPortNumber', "{0} port number", name); }
export function serverPassword(name: string) { return localize('serverPassword', "{0} admin password", name); }
@@ -160,6 +167,7 @@ export function baseDockerImage(name: string) { return localize('baseDockerImage
export const publishTo = localize('publishTo', "Publish Target");
export const enterConnectionStringEnvName = localize('enterConnectionStringEnvName', "Enter connection string environment variable name");
export const enterConnectionStringTemplate = localize('enterConnectionStringTemplate', "Enter connection string template");
export function enterUser(name: string) { return localize('enterUser', "Enter {0} admin user name", name); }
export function enterPassword(name: string) { return localize('enterPassword', "Enter {0} admin password", name); }
export function confirmPassword(name: string) { return localize('confirmPassword', "Confirm {0} admin password", name); }
export function selectBaseImage(name: string) { return localize('selectBaseImage', "Select the base {0} docker image", name); }
@@ -205,12 +213,16 @@ export const runningDockerMessage = localize('runningDockerMessage', "Running th
export function dockerNotRunningError(error: string) { return localize('dockerNotRunningError', "Failed to verify docker. Please make sure docker is installed and running. Error: '{0}'", error || ''); }
export const dockerContainerNotRunningErrorMessage = localize('dockerContainerNotRunningErrorMessage', "Docker container is not running");
export const dockerContainerFailedToRunErrorMessage = localize('dockerContainerFailedToRunErrorMessage', "Failed to run the docker container");
export const connectingToSqlServerMessage = localize('connectingToSqlServerOnDockerMessage', "Connecting to SQL Server");
export const connectingToSqlServerMessage = localize('connectingToSqlServerMessage', "Connecting to SQL Server");
export const serverCreated = localize('serverCreated', "Server created");
export const deployProjectFailedMessage = localize('deployProjectFailedMessage', "Failed to open a connection to the deployed database'");
export const containerAlreadyExistForProject = localize('containerAlreadyExistForProject', "Containers already exist for this project. Do you want to delete them before deploying a new one?");
export const checkoutOutputMessage = localize('checkoutOutputMessage', "Check output pane for more details");
export function creatingAzureSqlServer(name: string): string { return localize('creatingAzureSqlServer', "Creating Azure SQL Server '{0}' ...", name); }
export function azureSqlServerCreated(name: string): string { return localize('azureSqlServerCreated', "Azure SQL Server '{0}' created", name); }
export function taskFailedError(taskName: string, err: string): string { return localize('taskFailedError.error', "Failed to complete task '{0}'. Error: {1}", taskName, err); }
export function publishToContainerFailed(errorMessage: string) { return localize('publishToContainerFailed', "Failed to publish to container. {0}", errorMessage); }
export function publishToNewAzureServerFailed(errorMessage: string) { return localize('publishToNewAzureServerFailed', "Failed to publish to new Azure SQL server. {0}", errorMessage); }
export function deployAppSettingUpdateFailed(appSetting: string) { return localize('deployAppSettingUpdateFailed', "Failed to update app setting '{0}'", appSetting); }
export function deployAppSettingUpdating(appSetting: string) { return localize('deployAppSettingUpdating', "Updating app setting: '{0}'", appSetting); }
export function connectionFailedError(error: string) { return localize('connectionFailedError', "Connection failed error: '{0}'", error); }
@@ -220,7 +232,7 @@ export function retryWaitMessage(numberOfSeconds: number, name: string) { return
export function retryRunMessage(attemptNumber: number, numberOfAttempts: number, name: string) { return localize('retryRunMessage', "Running operation '{2}' Attempt {0} of {1}", attemptNumber, numberOfAttempts, name); }
export function retrySucceedMessage(name: string, result: string) { return localize('retrySucceedMessage', "Operation '{0}' completed successfully. Result: {1}", name, result); }
export function retryFailedMessage(name: string, result: string, error: string) { return localize('retryFailedMessage', "Operation '{0}' failed. Re-trying... Current Result: {1}. Error: '{2}'", name, result, error); }
export function retryMessage(name: string, error: string) { return localize('retryMessage', "Operation '{0}' failed. Re-trying... Error: '{1}'", name, error || ''); }
export function retryMessage(name: string, error: string) { return localize('retryMessage', "Operation '{0}' failed. Re-trying... Error: '{1}' ", name, error); }
// Add Database Reference dialog strings
@@ -492,6 +504,8 @@ export const integratedAuth = 'Integrated';
export const azureMfaAuth = 'AzureMFA';
export const sqlAuth = 'SqlAuth';
export const azureAddAccount = localize('azureAddAccount', "Add an Account...");
// Tree item types
export enum DatabaseProjectItemType {
project = 'databaseProject.itemType.project',
@@ -560,4 +574,5 @@ export function getTargetPlatformFromVersion(version: string): string {
export enum PublishTargetType {
existingServer = 'existingServer',
docker = 'docker',
newAzureServer = 'newAzureServer'
}

View File

@@ -39,5 +39,6 @@ export enum TelemetryActions {
createProjectFromDatabase = 'createProjectFromDatabase',
updateProjectFromDatabase = 'updateProjectFromDatabase',
publishToContainer = 'publishToContainer',
publishToNewAzureServer = 'publishToNewAzureServer',
generateProjectFromOpenApiSpec = 'generateProjectFromOpenApiSpec'
}

View File

@@ -307,6 +307,18 @@ export async function getVscodeMssqlApi(): Promise<vscodeMssql.IExtension> {
return ext.activate();
}
export type AzureResourceServiceFactory = () => Promise<vscodeMssql.IAzureResourceService>;
export async function defaultAzureResourceServiceFactory(): Promise<vscodeMssql.IAzureResourceService> {
const vscodeMssqlApi = await getVscodeMssqlApi();
return vscodeMssqlApi.azureResourceService;
}
export type AzureAccountServiceFactory = () => Promise<vscodeMssql.IAzureAccountService>;
export async function defaultAzureAccountServiceFactory(): Promise<vscodeMssql.IAzureAccountService> {
const vscodeMssqlApi = await getVscodeMssqlApi();
return vscodeMssqlApi.azureAccountService;
}
/*
* Returns the default deployment options from DacFx, filtered to appropriate options for the given project.
*/
@@ -441,7 +453,7 @@ export async function retry<T>(
}
} catch (err) {
outputChannel.appendLine(constants.retryMessage(name, err));
outputChannel.appendLine(constants.retryMessage(name, getErrorMessage(err)));
}
}

View File

@@ -38,16 +38,17 @@ import { TelemetryActions, TelemetryReporter, TelemetryViews } from '../common/t
import { IconPathHelper } from '../common/iconHelper';
import { DashboardData, PublishData, Status } from '../models/dashboardData/dashboardData';
import { getPublishDatabaseSettings, launchPublishTargetOption } from '../dialogs/publishDatabaseQuickpick';
import { launchPublishToDockerContainerQuickpick } from '../dialogs/deployDatabaseQuickpick';
import { launchCreateAzureServerQuickPick, launchPublishToDockerContainerQuickpick } from '../dialogs/deployDatabaseQuickpick';
import { DeployService } from '../models/deploy/deployService';
import { GenerateProjectFromOpenApiSpecOptions, SqlTargetPlatform } from 'sqldbproj';
import { AutorestHelper } from '../tools/autorestHelper';
import { createNewProjectFromDatabaseWithQuickpick } from '../dialogs/createProjectFromDatabaseQuickpick';
import { addDatabaseReferenceQuickpick } from '../dialogs/addDatabaseReferenceQuickpick';
import { IDeployProfile } from '../models/deploy/deployProfile';
import { ILocalDbDeployProfile, ISqlDbDeployProfile } from '../models/deploy/deployProfile';
import { EntryType, FileProjectEntry, IDatabaseReferenceProjectEntry, SqlProjectReferenceProjectEntry } from '../models/projectEntry';
import { UpdateProjectAction, UpdateProjectDataModel } from '../models/api/updateProject';
import { targetPlatformToAssets } from '../projectProvider/projectAssets';
import { AzureSqlClient } from '../models/deploy/azureSqlClient';
const maxTableLength = 10;
@@ -75,6 +76,7 @@ export class ProjectsController {
private buildInfo: DashboardData[] = [];
private publishInfo: PublishData[] = [];
private deployService: DeployService;
private azureSqlClient: AzureSqlClient;
private autorestHelper: AutorestHelper;
projFileWatchers = new Map<string, vscode.FileSystemWatcher>();
@@ -82,7 +84,8 @@ export class ProjectsController {
constructor(private _outputChannel: vscode.OutputChannel) {
this.netCoreTool = new NetCoreTool(this._outputChannel);
this.buildHelper = new BuildHelper();
this.deployService = new DeployService(this._outputChannel);
this.azureSqlClient = new AzureSqlClient();
this.deployService = new DeployService(this.azureSqlClient, this._outputChannel);
this.autorestHelper = new AutorestHelper(this._outputChannel);
}
@@ -287,12 +290,47 @@ export class ProjectsController {
}
}
/**
* Publishes a project to a new Azure server
* @param context a treeItem in a project's hierarchy, to be used to obtain a Project or the Project itself
* @param deployProfile deploy profile
*/
public async publishToNewAzureServer(context: Project | dataworkspace.WorkspaceTreeItem, deployProfile: ISqlDbDeployProfile): Promise<void> {
try {
TelemetryReporter.sendActionEvent(TelemetryViews.ProjectController, TelemetryActions.publishToNewAzureServer);
const project: Project = this.getProjectFromContext(context);
if (deployProfile?.deploySettings && deployProfile?.sqlDbSetting) {
void utils.showInfoMessageWithOutputChannel(constants.creatingAzureSqlServer(deployProfile?.sqlDbSetting?.serverName), this._outputChannel);
const connectionUri = await this.deployService.createNewAzureSqlServer(deployProfile);
if (connectionUri) {
deployProfile.deploySettings.connectionUri = connectionUri;
const publishResult = await this.publishOrScriptProject(project, deployProfile.deploySettings, true);
if (publishResult && publishResult.success) {
if (deployProfile.sqlDbSetting) {
// Connecting to the deployed db to add the profile to connection viewlet
await this.deployService.getConnection(deployProfile.sqlDbSetting, true, deployProfile.sqlDbSetting.dbName);
}
void vscode.window.showInformationMessage(constants.publishProjectSucceed);
} else {
void utils.showErrorMessageWithOutputChannel(constants.publishToNewAzureServerFailed, publishResult?.errorMessage || '', this._outputChannel);
}
} else {
void utils.showErrorMessageWithOutputChannel(constants.publishToNewAzureServerFailed, constants.deployProjectFailedMessage, this._outputChannel);
}
}
} catch (error) {
void utils.showErrorMessageWithOutputChannel(constants.publishToNewAzureServerFailed, error, this._outputChannel);
TelemetryReporter.sendErrorEvent(TelemetryViews.ProjectController, TelemetryActions.publishToNewAzureServer);
}
}
/**
* Publishes a project to docker container
* @param context a treeItem in a project's hierarchy, to be used to obtain a Project or the Project itself
* @param deployProfile
*/
public async publishToDockerContainer(context: Project | dataworkspace.WorkspaceTreeItem, deployProfile: IDeployProfile): Promise<void> {
public async publishToDockerContainer(context: Project | dataworkspace.WorkspaceTreeItem, deployProfile: ILocalDbDeployProfile): Promise<void> {
const project: Project = this.getProjectFromContext(context);
try {
TelemetryReporter.sendActionEvent(TelemetryViews.ProjectController, TelemetryActions.publishToContainer);
@@ -301,7 +339,7 @@ export class ProjectsController {
let connectionUri: string | undefined;
if (deployProfile.localDbSetting) {
void utils.showInfoMessageWithOutputChannel(constants.publishingProjectMessage, this._outputChannel);
connectionUri = await this.deployService.deploy(deployProfile, project);
connectionUri = await this.deployService.deployToContainer(deployProfile, project);
if (connectionUri) {
deployProfile.deploySettings.connectionUri = connectionUri;
}
@@ -369,9 +407,19 @@ export class ProjectsController {
if (publishTarget === constants.PublishTargetType.docker) {
const deployProfile = await launchPublishToDockerContainerQuickpick(project);
if (deployProfile?.deploySettings) {
if (deployProfile?.deploySettings && deployProfile?.localDbSetting) {
await this.publishToDockerContainer(project, deployProfile);
}
} else if (publishTarget === constants.PublishTargetType.newAzureServer) {
try {
const settings = await launchCreateAzureServerQuickPick(project, this.azureSqlClient);
if (settings?.deploySettings && settings?.sqlDbSetting) {
await this.publishToNewAzureServer(project, settings);
}
} catch (error) {
void utils.showErrorMessageWithOutputChannel(constants.publishToNewAzureServerFailed, error, this._outputChannel);
}
} else {
let settings: IDeploySettings | undefined = await getPublishDatabaseSettings(project);

View File

@@ -7,11 +7,14 @@ import * as vscode from 'vscode';
import * as constants from '../common/constants';
import * as utils from '../common/utils';
import * as uiUtils from './utils';
import { AppSettingType, DockerImageInfo, IDeployAppIntegrationProfile, IDeployProfile, ILocalDbSetting } from '../models/deploy/deployProfile';
import { AppSettingType, DockerImageInfo, IDeployAppIntegrationProfile, ISqlDbDeployProfile, ILocalDbDeployProfile, ILocalDbSetting } from '../models/deploy/deployProfile';
import { Project } from '../models/project';
import { getPublishDatabaseSettings } from './publishDatabaseQuickpick';
import * as path from 'path';
import * as fse from 'fs-extra';
import { AzureSqlClient } from '../models/deploy/azureSqlClient';
import { IDeploySettings } from '../models/IDeploySettings';
import { IAccount } from 'vscode-mssql';
/**
* Create flow for Deploying a database using only VS Code-native APIs such as QuickPick
@@ -112,11 +115,166 @@ async function launchEulaQuickPick(imageInfo: DockerImageInfo | undefined): Prom
return false;
}
export async function launchCreateAzureServerQuickPick(project: Project, azureSqlClient: AzureSqlClient): Promise<ISqlDbDeployProfile | undefined> {
const name = uiUtils.getPublishServerName(project.getProjectTargetVersion());
const accounts = await azureSqlClient.getAccounts();
const accountOptions = accounts.map(x => x.displayInfo?.displayName || '');
accountOptions.unshift(constants.azureAddAccount);
let account: IAccount | undefined;
let accountOption = await vscode.window.showQuickPick(
accountOptions,
{ title: constants.azureAccounts, ignoreFocusOut: true });
// Return when user hits escape
if (!accountOption) {
return undefined;
}
if (accountOption === constants.azureAddAccount) {
account = await azureSqlClient.getAccount();
} else {
account = accounts.find(x => x.displayInfo.displayName === accountOption);
}
if (!account) {
return undefined;
}
const sessions = await azureSqlClient.getSessions(account);
const subscriptionName = await vscode.window.showQuickPick(
sessions.map(x => x.subscription.displayName || ''),
{ title: constants.azureSubscription, ignoreFocusOut: true });
// Return when user hits escape
if (!subscriptionName) {
return undefined;
}
const session = sessions.find(x => x.subscription.displayName === subscriptionName);
if (!session?.subscription?.subscriptionId) {
return undefined;
}
const resourceGroups = await azureSqlClient.getResourceGroups(session);
const resourceGroupName = await vscode.window.showQuickPick(
resourceGroups.map(x => x.name || ''),
{ title: constants.resourceGroup, ignoreFocusOut: true });
// Return when user hits escape
if (!resourceGroupName) {
return undefined;
}
const resourceGroup = resourceGroups.find(x => x.name === resourceGroupName);
// Return resource group is invalid
if (!resourceGroup) {
return undefined;
}
let locations = await azureSqlClient.getLocations(session);
if (resourceGroup.location) {
const defaultLocation = locations.find(x => x.name === resourceGroup.location);
if (defaultLocation) {
locations = locations.filter(x => x.name !== defaultLocation.name);
locations.unshift(defaultLocation);
}
}
let locationName = await vscode.window.showQuickPick(
locations.map(x => x.name || ''),
{ title: constants.azureLocation, ignoreFocusOut: true, placeHolder: resourceGroup?.location });
// Return when user hits escape
if (!locationName) {
return undefined;
}
let serverName: string | undefined = '';
serverName = await vscode.window.showInputBox({
title: constants.azureServerName,
ignoreFocusOut: true,
value: serverName,
password: false
}
);
// Return when user hits escape
if (!serverName) {
return undefined;
}
let user: string | undefined = '';
user = await vscode.window.showInputBox({
title: constants.enterUser(name),
ignoreFocusOut: true,
value: user,
password: false
}
);
// Return when user hits escape
if (!user) {
return undefined;
}
let password: string | undefined = '';
password = await vscode.window.showInputBox({
title: constants.enterPassword(name),
ignoreFocusOut: true,
value: password,
validateInput: input => !utils.isValidSQLPassword(input) ? constants.invalidSQLPasswordMessage(name) : undefined,
password: true
}
);
// Return when user hits escape
if (!password) {
return undefined;
}
let confirmPassword: string | undefined = '';
confirmPassword = await vscode.window.showInputBox({
title: constants.confirmPassword(name),
ignoreFocusOut: true,
value: confirmPassword,
validateInput: input => input !== password ? constants.passwordNotMatch(name) : undefined,
password: true
}
);
// Return when user hits escape
if (!confirmPassword) {
return undefined;
}
let settings: IDeploySettings | undefined = await getPublishDatabaseSettings(project, false);
return {
// TODO add tenant
deploySettings: settings, sqlDbSetting: {
tenantId: session.tenantId,
accountId: session.account.key.id,
serverName: serverName,
userName: user,
password: password,
port: 1433,
dbName: '',
session: session,
resourceGroupName: resourceGroup.name || '',
location: locationName
}
};
}
/**
* Create flow for publishing a database to docker container using only VS Code-native APIs such as QuickPick
*/
export async function launchPublishToDockerContainerQuickpick(project: Project): Promise<IDeployProfile | undefined> {
export async function launchPublishToDockerContainerQuickpick(project: Project): Promise<ILocalDbDeployProfile | undefined> {
const name = uiUtils.getPublishServerName(project.getProjectTargetVersion());
let localDbSetting: ILocalDbSetting | undefined;
// Deploy to docker selected

View File

@@ -16,7 +16,7 @@ import { IconPathHelper } from '../common/iconHelper';
import { cssStyles } from '../common/uiConstants';
import { getAgreementDisplayText, getConnectionName, getDockerBaseImages, getPublishServerName } from './utils';
import { TelemetryActions, TelemetryReporter, TelemetryViews } from '../common/telemetry';
import { IDeployProfile } from '../models/deploy/deployProfile';
import { ILocalDbDeployProfile } from '../models/deploy/deployProfile';
import { Deferred } from '../common/promise';
interface DataSourceDropdownValue extends azdataType.CategoryValue {
@@ -62,7 +62,7 @@ export class PublishDatabaseDialog {
private toDispose: vscode.Disposable[] = [];
public publish: ((proj: Project, profile: IDeploySettings) => any) | undefined;
public publishToContainer: ((proj: Project, profile: IDeployProfile) => any) | undefined;
public publishToContainer: ((proj: Project, profile: ILocalDbDeployProfile) => any) | undefined;
public generateScript: ((proj: Project, profile: IDeploySettings) => any) | undefined;
public readPublishProfile: ((profileUri: vscode.Uri) => any) | undefined;
@@ -232,7 +232,7 @@ export class PublishDatabaseDialog {
const dockerBaseImage = this.getBaseDockerImageName();
const baseImages = getDockerBaseImages(this.project.getProjectTargetVersion());
const imageInfo = baseImages.find(x => x.name === dockerBaseImage);
const settings: IDeployProfile = {
const settings: ILocalDbDeployProfile = {
localDbSetting: {
dbName: this.targetDatabaseName,
dockerBaseImage: dockerBaseImage,

View File

@@ -12,6 +12,7 @@ import { getDefaultPublishDeploymentOptions, getVscodeMssqlApi } from '../common
import { IConnectionInfo } from 'vscode-mssql';
import { IDeploySettings } from '../models/IDeploySettings';
import { getPublishServerName } from './utils';
import { SqlTargetPlatform } from 'sqldbproj';
/**
* Create flow for Publishing a database using only VS Code-native APIs such as QuickPick
@@ -209,9 +210,14 @@ export async function getPublishDatabaseSettings(project: Project, promptForConn
export async function launchPublishTargetOption(project: Project): Promise<constants.PublishTargetType | undefined> {
// Show options to user for deploy to existing server or docker
const name = getPublishServerName(project.getProjectTargetVersion());
const target = project.getProjectTargetVersion();
const name = getPublishServerName(target);
const logicalServerName = target === constants.targetPlatformToVersion.get(SqlTargetPlatform.sqlAzure) ? constants.AzureSqlLogicalServerName : constants.SqlServerName;
const options = target === constants.targetPlatformToVersion.get(SqlTargetPlatform.sqlAzure) ?
[constants.publishToDockerContainer(name), constants.publishToNewAzureServer, constants.publishToExistingServer(logicalServerName)] :
[constants.publishToDockerContainer(name), constants.publishToExistingServer(logicalServerName)];
const publishOption = await vscode.window.showQuickPick(
[constants.publishToExistingServer(name), constants.publishToDockerContainer(name)],
options,
{ title: constants.selectPublishOption, ignoreFocusOut: true });
// Return when user hits escape
@@ -224,6 +230,8 @@ export async function launchPublishTargetOption(project: Project): Promise<const
return constants.PublishTargetType.existingServer;
case constants.publishToDockerContainer(name):
return constants.PublishTargetType.docker;
case constants.publishToNewAzureServer:
return constants.PublishTargetType.newAzureServer;
default:
return constants.PublishTargetType.existingServer;
}

View 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.
*--------------------------------------------------------------------------------------------*/
import * as utils from '../../common/utils';
import { IAccount, IAzureAccountSession, azure } from 'vscode-mssql';
/**
* Client module to call Azure APIs for getting or creating resources
*/
export class AzureSqlClient {
constructor(
private _azureAccountServiceFactory: utils.AzureAccountServiceFactory = utils.defaultAzureAccountServiceFactory,
private _azureResourceServiceFactory: utils.AzureResourceServiceFactory = utils.defaultAzureResourceServiceFactory
) { }
/**
* Returns existing Azure accounts
*/
public async getAccounts(): Promise<IAccount[]> {
const azureAccountService = await this._azureAccountServiceFactory();
return await azureAccountService.getAccounts();
}
/**
* Prompt user to login to Azure and returns the account
* @returns Azure account that user logged in to
*/
public async getAccount(): Promise<IAccount> {
const azureAccountService = await this._azureAccountServiceFactory();
return await azureAccountService.addAccount();
}
/**
* Returns Azure locations for given subscription
*/
public async getLocations(session: IAzureAccountSession): Promise<azure.subscription.Location[]> {
const azureResourceService = await this._azureResourceServiceFactory();
return await azureResourceService.getLocations(session);
}
/**
* Returns Azure sessions with subscription, tenant and token for given account
*/
public async getSessions(account: IAccount): Promise<IAzureAccountSession[]> {
const azureAccountService = await this._azureAccountServiceFactory();
return await azureAccountService.getAccountSessions(account);
}
/**
* Creates a new Azure SQL server for given subscription, resource group and location
*/
public async createOrUpdateServer(session: IAzureAccountSession, resourceGroupName: string, serverName: string, parameters: azure.sql.Server): Promise<string | undefined> {
const azureResourceService = await this._azureResourceServiceFactory();
return await azureResourceService.createOrUpdateServer(session, resourceGroupName, serverName, parameters);
}
/**
* Returns Azure resource groups for given subscription
*/
public async getResourceGroups(session: IAzureAccountSession): Promise<Array<azure.resources.ResourceGroup> | []> {
const azureResourceService = await this._azureResourceServiceFactory();
return await azureResourceService.getResourceGroups(session);
}
}

View File

@@ -5,32 +5,49 @@
import { IDeploySettings } from '../IDeploySettings';
import type * as azdataType from 'azdata';
import { IAzureAccountSession } from 'vscode-mssql';
export enum AppSettingType {
None,
AzureFunction
}
export interface IDeployProfile {
export interface ILocalDbDeployProfile {
localDbSetting?: ILocalDbSetting;
deploySettings?: IDeploySettings;
}
export interface ISqlDbDeployProfile {
sqlDbSetting?: ISqlDbSetting;
deploySettings?: IDeploySettings;
}
export interface IDeployAppIntegrationProfile {
envVariableName?: string;
appSettingFile?: string;
appSettingType: AppSettingType;
}
export interface ILocalDbSetting {
serverName: string,
port: number,
userName: string,
password: string,
dbName: string,
export interface ISqlDbSetting extends ISqlConnectionProperties {
session: IAzureAccountSession
resourceGroupName: string,
location: string
}
export interface ILocalDbSetting extends ISqlConnectionProperties {
dockerBaseImage: string,
dockerBaseImageEula: string,
connectionRetryTimeout?: number,
profileName?: string
}
export interface ISqlConnectionProperties {
tenantId?: string,
accountId?: string
serverName: string,
userName: string,
password: string,
port: number,
dbName: string,
profileName?: string,
connectionRetryTimeout?: number
}
export interface DockerImageInfo {
@@ -41,3 +58,4 @@ export interface DockerImageInfo {
export interface AgreementInfo {
link: azdataType.LinkArea;
}

View File

@@ -3,7 +3,7 @@
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { AppSettingType, IDeployAppIntegrationProfile, IDeployProfile, ILocalDbSetting } from './deployProfile';
import { AppSettingType, IDeployAppIntegrationProfile, ILocalDbDeployProfile, ILocalDbSetting, ISqlConnectionProperties, ISqlDbDeployProfile } from './deployProfile';
import * as UUID from 'vscode-languageclient/lib/utils/uuid';
import { Project } from '../project';
import * as constants from '../../common/constants';
@@ -13,6 +13,8 @@ import * as vscode from 'vscode';
import { ConnectionResult } from 'azdata';
import * as templates from '../../templates/templates';
import { ShellExecutionHelper } from '../../tools/shellExecutionHelper';
import { AzureSqlClient } from './azureSqlClient';
import { IFireWallRuleError } from 'vscode-mssql';
interface DockerImageSpec {
label: string;
@@ -21,7 +23,7 @@ interface DockerImageSpec {
}
export class DeployService {
constructor(private _outputChannel: vscode.OutputChannel, shellExecutionHelper: ShellExecutionHelper | undefined = undefined) {
constructor(private _azureSqlClient = new AzureSqlClient(), private _outputChannel: vscode.OutputChannel, shellExecutionHelper: ShellExecutionHelper | undefined = undefined) {
this._shellExecutionHelper = shellExecutionHelper ?? new ShellExecutionHelper(this._outputChannel);
}
@@ -50,7 +52,7 @@ export class DeployService {
return undefined;
}
public async updateAppSettings(profile: IDeployAppIntegrationProfile, deployProfile: IDeployProfile | undefined): Promise<void> {
public async updateAppSettings(profile: IDeployAppIntegrationProfile, deployProfile: ILocalDbDeployProfile | undefined): Promise<void> {
// Update app settings
//
if (!profile.appSettingFile) {
@@ -122,7 +124,37 @@ export class DeployService {
return { label: imageLabel, tag: imageTag, containerName: dockerName };
}
public async deploy(profile: IDeployProfile, project: Project): Promise<string | undefined> {
/**
* Creates a new Azure Sql server and tries to connect to the new server. If connection fails because of firewall rule, it prompts user to add firewall rule settings
* @param profile Azure Sql server settings
* @returns connection url for the new server
*/
public async createNewAzureSqlServer(profile: ISqlDbDeployProfile | undefined): Promise<string | undefined> {
if (!profile?.sqlDbSetting) {
return undefined;
}
this.logToOutput(constants.creatingAzureSqlServer(profile?.sqlDbSetting?.serverName));
// Create the server
const server = await this._azureSqlClient.createOrUpdateServer(profile.sqlDbSetting.session, profile?.sqlDbSetting.resourceGroupName, profile?.sqlDbSetting.serverName, {
location: profile?.sqlDbSetting?.location,
administratorLogin: profile?.sqlDbSetting.userName,
administratorLoginPassword: profile?.sqlDbSetting.password
});
if (server) {
this._outputChannel.appendLine(constants.serverCreated);
profile.sqlDbSetting.serverName = server;
this.logToOutput(constants.azureSqlServerCreated(profile?.sqlDbSetting?.serverName));
// Connect to the server
return await this.getConnection(profile.sqlDbSetting, false, constants.master);
}
return undefined;
}
public async deployToContainer(profile: ILocalDbDeployProfile, project: Project): Promise<string | undefined> {
return await this.executeTask(constants.deployDbTaskName, async () => {
if (!profile.localDbSetting) {
return undefined;
@@ -218,7 +250,7 @@ export class DeployService {
}
// Connects to a database
private async connectToDatabase(profile: ILocalDbSetting, saveConnectionAndPassword: boolean, database: string): Promise<ConnectionResult | string | undefined> {
private async connectToDatabase(profile: ISqlConnectionProperties, saveConnectionAndPassword: boolean, database: string): Promise<ConnectionResult | string | undefined> {
const getAzdataApi = await utils.getAzdataApi();
const vscodeMssqlApi = getAzdataApi ? undefined : await utils.getVscodeMssqlApi();
if (getAzdataApi) {
@@ -248,7 +280,7 @@ export class DeployService {
encrypt: false,
connectTimeout: 30,
applicationName: 'SQL Database Project',
accountId: undefined,
accountId: profile.accountId,
azureAccountToken: undefined,
applicationIntent: undefined,
attachDbFilename: undefined,
@@ -272,9 +304,19 @@ export class DeployService {
workstationId: undefined,
profileName: profile.profileName,
expiresOn: undefined,
tenantId: undefined
tenantId: profile.tenantId
};
let connectionUrl = await vscodeMssqlApi.connect(connectionProfile, saveConnectionAndPassword);
let connectionUrl = '';
try {
connectionUrl = await vscodeMssqlApi.connect(connectionProfile, saveConnectionAndPassword);
} catch (err) {
const firewallRuleError = <IFireWallRuleError>err;
if (firewallRuleError?.connectionUri) {
await vscodeMssqlApi.promptForFirewallRule(err.connectionUri, connectionProfile);
} else {
throw err;
}
}
return connectionUrl;
} else {
return undefined;
@@ -307,7 +349,7 @@ export class DeployService {
return connectionResult ? connectionResult.connectionId : <string>connection;
}
public async getConnection(profile: ILocalDbSetting, saveConnectionAndPassword: boolean, database: string): Promise<string | undefined> {
public async getConnection(profile: ISqlConnectionProperties, saveConnectionAndPassword: boolean, database: string): Promise<string | undefined> {
const getAzdataApi = await utils.getAzdataApi();
let connection = await utils.retry(
constants.connectingToSqlServerMessage,

View File

@@ -0,0 +1,109 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as should from 'should';
import { AzureSqlClient } from '../../models/deploy/azureSqlClient';
import { IAccount, IAzureAccountService, IAzureAccountSession, IAzureResourceService, azure } from 'vscode-mssql';
export interface TestContext {
azureAccountService: IAzureAccountService;
azureResourceService: IAzureResourceService;
accounts: IAccount[];
session: IAzureAccountSession;
subscriptions: azure.subscription.Subscription[];
locations: azure.subscription.Location[];
groups: azure.resources.ResourceGroup[];
}
export function createContext(): TestContext {
const accounts = [{
key: undefined!,
displayInfo: undefined!,
properties: {
tenants: [{
id: '',
displayName: ''
}]
},
isStale: false,
isSignedIn: true
}];
const subscriptions: azure.subscription.Subscription[] = [{ subscriptionId: 'id1' }, { subscriptionId: 'id2' }];
const locations: azure.subscription.Location[] = [{ id: 'id1' }, { id: 'id2' }];
const groups: azure.resources.ResourceGroup[] = [{ id: 'id1', location: 'l1' }, { id: 'id2', location: 'l2' }];
const session: IAzureAccountSession = {
account: accounts[0],
subscription: subscriptions[0],
tenantId: 'tenantId',
token: {
key: '',
token: '',
tokenType: '',
}
};
return {
groups: groups,
locations: locations,
subscriptions: subscriptions,
session: session,
accounts: accounts,
azureAccountService: {
addAccount: () => Promise.resolve(accounts[0]),
getAccounts: () => Promise.resolve(accounts),
getAccountSecurityToken: () => Promise.resolve({
key: '',
token: '',
tokenType: ''
}),
getAccountSessions: () => Promise.resolve([session])
},
azureResourceService: {
getLocations: () => Promise.resolve(locations),
getResourceGroups: () => Promise.resolve(groups),
createOrUpdateServer: () => Promise.resolve('new_server')
}
};
}
describe('Azure SQL client', function (): void {
it('Should return accounts successfully', async function (): Promise<void> {
const testContext = createContext();
const azureSqlClient = new AzureSqlClient(() => Promise.resolve(testContext.azureAccountService));
const accounts = await azureSqlClient.getAccounts();
should(accounts.length).equal(testContext.accounts.length);
});
it('Should create and return new account successfully', async function (): Promise<void> {
const testContext = createContext();
const azureSqlClient = new AzureSqlClient(() => Promise.resolve(testContext.azureAccountService));
const account = await azureSqlClient.getAccount();
should(account.key).equal(testContext.accounts[0].key);
});
it('Should return subscriptions successfully', async function (): Promise<void> {
const testContext = createContext();
const azureSqlClient = new AzureSqlClient(() => Promise.resolve(testContext.azureAccountService));
const result = await azureSqlClient.getSessions(testContext.accounts[0]);
should(result[0].subscription.id).deepEqual(testContext.subscriptions[0].id);
});
it('Should return locations successfully', async function (): Promise<void> {
const testContext = createContext();
const azureSqlClient = new AzureSqlClient(() => Promise.resolve(testContext.azureAccountService), () => Promise.resolve(testContext.azureResourceService));
const result = await azureSqlClient.getLocations(testContext.session);
should(result.length).deepEqual(testContext.locations.length);
});
it('Should return resource groups successfully', async function (): Promise<void> {
const testContext = createContext();
const azureSqlClient = new AzureSqlClient(() => Promise.resolve(testContext.azureAccountService), () => Promise.resolve(testContext.azureResourceService));
const result = await azureSqlClient.getResourceGroups(testContext.session);
should(result.length).deepEqual(testContext.groups.length);
should(result[0].location).deepEqual(testContext.groups[0].location);
});
});

View File

@@ -11,16 +11,18 @@ import { DeployService } from '../../models/deploy/deployService';
import { Project } from '../../models/project';
import * as vscode from 'vscode';
import * as azdata from 'azdata';
import { AppSettingType, IDeployProfile } from '../../models/deploy/deployProfile';
import { AppSettingType, ILocalDbDeployProfile, ISqlDbDeployProfile } from '../../models/deploy/deployProfile';
import * as UUID from 'vscode-languageclient/lib/utils/uuid';
import * as fse from 'fs-extra';
import * as path from 'path';
import * as constants from '../../common/constants';
import { ShellExecutionHelper } from '../../tools/shellExecutionHelper';
import * as TypeMoq from 'typemoq';
import { AzureSqlClient } from '../../models/deploy/azureSqlClient';
export interface TestContext {
outputChannel: vscode.OutputChannel;
azureSqlClient: TypeMoq.IMock<AzureSqlClient>;
}
export const mockConnectionResult: azdata.ConnectionResult = {
@@ -47,7 +49,8 @@ export function createContext(): TestContext {
show: () => { },
hide: () => { },
dispose: () => { }
}
},
azureSqlClient: TypeMoq.Mock.ofType(AzureSqlClient)
};
}
@@ -68,7 +71,7 @@ describe('deploy service', function (): void {
it('Should deploy a database to docker container successfully', async function (): Promise<void> {
const testContext = createContext();
const deployProfile: IDeployProfile = {
const deployProfile: ILocalDbDeployProfile = {
localDbSetting: {
dbName: 'test',
password: 'PLACEHOLDER',
@@ -84,21 +87,21 @@ describe('deploy service', function (): void {
const project1 = await Project.openProject(vscode.Uri.file(projFilePath).fsPath);
const shellExecutionHelper = TypeMoq.Mock.ofType(ShellExecutionHelper);
shellExecutionHelper.setup(x => x.runStreamedCommand(TypeMoq.It.isAny(),
undefined, TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve('id'));
const deployService = new DeployService(testContext.outputChannel, shellExecutionHelper.object);
undefined, TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve('id'));
const deployService = new DeployService(testContext.azureSqlClient.object, testContext.outputChannel, shellExecutionHelper.object);
sandbox.stub(azdata.connection, 'connect').returns(Promise.resolve(mockConnectionResult));
sandbox.stub(azdata.connection, 'getUriForConnection').returns(Promise.resolve('connection'));
sandbox.stub(vscode.window, 'showWarningMessage').returns(<any>Promise.resolve(constants.yesString));
sandbox.stub(azdata.tasks, 'startBackgroundOperation').callThrough();
let connection = await deployService.deploy(deployProfile, project1);
let connection = await deployService.deployToContainer(deployProfile, project1);
should(connection).equals('connection');
});
it('Should fail the deploy if docker is not running', async function (): Promise<void> {
const testContext = createContext();
const deployProfile: IDeployProfile = {
const deployProfile: ILocalDbDeployProfile = {
localDbSetting: {
dbName: 'test',
password: 'PLACEHOLDER',
@@ -114,11 +117,11 @@ describe('deploy service', function (): void {
const project1 = await Project.openProject(vscode.Uri.file(projFilePath).fsPath);
const shellExecutionHelper = TypeMoq.Mock.ofType(ShellExecutionHelper);
shellExecutionHelper.setup(x => x.runStreamedCommand(TypeMoq.It.isAny(),
undefined, TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.reject('error'));
const deployService = new DeployService(testContext.outputChannel, shellExecutionHelper.object);
undefined, TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.reject('error'));
const deployService = new DeployService(testContext.azureSqlClient.object, testContext.outputChannel, shellExecutionHelper.object);
sandbox.stub(azdata.tasks, 'startBackgroundOperation').callThrough();
await should(deployService.deploy(deployProfile, project1)).rejected();
await should(deployService.deployToContainer(deployProfile, project1)).rejected();
});
it('Should retry connecting to the server', async function (): Promise<void> {
@@ -136,8 +139,8 @@ describe('deploy service', function (): void {
const shellExecutionHelper = TypeMoq.Mock.ofType(ShellExecutionHelper);
shellExecutionHelper.setup(x => x.runStreamedCommand(TypeMoq.It.isAny(),
undefined, TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve('id'));
const deployService = new DeployService(testContext.outputChannel, shellExecutionHelper.object);
undefined, TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve('id'));
const deployService = new DeployService(testContext.azureSqlClient.object, testContext.outputChannel, shellExecutionHelper.object);
let connectionStub = sandbox.stub(azdata.connection, 'connect');
connectionStub.onFirstCall().returns(Promise.resolve(mockFailedConnectionResult));
connectionStub.onSecondCall().returns(Promise.resolve(mockConnectionResult));
@@ -173,7 +176,7 @@ describe('deploy service', function (): void {
const filePath = path.join(project1.projectFolderPath, 'local.settings.json');
await fse.writeFile(filePath, settingContent);
const deployProfile: IDeployProfile = {
const deployProfile: ILocalDbDeployProfile = {
localDbSetting: {
dbName: 'test',
password: 'PLACEHOLDER',
@@ -194,8 +197,8 @@ describe('deploy service', function (): void {
const shellExecutionHelper = TypeMoq.Mock.ofType(ShellExecutionHelper);
shellExecutionHelper.setup(x => x.runStreamedCommand(TypeMoq.It.isAny(),
undefined, TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve('id'));
const deployService = new DeployService(testContext.outputChannel, shellExecutionHelper.object);
undefined, TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve('id'));
const deployService = new DeployService(testContext.azureSqlClient.object, testContext.outputChannel, shellExecutionHelper.object);
await deployService.updateAppSettings(appInteg, deployProfile);
let newContent = JSON.parse(fse.readFileSync(filePath, 'utf8'));
@@ -228,7 +231,7 @@ describe('deploy service', function (): void {
const filePath = path.join(project1.projectFolderPath, 'local.settings.json');
await fse.writeFile(filePath, settingContent);
const deployProfile: IDeployProfile = {
const deployProfile: ILocalDbDeployProfile = {
deploySettings: {
connectionUri: 'connection',
@@ -245,8 +248,8 @@ describe('deploy service', function (): void {
};
const shellExecutionHelper = TypeMoq.Mock.ofType(ShellExecutionHelper);
shellExecutionHelper.setup(x => x.runStreamedCommand(TypeMoq.It.isAny(),
undefined, TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve('id'));
const deployService = new DeployService(testContext.outputChannel, shellExecutionHelper.object);
undefined, TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve('id'));
const deployService = new DeployService(testContext.azureSqlClient.object, testContext.outputChannel, shellExecutionHelper.object);
let connection = new azdata.connection.ConnectionProfile();
sandbox.stub(azdata.connection, 'getConnection').returns(Promise.resolve(connection));
@@ -260,10 +263,10 @@ describe('deploy service', function (): void {
const testContext = createContext();
const shellExecutionHelper = TypeMoq.Mock.ofType(ShellExecutionHelper);
shellExecutionHelper.setup(x => x.runStreamedCommand(TypeMoq.It.isAny(),
undefined, TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve(`id
undefined, TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve(`id
id2
id3`));
const deployService = new DeployService(testContext.outputChannel, shellExecutionHelper.object);
const deployService = new DeployService(testContext.azureSqlClient.object, testContext.outputChannel, shellExecutionHelper.object);
const ids = await deployService.getCurrentDockerContainer('label');
await deployService.cleanDockerObjects(ids, ['docker stop', 'docker rm']);
shellExecutionHelper.verify(x => x.runStreamedCommand(TypeMoq.It.isAny(), undefined, TypeMoq.It.isAny(), TypeMoq.It.isAny()), TypeMoq.Times.exactly(7));
@@ -271,7 +274,7 @@ describe('deploy service', function (): void {
it('Should create docker image info correctly', () => {
const testContext = createContext();
const deployService = new DeployService(testContext.outputChannel);
const deployService = new DeployService(testContext.azureSqlClient.object, testContext.outputChannel);
const id = UUID.generateUuid().toLocaleLowerCase();
const baseImage = 'baseImage:latest';
const tag = baseImage.replace(':', '-').replace(constants.sqlServerDockerRegistry, '').replace(/[^a-zA-Z0-9_,\-]/g, '').toLocaleLowerCase();
@@ -311,4 +314,53 @@ describe('deploy service', function (): void {
tag: `${constants.dockerImageNamePrefix}-${imageProjectName}-${tag}`
});
});
it('Should create a new Azure SQL server successfully', async function (): Promise<void> {
const testContext = createContext();
const deployProfile: ISqlDbDeployProfile = {
sqlDbSetting: {
dbName: 'test',
password: 'PLACEHOLDER',
port: 1433,
serverName: 'localhost',
userName: 'sa',
connectionRetryTimeout: 1,
resourceGroupName: 'resourceGroups',
session: {
subscription: {
subscriptionId: 'subscriptionId',
},token: {
key: '',
token: '',
tokenType: '',
},
tenantId: '',
account: undefined!
},
location: 'location'
}
};
const fullyQualifiedDomainName = 'servername';
const shellExecutionHelper = TypeMoq.Mock.ofType(ShellExecutionHelper);
shellExecutionHelper.setup(x => x.runStreamedCommand(TypeMoq.It.isAny(),
undefined, TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve('id'));
const session = deployProfile?.sqlDbSetting?.session;
if (deployProfile?.sqlDbSetting?.session && session) {
testContext.azureSqlClient.setup(x => x.createOrUpdateServer(
session,
deployProfile.sqlDbSetting?.resourceGroupName || '',
deployProfile.sqlDbSetting?.serverName || '',
{
location: deployProfile?.sqlDbSetting?.location || '',
administratorLogin: deployProfile?.sqlDbSetting?.userName,
administratorLoginPassword: deployProfile?.sqlDbSetting?.password
})).returns(() => Promise.resolve(fullyQualifiedDomainName));
}
sandbox.stub(azdata.connection, 'connect').returns(Promise.resolve(mockConnectionResult));
sandbox.stub(azdata.connection, 'getUriForConnection').returns(Promise.resolve('connection'));
const deployService = new DeployService(testContext.azureSqlClient.object, testContext.outputChannel, shellExecutionHelper.object);
let connection = await deployService.createNewAzureSqlServer(deployProfile);
should(deployProfile.sqlDbSetting?.serverName).equal(fullyQualifiedDomainName);
should(connection).equals('connection');
});
});

View File

@@ -18,7 +18,7 @@ import { ProjectsController } from '../../controllers/projectController';
import { IDeploySettings } from '../../models/IDeploySettings';
import { emptySqlDatabaseProjectTypeId } from '../../common/constants';
import { createContext, mockDacFxOptionsResult, TestContext } from '../testContext';
import { IDeployProfile } from '../../models/deploy/deployProfile';
import { ILocalDbDeployProfile } from '../../models/deploy/deployProfile';
let testContext: TestContext;
describe('Publish Database Dialog', () => {
@@ -112,7 +112,7 @@ describe('Publish Database Dialog', () => {
should(profile).deepEqual(expectedGenScript);
const expectedContainerPublishProfile: IDeployProfile = {
const expectedContainerPublishProfile: ILocalDbDeployProfile = {
localDbSetting: {
dbName: 'MockDatabaseName',
dockerBaseImage: '',
@@ -136,7 +136,7 @@ describe('Publish Database Dialog', () => {
}
};
dialog.object.publishToExistingServer = false;
let deployProfile: IDeployProfile | undefined;
let deployProfile: ILocalDbDeployProfile | undefined;
dialog.object.publishToContainer = (_, prof) => { deployProfile = prof; };
await dialog.object.publishClick();