From 3860f07cab2f4fc86e93cb68863c9d78d8546f7a Mon Sep 17 00:00:00 2001 From: Amir Omidi Date: Wed, 3 Jun 2020 14:02:23 -0700 Subject: [PATCH] Make azure portal work from server area (#10699) --- .../azurecore/src/azureResource/commands.ts | 17 ++++------ .../connection/common/connectionProfile.ts | 22 ++++++++++++- .../azure/browser/azure.contribution.ts | 33 ++++++++++++++++++- .../connection/common/connectionContextKey.ts | 15 +++++++++ 4 files changed, 74 insertions(+), 13 deletions(-) diff --git a/extensions/azurecore/src/azureResource/commands.ts b/extensions/azurecore/src/azureResource/commands.ts index 39757c0e83..edc5eb1784 100644 --- a/extensions/azurecore/src/azureResource/commands.ts +++ b/extensions/azurecore/src/azureResource/commands.ts @@ -238,18 +238,13 @@ export function registerAzureResourceCommands(appContext: AppContext, tree: Azur } }); - appContext.apiWrapper.registerCommand('azure.resource.openInAzurePortal', async (node?: TreeNode) => { - if (!node) { - return; - } + appContext.apiWrapper.registerCommand('azure.resource.openInAzurePortal', async (connectionProfile: azdata.IConnectionProfile) => { - const treeItem: azdata.TreeItem = await node.getTreeItem(); - if (!treeItem.payload) { - return; - } - let connectionProfile = Object.assign({}, treeItem.payload, { saveProfile: true }); - - if (!connectionProfile.azureResourceId) { + if ( + !connectionProfile.azureResourceId || + !connectionProfile.azurePortalEndpoint || + !connectionProfile.azureTenantId + ) { return; } diff --git a/src/sql/platform/connection/common/connectionProfile.ts b/src/sql/platform/connection/common/connectionProfile.ts index 696434270a..8f41947ecc 100644 --- a/src/sql/platform/connection/common/connectionProfile.ts +++ b/src/sql/platform/connection/common/connectionProfile.ts @@ -43,6 +43,8 @@ export class ConnectionProfile extends ProviderConnectionInfo implements interfa this._id = model.id; this.azureTenantId = model.azureTenantId; this.azureAccount = model.azureAccount; + this.azureResourceId = model.azureResourceId; + this.azurePortalEndpoint = model.azurePortalEndpoint; if (this.capabilitiesService && model.providerName) { let capabilities = this.capabilitiesService.getCapabilities(model.providerName); if (capabilities && capabilities.connection && capabilities.connection.connectionOptions) { @@ -125,6 +127,22 @@ export class ConnectionProfile extends ProviderConnectionInfo implements interfa this.options['azureAccount'] = value; } + public get azurePortalEndpoint() { + return this.options['azurePortalEndpoint']; + } + + public set azurePortalEndpoint(value: string | undefined) { + this.options['azurePortalEndpoint'] = value; + } + + public get azureResourceId() { + return this.options['azureResourceId']; + } + + public set azureResourceId(value: string | undefined) { + this.options['azureResourceId'] = value; + } + public get registeredServerDescription(): string { return this.options['registeredServerDescription']; } @@ -210,7 +228,9 @@ export class ConnectionProfile extends ProviderConnectionInfo implements interfa saveProfile: this.saveProfile, id: this.id, azureTenantId: this.azureTenantId, - azureAccount: this.azureAccount + azureAccount: this.azureAccount, + azurePortalEndpoint: this.azurePortalEndpoint, + azureResourceId: this.azureResourceId }; return result; diff --git a/src/sql/workbench/contrib/azure/browser/azure.contribution.ts b/src/sql/workbench/contrib/azure/browser/azure.contribution.ts index 856ae4adca..09d6ed11b3 100644 --- a/src/sql/workbench/contrib/azure/browser/azure.contribution.ts +++ b/src/sql/workbench/contrib/azure/browser/azure.contribution.ts @@ -5,14 +5,45 @@ import { MenuRegistry, MenuId } from 'vs/platform/actions/common/actions'; import { localize } from 'vs/nls'; import { MssqlNodeContext } from 'sql/workbench/services/objectExplorer/browser/mssqlNodeContext'; +import { ConnectionContextKey } from 'sql/workbench/services/connection/common/connectionContextKey'; +import { CommandsRegistry, ICommandService } from 'vs/platform/commands/common/commands'; +import { TreeViewItemHandleArg } from 'sql/workbench/common/views'; +import { ObjectExplorerActionsContext } from 'sql/workbench/services/objectExplorer/browser/objectExplorerActions'; +const openInAzureDECommandId: string = 'azure.openInAzureCoreDE'; MenuRegistry.appendMenuItem(MenuId.DataExplorerContext, { group: 'z-azurecore', order: 1, command: { - id: 'azure.resource.openInAzurePortal', + id: openInAzureDECommandId, title: localize('azure.openInAzurePortal.title', "Open in Azure Portal") }, when: MssqlNodeContext.CanOpenInAzurePortal }); +CommandsRegistry.registerCommand({ + id: openInAzureDECommandId, + handler: (accessor, args: TreeViewItemHandleArg) => { + const commandService = accessor.get(ICommandService); + return commandService.executeCommand('azure.resource.openInAzurePortal', args.$treeItem.payload); + } +}); + +const openInAzureOECommandId: string = 'azure.openInAzureCoreOE'; +MenuRegistry.appendMenuItem(MenuId.ObjectExplorerItemContext, { + group: 'z-azurecore', + order: 1, + command: { + id: openInAzureOECommandId, + title: localize('azure.openInAzurePortal.title', "Open in Azure Portal") + }, + when: ConnectionContextKey.CanOpenInAzurePortal +}); + +CommandsRegistry.registerCommand({ + id: openInAzureOECommandId, + handler: (accessor, args: ObjectExplorerActionsContext) => { + const commandService = accessor.get(ICommandService); + return commandService.executeCommand('azure.resource.openInAzurePortal', args.connectionProfile); + } +}); diff --git a/src/sql/workbench/services/connection/common/connectionContextKey.ts b/src/sql/workbench/services/connection/common/connectionContextKey.ts index 0c40c82db7..d7e0fd8a9f 100644 --- a/src/sql/workbench/services/connection/common/connectionContextKey.ts +++ b/src/sql/workbench/services/connection/common/connectionContextKey.ts @@ -14,12 +14,15 @@ export class ConnectionContextKey implements IContextKey { static Database = new RawContextKey('databaseName', undefined); static Connection = new RawContextKey('connection', undefined); static IsQueryProvider = new RawContextKey('isQueryProvider', false); + static CanOpenInAzurePortal = new RawContextKey('canOpenInAzurePortal', false); private _providerKey: IContextKey; private _serverKey: IContextKey; private _databaseKey: IContextKey; private _connectionKey: IContextKey; private _isQueryProviderKey: IContextKey; + private _canOpenInAzurePortal: IContextKey; + constructor( @IContextKeyService contextKeyService: IContextKeyService, @@ -30,9 +33,11 @@ export class ConnectionContextKey implements IContextKey { this._databaseKey = ConnectionContextKey.Database.bindTo(contextKeyService); this._connectionKey = ConnectionContextKey.Connection.bindTo(contextKeyService); this._isQueryProviderKey = ConnectionContextKey.IsQueryProvider.bindTo(contextKeyService); + this._canOpenInAzurePortal = ConnectionContextKey.CanOpenInAzurePortal.bindTo(contextKeyService); } set(value: IConnectionProfile) { + this.setCanOpenInPortal(value); let queryProviders = this.queryManagementService.getRegisteredProviders(); this._connectionKey.set(value); this._providerKey.set(value && value.providerName); @@ -41,12 +46,22 @@ export class ConnectionContextKey implements IContextKey { this._isQueryProviderKey.set(value && value.providerName && queryProviders.indexOf(value.providerName) !== -1); } + private setCanOpenInPortal(connectionProfile: IConnectionProfile): void { + if (connectionProfile && + connectionProfile.azureResourceId && + connectionProfile.azureTenantId && + connectionProfile.azurePortalEndpoint) { + this._canOpenInAzurePortal.set(true); + } + } + reset(): void { this._providerKey.reset(); this._serverKey.reset(); this._databaseKey.reset(); this._connectionKey.reset(); this._isQueryProviderKey.reset(); + this._canOpenInAzurePortal.reset(); } public get(): IConnectionProfile {