simplify object management feature APIs (#22781)

This commit is contained in:
Alan Ren
2023-04-19 19:26:29 -07:00
committed by GitHub
parent 34d092a7dd
commit decbe8dded
14 changed files with 413 additions and 578 deletions

View File

@@ -9,22 +9,20 @@ import * as vscode from 'vscode';
import { LoginDialog } from './ui/loginDialog';
import { TestObjectManagementService } from './objectManagementService';
import { getErrorMessage } from '../utils';
import { NodeType, TelemetryActions, TelemetryViews } from './constants';
import { FolderType, TelemetryActions, ObjectManagementViewName } from './constants';
import * as localizedConstants from './localizedConstants';
import { UserDialog } from './ui/userDialog';
import { IObjectManagementService } from 'mssql';
import { IObjectManagementService, ObjectManagement } from 'mssql';
import * as constants from '../constants';
import { getNodeTypeDisplayName, refreshParentNode } from './utils';
import { TelemetryReporter } from '../telemetry';
import { ObjectManagementDialogBase, ObjectManagementDialogOptions } from './ui/objectManagementDialogBase';
export function registerObjectManagementCommands(appContext: AppContext) {
// Notes: Change the second parameter to false to use the actual object management service.
const service = getObjectManagementService(appContext, false);
appContext.extensionContext.subscriptions.push(vscode.commands.registerCommand('mssql.newLogin', async (context: azdata.ObjectExplorerContext) => {
await handleNewLoginDialogCommand(context, service);
}));
appContext.extensionContext.subscriptions.push(vscode.commands.registerCommand('mssql.newUser', async (context: azdata.ObjectExplorerContext) => {
await handleNewUserDialogCommand(context, service);
appContext.extensionContext.subscriptions.push(vscode.commands.registerCommand('mssql.newObject', async (context: azdata.ObjectExplorerContext) => {
await handleNewObjectDialogCommand(context, service);
}));
appContext.extensionContext.subscriptions.push(vscode.commands.registerCommand('mssql.objectProperties', async (context: azdata.ObjectExplorerContext) => {
await handleObjectPropertiesDialogCommand(context, service);
@@ -45,41 +43,45 @@ function getObjectManagementService(appContext: AppContext, useTestService: bool
}
}
async function handleNewLoginDialogCommand(context: azdata.ObjectExplorerContext, service: IObjectManagementService): Promise<void> {
async function handleNewObjectDialogCommand(context: azdata.ObjectExplorerContext, service: IObjectManagementService): Promise<void> {
const connectionUri = await getConnectionUri(context);
if (!connectionUri) {
return;
}
let newObjectType: ObjectManagement.NodeType;
switch (context.nodeInfo!.objectType) {
case FolderType.ServerLevelLogins:
newObjectType = ObjectManagement.NodeType.ServerLevelLogin;
break;
case FolderType.Users:
newObjectType = ObjectManagement.NodeType.User;
break;
default:
throw new Error(`Unsupported folder type: ${context.nodeInfo!.objectType}`);
}
try {
const dialog = new LoginDialog(service, connectionUri, true, undefined, context);
const parentUrn = await getParentUrn(context);
const options: ObjectManagementDialogOptions = {
connectionUri: connectionUri,
isNewObject: true,
database: context.connectionProfile!.databaseName!,
objectType: newObjectType,
objectName: '',
parentUrn: parentUrn,
objectExplorerContext: context
};
const dialog = getDialog(service, options);
await dialog.open();
}
catch (err) {
TelemetryReporter.createErrorEvent2(TelemetryViews.ObjectManagement, TelemetryActions.OpenNewObjectDialog, err).withAdditionalProperties({
objectType: NodeType.Login
TelemetryReporter.createErrorEvent2(ObjectManagementViewName, TelemetryActions.OpenNewObjectDialog, err).withAdditionalProperties({
objectType: context.nodeInfo!.nodeType
}).send();
await vscode.window.showErrorMessage(localizedConstants.OpenNewObjectDialogError(localizedConstants.LoginTypeDisplayName, getErrorMessage(err)));
}
}
async function handleNewUserDialogCommand(context: azdata.ObjectExplorerContext, service: IObjectManagementService): Promise<void> {
const connectionUri = await getConnectionUri(context);
if (!connectionUri) {
return;
}
try {
const dialog = new UserDialog(service, connectionUri, context.connectionProfile!.databaseName!, true, undefined, context);
await dialog.open();
}
catch (err) {
TelemetryReporter.createErrorEvent2(TelemetryViews.ObjectManagement, TelemetryActions.OpenNewObjectDialog, err).withAdditionalProperties({
objectType: NodeType.User
}).send();
await vscode.window.showErrorMessage(localizedConstants.OpenNewObjectDialogError(localizedConstants.UserTypeDisplayName, getErrorMessage(err)));
}
}
async function handleObjectPropertiesDialogCommand(context: azdata.ObjectExplorerContext, service: IObjectManagementService): Promise<void> {
const connectionUri = await getConnectionUri(context);
if (!connectionUri) {
@@ -87,23 +89,22 @@ async function handleObjectPropertiesDialogCommand(context: azdata.ObjectExplore
}
const nodeTypeDisplayName = getNodeTypeDisplayName(context.nodeInfo!.nodeType);
try {
let dialog;
switch (context.nodeInfo!.nodeType) {
case NodeType.Login:
dialog = new LoginDialog(service, connectionUri, false, context.nodeInfo!.label);
break;
case NodeType.User:
dialog = new UserDialog(service, connectionUri, context.connectionProfile!.databaseName!, false, context.nodeInfo!.label);
break;
default:
break;
}
if (dialog) {
await dialog.open();
}
const parentUrn = await getParentUrn(context);
const options: ObjectManagementDialogOptions = {
connectionUri: connectionUri,
isNewObject: false,
database: context.connectionProfile!.databaseName!,
objectType: context.nodeInfo.nodeType as ObjectManagement.NodeType,
objectName: context.nodeInfo.label,
parentUrn: parentUrn,
objectUrn: context.nodeInfo!.metadata!.urn,
objectExplorerContext: context
};
const dialog = getDialog(service, options);
await dialog.open();
}
catch (err) {
TelemetryReporter.createErrorEvent2(TelemetryViews.ObjectManagement, TelemetryActions.OpenPropertiesDialog, err).withAdditionalProperties({
TelemetryReporter.createErrorEvent2(ObjectManagementViewName, TelemetryActions.OpenPropertiesDialog, err).withAdditionalProperties({
objectType: context.nodeInfo!.nodeType
}).send();
await vscode.window.showErrorMessage(localizedConstants.OpenObjectPropertiesDialogError(nodeTypeDisplayName, context.nodeInfo!.label, getErrorMessage(err)));
@@ -117,7 +118,7 @@ async function handleDeleteObjectCommand(context: azdata.ObjectExplorerContext,
}
let additionalConfirmationMessage: string | undefined = undefined;
switch (context.nodeInfo!.nodeType) {
case NodeType.Login:
case ObjectManagement.NodeType.ServerLevelLogin:
additionalConfirmationMessage = localizedConstants.DeleteLoginConfirmationText;
break;
default:
@@ -139,7 +140,7 @@ async function handleDeleteObjectCommand(context: azdata.ObjectExplorerContext,
operation: async (operation) => {
try {
const startTime = Date.now();
await service.drop(connectionUri, context.nodeInfo!.metadata!.urn);
await service.drop(connectionUri, context.nodeInfo.nodeType as ObjectManagement.NodeType, context.nodeInfo!.metadata!.urn);
TelemetryReporter.sendTelemetryEvent(TelemetryActions.DeleteObject, {
objectType: context.nodeInfo!.nodeType
}, {
@@ -148,7 +149,7 @@ async function handleDeleteObjectCommand(context: azdata.ObjectExplorerContext,
}
catch (err) {
operation.updateStatus(azdata.TaskStatus.Failed, localizedConstants.DeleteObjectError(nodeTypeDisplayName, context.nodeInfo!.label, getErrorMessage(err)));
TelemetryReporter.createErrorEvent2(TelemetryViews.ObjectManagement, TelemetryActions.DeleteObject, err).withAdditionalProperties({
TelemetryReporter.createErrorEvent2(ObjectManagementViewName, TelemetryActions.DeleteObject, err).withAdditionalProperties({
objectType: context.nodeInfo!.nodeType
}).send();
return;
@@ -191,7 +192,7 @@ async function handleRenameObjectCommand(context: azdata.ObjectExplorerContext,
operation: async (operation) => {
try {
const startTime = Date.now();
await service.rename(connectionUri, context.nodeInfo!.metadata!.urn, newName);
await service.rename(connectionUri, context.nodeInfo.nodeType as ObjectManagement.NodeType, context.nodeInfo!.metadata!.urn, newName);
TelemetryReporter.sendTelemetryEvent(TelemetryActions.RenameObject, {
objectType: context.nodeInfo!.nodeType
}, {
@@ -200,7 +201,7 @@ async function handleRenameObjectCommand(context: azdata.ObjectExplorerContext,
}
catch (err) {
operation.updateStatus(azdata.TaskStatus.Failed, localizedConstants.RenameObjectError(nodeTypeDisplayName, originalName, newName, getErrorMessage(err)));
TelemetryReporter.createErrorEvent2(TelemetryViews.ObjectManagement, TelemetryActions.RenameObject, err).withAdditionalProperties({
TelemetryReporter.createErrorEvent2(ObjectManagementViewName, TelemetryActions.RenameObject, err).withAdditionalProperties({
objectType: context.nodeInfo!.nodeType
}).send();
return;
@@ -211,6 +212,17 @@ async function handleRenameObjectCommand(context: azdata.ObjectExplorerContext,
});
}
function getDialog(service: IObjectManagementService, dialogOptions: ObjectManagementDialogOptions): ObjectManagementDialogBase<ObjectManagement.SqlObject, ObjectManagement.ObjectViewInfo<ObjectManagement.SqlObject>> {
switch (dialogOptions.objectType) {
case ObjectManagement.NodeType.ServerLevelLogin:
return new LoginDialog(service, dialogOptions);
case ObjectManagement.NodeType.User:
return new UserDialog(service, dialogOptions);
default:
throw new Error(`Unsupported object type: ${dialogOptions.objectType}`);
}
}
async function getConnectionUri(context: azdata.ObjectExplorerContext): Promise<string> {
const connectionUri = await azdata.connection.getUriForConnection(context.connectionProfile!.id);
if (!connectionUri) {
@@ -218,3 +230,13 @@ async function getConnectionUri(context: azdata.ObjectExplorerContext): Promise<
}
return connectionUri;
}
async function getParentUrn(context: azdata.ObjectExplorerContext): Promise<string> {
let node = undefined;
let currentNodePath = context.nodeInfo!.parentNodePath;
do {
node = await azdata.objectexplorer.getNode(context.connectionProfile!.id, currentNodePath);
currentNodePath = node?.parentNodePath;
} while (node && currentNodePath && !node.metadata?.urn);
return node?.metadata?.urn;
}