Fix error when getting subscriptions for all tenants (#16837)

This commit is contained in:
Charles Gagnon
2021-08-19 22:11:15 -07:00
committed by GitHub
parent bbe77d6823
commit d1dc226ff6
4 changed files with 12 additions and 11 deletions

View File

@@ -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<azureResource.AzureResourceSubscription[]> {
public async getSubscriptions(account: AzureAccount, tenantIds?: string[]): Promise<azureResource.AzureResourceSubscription[]> {
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);