support scripting in object management dialogs (#22429)

* user management - scripting

* remove confirmation

* update sts

* update string
This commit is contained in:
Alan Ren
2023-04-14 13:52:06 -07:00
committed by GitHub
parent d69e5b97df
commit 9456285c65
8 changed files with 126 additions and 9 deletions

View File

@@ -1,6 +1,6 @@
{ {
"downloadUrl": "https://github.com/Microsoft/sqltoolsservice/releases/download/{#version#}/microsoft.sqltools.servicelayer-{#fileName#}", "downloadUrl": "https://github.com/Microsoft/sqltoolsservice/releases/download/{#version#}/microsoft.sqltools.servicelayer-{#fileName#}",
"version": "4.7.0.8", "version": "4.7.0.10",
"downloadFileNames": { "downloadFileNames": {
"Windows_86": "win-x86-net7.0.zip", "Windows_86": "win-x86-net7.0.zip",
"Windows_64": "win-x64-net7.0.zip", "Windows_64": "win-x64-net7.0.zip",

View File

@@ -1538,6 +1538,15 @@ export namespace CreateLoginRequest {
export const type = new RequestType<CreateLoginRequestParams, void, void, void>('objectManagement/createLogin'); export const type = new RequestType<CreateLoginRequestParams, void, void, void>('objectManagement/createLogin');
} }
export interface ScriptLoginRequestParams {
contextId: string;
login: mssql.ObjectManagement.Login;
}
export namespace ScriptLoginRequest {
export const type = new RequestType<ScriptLoginRequestParams, string, void, void>('objectManagement/scriptLogin');
}
export interface UpdateLoginRequestParams { export interface UpdateLoginRequestParams {
contextId: string; contextId: string;
login: mssql.ObjectManagement.Login; login: mssql.ObjectManagement.Login;
@@ -1576,6 +1585,15 @@ export namespace CreateUserRequest {
export const type = new RequestType<CreateUserRequestParams, void, void, void>('objectManagement/createUser'); export const type = new RequestType<CreateUserRequestParams, void, void, void>('objectManagement/createUser');
} }
export interface ScriptUserRequestParams {
contextId: string;
user: mssql.ObjectManagement.User;
}
export namespace ScriptUserRequest {
export const type = new RequestType<ScriptUserRequestParams, string, void, void>('objectManagement/scriptUser');
}
export interface UpdateUserRequestParams { export interface UpdateUserRequestParams {
contextId: string; contextId: string;
user: mssql.ObjectManagement.User; user: mssql.ObjectManagement.User;

View File

@@ -1185,6 +1185,12 @@ declare module 'mssql' {
* @param login The login information. * @param login The login information.
*/ */
updateLogin(contextId: string, login: ObjectManagement.Login): Thenable<void>; updateLogin(contextId: string, login: ObjectManagement.Login): Thenable<void>;
/**
* Script a login.
* @param contextId The login view's context id.
* @param login The login information.
*/
scriptLogin(contextId: string, login: ObjectManagement.Login): Thenable<string>;
/** /**
* Dispose the login view. * Dispose the login view.
* @param contextId The id of the view. * @param contextId The id of the view.
@@ -1211,6 +1217,12 @@ declare module 'mssql' {
* @param user The user information. * @param user The user information.
*/ */
updateUser(contextId: string, user: ObjectManagement.User): Thenable<void>; updateUser(contextId: string, user: ObjectManagement.User): Thenable<void>;
/**
* Script a user.
* @param contextId Id of the view.
* @param user The user information.
*/
scriptUser(contextId: string, user: ObjectManagement.User): Thenable<string>;
/** /**
* Dispose the user view. * Dispose the user view.
* @param contextId The id of the view. * @param contextId The id of the view.

View File

@@ -23,6 +23,9 @@ export const OkText: string = localize('objectManagement.OkText', "OK");
export const LoadingDialogText: string = localize('objectManagement.loadingDialog', "Loading dialog..."); export const LoadingDialogText: string = localize('objectManagement.loadingDialog', "Loading dialog...");
export const FailedToRetrieveConnectionInfoErrorMessage: string = localize('objectManagement.noConnectionUriError', "Failed to retrieve the connection information, please reconnect and try again.") export const FailedToRetrieveConnectionInfoErrorMessage: string = localize('objectManagement.noConnectionUriError', "Failed to retrieve the connection information, please reconnect and try again.")
export const RenameObjectDialogTitle: string = localize('objectManagement.renameObjectDialogTitle', "Enter new name"); export const RenameObjectDialogTitle: string = localize('objectManagement.renameObjectDialogTitle', "Enter new name");
export const ScriptText: string = localize('objectManagement.scriptText', "Script");
export const ScriptGeneratedText: string = localize('objectManagement.scriptGenerated', "Script has been generated successfully. You can close the dialog to view it in the newly opened editor.")
export function RefreshObjectExplorerError(error: string): string { export function RefreshObjectExplorerError(error: string): string {
return localize({ return localize({
@@ -108,6 +111,10 @@ export function RenameObjectError(objectType: string, originalName: string, newN
}, "An error occurred while renaming {0} '{1}' to '{2}'. {3}", objectType, originalName, newName, error); }, "An error occurred while renaming {0} '{1}' to '{2}'. {3}", objectType, originalName, newName, error);
} }
export function ScriptError(error: string): string {
return localize('objectManagement.scriptError', "An error occurred while generating script. {0}", error);
}
export const NameText = localize('objectManagement.nameLabel', "Name"); export const NameText = localize('objectManagement.nameLabel', "Name");
export const SelectedText = localize('objectManagement.selectedLabel', "Selected"); export const SelectedText = localize('objectManagement.selectedLabel', "Selected");
export const GeneralSectionHeader = localize('objectManagement.generalSectionHeader', "General"); export const GeneralSectionHeader = localize('objectManagement.generalSectionHeader', "General");

View File

@@ -63,6 +63,16 @@ export class ObjectManagementService implements IObjectManagementService {
} }
); );
} }
scriptLogin(contextId: string, login: ObjectManagement.Login): Thenable<string> {
const params: contracts.ScriptLoginRequestParams = { contextId, login };
return this.client.sendRequest(contracts.ScriptLoginRequest.type, params).then(
r => { return r; },
e => {
this.client.logFailedRequest(contracts.ScriptLoginRequest.type, e);
return Promise.reject(e);
}
);
}
disposeLoginView(contextId: string): Thenable<void> { disposeLoginView(contextId: string): Thenable<void> {
const params: contracts.DisposeLoginViewRequestParams = { contextId }; const params: contracts.DisposeLoginViewRequestParams = { contextId };
return this.client.sendRequest(contracts.DisposeLoginViewRequest.type, params).then( return this.client.sendRequest(contracts.DisposeLoginViewRequest.type, params).then(
@@ -105,6 +115,16 @@ export class ObjectManagementService implements IObjectManagementService {
} }
); );
} }
scriptUser(contextId: string, user: ObjectManagement.User): Thenable<string> {
const params: contracts.ScriptUserRequestParams = { contextId, user };
return this.client.sendRequest(contracts.ScriptUserRequest.type, params).then(
r => { return r; },
e => {
this.client.logFailedRequest(contracts.ScriptUserRequest.type, e);
return Promise.reject(e);
}
);
}
disposeUserView(contextId: string): Thenable<void> { disposeUserView(contextId: string): Thenable<void> {
const params: contracts.DisposeUserViewRequestParams = { contextId }; const params: contracts.DisposeUserViewRequestParams = { contextId };
return this.client.sendRequest(contracts.DisposeUserViewRequest.type, params).then( return this.client.sendRequest(contracts.DisposeUserViewRequest.type, params).then(
@@ -215,6 +235,13 @@ export class TestObjectManagementService implements IObjectManagementService {
}, 3000); }, 3000);
}); });
} }
async scriptLogin(contextId: string, login: ObjectManagement.Login): Promise<string> {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('test script');
}, 1000);
});
}
async disposeLoginView(contextId: string): Promise<void> { async disposeLoginView(contextId: string): Promise<void> {
} }
async initializeUserView(connectionUri: string, database: string, contextId: string, isNewObject: boolean, name: string): Promise<ObjectManagement.UserViewInfo> { async initializeUserView(connectionUri: string, database: string, contextId: string, isNewObject: boolean, name: string): Promise<ObjectManagement.UserViewInfo> {
@@ -280,6 +307,13 @@ export class TestObjectManagementService implements IObjectManagementService {
async updateUser(contextId: string, login: ObjectManagement.User): Promise<void> { async updateUser(contextId: string, login: ObjectManagement.User): Promise<void> {
return this.delayAndResolve(); return this.delayAndResolve();
} }
async scriptUser(contextId: string, login: ObjectManagement.User): Promise<string> {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject('generate script for user not supported');
}, 1000);
});
}
async disposeUserView(contextId: string): Promise<void> { async disposeUserView(contextId: string): Promise<void> {
} }
async rename(connectionUri: string, objectUrn: string, newName: string): Promise<void> { async rename(connectionUri: string, objectUrn: string, newName: string): Promise<void> {

View File

@@ -114,6 +114,10 @@ export class LoginDialog extends ObjectManagementDialogBase<ObjectManagement.Log
this.formContainer.addItems(sections); this.formContainer.addItems(sections);
} }
protected async generateScript(): Promise<string> {
return this.objectManagementService.scriptLogin(this.contextId, this.objectInfo);
}
private initializeGeneralSection(): void { private initializeGeneralSection(): void {
this.nameInput = this.modelView.modelBuilder.inputBox().withProps({ this.nameInput = this.modelView.modelBuilder.inputBox().withProps({
ariaLabel: localizedConstants.NameText, ariaLabel: localizedConstants.NameText,

View File

@@ -16,10 +16,11 @@ import { NodeType, TelemetryActions, TelemetryViews } from '../constants';
import { import {
CreateObjectOperationDisplayName, HelpText, LoadingDialogText, CreateObjectOperationDisplayName, HelpText, LoadingDialogText,
NameText, NameText,
NewObjectDialogTitle, ObjectPropertiesDialogTitle, OkText, SelectedText, UpdateObjectOperationDisplayName NewObjectDialogTitle, ObjectPropertiesDialogTitle, OkText, ScriptError, ScriptGeneratedText, ScriptText, SelectedText, UpdateObjectOperationDisplayName
} from '../localizedConstants'; } from '../localizedConstants';
import { deepClone, getNodeTypeDisplayName, refreshNode } from '../utils'; import { deepClone, getNodeTypeDisplayName, refreshNode } from '../utils';
import { TelemetryReporter } from '../../telemetry'; import { TelemetryReporter } from '../../telemetry';
import { providerId } from '../../constants';
export const DefaultLabelWidth = 150; export const DefaultLabelWidth = 150;
export const DefaultInputWidth = 300; export const DefaultInputWidth = 300;
@@ -47,6 +48,7 @@ export abstract class ObjectManagementDialogBase<ObjectInfoType extends ObjectMa
private _loadingComponent: azdata.LoadingComponent; private _loadingComponent: azdata.LoadingComponent;
private _formContainer: azdata.DivContainer; private _formContainer: azdata.DivContainer;
private _helpButton: azdata.window.Button; private _helpButton: azdata.window.Button;
private _scriptButton: azdata.window.Button;
constructor(private readonly objectType: NodeType, constructor(private readonly objectType: NodeType,
docUrl: string, docUrl: string,
@@ -65,9 +67,10 @@ export abstract class ObjectManagementDialogBase<ObjectInfoType extends ObjectMa
this.disposables.push(this._helpButton.onClick(async () => { this.disposables.push(this._helpButton.onClick(async () => {
await vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(docUrl)); await vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(docUrl));
})); }));
this.dialogObject.customButtons = [this._helpButton]; this._scriptButton = azdata.window.createButton(ScriptText, 'left');
this.dialogObject.okButton.hidden = true; this.disposables.push(this._scriptButton.onClick(async () => { await this.onScriptButtonClick(); }));
this._helpButton.hidden = true; this.dialogObject.customButtons = [this._helpButton, this._scriptButton];
this.updateLoadingStatus(true);
this.contextId = generateUuid(); this.contextId = generateUuid();
this.dialogObject.registerCloseValidator(async (): Promise<boolean> => { this.dialogObject.registerCloseValidator(async (): Promise<boolean> => {
const confirmed = await this.onConfirmation(); const confirmed = await this.onConfirmation();
@@ -83,6 +86,7 @@ export abstract class ObjectManagementDialogBase<ObjectInfoType extends ObjectMa
protected abstract onComplete(): Promise<void>; protected abstract onComplete(): Promise<void>;
protected async onDispose(): Promise<void> { } protected async onDispose(): Promise<void> { }
protected abstract validateInput(): Promise<string[]>; protected abstract validateInput(): Promise<string[]>;
protected abstract generateScript(): Promise<string>;
/** /**
* Dispose the information related to this view in the backend service. * Dispose the information related to this view in the backend service.
@@ -90,7 +94,7 @@ export abstract class ObjectManagementDialogBase<ObjectInfoType extends ObjectMa
protected abstract disposeView(): Promise<void>; protected abstract disposeView(): Promise<void>;
protected onObjectValueChange(): void { protected onObjectValueChange(): void {
this.dialogObject.okButton.enabled = JSON.stringify(this.objectInfo) !== JSON.stringify(this._originalObjectInfo); this.dialogObject.okButton.enabled = this.isDirty;
} }
protected async onConfirmation(): Promise<boolean> { protected async onConfirmation(): Promise<boolean> {
@@ -175,9 +179,7 @@ export abstract class ObjectManagementDialogBase<ObjectInfoType extends ObjectMa
} }
} }
}); });
this.dialogObject.okButton.hidden = false; this.updateLoadingStatus(false);
this._helpButton.hidden = false;
this._loadingComponent.loading = false;
} catch (err) { } catch (err) {
const actionName = this.isNewObject ? TelemetryActions.OpenNewObjectDialog : TelemetryActions.OpenPropertiesDialog; const actionName = this.isNewObject ? TelemetryActions.OpenNewObjectDialog : TelemetryActions.OpenPropertiesDialog;
TelemetryReporter.createErrorEvent2(TelemetryViews.ObjectManagement, actionName, err).withAdditionalProperties({ TelemetryReporter.createErrorEvent2(TelemetryViews.ObjectManagement, actionName, err).withAdditionalProperties({
@@ -316,4 +318,40 @@ export abstract class ObjectManagementDialogBase<ObjectInfoType extends ObjectMa
} }
} }
} }
private updateLoadingStatus(isLoading: boolean): void {
this._scriptButton.enabled = !isLoading;
this._helpButton.enabled = !isLoading;
this.dialogObject.okButton.enabled = isLoading ? false : this.isDirty;
if (this._loadingComponent) {
this._loadingComponent.loading = isLoading;
}
}
private async onScriptButtonClick(): Promise<void> {
this.updateLoadingStatus(true);
try {
const isValid = await this.runValidation();
if (!isValid) {
return;
}
const script = await this.generateScript();
await azdata.queryeditor.openQueryDocument({ content: script }, providerId);
this.dialogObject.message = {
text: ScriptGeneratedText,
level: azdata.window.MessageLevel.Information
};
} catch (err) {
this.dialogObject.message = {
text: ScriptError(getErrorMessage(err)),
level: azdata.window.MessageLevel.Error
};
} finally {
this.updateLoadingStatus(false);
}
}
private get isDirty(): boolean {
return JSON.stringify(this.objectInfo) !== JSON.stringify(this._originalObjectInfo);
}
} }

View File

@@ -88,6 +88,10 @@ export class UserDialog extends ObjectManagementDialogBase<ObjectManagement.User
}, 100); }, 100);
} }
protected async generateScript(): Promise<string> {
return this.objectManagementService.scriptUser(this.contextId, this.objectInfo);
}
private initializeGeneralSection(): void { private initializeGeneralSection(): void {
this.nameInput = this.modelView.modelBuilder.inputBox().withProps({ this.nameInput = this.modelView.modelBuilder.inputBox().withProps({
ariaLabel: localizedConstants.NameText, ariaLabel: localizedConstants.NameText,