Move CMS saved servers to be stored in memento instead of config (#6535)

* Move CMS saved servers to be stored in memento instead of config

* Add in code to port old servers into memento
This commit is contained in:
Charles Gagnon
2019-07-30 16:47:11 -07:00
committed by GitHub
parent 06e9a6e684
commit 3f5bdb86cd
7 changed files with 45 additions and 46 deletions

View File

@@ -68,9 +68,6 @@
"type": "object",
"title": "%text.sqlServerBigDataClusters%",
"properties": {
"clusterControllers.controllers": {
"type": "array"
},
"bigDataClusters.enabled": {
"type": "boolean",
"default": false,

View File

@@ -2,7 +2,7 @@
"name": "cms",
"displayName": "%cms.displayName%",
"description": "%cms.description%",
"version": "0.3.0",
"version": "0.4.0",
"publisher": "Microsoft",
"preview": true,
"license": "https://raw.githubusercontent.com/Microsoft/azuredatastudio/master/LICENSE.txt",
@@ -635,4 +635,4 @@
"publisherDisplayName": "Microsoft",
"publisherId": "Microsoft"
}
}
}

View File

@@ -87,19 +87,6 @@ export class ApiWrapper {
return vscode.window.registerTreeDataProvider(viewId, treeDataProvider);
}
/**
* Get the configuration for a extensionName
* @param extensionName The string name of the extension to get the configuration for
* @param resource The optional URI, as a URI object or a string, to use to get resource-scoped configurations
*/
public getConfiguration(): vscode.WorkspaceConfiguration {
return vscode.workspace.getConfiguration('centralManagementServers');
}
public async setConfiguration(value: any): Promise<void> {
await vscode.workspace.getConfiguration('centralManagementServers').update('servers', value, true);
}
/**
* Parse uri
*/
@@ -169,4 +156,4 @@ export class ApiWrapper {
public registerCompletionItemProvider(selector: vscode.DocumentSelector, provider: vscode.CompletionItemProvider, ...triggerCharacters: string[]): vscode.Disposable {
return vscode.languages.registerCompletionItemProvider(selector, provider, ...triggerCharacters);
}
}
}

View File

@@ -18,8 +18,5 @@ export class AppContext {
public readonly extensionContext: vscode.ExtensionContext,
public readonly apiWrapper: ApiWrapper,
public readonly cmsUtils: CmsUtils
) {
this.apiWrapper = apiWrapper || new ApiWrapper();
this.cmsUtils = cmsUtils || new CmsUtils();
}
) { }
}

View File

@@ -36,10 +36,9 @@ export class CmsResourceTreeProvider implements TreeDataProvider<TreeNode>, ICms
try {
// Call to collect all locally saved CMS servers
// to determine whether the system has been initialized.
let cmsConfig = this._appContext.cmsUtils.getConfiguration();
let cachedServers = cmsConfig.servers ? cmsConfig.servers : [];
const cachedServers = this._appContext.cmsUtils.getSavedServers();
if (cachedServers && cachedServers.length > 0) {
let servers = [];
const servers = [];
cachedServers.forEach(async (server) => {
servers.push(new CmsResourceTreeNode(
server.name,

View File

@@ -32,6 +32,10 @@ export interface CreateCmsResult {
*/
export class CmsUtils {
constructor(private _memento: vscode.Memento) {
this._registeredCmsServers = this.getSavedServers();
}
private _credentialProvider: azdata.CredentialProvider;
private _cmsService: mssql.CmsService;
private _registeredCmsServers: ICmsResourceNodeInfo[] = [];
@@ -52,17 +56,12 @@ export class CmsUtils {
return vscode.window.showErrorMessage(message, ...items);
}
/**
* Get the configuration for a extensionName
* @param extensionName The string name of the extension to get the configuration for
* @param resource The optional URI, as a URI object or a string, to use to get resource-scoped configurations
*/
public getConfiguration(): vscode.WorkspaceConfiguration {
return vscode.workspace.getConfiguration('centralManagementServers');
public getSavedServers(): ICmsResourceNodeInfo[] {
return this._memento.get('centralManagementServers') || [];
}
public async setConfiguration(value: any): Promise<void> {
await vscode.workspace.getConfiguration('centralManagementServers').update('servers', value, true);
public async saveServers(servers: ICmsResourceNodeInfo[]): Promise<void> {
await this._memento.update('centralManagementServers', servers);
}
// Connection APIs
@@ -128,12 +127,12 @@ export class CmsUtils {
}
public async deleteCmsServer(cmsServerName: string, connection: azdata.connection.Connection): Promise<void> {
let config = this.getConfiguration();
if (config && config.servers) {
let newServers = config.servers.filter((cachedServer) => {
const servers: ICmsResourceNodeInfo[] = this._memento.get('servers');
if (servers) {
const newServers: ICmsResourceNodeInfo[] = servers.filter((cachedServer) => {
return cachedServer.name !== cmsServerName;
});
await this.setConfiguration(newServers);
await this.saveServers(newServers);
this._registeredCmsServers = this._registeredCmsServers.filter((cachedServer) => {
return cachedServer.name !== cmsServerName;
});
@@ -164,7 +163,7 @@ export class CmsUtils {
// don't save password in config
server.connection.options.password = '';
});
await this.setConfiguration(toSaveCmsServers);
await this.saveServers(toSaveCmsServers);
}
public async addRegisteredServer(relativePath: string, ownerUri: string,
@@ -291,4 +290,4 @@ export class CmsUtils {
(connectionA.options.savePassword !== connectionA.options.savePassword));
}
}
}

View File

@@ -11,16 +11,19 @@ import { AppContext } from './appContext';
import ControllerBase from './controllers/controllerBase';
import { ApiWrapper } from './apiWrapper';
import { CmsUtils } from './cmsUtils';
import { ICmsResourceNodeInfo } from './cmsResource/tree/baseTreeNodes';
let controllers: ControllerBase[] = [];
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(extensionContext: vscode.ExtensionContext) {
export async function activate(extensionContext: vscode.ExtensionContext): Promise<void> {
const apiWrapper = new ApiWrapper();
const cmsUtils = new CmsUtils();
let appContext = new AppContext(extensionContext, apiWrapper, cmsUtils);
let activations: Thenable<boolean>[] = [];
const cmsUtils = new CmsUtils(extensionContext.globalState);
const appContext = new AppContext(extensionContext, apiWrapper, cmsUtils);
const activations: Thenable<boolean>[] = [];
await portSavedConfigServers(appContext);
const cmsResourceController = new CmsResourceController(appContext);
controllers.push(cmsResourceController);
@@ -29,8 +32,25 @@ export function activate(extensionContext: vscode.ExtensionContext) {
}
// this method is called when your extension is deactivated
export function deactivate() {
export function deactivate(): void {
for (let controller of controllers) {
controller.deactivate();
}
}
/**
* Helper method to port over servers that were previously saved in the configuration (in versions <= 0.3.0 of the extension)
* @param appContext The context to use to store the new saved servers
*/
async function portSavedConfigServers(appContext: AppContext): Promise<void> {
const config: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration('centralManagementServers');
if (config) {
const oldServers = config.get<ICmsResourceNodeInfo[]>('servers');
if (oldServers) {
oldServers.forEach(s => appContext.cmsUtils.cacheRegisteredCmsServer(s.name, s.description, s.ownerUri, s.connection));
// Now delete the config value since we don't need it anymore
await config.update('servers', undefined, true);
}
}
}