Azure Monitor: Fix bug that was not showing resources for certain locations (#66502)

This commit is contained in:
Alyssa Bull
2023-04-14 13:47:41 -06:00
committed by GitHub
parent 1b0cee491a
commit d43482a463
3 changed files with 21 additions and 46 deletions

View File

@@ -45,7 +45,7 @@ export function createMockResourcePickerData() {
mockResourcePicker.getResourcesForResourceGroup = jest.fn().mockResolvedValue(mockResourcesByResourceGroup());
mockResourcePicker.getResourceURIFromWorkspace = jest.fn().mockReturnValue('');
mockResourcePicker.getResourceURIDisplayProperties = jest.fn().mockResolvedValue({});
mockResourcePicker.getLogsLocations = jest.fn().mockResolvedValue(mockGetValidLocations());
mockResourcePicker.getLocations = jest.fn().mockResolvedValue(mockGetValidLocations());
return mockResourcePicker;
}

View File

@@ -32,11 +32,11 @@ const createResourcePickerData = (responses: AzureGraphResponse[]) => {
postResource.mockResolvedValueOnce(res);
});
resourcePickerData.postResource = postResource;
const logLocationsMap = mockGetValidLocations();
const getLogsLocations = jest.spyOn(resourcePickerData, 'getLogsLocations').mockResolvedValue(logLocationsMap);
resourcePickerData.logLocationsMap = logLocationsMap;
resourcePickerData.logLocations = Array.from(logLocationsMap.values()).map((location) => `"${location.name}"`);
return { resourcePickerData, postResource, mockDatasource, getValidLocations: getLogsLocations };
const locationsMap = mockGetValidLocations();
const getLocations = jest.spyOn(resourcePickerData, 'getLocations').mockResolvedValue(locationsMap);
resourcePickerData.locationsMap = locationsMap;
resourcePickerData.locations = Array.from(locationsMap.values()).map((location) => `"${location.name}"`);
return { resourcePickerData, postResource, mockDatasource, getValidLocations: getLocations };
};
describe('AzureMonitor resourcePickerData', () => {
@@ -397,13 +397,12 @@ describe('AzureMonitor resourcePickerData', () => {
const { resourcePickerData, getValidLocations } = createResourcePickerData([createMockARGSubscriptionResponse()]);
getValidLocations.mockRestore();
const subscriptions = await resourcePickerData.getSubscriptions();
const locations = await resourcePickerData.getLogsLocations(subscriptions);
const locations = await resourcePickerData.getLocations(subscriptions);
expect(locations.size).toBe(1);
expect(locations.has('northeurope')).toBe(true);
expect(locations.get('northeurope')?.name).toBe('northeurope');
expect(locations.get('northeurope')?.displayName).toBe('North Europe');
expect(locations.get('northeurope')?.supportsLogs).toBe(true);
});
it('returns the raw locations map if provider is undefined', async () => {
@@ -413,13 +412,12 @@ describe('AzureMonitor resourcePickerData', () => {
getValidLocations.mockRestore();
mockDatasource.azureMonitorDatasource.getProvider = jest.fn().mockResolvedValue(undefined);
const subscriptions = await resourcePickerData.getSubscriptions();
const locations = await resourcePickerData.getLogsLocations(subscriptions);
const locations = await resourcePickerData.getLocations(subscriptions);
expect(locations.size).toBe(1);
expect(locations.has('northeurope')).toBe(true);
expect(locations.get('northeurope')?.name).toBe('northeurope');
expect(locations.get('northeurope')?.displayName).toBe('North Europe');
expect(locations.get('northeurope')?.supportsLogs).toBe(false);
});
});

View File

@@ -39,8 +39,8 @@ export default class ResourcePickerData extends DataSourceWithBackend<AzureMonit
resultLimit = 200;
azureMonitorDatasource;
supportedMetricNamespaces = '';
logLocationsMap: Map<string, AzureMonitorLocations> = new Map();
logLocations: string[] = [];
locationsMap: Map<string, AzureMonitorLocations> = new Map();
locations: string[] = [];
constructor(
instanceSettings: DataSourceInstanceSettings<AzureDataSourceJsonData>,
@@ -57,9 +57,9 @@ export default class ResourcePickerData extends DataSourceWithBackend<AzureMonit
): Promise<ResourceRowGroup> {
const subscriptions = await this.getSubscriptions();
if (this.logLocationsMap.size === 0) {
this.logLocationsMap = await this.getLogsLocations(subscriptions);
this.logLocations = Array.from(this.logLocationsMap.values()).map((location) => `"${location.name}"`);
if (this.locationsMap.size === 0) {
this.locationsMap = await this.getLocations(subscriptions);
this.locations = Array.from(this.locationsMap.values()).map((location) => `"${location.name}"`);
}
if (!currentSelection) {
@@ -140,7 +140,7 @@ export default class ResourcePickerData extends DataSourceWithBackend<AzureMonit
resourceGroupName: item.resourceGroup,
type,
typeLabel: resourceTypeDisplayNames[item.type] || item.type,
location: this.logLocationsMap.get(item.location)?.displayName || item.location,
location: this.locationsMap.get(item.location)?.displayName || item.location,
};
});
};
@@ -243,13 +243,14 @@ export default class ResourcePickerData extends DataSourceWithBackend<AzureMonit
resourceGroupId: string,
type: ResourcePickerQueryType
): Promise<ResourceRowGroup> {
if (!this.logLocations) {
if (!this.locations) {
return [];
}
const { data: response } = await this.makeResourceGraphRequest<RawAzureResourceItem[]>(`
resources
| where id hasprefix "${resourceGroupId}"
${await this.filterByType(type)} and location in (${this.logLocations})
${await this.filterByType(type)} and location in (${this.locations})
`);
return response.map((item) => {
@@ -264,7 +265,7 @@ export default class ResourcePickerData extends DataSourceWithBackend<AzureMonit
resourceGroupName: item.resourceGroup,
type: ResourceRowType.Resource,
typeLabel: resourceTypeDisplayNames[item.type] || item.type,
locationDisplayName: this.logLocationsMap.get(item.location)?.displayName || item.location,
locationDisplayName: this.locationsMap.get(item.location)?.displayName || item.location,
location: item.location,
};
});
@@ -391,34 +392,10 @@ export default class ResourcePickerData extends DataSourceWithBackend<AzureMonit
this.supportedMetricNamespaces = uniq(supportedMetricNamespaces).join(',');
}
async getLogsLocations(subscriptions: ResourceRowGroup): Promise<Map<string, AzureMonitorLocations>> {
async getLocations(subscriptions: ResourceRowGroup): Promise<Map<string, AzureMonitorLocations>> {
const subscriptionIds = subscriptions.map((sub) => sub.id);
const locations = await this.azureMonitorDatasource.getLocations(subscriptionIds);
const insightsProvider = await this.azureMonitorDatasource.getProvider('Microsoft.Insights');
const logsProvider = insightsProvider?.resourceTypes.find((provider) => provider.resourceType === 'logs');
if (!logsProvider) {
return locations;
}
const logsLocations = logsProvider.locations.map((location) => ({
displayName: location,
name: '',
supportsLogs: true,
}));
const logLocationsMap = new Map<string, AzureMonitorLocations>();
for (const logLocation of logsLocations) {
const name =
Array.from(locations.values()).find((location) => logLocation.displayName === location.displayName)?.name || '';
if (name !== '') {
logLocationsMap.set(name, { ...logLocation, name });
}
}
return logLocationsMap;
return locations;
}
parseRows(resources: Array<string | AzureMonitorResource>): ResourceRow[] {
@@ -445,7 +422,7 @@ export default class ResourcePickerData extends DataSourceWithBackend<AzureMonit
uri: resourceToString(resource),
typeLabel:
resourceTypeDisplayNames[resource.metricNamespace?.toLowerCase() ?? ''] ?? resource.metricNamespace ?? '',
locationDisplayName: this.logLocationsMap.get(resource.region ?? '')?.displayName || resource.region,
locationDisplayName: this.locationsMap.get(resource.region ?? '')?.displayName || resource.region,
location: resource.region,
});
});