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", "type": "object",
"title": "%text.sqlServerBigDataClusters%", "title": "%text.sqlServerBigDataClusters%",
"properties": { "properties": {
"clusterControllers.controllers": {
"type": "array"
},
"bigDataClusters.enabled": { "bigDataClusters.enabled": {
"type": "boolean", "type": "boolean",
"default": false, "default": false,

View File

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

View File

@@ -87,19 +87,6 @@ export class ApiWrapper {
return vscode.window.registerTreeDataProvider(viewId, treeDataProvider); 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 * Parse uri
*/ */

View File

@@ -18,8 +18,5 @@ export class AppContext {
public readonly extensionContext: vscode.ExtensionContext, public readonly extensionContext: vscode.ExtensionContext,
public readonly apiWrapper: ApiWrapper, public readonly apiWrapper: ApiWrapper,
public readonly cmsUtils: CmsUtils 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 { try {
// Call to collect all locally saved CMS servers // Call to collect all locally saved CMS servers
// to determine whether the system has been initialized. // to determine whether the system has been initialized.
let cmsConfig = this._appContext.cmsUtils.getConfiguration(); const cachedServers = this._appContext.cmsUtils.getSavedServers();
let cachedServers = cmsConfig.servers ? cmsConfig.servers : [];
if (cachedServers && cachedServers.length > 0) { if (cachedServers && cachedServers.length > 0) {
let servers = []; const servers = [];
cachedServers.forEach(async (server) => { cachedServers.forEach(async (server) => {
servers.push(new CmsResourceTreeNode( servers.push(new CmsResourceTreeNode(
server.name, server.name,

View File

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

View File

@@ -11,16 +11,19 @@ import { AppContext } from './appContext';
import ControllerBase from './controllers/controllerBase'; import ControllerBase from './controllers/controllerBase';
import { ApiWrapper } from './apiWrapper'; import { ApiWrapper } from './apiWrapper';
import { CmsUtils } from './cmsUtils'; import { CmsUtils } from './cmsUtils';
import { ICmsResourceNodeInfo } from './cmsResource/tree/baseTreeNodes';
let controllers: ControllerBase[] = []; let controllers: ControllerBase[] = [];
// this method is called when your extension is activated // this method is called when your extension is activated
// your extension is activated the very first time the command is executed // 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 apiWrapper = new ApiWrapper();
const cmsUtils = new CmsUtils(); const cmsUtils = new CmsUtils(extensionContext.globalState);
let appContext = new AppContext(extensionContext, apiWrapper, cmsUtils); const appContext = new AppContext(extensionContext, apiWrapper, cmsUtils);
let activations: Thenable<boolean>[] = []; const activations: Thenable<boolean>[] = [];
await portSavedConfigServers(appContext);
const cmsResourceController = new CmsResourceController(appContext); const cmsResourceController = new CmsResourceController(appContext);
controllers.push(cmsResourceController); controllers.push(cmsResourceController);
@@ -29,8 +32,25 @@ export function activate(extensionContext: vscode.ExtensionContext) {
} }
// this method is called when your extension is deactivated // this method is called when your extension is deactivated
export function deactivate() { export function deactivate(): void {
for (let controller of controllers) { for (let controller of controllers) {
controller.deactivate(); 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);
}
}
}