mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-16 10:58:30 -05:00
Consolidate getSubscriptions (#16821)
* Consolidate getSubscriptions * Undo test change
This commit is contained in:
@@ -111,7 +111,7 @@ export function registerAzureResourceCommands(appContext: AppContext, azureViewT
|
||||
let subscriptions: azureResource.AzureResourceSubscription[] = [];
|
||||
if (subscriptions.length === 0) {
|
||||
try {
|
||||
subscriptions = await subscriptionService.getAllSubscriptions(account);
|
||||
subscriptions = await subscriptionService.getSubscriptions(account);
|
||||
} catch (error) {
|
||||
account.isStale = true;
|
||||
vscode.window.showErrorMessage(AzureResourceErrorMessageUtil.getErrorMessage(error));
|
||||
|
||||
@@ -11,8 +11,14 @@ import { azureResource } from 'azureResource';
|
||||
import { AzureAccount, Tenant } from 'azurecore';
|
||||
|
||||
export interface IAzureResourceSubscriptionService {
|
||||
getSubscriptions(account: Account, credential: msRest.ServiceClientCredentials, tenantId: string): Promise<azureResource.AzureResourceSubscription[]>;
|
||||
getAllSubscriptions(account: Account): Promise<azureResource.AzureResourceSubscription[]>;
|
||||
/**
|
||||
* 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
|
||||
* @returns The list of all subscriptions on this account that were able to be retrieved
|
||||
*/
|
||||
getSubscriptions(account: Account, tenants?: string[] | undefined): Promise<azureResource.AzureResourceSubscription[]>;
|
||||
}
|
||||
|
||||
export interface IAzureResourceSubscriptionFilterService {
|
||||
|
||||
@@ -14,47 +14,33 @@ 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 {
|
||||
/**
|
||||
* Gets all of the subscriptions for the specified account using the specified credential. This assumes that the credential passed is for
|
||||
* the specified tenant - which the subscriptions returned will be associated with.
|
||||
* @param account The account to get the subscriptions for
|
||||
* @param credential The credential to use for querying the subscriptions
|
||||
* @param tenantId The ID of the tenant these subscriptions are for
|
||||
* @returns The list of all subscriptions on this account for the specified tenant
|
||||
*/
|
||||
public async getSubscriptions(account: azdata.Account, credential: any, tenantId: string): Promise<azureResource.AzureResourceSubscription[]> {
|
||||
const subscriptions: azureResource.AzureResourceSubscription[] = [];
|
||||
|
||||
const subClient = new SubscriptionClient(credential, { baseUri: account.properties.providerSettings.settings.armResource.endpoint });
|
||||
const subs = await subClient.subscriptions.list();
|
||||
subs.forEach((sub) => subscriptions.push({
|
||||
id: sub.subscriptionId,
|
||||
name: sub.displayName,
|
||||
tenant: tenantId
|
||||
}));
|
||||
|
||||
return subscriptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all subscriptions for all tenants of the given account. Any errors that occur while fetching the subscriptions for each tenant
|
||||
* 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
|
||||
* @returns The list of all subscriptions on this account that were able to be retrieved
|
||||
*/
|
||||
public async getAllSubscriptions(account: AzureAccount): Promise<azureResource.AzureResourceSubscription[]> {
|
||||
public async getSubscriptions(account: azdata.Account, tenants?: string[]): Promise<azureResource.AzureResourceSubscription[]> {
|
||||
const subscriptions: azureResource.AzureResourceSubscription[] = [];
|
||||
let gotSubscriptions = false;
|
||||
const errors: Error[] = [];
|
||||
|
||||
for (const tenant of account.properties.tenants) {
|
||||
for (const tenant of tenants ?? account.properties.tenants) {
|
||||
try {
|
||||
const token = await azdata.accounts.getAccountSecurityToken(account, tenant.id, azdata.AzureResource.ResourceManagement);
|
||||
subscriptions.push(...(await this.getSubscriptions(account, new TokenCredentials(token.token, token.tokenType), tenant.id) || <azureResource.AzureResourceSubscription[]>[]));
|
||||
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
|
||||
};
|
||||
}));
|
||||
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));
|
||||
|
||||
@@ -43,7 +43,7 @@ export class AzureResourceAccountTreeNode extends AzureResourceContainerTreeNode
|
||||
let subscriptions: azureResource.AzureResourceSubscription[] = [];
|
||||
|
||||
if (this._isClearingCache) {
|
||||
subscriptions = await this._subscriptionService.getAllSubscriptions(this.account);
|
||||
subscriptions = await this._subscriptionService.getSubscriptions(this.account);
|
||||
this.updateCache<azureResource.AzureResourceSubscription[]>(subscriptions);
|
||||
this._isClearingCache = false;
|
||||
} else {
|
||||
|
||||
@@ -118,7 +118,7 @@ async function getSubscriptionInfo(account: AzureAccount, subscriptionService: I
|
||||
total: number,
|
||||
selected: number
|
||||
}> {
|
||||
let subscriptions = await subscriptionService.getAllSubscriptions(account);
|
||||
let subscriptions = await subscriptionService.getSubscriptions(account);
|
||||
const total = subscriptions.length;
|
||||
let selected = total;
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ import * as vscode from 'vscode';
|
||||
import * as azdata from 'azdata';
|
||||
import { AppContext } from '../../appContext';
|
||||
import * as nls from 'vscode-nls';
|
||||
import { TokenCredentials } from '@azure/ms-rest-js';
|
||||
const localize = nls.loadMessageBundle();
|
||||
|
||||
import { TreeNode } from '../treeNode';
|
||||
@@ -123,9 +122,7 @@ class ResourceLoader {
|
||||
|
||||
for (const account of accounts) {
|
||||
for (const tenant of account.properties.tenants) {
|
||||
const token = await azdata.accounts.getAccountSecurityToken(account, tenant.id, azdata.AzureResource.ResourceManagement);
|
||||
|
||||
for (const subscription of await this.subscriptionService.getSubscriptions(account, new TokenCredentials(token.token, token.tokenType), tenant.id)) {
|
||||
for (const subscription of await this.subscriptionService.getSubscriptions(account, [tenant.id])) {
|
||||
for (const providerId of await this.resourceService.listResourceProviderIds()) {
|
||||
for (const group of await this.resourceService.getRootChildren(providerId, account, subscription, subscription.tenant)) {
|
||||
const children = await this.resourceService.getChildren(providerId, group.resourceNode);
|
||||
|
||||
@@ -276,11 +276,7 @@ export async function getSubscriptions(appContext: AppContext, account?: azdata.
|
||||
const subscriptionService = appContext.getService<IAzureResourceSubscriptionService>(AzureResourceServiceNames.subscriptionService);
|
||||
await Promise.all(account.properties.tenants.map(async (tenant: { id: string; }) => {
|
||||
try {
|
||||
const response = await azdata.accounts.getAccountSecurityToken(account, tenant.id, azdata.AzureResource.ResourceManagement);
|
||||
const token = response.token;
|
||||
const tokenType = response.tokenType;
|
||||
|
||||
result.subscriptions.push(...await subscriptionService.getSubscriptions(account, new TokenCredentials(token, tokenType), tenant.id));
|
||||
result.subscriptions.push(...await subscriptionService.getSubscriptions(account, [tenant.id]));
|
||||
} catch (err) {
|
||||
const error = new Error(localize('azure.accounts.getSubscriptions.queryError', "Error fetching subscriptions for account {0} tenant {1} : {2}",
|
||||
account.displayInfo.displayName,
|
||||
|
||||
Reference in New Issue
Block a user