From d1dc226ff6909237650cea8f30d516cd55e7886e Mon Sep 17 00:00:00 2001 From: Charles Gagnon Date: Thu, 19 Aug 2021 22:11:15 -0700 Subject: [PATCH] Fix error when getting subscriptions for all tenants (#16837) --- .../azurecore/src/azureResource/interfaces.ts | 4 ++-- .../azureResource/services/subscriptionService.ts | 13 +++++++------ extensions/azurecore/src/azureResource/utils.ts | 4 ++-- extensions/azurecore/src/extension.ts | 2 +- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/extensions/azurecore/src/azureResource/interfaces.ts b/extensions/azurecore/src/azureResource/interfaces.ts index ea5a10a12c..d5ab3e3044 100644 --- a/extensions/azurecore/src/azureResource/interfaces.ts +++ b/extensions/azurecore/src/azureResource/interfaces.ts @@ -15,10 +15,10 @@ export interface IAzureResourceSubscriptionService { * Gets subscriptions for the given account. Any errors that occur while fetching the subscriptions for each tenant * will be displayed to the user, but this function will only throw an error if it's unable to fetch any subscriptions. * @param account The account to get the subscriptions for - * @param tenants The list of tenants to get subscriptions for - if undefined then subscriptions for all tenants will be retrieved + * @param tenantIds The list of tenant IDs to get subscriptions for - if undefined then subscriptions for all tenants will be retrieved * @returns The list of all subscriptions on this account that were able to be retrieved */ - getSubscriptions(account: Account, tenants?: string[] | undefined): Promise; + getSubscriptions(account: AzureAccount, tenantIds?: string[] | undefined): Promise; } export interface IAzureResourceSubscriptionFilterService { diff --git a/extensions/azurecore/src/azureResource/services/subscriptionService.ts b/extensions/azurecore/src/azureResource/services/subscriptionService.ts index eb5c78b623..000fe93efa 100644 --- a/extensions/azurecore/src/azureResource/services/subscriptionService.ts +++ b/extensions/azurecore/src/azureResource/services/subscriptionService.ts @@ -14,6 +14,7 @@ import { AzureSubscriptionError } from '../errors'; import { AzureResourceErrorMessageUtil } from '../utils'; import * as nls from 'vscode-nls'; +import { AzureAccount } from 'azurecore'; const localize = nls.loadMessageBundle(); export class AzureResourceSubscriptionService implements IAzureResourceSubscriptionService { @@ -21,29 +22,29 @@ export class AzureResourceSubscriptionService implements IAzureResourceSubscript * Gets subscriptions for the given account. Any errors that occur while fetching the subscriptions for each tenant * will be displayed to the user, but this function will only throw an error if it's unable to fetch any subscriptions. * @param account The account to get the subscriptions for - * @param tenants The list of tenants to get subscriptions for - if undefined then subscriptions for all tenants will be retrieved + * @param tenantIds The list of tenants to get subscriptions for - if undefined then subscriptions for all tenants will be retrieved * @returns The list of all subscriptions on this account that were able to be retrieved */ - public async getSubscriptions(account: azdata.Account, tenants?: string[]): Promise { + public async getSubscriptions(account: AzureAccount, tenantIds?: string[]): Promise { const subscriptions: azureResource.AzureResourceSubscription[] = []; let gotSubscriptions = false; const errors: Error[] = []; - for (const tenant of tenants ?? account.properties.tenants) { + for (const tenantId of tenantIds ?? account.properties.tenants.map(t => t.id)) { try { - const token = await azdata.accounts.getAccountSecurityToken(account, tenant.id, azdata.AzureResource.ResourceManagement); + const token = await azdata.accounts.getAccountSecurityToken(account, tenantId, azdata.AzureResource.ResourceManagement); const subClient = new SubscriptionClient(new TokenCredentials(token.token, token.tokenType), { baseUri: account.properties.providerSettings.settings.armResource.endpoint }); const newSubs = await subClient.subscriptions.list(); subscriptions.push(...newSubs.map(newSub => { return { id: newSub.subscriptionId, name: newSub.displayName, - tenant: tenant.id + tenant: tenantId }; })); gotSubscriptions = true; } catch (error) { - const errorMsg = localize('azure.resource.tenantSubscriptionsError', "Failed to get subscriptions for account {0} (tenant '{1}'). {2}", account.key.accountId, tenant.id, AzureResourceErrorMessageUtil.getErrorMessage(error)); + const errorMsg = localize('azure.resource.tenantSubscriptionsError', "Failed to get subscriptions for account {0} (tenant '{1}'). {2}", account.key.accountId, tenantId, AzureResourceErrorMessageUtil.getErrorMessage(error)); console.warn(errorMsg); errors.push(error); vscode.window.showWarningMessage(errorMsg); diff --git a/extensions/azurecore/src/azureResource/utils.ts b/extensions/azurecore/src/azureResource/utils.ts index 0ffd03a69b..17ec51db8b 100644 --- a/extensions/azurecore/src/azureResource/utils.ts +++ b/extensions/azurecore/src/azureResource/utils.ts @@ -7,7 +7,7 @@ import { ResourceGraphClient } from '@azure/arm-resourcegraph'; import { TokenCredentials } from '@azure/ms-rest-js'; import axios, { AxiosRequestConfig } from 'axios'; import * as azdata from 'azdata'; -import { AzureRestResponse, GetResourceGroupsResult, GetSubscriptionsResult, ResourceQueryResult, GetBlobContainersResult, GetFileSharesResult, HttpRequestMethod, GetLocationsResult, GetManagedDatabasesResult, CreateResourceGroupResult, GetBlobsResult, GetStorageAccountAccessKeyResult } from 'azurecore'; +import { AzureRestResponse, GetResourceGroupsResult, GetSubscriptionsResult, ResourceQueryResult, GetBlobContainersResult, GetFileSharesResult, HttpRequestMethod, GetLocationsResult, GetManagedDatabasesResult, CreateResourceGroupResult, GetBlobsResult, GetStorageAccountAccessKeyResult, AzureAccount } from 'azurecore'; import { azureResource } from 'azureResource'; import { EOL } from 'os'; import * as nls from 'vscode-nls'; @@ -262,7 +262,7 @@ export async function runResourceQuery { +export async function getSubscriptions(appContext: AppContext, account?: AzureAccount, ignoreErrors: boolean = false): Promise { const result: GetSubscriptionsResult = { subscriptions: [], errors: [] }; if (!account?.properties?.tenants || !Array.isArray(account.properties.tenants)) { const error = new Error(invalidAzureAccount); diff --git a/extensions/azurecore/src/extension.ts b/extensions/azurecore/src/extension.ts index 57ec312987..050ab31a9c 100644 --- a/extensions/azurecore/src/extension.ts +++ b/extensions/azurecore/src/extension.ts @@ -143,7 +143,7 @@ export async function activate(context: vscode.ExtensionContext): Promise { + getSubscriptions(account?: azurecore.AzureAccount, ignoreErrors?: boolean, selectedOnly: boolean = false): Promise { return selectedOnly ? azureResourceUtils.getSelectedSubscriptions(appContext, account, ignoreErrors) : azureResourceUtils.getSubscriptions(appContext, account, ignoreErrors);