Display azure resource fetch errors as warnings (#9158)

This commit is contained in:
Charles Gagnon
2020-02-18 10:48:39 -08:00
committed by GitHub
parent 48c3702c7a
commit 6bd3ed72dd
2 changed files with 53 additions and 16 deletions

View File

@@ -28,7 +28,7 @@ export function registerAzureResourceCommands(appContext: AppContext, tree: Azur
appContext.apiWrapper.registerCommand('azure.accounts.getSubscriptions', async (account?: azdata.Account, ignoreErrors: boolean = false): Promise<GetSubscriptionsResult> => {
const result: GetSubscriptionsResult = { subscriptions: [], errors: [] };
if (!account?.properties?.tenants || !isArray(account.properties.tenants)) {
const error = new Error('Invalid account');
const error = new Error(localize('azure.accounts.getSubscriptions.invalidParamsError', "Invalid account"));
if (!ignoreErrors) {
throw error;
}
@@ -44,11 +44,15 @@ export function registerAzureResourceCommands(appContext: AppContext, tree: Azur
result.subscriptions.push(...await subscriptionService.getSubscriptions(account, new TokenCredentials(token, tokenType)));
} catch (err) {
console.warn(`Error fetching subscriptions for account ${account.displayInfo.displayName} tenant ${tenant.id} : ${err}`);
const error = new Error(localize('azure.accounts.getSubscriptions.queryError', "Error fetching subscriptions for account {0} tenant {1} : {2}",
account.displayInfo.displayName,
tenant.id,
err instanceof Error ? err.message : err));
console.warn(error);
if (!ignoreErrors) {
throw err;
throw error;
}
result.errors.push(err);
result.errors.push(error);
}
return Promise.resolve();
}));
@@ -58,7 +62,7 @@ export function registerAzureResourceCommands(appContext: AppContext, tree: Azur
appContext.apiWrapper.registerCommand('azure.accounts.getResourceGroups', async (account?: azdata.Account, subscription?: azureResource.AzureResourceSubscription, ignoreErrors: boolean = false): Promise<GetResourceGroupsResult> => {
const result: GetResourceGroupsResult = { resourceGroups: [], errors: [] };
if (!account?.properties?.tenants || !isArray(account.properties.tenants) || !subscription) {
const error = new Error('Invalid account or subscription');
const error = new Error(localize('azure.accounts.getResourceGroups.invalidParamsError', "Invalid account or subscription"));
if (!ignoreErrors) {
throw error;
}
@@ -74,11 +78,18 @@ export function registerAzureResourceCommands(appContext: AppContext, tree: Azur
result.resourceGroups.push(...await service.getResources(subscription, new TokenCredentials(token, tokenType)));
} catch (err) {
console.warn(`Error fetching resource groups for account ${account.displayInfo.displayName} (${account.displayInfo.userId}) subscription ${subscription.id} (${subscription.name}) tenant ${tenant.id} : ${err}`);
const error = new Error(localize('azure.accounts.getResourceGroups.queryError', "Error fetching resource groups for account {0} ({1}) subscription {2} ({3}) tenant {4} : {5}",
account.displayInfo.displayName,
account.displayInfo.userId,
subscription.id,
subscription.name,
tenant.id,
err instanceof Error ? err.message : err));
console.warn(error);
if (!ignoreErrors) {
throw err;
throw error;
}
result.errors.push(err);
result.errors.push(error);
}
return Promise.resolve();
}));

View File

@@ -12,6 +12,7 @@ import { getDateTimeString } from '../utils';
import { azureResource } from '../../../azurecore/src/azureResource/azure-resource';
import * as azurecore from '../../../azurecore/src/azurecore';
import * as loc from '../localizedConstants';
import { EOL } from 'os';
const localize = nls.loadMessageBundle();
@@ -503,22 +504,33 @@ function handleSelectedAccountChanged(
): void {
subscriptionValueToSubscriptionMap.clear();
subscriptionDropdown.values = [];
handleSelectedSubscriptionChanged(selectedAccount, undefined, resourceGroupDropdown);
handleSelectedSubscriptionChanged(context, selectedAccount, undefined, resourceGroupDropdown);
if (selectedAccount) {
if (locationDropdown.values && locationDropdown.values.length === 0) {
locationDropdown.values = context.fieldInfo.locations;
}
} else {
locationDropdown.values = [];
return;
}
vscode.commands.executeCommand('azure.accounts.getSubscriptions', selectedAccount, true /*ignoreErrors*/).then(response => {
subscriptionDropdown.values = (<azurecore.GetSubscriptionsResult>response).subscriptions.map(subscription => {
vscode.commands.executeCommand<azurecore.GetSubscriptionsResult>('azure.accounts.getSubscriptions', selectedAccount, true /*ignoreErrors*/).then(response => {
if (!response) {
return;
}
if (response.errors.length > 0) {
context.container.message = {
text: response.errors.join(EOL) || '',
description: '',
level: azdata.window.MessageLevel.Warning
};
}
subscriptionDropdown.values = response.subscriptions.map(subscription => {
const displayName = `${subscription.name} (${subscription.id})`;
subscriptionValueToSubscriptionMap.set(displayName, subscription);
return displayName;
}).sort((a: string, b: string) => a.toLocaleLowerCase().localeCompare(b.toLocaleLowerCase()));
const selectedSubscription = subscriptionDropdown.values.length > 0 ? subscriptionValueToSubscriptionMap.get(subscriptionDropdown.values[0]) : undefined;
handleSelectedSubscriptionChanged(selectedAccount, selectedSubscription, resourceGroupDropdown);
handleSelectedSubscriptionChanged(context, selectedAccount, selectedSubscription, resourceGroupDropdown);
}, err => { vscode.window.showErrorMessage(localize('azure.accounts.unexpectedSubscriptionsError', "Unexpected error fetching subscriptions for account {0} ({1}): {2}", selectedAccount?.displayInfo.displayName, selectedAccount?.key.accountId, err.message)); });
}
@@ -545,15 +557,29 @@ function createAzureResourceGroupsDropdown(
subscriptionDropdown.onValueChanged(selectedItem => {
const selectedAccount = !accountDropdown || !accountDropdown.value ? undefined : accountValueToAccountMap.get(accountDropdown.value.toString());
const selectedSubscription = subscriptionValueToSubscriptionMap.get(selectedItem.selected);
handleSelectedSubscriptionChanged(selectedAccount, selectedSubscription, resourceGroupDropdown);
handleSelectedSubscriptionChanged(context, selectedAccount, selectedSubscription, resourceGroupDropdown);
});
return resourceGroupDropdown;
}
function handleSelectedSubscriptionChanged(selectedAccount: azdata.Account | undefined, selectedSubscription: azureResource.AzureResourceSubscription | undefined, resourceGroupDropdown: azdata.DropDownComponent): void {
function handleSelectedSubscriptionChanged(context: AzureAccountFieldContext, selectedAccount: azdata.Account | undefined, selectedSubscription: azureResource.AzureResourceSubscription | undefined, resourceGroupDropdown: azdata.DropDownComponent): void {
resourceGroupDropdown.values = [];
vscode.commands.executeCommand('azure.accounts.getResourceGroups', selectedAccount, selectedSubscription, true /*ignoreErrors*/).then(response => {
resourceGroupDropdown.values = (<azurecore.GetResourceGroupsResult>response).resourceGroups.map(resourceGroup => resourceGroup.name).sort((a: string, b: string) => a.toLocaleLowerCase().localeCompare(b.toLocaleLowerCase()));
if (!selectedAccount || !selectedSubscription) {
// Don't need to execute command if we don't have both an account and subscription selected
return;
}
vscode.commands.executeCommand<azurecore.GetResourceGroupsResult>('azure.accounts.getResourceGroups', selectedAccount, selectedSubscription, true /*ignoreErrors*/).then(response => {
if (!response) {
return;
}
if (response.errors.length > 0) {
context.container.message = {
text: response.errors.join(EOL) || '',
description: '',
level: azdata.window.MessageLevel.Warning
};
}
resourceGroupDropdown.values = response.resourceGroups.map(resourceGroup => resourceGroup.name).sort((a: string, b: string) => a.toLocaleLowerCase().localeCompare(b.toLocaleLowerCase()));
}, err => { vscode.window.showErrorMessage(localize('azure.accounts.unexpectedResourceGroupsError', "Unexpected error fetching resource groups for subscription {0} ({1}): {2}", selectedSubscription?.name, selectedSubscription?.id, err.message)); });
}