mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-01-24 17:23:05 -05:00
Refactor Azure Core extension for easier resource addition (#7958)
Consolidated most logic into a base class and common resource request pattern. Reduces cost to add new providers, which will help for SQL Managed Instance support
This commit is contained in:
@@ -138,10 +138,6 @@
|
||||
{
|
||||
"command": "azure.resource.connectsqlserver",
|
||||
"when": "false"
|
||||
},
|
||||
{
|
||||
"command": "azure.resource.connectsqldb",
|
||||
"when": "false"
|
||||
}
|
||||
],
|
||||
"view/title": [
|
||||
@@ -169,12 +165,7 @@
|
||||
},
|
||||
{
|
||||
"command": "azure.resource.connectsqlserver",
|
||||
"when": "viewItem == azure.resource.itemType.databaseServer",
|
||||
"group": "inline"
|
||||
},
|
||||
{
|
||||
"command": "azure.resource.connectsqldb",
|
||||
"when": "viewItem == azure.resource.itemType.database",
|
||||
"when": "viewItem == azure.resource.itemType.databaseServer || viewItem == azure.resource.itemType.database || viewItem == azure.resource.itemType.sqlInstance",
|
||||
"group": "inline"
|
||||
}
|
||||
]
|
||||
|
||||
@@ -3,10 +3,9 @@
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import { window, QuickPickItem } from 'vscode';
|
||||
import { AzureResource } from 'azdata';
|
||||
import * as azdata from 'azdata';
|
||||
import { TokenCredentials } from 'ms-rest';
|
||||
import { AppContext } from '../appContext';
|
||||
import * as nls from 'vscode-nls';
|
||||
@@ -34,7 +33,7 @@ export function registerAzureResourceCommands(appContext: AppContext, tree: Azur
|
||||
const subscriptions = (await accountNode.getCachedSubscriptions()) || <azureResource.AzureResourceSubscription[]>[];
|
||||
if (subscriptions.length === 0) {
|
||||
try {
|
||||
const tokens = await this.servicePool.apiWrapper.getSecurityToken(this.account, AzureResource.ResourceManagement);
|
||||
const tokens = await this.servicePool.apiWrapper.getSecurityToken(this.account, azdata.AzureResource.ResourceManagement);
|
||||
|
||||
for (const tenant of this.account.properties.tenants) {
|
||||
const token = tokens[tenant.id].token;
|
||||
@@ -86,4 +85,21 @@ export function registerAzureResourceCommands(appContext: AppContext, tree: Azur
|
||||
appContext.apiWrapper.registerCommand('azure.resource.signin', async (node?: TreeNode) => {
|
||||
appContext.apiWrapper.executeCommand('workbench.actions.modal.linkedAccount');
|
||||
});
|
||||
|
||||
appContext.apiWrapper.registerCommand('azure.resource.connectsqlserver', async (node?: TreeNode) => {
|
||||
if (!node) {
|
||||
return;
|
||||
}
|
||||
|
||||
const treeItem: azdata.TreeItem = await node.getTreeItem();
|
||||
if (!treeItem.payload) {
|
||||
return;
|
||||
}
|
||||
// Ensure connection is saved to the Connections list, then open connection dialog
|
||||
let connectionProfile = Object.assign({}, treeItem.payload, { saveProfile: true });
|
||||
const conn = await appContext.apiWrapper.openConnectionDialog(undefined, connectionProfile, { saveConnection: true, showDashboard: true });
|
||||
if (conn) {
|
||||
appContext.apiWrapper.executeCommand('workbench.view.connections');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
'use strict';
|
||||
|
||||
export enum AzureResourceItemType {
|
||||
account = 'azure.resource.itemType.account',
|
||||
subscription = 'azure.resource.itemType.subscription',
|
||||
@@ -12,6 +10,7 @@ export enum AzureResourceItemType {
|
||||
database = 'azure.resource.itemType.database',
|
||||
databaseServerContainer = 'azure.resource.itemType.databaseServerContainer',
|
||||
databaseServer = 'azure.resource.itemType.databaseServer',
|
||||
sqlInstance = 'azure.resource.itemType.sqlInstance',
|
||||
message = 'azure.resource.itemType.message'
|
||||
}
|
||||
|
||||
@@ -22,4 +21,4 @@ export enum AzureResourceServiceNames {
|
||||
subscriptionService = 'AzureResourceSubscriptionService',
|
||||
subscriptionFilterService = 'AzureResourceSubscriptionFilterService',
|
||||
tenantService = 'AzureResourceTenantService'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,4 +42,24 @@ export interface IAzureResourceTenantService {
|
||||
export interface IAzureResourceNodeWithProviderId {
|
||||
resourceProviderId: string;
|
||||
resourceNode: azureResource.IAzureResourceNode;
|
||||
}
|
||||
}
|
||||
|
||||
export interface AzureSqlResource {
|
||||
name: string;
|
||||
loginName: string;
|
||||
}
|
||||
|
||||
export interface IAzureResourceService<T extends AzureSqlResource> {
|
||||
getResources(subscription: azureResource.AzureResourceSubscription, credential: ServiceClientCredentials): Promise<T[]>;
|
||||
}
|
||||
|
||||
|
||||
export interface AzureResourceDatabase extends AzureSqlResource {
|
||||
serverName: string;
|
||||
serverFullName: string;
|
||||
}
|
||||
|
||||
export interface AzureResourceDatabaseServer extends AzureSqlResource {
|
||||
fullName: string;
|
||||
defaultDatabaseName: string;
|
||||
}
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* 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 { IConnectionProfile } from 'azdata';
|
||||
import { AppContext } from '../../../appContext';
|
||||
|
||||
import { TreeNode } from '../../treeNode';
|
||||
import { generateGuid } from '../../utils';
|
||||
import { AzureResourceItemType } from '../../constants';
|
||||
import { IAzureResourceDatabaseNode } from './interfaces';
|
||||
import { AzureResourceResourceTreeNode } from '../../resourceTreeNode';
|
||||
|
||||
export function registerAzureResourceDatabaseCommands(appContext: AppContext): void {
|
||||
appContext.apiWrapper.registerCommand('azure.resource.connectsqldb', async (node?: TreeNode) => {
|
||||
if (!node) {
|
||||
return;
|
||||
}
|
||||
|
||||
const treeItem = await node.getTreeItem();
|
||||
if (treeItem.contextValue !== AzureResourceItemType.database) {
|
||||
return;
|
||||
}
|
||||
|
||||
const resourceNode = (node as AzureResourceResourceTreeNode).resourceNodeWithProviderId.resourceNode;
|
||||
const database = (resourceNode as IAzureResourceDatabaseNode).database;
|
||||
|
||||
let connectionProfile: IConnectionProfile = {
|
||||
id: generateGuid(),
|
||||
connectionName: undefined,
|
||||
serverName: database.serverFullName,
|
||||
databaseName: database.name,
|
||||
userName: database.loginName,
|
||||
password: '',
|
||||
authenticationType: 'SqlLogin',
|
||||
savePassword: true,
|
||||
groupFullName: '',
|
||||
groupId: '',
|
||||
providerName: 'MSSQL',
|
||||
saveProfile: true,
|
||||
options: {}
|
||||
};
|
||||
|
||||
const conn = await appContext.apiWrapper.openConnectionDialog(undefined, connectionProfile, { saveConnection: true, showDashboard: true });
|
||||
if (conn) {
|
||||
appContext.apiWrapper.executeCommand('workbench.view.connections');
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -3,24 +3,20 @@
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import { ExtensionContext } from 'vscode';
|
||||
import { ApiWrapper } from '../../../apiWrapper';
|
||||
|
||||
import { azureResource } from '../../azure-resource';
|
||||
import { IAzureResourceDatabaseService } from './interfaces';
|
||||
import { AzureResourceDatabaseTreeDataProvider } from './databaseTreeDataProvider';
|
||||
import { IAzureResourceService, AzureResourceDatabase } from '../../interfaces';
|
||||
|
||||
export class AzureResourceDatabaseProvider implements azureResource.IAzureResourceProvider {
|
||||
public constructor(
|
||||
databaseService: IAzureResourceDatabaseService,
|
||||
apiWrapper: ApiWrapper,
|
||||
extensionContext: ExtensionContext
|
||||
private _databaseService: IAzureResourceService<AzureResourceDatabase>,
|
||||
private _apiWrapper: ApiWrapper,
|
||||
private _extensionContext: ExtensionContext
|
||||
) {
|
||||
this._databaseService = databaseService;
|
||||
this._apiWrapper = apiWrapper;
|
||||
this._extensionContext = extensionContext;
|
||||
}
|
||||
|
||||
public getTreeDataProvider(): azureResource.IAzureResourceTreeDataProvider {
|
||||
@@ -30,8 +26,4 @@ export class AzureResourceDatabaseProvider implements azureResource.IAzureResour
|
||||
public get providerId(): string {
|
||||
return 'azure.resource.providers.database';
|
||||
}
|
||||
|
||||
private _databaseService: IAzureResourceDatabaseService = undefined;
|
||||
private _apiWrapper: ApiWrapper = undefined;
|
||||
private _extensionContext: ExtensionContext = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,17 +3,14 @@
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import { ServiceClientCredentials } from 'ms-rest';
|
||||
import { SqlManagementClient } from 'azure-arm-sql';
|
||||
|
||||
import { azureResource } from '../../azure-resource';
|
||||
import { IAzureResourceDatabaseService } from './interfaces';
|
||||
import { AzureResourceDatabase } from './models';
|
||||
import { IAzureResourceService, AzureResourceDatabase } from '../../interfaces';
|
||||
|
||||
export class AzureResourceDatabaseService implements IAzureResourceDatabaseService {
|
||||
public async getDatabases(subscription: azureResource.AzureResourceSubscription, credential: ServiceClientCredentials): Promise<AzureResourceDatabase[]> {
|
||||
export class AzureResourceDatabaseService implements IAzureResourceService<AzureResourceDatabase> {
|
||||
public async getResources(subscription: azureResource.AzureResourceSubscription, credential: ServiceClientCredentials): Promise<AzureResourceDatabase[]> {
|
||||
const databases: AzureResourceDatabase[] = [];
|
||||
const sqlManagementClient = new SqlManagementClient(credential, subscription.id);
|
||||
const svrs = await sqlManagementClient.servers.list();
|
||||
|
||||
@@ -3,82 +3,61 @@
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import { AzureResource, ExtensionNodeType } from 'azdata';
|
||||
import { TreeItem, TreeItemCollapsibleState, ExtensionContext } from 'vscode';
|
||||
import { TokenCredentials } from 'ms-rest';
|
||||
import { TreeItem, ExtensionNodeType } from 'azdata';
|
||||
import { TreeItemCollapsibleState, ExtensionContext } from 'vscode';
|
||||
import * as nls from 'vscode-nls';
|
||||
const localize = nls.loadMessageBundle();
|
||||
|
||||
import { azureResource } from '../../azure-resource';
|
||||
import { IAzureResourceDatabaseService, IAzureResourceDatabaseNode } from './interfaces';
|
||||
import { AzureResourceDatabase } from './models';
|
||||
import { AzureResourceItemType } from '../../../azureResource/constants';
|
||||
import { ApiWrapper } from '../../../apiWrapper';
|
||||
import { generateGuid } from '../../utils';
|
||||
import { IAzureResourceService, AzureResourceDatabase } from '../../interfaces';
|
||||
import { ResourceTreeDataProviderBase } from '../resourceTreeDataProviderBase';
|
||||
|
||||
export class AzureResourceDatabaseTreeDataProvider extends ResourceTreeDataProviderBase<AzureResourceDatabase> {
|
||||
|
||||
private static readonly containerId = 'azure.resource.providers.database.treeDataProvider.databaseContainer';
|
||||
private static readonly containerLabel = localize('azure.resource.providers.database.treeDataProvider.databaseContainerLabel', "SQL Databases");
|
||||
|
||||
export class AzureResourceDatabaseTreeDataProvider implements azureResource.IAzureResourceTreeDataProvider {
|
||||
public constructor(
|
||||
databaseService: IAzureResourceDatabaseService,
|
||||
databaseService: IAzureResourceService<AzureResourceDatabase>,
|
||||
apiWrapper: ApiWrapper,
|
||||
extensionContext: ExtensionContext
|
||||
private _extensionContext: ExtensionContext
|
||||
) {
|
||||
this._databaseService = databaseService;
|
||||
this._apiWrapper = apiWrapper;
|
||||
this._extensionContext = extensionContext;
|
||||
super(databaseService, apiWrapper);
|
||||
}
|
||||
protected getTreeItemForResource(database: AzureResourceDatabase): TreeItem {
|
||||
return {
|
||||
id: `databaseServer_${database.serverFullName}.database_${database.name}`,
|
||||
label: `${database.name} (${database.serverName})`,
|
||||
iconPath: {
|
||||
dark: this._extensionContext.asAbsolutePath('resources/dark/sql_database_inverse.svg'),
|
||||
light: this._extensionContext.asAbsolutePath('resources/light/sql_database.svg')
|
||||
},
|
||||
collapsibleState: TreeItemCollapsibleState.Collapsed,
|
||||
contextValue: AzureResourceItemType.database,
|
||||
payload: {
|
||||
id: generateGuid(),
|
||||
connectionName: undefined,
|
||||
serverName: database.serverFullName,
|
||||
databaseName: database.name,
|
||||
userName: database.loginName,
|
||||
password: '',
|
||||
authenticationType: 'SqlLogin',
|
||||
savePassword: true,
|
||||
groupFullName: '',
|
||||
groupId: '',
|
||||
providerName: 'MSSQL',
|
||||
saveProfile: false,
|
||||
options: {}
|
||||
},
|
||||
childProvider: 'MSSQL',
|
||||
type: ExtensionNodeType.Database
|
||||
};
|
||||
}
|
||||
|
||||
public getTreeItem(element: azureResource.IAzureResourceNode): TreeItem | Thenable<TreeItem> {
|
||||
return element.treeItem;
|
||||
}
|
||||
|
||||
public async getChildren(element?: azureResource.IAzureResourceNode): Promise<azureResource.IAzureResourceNode[]> {
|
||||
if (!element) {
|
||||
return [this.createContainerNode()];
|
||||
}
|
||||
|
||||
const tokens = await this._apiWrapper.getSecurityToken(element.account, AzureResource.ResourceManagement);
|
||||
const credential = new TokenCredentials(tokens[element.tenantId].token, tokens[element.tenantId].tokenType);
|
||||
|
||||
const databases: AzureResourceDatabase[] = (await this._databaseService.getDatabases(element.subscription, credential)) || <AzureResourceDatabase[]>[];
|
||||
|
||||
return databases.map((database) => <IAzureResourceDatabaseNode>{
|
||||
account: element.account,
|
||||
subscription: element.subscription,
|
||||
tenantId: element.tenantId,
|
||||
database: database,
|
||||
treeItem: {
|
||||
id: `databaseServer_${database.serverFullName}.database_${database.name}`,
|
||||
label: `${database.name} (${database.serverName})`,
|
||||
iconPath: {
|
||||
dark: this._extensionContext.asAbsolutePath('resources/dark/sql_database_inverse.svg'),
|
||||
light: this._extensionContext.asAbsolutePath('resources/light/sql_database.svg')
|
||||
},
|
||||
collapsibleState: TreeItemCollapsibleState.Collapsed,
|
||||
contextValue: AzureResourceItemType.database,
|
||||
payload: {
|
||||
id: generateGuid(),
|
||||
connectionName: undefined,
|
||||
serverName: database.serverFullName,
|
||||
databaseName: database.name,
|
||||
userName: database.loginName,
|
||||
password: '',
|
||||
authenticationType: 'SqlLogin',
|
||||
savePassword: true,
|
||||
groupFullName: '',
|
||||
groupId: '',
|
||||
providerName: 'MSSQL',
|
||||
saveProfile: false,
|
||||
options: {}
|
||||
},
|
||||
childProvider: 'MSSQL',
|
||||
type: ExtensionNodeType.Database
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private createContainerNode(): azureResource.IAzureResourceNode {
|
||||
protected createContainerNode(): azureResource.IAzureResourceNode {
|
||||
return {
|
||||
account: undefined,
|
||||
subscription: undefined,
|
||||
@@ -95,11 +74,4 @@ export class AzureResourceDatabaseTreeDataProvider implements azureResource.IAzu
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private _databaseService: IAzureResourceDatabaseService = undefined;
|
||||
private _apiWrapper: ApiWrapper = undefined;
|
||||
private _extensionContext: ExtensionContext = undefined;
|
||||
|
||||
private static readonly containerId = 'azure.resource.providers.database.treeDataProvider.databaseContainer';
|
||||
private static readonly containerLabel = localize('azure.resource.providers.database.treeDataProvider.databaseContainerLabel', "SQL Databases");
|
||||
}
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* 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 { ServiceClientCredentials } from 'ms-rest';
|
||||
|
||||
import { azureResource } from '../../azure-resource';
|
||||
import { AzureResourceDatabase } from './models';
|
||||
|
||||
export interface IAzureResourceDatabaseService {
|
||||
getDatabases(subscription: azureResource.AzureResourceSubscription, credential: ServiceClientCredentials): Promise<AzureResourceDatabase[]>;
|
||||
}
|
||||
|
||||
export interface IAzureResourceDatabaseNode extends azureResource.IAzureResourceNode {
|
||||
readonly database: AzureResourceDatabase;
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
'use strict';
|
||||
|
||||
export interface AzureResourceDatabase {
|
||||
name: string;
|
||||
serverName: string;
|
||||
serverFullName: string;
|
||||
loginName: string;
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* 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 { IConnectionProfile } from 'azdata';
|
||||
import { AppContext } from '../../../appContext';
|
||||
|
||||
import { TreeNode } from '../../treeNode';
|
||||
import { generateGuid } from '../../utils';
|
||||
import { AzureResourceItemType } from '../../constants';
|
||||
import { IAzureResourceDatabaseServerNode } from './interfaces';
|
||||
import { AzureResourceResourceTreeNode } from '../../resourceTreeNode';
|
||||
|
||||
export function registerAzureResourceDatabaseServerCommands(appContext: AppContext): void {
|
||||
appContext.apiWrapper.registerCommand('azure.resource.connectsqlserver', async (node?: TreeNode) => {
|
||||
if (!node) {
|
||||
return;
|
||||
}
|
||||
|
||||
const treeItem = await node.getTreeItem();
|
||||
if (treeItem.contextValue !== AzureResourceItemType.databaseServer) {
|
||||
return;
|
||||
}
|
||||
|
||||
const resourceNode = (node as AzureResourceResourceTreeNode).resourceNodeWithProviderId.resourceNode;
|
||||
const databaseServer = (resourceNode as IAzureResourceDatabaseServerNode).databaseServer;
|
||||
|
||||
let connectionProfile: IConnectionProfile = {
|
||||
id: generateGuid(),
|
||||
connectionName: undefined,
|
||||
serverName: databaseServer.fullName,
|
||||
databaseName: databaseServer.defaultDatabaseName,
|
||||
userName: databaseServer.loginName,
|
||||
password: '',
|
||||
authenticationType: 'SqlLogin',
|
||||
savePassword: true,
|
||||
groupFullName: '',
|
||||
groupId: '',
|
||||
providerName: 'MSSQL',
|
||||
saveProfile: true,
|
||||
options: {}
|
||||
};
|
||||
|
||||
const conn = await appContext.apiWrapper.openConnectionDialog(undefined, connectionProfile, { saveConnection: true, showDashboard: true });
|
||||
if (conn) {
|
||||
appContext.apiWrapper.executeCommand('workbench.view.connections');
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -3,24 +3,19 @@
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import { ExtensionContext } from 'vscode';
|
||||
import { ApiWrapper } from '../../../apiWrapper';
|
||||
|
||||
import { azureResource } from '../../azure-resource';
|
||||
import { IAzureResourceDatabaseServerService } from './interfaces';
|
||||
import { IAzureResourceService, AzureResourceDatabaseServer } from '../../interfaces';
|
||||
import { AzureResourceDatabaseServerTreeDataProvider } from './databaseServerTreeDataProvider';
|
||||
|
||||
export class AzureResourceDatabaseServerProvider implements azureResource.IAzureResourceProvider {
|
||||
public constructor(
|
||||
databaseServerService: IAzureResourceDatabaseServerService,
|
||||
apiWrapper: ApiWrapper,
|
||||
extensionContext: ExtensionContext
|
||||
private _databaseServerService: IAzureResourceService<AzureResourceDatabaseServer>,
|
||||
private _apiWrapper: ApiWrapper,
|
||||
private _extensionContext: ExtensionContext
|
||||
) {
|
||||
this._databaseServerService = databaseServerService;
|
||||
this._apiWrapper = apiWrapper;
|
||||
this._extensionContext = extensionContext;
|
||||
}
|
||||
|
||||
public getTreeDataProvider(): azureResource.IAzureResourceTreeDataProvider {
|
||||
@@ -30,8 +25,4 @@ export class AzureResourceDatabaseServerProvider implements azureResource.IAzure
|
||||
public get providerId(): string {
|
||||
return 'azure.resource.providers.databaseServer';
|
||||
}
|
||||
|
||||
private _databaseServerService: IAzureResourceDatabaseServerService = undefined;
|
||||
private _apiWrapper: ApiWrapper = undefined;
|
||||
private _extensionContext: ExtensionContext = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,17 +3,14 @@
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import { ServiceClientCredentials } from 'ms-rest';
|
||||
import { SqlManagementClient } from 'azure-arm-sql';
|
||||
|
||||
import { azureResource } from '../../azure-resource';
|
||||
import { IAzureResourceDatabaseServerService } from './interfaces';
|
||||
import { AzureResourceDatabaseServer } from './models';
|
||||
import { IAzureResourceService, AzureResourceDatabaseServer } from '../../interfaces';
|
||||
|
||||
export class AzureResourceDatabaseServerService implements IAzureResourceDatabaseServerService {
|
||||
public async getDatabaseServers(subscription: azureResource.AzureResourceSubscription, credential: ServiceClientCredentials): Promise<AzureResourceDatabaseServer[]> {
|
||||
export class AzureResourceDatabaseServerService implements IAzureResourceService<AzureResourceDatabaseServer> {
|
||||
public async getResources(subscription: azureResource.AzureResourceSubscription, credential: ServiceClientCredentials): Promise<AzureResourceDatabaseServer[]> {
|
||||
const databaseServers: AzureResourceDatabaseServer[] = [];
|
||||
|
||||
const sqlManagementClient = new SqlManagementClient(credential, subscription.id);
|
||||
|
||||
@@ -3,82 +3,62 @@
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import { AzureResource, ExtensionNodeType } from 'azdata';
|
||||
import { TreeItem, TreeItemCollapsibleState, ExtensionContext } from 'vscode';
|
||||
import { TokenCredentials } from 'ms-rest';
|
||||
import { ExtensionNodeType, TreeItem } from 'azdata';
|
||||
import { TreeItemCollapsibleState, ExtensionContext } from 'vscode';
|
||||
import * as nls from 'vscode-nls';
|
||||
const localize = nls.loadMessageBundle();
|
||||
|
||||
import { azureResource } from '../../azure-resource';
|
||||
import { IAzureResourceDatabaseServerService, IAzureResourceDatabaseServerNode } from './interfaces';
|
||||
import { AzureResourceDatabaseServer } from './models';
|
||||
import { AzureResourceItemType } from '../../../azureResource/constants';
|
||||
import { ApiWrapper } from '../../../apiWrapper';
|
||||
import { generateGuid } from '../../utils';
|
||||
import { IAzureResourceService, AzureResourceDatabaseServer } from '../../interfaces';
|
||||
import { ResourceTreeDataProviderBase } from '../resourceTreeDataProviderBase';
|
||||
import { azureResource } from '../../azure-resource';
|
||||
|
||||
export class AzureResourceDatabaseServerTreeDataProvider extends ResourceTreeDataProviderBase<AzureResourceDatabaseServer> {
|
||||
private static readonly containerId = 'azure.resource.providers.databaseServer.treeDataProvider.databaseServerContainer';
|
||||
private static readonly containerLabel = localize('azure.resource.providers.databaseServer.treeDataProvider.databaseServerContainerLabel', "SQL Servers");
|
||||
|
||||
export class AzureResourceDatabaseServerTreeDataProvider implements azureResource.IAzureResourceTreeDataProvider {
|
||||
public constructor(
|
||||
databaseServerService: IAzureResourceDatabaseServerService,
|
||||
databaseServerService: IAzureResourceService<AzureResourceDatabaseServer>,
|
||||
apiWrapper: ApiWrapper,
|
||||
extensionContext: ExtensionContext
|
||||
private _extensionContext: ExtensionContext
|
||||
) {
|
||||
this._databaseServerService = databaseServerService;
|
||||
this._apiWrapper = apiWrapper;
|
||||
this._extensionContext = extensionContext;
|
||||
super(databaseServerService, apiWrapper);
|
||||
}
|
||||
|
||||
public getTreeItem(element: azureResource.IAzureResourceNode): TreeItem | Thenable<TreeItem> {
|
||||
return element.treeItem;
|
||||
|
||||
protected getTreeItemForResource(databaseServer: AzureResourceDatabaseServer): TreeItem {
|
||||
return {
|
||||
id: `databaseServer_${databaseServer.name}`,
|
||||
label: databaseServer.name,
|
||||
iconPath: {
|
||||
dark: this._extensionContext.asAbsolutePath('resources/dark/sql_server_inverse.svg'),
|
||||
light: this._extensionContext.asAbsolutePath('resources/light/sql_server.svg')
|
||||
},
|
||||
collapsibleState: TreeItemCollapsibleState.Collapsed,
|
||||
contextValue: AzureResourceItemType.databaseServer,
|
||||
payload: {
|
||||
id: generateGuid(),
|
||||
connectionName: undefined,
|
||||
serverName: databaseServer.fullName,
|
||||
databaseName: databaseServer.defaultDatabaseName,
|
||||
userName: databaseServer.loginName,
|
||||
password: '',
|
||||
authenticationType: 'SqlLogin',
|
||||
savePassword: true,
|
||||
groupFullName: '',
|
||||
groupId: '',
|
||||
providerName: 'MSSQL',
|
||||
saveProfile: false,
|
||||
options: {}
|
||||
},
|
||||
childProvider: 'MSSQL',
|
||||
type: ExtensionNodeType.Server
|
||||
};
|
||||
}
|
||||
|
||||
public async getChildren(element?: azureResource.IAzureResourceNode): Promise<azureResource.IAzureResourceNode[]> {
|
||||
if (!element) {
|
||||
return [this.createContainerNode()];
|
||||
}
|
||||
|
||||
const tokens = await this._apiWrapper.getSecurityToken(element.account, AzureResource.ResourceManagement);
|
||||
const credential = new TokenCredentials(tokens[element.tenantId].token, tokens[element.tenantId].tokenType);
|
||||
|
||||
const databaseServers: AzureResourceDatabaseServer[] = (await this._databaseServerService.getDatabaseServers(element.subscription, credential)) || <AzureResourceDatabaseServer[]>[];
|
||||
|
||||
return databaseServers.map((databaseServer) => <IAzureResourceDatabaseServerNode>{
|
||||
account: element.account,
|
||||
subscription: element.subscription,
|
||||
tenantId: element.tenantId,
|
||||
databaseServer: databaseServer,
|
||||
treeItem: {
|
||||
id: `databaseServer_${databaseServer.name}`,
|
||||
label: databaseServer.name,
|
||||
iconPath: {
|
||||
dark: this._extensionContext.asAbsolutePath('resources/dark/sql_server_inverse.svg'),
|
||||
light: this._extensionContext.asAbsolutePath('resources/light/sql_server.svg')
|
||||
},
|
||||
collapsibleState: TreeItemCollapsibleState.Collapsed,
|
||||
contextValue: AzureResourceItemType.databaseServer,
|
||||
payload: {
|
||||
id: generateGuid(),
|
||||
connectionName: undefined,
|
||||
serverName: databaseServer.fullName,
|
||||
databaseName: databaseServer.defaultDatabaseName,
|
||||
userName: databaseServer.loginName,
|
||||
password: '',
|
||||
authenticationType: 'SqlLogin',
|
||||
savePassword: true,
|
||||
groupFullName: '',
|
||||
groupId: '',
|
||||
providerName: 'MSSQL',
|
||||
saveProfile: false,
|
||||
options: {}
|
||||
},
|
||||
childProvider: 'MSSQL',
|
||||
type: ExtensionNodeType.Server
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private createContainerNode(): azureResource.IAzureResourceNode {
|
||||
protected createContainerNode(): azureResource.IAzureResourceNode {
|
||||
return {
|
||||
account: undefined,
|
||||
subscription: undefined,
|
||||
@@ -95,11 +75,4 @@ export class AzureResourceDatabaseServerTreeDataProvider implements azureResourc
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private _databaseServerService: IAzureResourceDatabaseServerService = undefined;
|
||||
private _apiWrapper: ApiWrapper = undefined;
|
||||
private _extensionContext: ExtensionContext = undefined;
|
||||
|
||||
private static readonly containerId = 'azure.resource.providers.databaseServer.treeDataProvider.databaseServerContainer';
|
||||
private static readonly containerLabel = localize('azure.resource.providers.databaseServer.treeDataProvider.databaseServerContainerLabel', "SQL Servers");
|
||||
}
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* 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 { ServiceClientCredentials } from 'ms-rest';
|
||||
|
||||
import { azureResource } from '../../azure-resource';
|
||||
import { AzureResourceDatabaseServer } from './models';
|
||||
|
||||
export interface IAzureResourceDatabaseServerService {
|
||||
getDatabaseServers(subscription: azureResource.AzureResourceSubscription, credentials: ServiceClientCredentials): Promise<AzureResourceDatabaseServer[]>;
|
||||
}
|
||||
|
||||
export interface IAzureResourceDatabaseServerNode extends azureResource.IAzureResourceNode {
|
||||
readonly databaseServer: AzureResourceDatabaseServer;
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
'use strict';
|
||||
|
||||
export interface AzureResourceDatabaseServer {
|
||||
name: string;
|
||||
fullName: string;
|
||||
loginName: string;
|
||||
defaultDatabaseName: string;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { AzureResource, TreeItem } from 'azdata';
|
||||
import { TokenCredentials } from 'ms-rest';
|
||||
|
||||
import { azureResource } from '../azure-resource';
|
||||
import { ApiWrapper } from '../../apiWrapper';
|
||||
import { IAzureResourceService, AzureSqlResource } from '../interfaces';
|
||||
|
||||
export abstract class ResourceTreeDataProviderBase<T extends AzureSqlResource> implements azureResource.IAzureResourceTreeDataProvider {
|
||||
|
||||
public constructor(
|
||||
protected _resourceService: IAzureResourceService<T>,
|
||||
protected _apiWrapper: ApiWrapper,
|
||||
) {
|
||||
}
|
||||
|
||||
public getTreeItem(element: azureResource.IAzureResourceNode): TreeItem | Thenable<TreeItem> {
|
||||
return element.treeItem;
|
||||
}
|
||||
|
||||
public async getChildren(element?: azureResource.IAzureResourceNode): Promise<azureResource.IAzureResourceNode[]> {
|
||||
if (!element) {
|
||||
return [this.createContainerNode()];
|
||||
}
|
||||
|
||||
const tokens = await this._apiWrapper.getSecurityToken(element.account, AzureResource.ResourceManagement);
|
||||
const credential = new TokenCredentials(tokens[element.tenantId].token, tokens[element.tenantId].tokenType);
|
||||
|
||||
const resources: T[] = (await this._resourceService.getResources(element.subscription, credential)) || <T[]>[];
|
||||
|
||||
return resources.map((resource) => <azureResource.IAzureResourceNode>{
|
||||
account: element.account,
|
||||
subscription: element.subscription,
|
||||
tenantId: element.tenantId,
|
||||
treeItem: this.getTreeItemForResource(resource)
|
||||
});
|
||||
}
|
||||
|
||||
protected abstract getTreeItemForResource(resource: T): TreeItem;
|
||||
|
||||
protected abstract createContainerNode(): azureResource.IAzureResourceNode;
|
||||
}
|
||||
@@ -26,8 +26,6 @@ import { AzureResourceSubscriptionFilterService } from './azureResource/services
|
||||
import { AzureResourceCacheService } from './azureResource/services/cacheService';
|
||||
import { AzureResourceTenantService } from './azureResource/services/tenantService';
|
||||
import { registerAzureResourceCommands } from './azureResource/commands';
|
||||
import { registerAzureResourceDatabaseServerCommands } from './azureResource/providers/databaseServer/commands';
|
||||
import { registerAzureResourceDatabaseCommands } from './azureResource/providers/database/commands';
|
||||
import { AzureResourceTreeProvider } from './azureResource/tree/treeProvider';
|
||||
|
||||
let extensionContext: vscode.ExtensionContext;
|
||||
@@ -70,7 +68,7 @@ export async function activate(context: vscode.ExtensionContext) {
|
||||
registerAzureServices(appContext);
|
||||
const azureResourceTree = new AzureResourceTreeProvider(appContext);
|
||||
pushDisposable(apiWrapper.registerTreeDataProvider('azureResourceExplorer', azureResourceTree));
|
||||
registerCommands(appContext, azureResourceTree);
|
||||
registerAzureResourceCommands(appContext, azureResourceTree);
|
||||
|
||||
return {
|
||||
provideResources() {
|
||||
@@ -129,10 +127,3 @@ function registerAzureServices(appContext: AppContext): void {
|
||||
appContext.registerService<IAzureResourceTenantService>(AzureResourceServiceNames.tenantService, new AzureResourceTenantService());
|
||||
}
|
||||
|
||||
function registerCommands(appContext: AppContext, azureResourceTree: AzureResourceTreeProvider): void {
|
||||
registerAzureResourceCommands(appContext, azureResourceTree);
|
||||
|
||||
registerAzureResourceDatabaseServerCommands(appContext);
|
||||
|
||||
registerAzureResourceDatabaseCommands(appContext);
|
||||
}
|
||||
|
||||
@@ -11,13 +11,12 @@ import 'mocha';
|
||||
|
||||
import { azureResource } from '../../../../azureResource/azure-resource';
|
||||
import { ApiWrapper } from '../../../../apiWrapper';
|
||||
import { IAzureResourceDatabaseService } from '../../../../azureResource/providers/database/interfaces';
|
||||
import { AzureResourceDatabaseTreeDataProvider } from '../../../../azureResource/providers/database/databaseTreeDataProvider';
|
||||
import { AzureResourceDatabase } from '../../../../azureResource/providers/database/models';
|
||||
import { AzureResourceItemType } from '../../../../azureResource/constants';
|
||||
import { IAzureResourceService, AzureResourceDatabase } from '../../../../azureResource/interfaces';
|
||||
|
||||
// Mock services
|
||||
let mockDatabaseService: TypeMoq.IMock<IAzureResourceDatabaseService>;
|
||||
let mockDatabaseService: TypeMoq.IMock<IAzureResourceService<AzureResourceDatabase>>;
|
||||
let mockApiWrapper: TypeMoq.IMock<ApiWrapper>;
|
||||
let mockExtensionContext: TypeMoq.IMock<vscode.ExtensionContext>;
|
||||
|
||||
@@ -80,7 +79,7 @@ const mockDatabases: AzureResourceDatabase[] = [
|
||||
|
||||
describe('AzureResourceDatabaseTreeDataProvider.info', function (): void {
|
||||
beforeEach(() => {
|
||||
mockDatabaseService = TypeMoq.Mock.ofType<IAzureResourceDatabaseService>();
|
||||
mockDatabaseService = TypeMoq.Mock.ofType<IAzureResourceService<AzureResourceDatabase>>();
|
||||
mockApiWrapper = TypeMoq.Mock.ofType<ApiWrapper>();
|
||||
mockExtensionContext = TypeMoq.Mock.ofType<vscode.ExtensionContext>();
|
||||
});
|
||||
@@ -98,12 +97,12 @@ describe('AzureResourceDatabaseTreeDataProvider.info', function (): void {
|
||||
|
||||
describe('AzureResourceDatabaseTreeDataProvider.getChildren', function (): void {
|
||||
beforeEach(() => {
|
||||
mockDatabaseService = TypeMoq.Mock.ofType<IAzureResourceDatabaseService>();
|
||||
mockDatabaseService = TypeMoq.Mock.ofType<IAzureResourceService<AzureResourceDatabase>>();
|
||||
mockApiWrapper = TypeMoq.Mock.ofType<ApiWrapper>();
|
||||
mockExtensionContext = TypeMoq.Mock.ofType<vscode.ExtensionContext>();
|
||||
|
||||
mockApiWrapper.setup((o) => o.getSecurityToken(mockAccount, azdata.AzureResource.ResourceManagement)).returns(() => Promise.resolve(mockTokens));
|
||||
mockDatabaseService.setup((o) => o.getDatabases(mockSubscription, TypeMoq.It.isAny())).returns(() => Promise.resolve(mockDatabases));
|
||||
mockDatabaseService.setup((o) => o.getResources(mockSubscription, TypeMoq.It.isAny())).returns(() => Promise.resolve(mockDatabases));
|
||||
mockExtensionContext.setup((o) => o.asAbsolutePath(TypeMoq.It.isAnyString())).returns(() => TypeMoq.It.isAnyString());
|
||||
});
|
||||
|
||||
|
||||
@@ -11,13 +11,12 @@ import 'mocha';
|
||||
|
||||
import { azureResource } from '../../../../azureResource/azure-resource';
|
||||
import { ApiWrapper } from '../../../../apiWrapper';
|
||||
import { IAzureResourceDatabaseServerService } from '../../../../azureResource/providers/databaseServer/interfaces';
|
||||
import { AzureResourceDatabaseServerTreeDataProvider } from '../../../../azureResource/providers/databaseServer/databaseServerTreeDataProvider';
|
||||
import { AzureResourceDatabaseServer } from '../../../../azureResource/providers/databaseServer/models';
|
||||
import { AzureResourceItemType } from '../../../../azureResource/constants';
|
||||
import { IAzureResourceService, AzureResourceDatabaseServer } from '../../../../azureResource/interfaces';
|
||||
|
||||
// Mock services
|
||||
let mockDatabaseServerService: TypeMoq.IMock<IAzureResourceDatabaseServerService>;
|
||||
let mockDatabaseServerService: TypeMoq.IMock<IAzureResourceService<AzureResourceDatabaseServer>>;
|
||||
let mockApiWrapper: TypeMoq.IMock<ApiWrapper>;
|
||||
let mockExtensionContext: TypeMoq.IMock<vscode.ExtensionContext>;
|
||||
|
||||
@@ -80,7 +79,7 @@ const mockDatabaseServers: AzureResourceDatabaseServer[] = [
|
||||
|
||||
describe('AzureResourceDatabaseServerTreeDataProvider.info', function (): void {
|
||||
beforeEach(() => {
|
||||
mockDatabaseServerService = TypeMoq.Mock.ofType<IAzureResourceDatabaseServerService>();
|
||||
mockDatabaseServerService = TypeMoq.Mock.ofType<IAzureResourceService<AzureResourceDatabaseServer>>();
|
||||
mockApiWrapper = TypeMoq.Mock.ofType<ApiWrapper>();
|
||||
mockExtensionContext = TypeMoq.Mock.ofType<vscode.ExtensionContext>();
|
||||
});
|
||||
@@ -98,12 +97,12 @@ describe('AzureResourceDatabaseServerTreeDataProvider.info', function (): void {
|
||||
|
||||
describe('AzureResourceDatabaseServerTreeDataProvider.getChildren', function (): void {
|
||||
beforeEach(() => {
|
||||
mockDatabaseServerService = TypeMoq.Mock.ofType<IAzureResourceDatabaseServerService>();
|
||||
mockDatabaseServerService = TypeMoq.Mock.ofType<IAzureResourceService<AzureResourceDatabaseServer>>();
|
||||
mockApiWrapper = TypeMoq.Mock.ofType<ApiWrapper>();
|
||||
mockExtensionContext = TypeMoq.Mock.ofType<vscode.ExtensionContext>();
|
||||
|
||||
mockApiWrapper.setup((o) => o.getSecurityToken(mockAccount, azdata.AzureResource.ResourceManagement)).returns(() => Promise.resolve(mockTokens));
|
||||
mockDatabaseServerService.setup((o) => o.getDatabaseServers(mockSubscription, TypeMoq.It.isAny())).returns(() => Promise.resolve(mockDatabaseServers));
|
||||
mockDatabaseServerService.setup((o) => o.getResources(mockSubscription, TypeMoq.It.isAny())).returns(() => Promise.resolve(mockDatabaseServers));
|
||||
mockExtensionContext.setup((o) => o.asAbsolutePath(TypeMoq.It.isAnyString())).returns(() => TypeMoq.It.isAnyString());
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user