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

@@ -23,6 +23,9 @@ export const OkText: string = localize('objectManagement.OkText', "OK");
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 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 {
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);
}
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 SelectedText = localize('objectManagement.selectedLabel', "Selected");
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> {
const params: contracts.DisposeLoginViewRequestParams = { contextId };
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> {
const params: contracts.DisposeUserViewRequestParams = { contextId };
return this.client.sendRequest(contracts.DisposeUserViewRequest.type, params).then(
@@ -215,6 +235,13 @@ export class TestObjectManagementService implements IObjectManagementService {
}, 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 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> {
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 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);
}
protected async generateScript(): Promise<string> {
return this.objectManagementService.scriptLogin(this.contextId, this.objectInfo);
}
private initializeGeneralSection(): void {
this.nameInput = this.modelView.modelBuilder.inputBox().withProps({
ariaLabel: localizedConstants.NameText,

View File

@@ -16,10 +16,11 @@ import { NodeType, TelemetryActions, TelemetryViews } from '../constants';
import {
CreateObjectOperationDisplayName, HelpText, LoadingDialogText,
NameText,
NewObjectDialogTitle, ObjectPropertiesDialogTitle, OkText, SelectedText, UpdateObjectOperationDisplayName
NewObjectDialogTitle, ObjectPropertiesDialogTitle, OkText, ScriptError, ScriptGeneratedText, ScriptText, SelectedText, UpdateObjectOperationDisplayName
} from '../localizedConstants';
import { deepClone, getNodeTypeDisplayName, refreshNode } from '../utils';
import { TelemetryReporter } from '../../telemetry';
import { providerId } from '../../constants';
export const DefaultLabelWidth = 150;
export const DefaultInputWidth = 300;
@@ -47,6 +48,7 @@ export abstract class ObjectManagementDialogBase<ObjectInfoType extends ObjectMa
private _loadingComponent: azdata.LoadingComponent;
private _formContainer: azdata.DivContainer;
private _helpButton: azdata.window.Button;
private _scriptButton: azdata.window.Button;
constructor(private readonly objectType: NodeType,
docUrl: string,
@@ -65,9 +67,10 @@ export abstract class ObjectManagementDialogBase<ObjectInfoType extends ObjectMa
this.disposables.push(this._helpButton.onClick(async () => {
await vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(docUrl));
}));
this.dialogObject.customButtons = [this._helpButton];
this.dialogObject.okButton.hidden = true;
this._helpButton.hidden = true;
this._scriptButton = azdata.window.createButton(ScriptText, 'left');
this.disposables.push(this._scriptButton.onClick(async () => { await this.onScriptButtonClick(); }));
this.dialogObject.customButtons = [this._helpButton, this._scriptButton];
this.updateLoadingStatus(true);
this.contextId = generateUuid();
this.dialogObject.registerCloseValidator(async (): Promise<boolean> => {
const confirmed = await this.onConfirmation();
@@ -83,6 +86,7 @@ export abstract class ObjectManagementDialogBase<ObjectInfoType extends ObjectMa
protected abstract onComplete(): Promise<void>;
protected async onDispose(): Promise<void> { }
protected abstract validateInput(): Promise<string[]>;
protected abstract generateScript(): Promise<string>;
/**
* 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 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> {
@@ -175,9 +179,7 @@ export abstract class ObjectManagementDialogBase<ObjectInfoType extends ObjectMa
}
}
});
this.dialogObject.okButton.hidden = false;
this._helpButton.hidden = false;
this._loadingComponent.loading = false;
this.updateLoadingStatus(false);
} catch (err) {
const actionName = this.isNewObject ? TelemetryActions.OpenNewObjectDialog : TelemetryActions.OpenPropertiesDialog;
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);
}
protected async generateScript(): Promise<string> {
return this.objectManagementService.scriptUser(this.contextId, this.objectInfo);
}
private initializeGeneralSection(): void {
this.nameInput = this.modelView.modelBuilder.inputBox().withProps({
ariaLabel: localizedConstants.NameText,