diff --git a/extensions/azurecore/src/azureResource/providers/azuremonitor/azuremonitorService.ts b/extensions/azurecore/src/azureResource/providers/azuremonitor/azuremonitorService.ts index 6ef82845fb..4a56048780 100644 --- a/extensions/azurecore/src/azureResource/providers/azuremonitor/azuremonitorService.ts +++ b/extensions/azurecore/src/azureResource/providers/azuremonitor/azuremonitorService.ts @@ -32,7 +32,7 @@ export class AzureMonitorResourceService extends ResourceServiceBase s.name === serverName); if (server) { @@ -80,7 +84,7 @@ export class AzureResourceDatabaseService implements IAzureResourceService sub.id === db.subscriptionId))?.name + name: (subscriptions.find(sub => sub.id === db.subscriptionId))?.name || '' }, tenant: db.tenantId, resourceGroup: db.resourceGroup diff --git a/extensions/azurecore/src/azureResource/providers/databaseServer/databaseServerService.ts b/extensions/azurecore/src/azureResource/providers/databaseServer/databaseServerService.ts index 40553851eb..c83017a5d9 100644 --- a/extensions/azurecore/src/azureResource/providers/databaseServer/databaseServerService.ts +++ b/extensions/azurecore/src/azureResource/providers/databaseServer/databaseServerService.ts @@ -67,7 +67,7 @@ export class AzureResourceDatabaseServerService implements IAzureResourceService combinedGraphResources.forEach((res) => { if (!ids.has(res.id)) { ids.add(res.id); - res.subscriptionName = subscriptions.find(sub => sub.id === res.subscriptionId).name; + res.subscriptionName = subscriptions.find(sub => sub.id === res.subscriptionId)?.name; const converted = this.convertResource(res); convertedResources.push(converted); } @@ -87,7 +87,7 @@ export class AzureResourceDatabaseServerService implements IAzureResourceService defaultDatabaseName: 'master', subscription: { id: resource.subscriptionId, - name: resource.subscriptionName + name: resource.subscriptionName || '' }, tenant: resource.tenantId, resourceGroup: resource.resourceGroup diff --git a/extensions/azurecore/src/azureResource/providers/kusto/kustoService.ts b/extensions/azurecore/src/azureResource/providers/kusto/kustoService.ts index a9e6526d8d..b62f029bc7 100644 --- a/extensions/azurecore/src/azureResource/providers/kusto/kustoService.ts +++ b/extensions/azurecore/src/azureResource/providers/kusto/kustoService.ts @@ -31,7 +31,7 @@ export class KustoResourceService extends ResourceServiceBase { const response = await azdata.accounts.getAccountSecurityToken(element.account, element.tenantId, azdata.AzureResource.ResourceManagement); + if (!response) { + throw new Error(`Did not receive security token when getting resources for account ${element.account.displayInfo.displayName}`); + } const credential = new msRest.TokenCredentials(response.token, response.tokenType); const resources: T[] = await this._resourceService.getResources([element.subscription], credential, element.account) || []; @@ -125,7 +128,7 @@ export abstract class ResourceServiceBase { if (!ids.has(res.id)) { ids.add(res.id); - res.subscriptionName = subscriptions.find(sub => sub.id === res.subscriptionId).name; + res.subscriptionName = subscriptions.find(sub => sub.id === res.subscriptionId)?.name; const converted = this.convertResource(res); convertedResources.push(converted); } diff --git a/extensions/azurecore/src/azureResource/providers/sqlinstance/sqlInstanceService.ts b/extensions/azurecore/src/azureResource/providers/sqlinstance/sqlInstanceService.ts index 00ab0f59b8..771af361a4 100644 --- a/extensions/azurecore/src/azureResource/providers/sqlinstance/sqlInstanceService.ts +++ b/extensions/azurecore/src/azureResource/providers/sqlinstance/sqlInstanceService.ts @@ -30,7 +30,7 @@ export class SqlInstanceResourceService extends ResourceServiceBasetreeItem.label, + label: typeof treeItem.label === 'object' ? treeItem.label.label : treeItem.label || '', isLeaf: treeItem.collapsibleState === TreeItemCollapsibleState.None ? true : false, errorMessage: undefined, metadata: undefined, nodePath: this.generateNodePath(), nodeStatus: undefined, - nodeType: treeItem.contextValue, + nodeType: treeItem.contextValue || '', nodeSubType: undefined, iconType: treeItem.contextValue }; } public get nodePathValue(): string { - return this.resourceNodeWithProviderId.resourceNode.treeItem.id; + return this.resourceNodeWithProviderId.resourceNode.treeItem.id || ''; } } diff --git a/extensions/azurecore/src/azureResource/services/cacheService.ts b/extensions/azurecore/src/azureResource/services/cacheService.ts index af3d2b69ed..d34710b121 100644 --- a/extensions/azurecore/src/azureResource/services/cacheService.ts +++ b/extensions/azurecore/src/azureResource/services/cacheService.ts @@ -9,10 +9,8 @@ import { IAzureResourceCacheService } from '../interfaces'; export class AzureResourceCacheService implements IAzureResourceCacheService { public constructor( - context: ExtensionContext - ) { - this._context = context; - } + private _context: ExtensionContext + ) { } public generateKey(id: string): string { return `${AzureResourceCacheService.cacheKeyPrefix}.${id}`; @@ -26,7 +24,5 @@ export class AzureResourceCacheService implements IAzureResourceCacheService { await this._context.workspaceState.update(key, value); } - private _context: ExtensionContext = undefined; - private static readonly cacheKeyPrefix = 'azure.resource.cache'; } diff --git a/extensions/azurecore/src/azureResource/services/subscriptionFilterService.ts b/extensions/azurecore/src/azureResource/services/subscriptionFilterService.ts index aba69bddaf..eda2af3708 100644 --- a/extensions/azurecore/src/azureResource/services/subscriptionFilterService.ts +++ b/extensions/azurecore/src/azureResource/services/subscriptionFilterService.ts @@ -11,11 +11,11 @@ interface AzureResourceSelectedSubscriptionsCache { } export class AzureResourceSubscriptionFilterService implements IAzureResourceSubscriptionFilterService { - public constructor( - cacheService: IAzureResourceCacheService - ) { - this._cacheService = cacheService; + private _cacheKey: string; + public constructor( + private _cacheService: IAzureResourceCacheService + ) { this._cacheKey = this._cacheService.generateKey('selectedSubscriptions'); } @@ -51,7 +51,4 @@ export class AzureResourceSubscriptionFilterService implements IAzureResourceSub filters.push(...selectedSubscriptionsCache[accountId].map((subscription) => `${accountId}/${subscription.id}/${subscription.name}`)); } } - - private _cacheService: IAzureResourceCacheService = undefined; - private _cacheKey: string = undefined; } diff --git a/extensions/azurecore/src/azureResource/services/subscriptionService.ts b/extensions/azurecore/src/azureResource/services/subscriptionService.ts index 03f377ce0c..2358e842ab 100644 --- a/extensions/azurecore/src/azureResource/services/subscriptionService.ts +++ b/extensions/azurecore/src/azureResource/services/subscriptionService.ts @@ -36,8 +36,8 @@ export class AzureResourceSubscriptionService implements IAzureResourceSubscript const newSubs = await subClient.subscriptions.list(); subscriptions.push(...newSubs.map(newSub => { return { - id: newSub.subscriptionId, - name: newSub.displayName, + id: newSub.subscriptionId || '', + name: newSub.displayName || '', tenant: tenantId }; })); diff --git a/extensions/azurecore/src/azureResource/services/terminalService.ts b/extensions/azurecore/src/azureResource/services/terminalService.ts index 1c84c39dde..3f90da6715 100644 --- a/extensions/azurecore/src/azureResource/services/terminalService.ts +++ b/extensions/azurecore/src/azureResource/services/terminalService.ts @@ -36,7 +36,7 @@ const handleNeverUsed = async (): Promise => { const option = await vscode.window.showInformationMessage(neverUsedString, openAzureShellButton, okButton); - if (option.action === TerminalOption.OPEN_SITE) { + if (option?.action === TerminalOption.OPEN_SITE) { void vscode.env.openExternal(vscode.Uri.parse('https://aka.ms/AA83f8f')); } }; @@ -50,6 +50,9 @@ export class AzureTerminalService implements IAzureTerminalService { public async getOrCreateCloudConsole(account: AzureAccount, tenant: Tenant): Promise { const token = await azdata.accounts.getAccountSecurityToken(account, tenant.id, azdata.AzureResource.MicrosoftResourceManagement); + if (!token) { + throw new Error('Did not receive security token when creating cloud console'); + } const settings: AxiosRequestConfig = { headers: { 'Accept': 'application/json', @@ -76,7 +79,7 @@ export class AzureTerminalService implements IAzureTerminalService { const consoleRequestUri = this.getConsoleRequestUri(metadata.settings.armResource.endpoint); if (preferredLocation) { - settings.headers['x-ms-console-preferred-location'] = preferredLocation; + settings.headers!['x-ms-console-preferred-location'] = preferredLocation; } let provisionResult: AxiosResponse; @@ -143,9 +146,9 @@ class AzureTerminal implements vscode.Pseudoterminal { private readonly writeEmitter: vscode.EventEmitter; public readonly onDidWrite: vscode.Event; - private socket: WS; - private intervalTimer: NodeJS.Timer; - private terminalDimensions: vscode.TerminalDimensions; + private socket: WS | undefined = undefined; + private intervalTimer: NodeJS.Timer | undefined = undefined; + private terminalDimensions: vscode.TerminalDimensions | undefined; constructor(private readonly consoleUri: string, private readonly token: string, private shell: string) { this.writeEmitter = new vscode.EventEmitter(); @@ -209,7 +212,7 @@ class AzureTerminal implements vscode.Pseudoterminal { // Keep alives this.intervalTimer = setInterval(() => { - this.socket.ping(); + this.socket?.ping(); }, 5000); } catch (ex) { console.log(ex); @@ -217,10 +220,13 @@ class AzureTerminal implements vscode.Pseudoterminal { } - private async establishTerminal(dimensions: vscode.TerminalDimensions): Promise { + private async establishTerminal(dimensions: vscode.TerminalDimensions | undefined): Promise { let terminalResult: AxiosResponse; try { - terminalResult = await axios.post(`${this.consoleUri}/terminals?rows=${dimensions.rows}&cols=${dimensions.columns}&shell=${this.shell}`, undefined, { + const url = dimensions ? + `${this.consoleUri}/terminals?rows=${dimensions.rows}&cols=${dimensions.columns}&shell=${this.shell}` + : `${this.consoleUri}/terminals?shell=${this.shell}`; + terminalResult = await axios.post(url, undefined, { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', @@ -228,7 +234,7 @@ class AzureTerminal implements vscode.Pseudoterminal { } }); } catch (ex) { - console.log(ex, ex.response); + console.log(`Error establishing terminal. ${ex}, ${ex.response}`); await handleNeverUsed(); return undefined; }