AzureMonitor: Filter list of resources by resourceType (#43522)

This commit is contained in:
Andres Martinez Gotor 2022-01-10 11:22:06 +01:00 committed by GitHub
parent 1a0f5595c3
commit de4ac8ae59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 31 deletions

View File

@ -1,10 +1,10 @@
import AzureMonitorDatasource from '../datasource';
import { TemplateSrv } from 'app/features/templating/template_srv';
import { DataSourceInstanceSettings } from '@grafana/data';
import { AzureDataSourceJsonData, AzureQueryType, DatasourceValidationResult } from '../types';
import { TemplateSrv } from 'app/features/templating/template_srv';
import createMockQuery from '../__mocks__/query';
import { singleVariable, subscriptionsVariable } from '../__mocks__/variables';
import AzureMonitorDatasource from '../datasource';
import { AzureDataSourceJsonData, AzureQueryType, DatasourceValidationResult } from '../types';
const templateSrv = new TemplateSrv();
@ -163,7 +163,7 @@ describe('AzureMonitorDatasource', () => {
beforeEach(() => {
ctx.ds.azureMonitorDatasource.getResource = jest.fn().mockImplementation((path: string) => {
const basePath = 'azuremonitor/subscriptions/9935389e-9122-4ef9-95f9-1513dd24753f/resourceGroups';
expect(path).toBe(basePath + '/nodesapp/resources?api-version=2018-01-01');
expect(path).toBe(basePath + '/nodesapp/resources?api-version=2021-04-01');
return Promise.resolve(response);
});
});
@ -192,6 +192,16 @@ describe('AzureMonitorDatasource', () => {
});
describe('When performing getResourceNames', () => {
let subscription = '9935389e-9122-4ef9-95f9-1513dd24753f';
let resourceGroup = 'nodeapp';
let metricDefinition = 'microsoft.insights/components';
beforeEach(() => {
subscription = '9935389e-9122-4ef9-95f9-1513dd24753f';
resourceGroup = 'nodeapp';
metricDefinition = 'microsoft.insights/components';
});
describe('and there are no special cases', () => {
const response = {
value: [
@ -200,23 +210,25 @@ describe('AzureMonitorDatasource', () => {
type: 'microsoft.insights/alertrules',
},
{
name: 'nodeapp',
type: 'microsoft.insights/components',
name: resourceGroup,
type: metricDefinition,
},
],
};
beforeEach(() => {
ctx.ds.azureMonitorDatasource.getResource = jest.fn().mockImplementation((path: string) => {
const basePath = 'azuremonitor/subscriptions/9935389e-9122-4ef9-95f9-1513dd24753f/resourceGroups';
expect(path).toBe(basePath + '/nodeapp/resources?api-version=2018-01-01');
const basePath = `azuremonitor/subscriptions/${subscription}/resourceGroups`;
expect(path).toBe(
`${basePath}/${resourceGroup}/resources?$filter=resourceType eq '${metricDefinition}'&api-version=2021-04-01`
);
return Promise.resolve(response);
});
});
it('should return list of Resource Names', () => {
return ctx.ds
.getResourceNames('9935389e-9122-4ef9-95f9-1513dd24753f', 'nodeapp', 'microsoft.insights/components')
.getResourceNames(subscription, resourceGroup, metricDefinition)
.then((results: Array<{ text: string; value: string }>) => {
expect(results.length).toEqual(1);
expect(results[0].text).toEqual('nodeapp');
@ -225,8 +237,9 @@ describe('AzureMonitorDatasource', () => {
});
it('should return ignore letter case', () => {
metricDefinition = 'microsoft.insights/Components';
return ctx.ds
.getResourceNames('9935389e-9122-4ef9-95f9-1513dd24753f', 'nodeapp', 'microsoft.insights/Components')
.getResourceNames(subscription, resourceGroup, metricDefinition)
.then((results: Array<{ text: string; value: string }>) => {
expect(results.length).toEqual(1);
expect(results[0].text).toEqual('nodeapp');
@ -251,19 +264,19 @@ describe('AzureMonitorDatasource', () => {
beforeEach(() => {
ctx.ds.azureMonitorDatasource.getResource = jest.fn().mockImplementation((path: string) => {
const basePath = 'azuremonitor/subscriptions/9935389e-9122-4ef9-95f9-1513dd24753f/resourceGroups';
expect(path).toBe(basePath + '/nodeapp/resources?api-version=2018-01-01');
const basePath = `azuremonitor/subscriptions/${subscription}/resourceGroups`;
expect(path).toBe(
basePath +
`/${resourceGroup}/resources?$filter=resourceType eq '${metricDefinition}'&api-version=2021-04-01`
);
return Promise.resolve(response);
});
});
it('should return list of Resource Names', () => {
metricDefinition = 'Microsoft.Storage/storageAccounts/blobServices';
return ctx.ds
.getResourceNames(
'9935389e-9122-4ef9-95f9-1513dd24753f',
'nodeapp',
'Microsoft.Storage/storageAccounts/blobServices'
)
.getResourceNames(subscription, resourceGroup, metricDefinition)
.then((results: Array<{ text: string; value: string }>) => {
expect(results.length).toEqual(1);
expect(results[0].text).toEqual('storagetest/default');

View File

@ -1,29 +1,30 @@
import { DataSourceInstanceSettings, ScopedVars } from '@grafana/data';
import { DataSourceWithBackend, getTemplateSrv } from '@grafana/runtime';
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
import { filter, startsWith } from 'lodash';
import UrlBuilder from './url_builder';
import ResponseParser from './response_parser';
import SupportedNamespaces from './supported_namespaces';
import { resourceTypeDisplayNames } from '../azureMetadata';
import { getAuthType, getAzureCloud, getAzurePortalUrl } from '../credentials';
import TimegrainConverter from '../time_grain_converter';
import {
AzureMonitorQuery,
AzureDataSourceJsonData,
AzureMonitorMetricDefinitionsResponse,
AzureMonitorQuery,
AzureMonitorResourceGroupsResponse,
AzureQueryType,
DatasourceValidationResult,
} from '../types';
import { DataSourceInstanceSettings, ScopedVars } from '@grafana/data';
import { DataSourceWithBackend, getTemplateSrv } from '@grafana/runtime';
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
import { getAuthType, getAzureCloud, getAzurePortalUrl } from '../credentials';
import { resourceTypeDisplayNames } from '../azureMetadata';
import { routeNames } from '../utils/common';
import ResponseParser from './response_parser';
import SupportedNamespaces from './supported_namespaces';
import UrlBuilder from './url_builder';
const defaultDropdownValue = 'select';
export default class AzureMonitorDatasource extends DataSourceWithBackend<AzureMonitorQuery, AzureDataSourceJsonData> {
apiVersion = '2018-01-01';
apiPreviewVersion = '2017-12-01-preview';
listByResourceGroupApiVersion = '2021-04-01';
defaultSubscriptionId?: string;
resourcePath: string;
azurePortalUrl: string;
@ -134,7 +135,7 @@ export default class AzureMonitorDatasource extends DataSourceWithBackend<AzureM
getResourceGroups(subscriptionId: string) {
return this.getResource(
`${this.resourcePath}/${subscriptionId}/resourceGroups?api-version=${this.apiVersion}`
`${this.resourcePath}/${subscriptionId}/resourceGroups?api-version=${this.listByResourceGroupApiVersion}`
).then((result: AzureMonitorResourceGroupsResponse) => {
return ResponseParser.parseResponseValues(result, 'name', 'name');
});
@ -142,7 +143,7 @@ export default class AzureMonitorDatasource extends DataSourceWithBackend<AzureM
getMetricDefinitions(subscriptionId: string, resourceGroup: string) {
return this.getResource(
`${this.resourcePath}/${subscriptionId}/resourceGroups/${resourceGroup}/resources?api-version=${this.apiVersion}`
`${this.resourcePath}/${subscriptionId}/resourceGroups/${resourceGroup}/resources?api-version=${this.listByResourceGroupApiVersion}`
)
.then((result: AzureMonitorMetricDefinitionsResponse) => {
return ResponseParser.parseResponseValues(result, 'type', 'type');
@ -195,7 +196,7 @@ export default class AzureMonitorDatasource extends DataSourceWithBackend<AzureM
getResourceNames(subscriptionId: string, resourceGroup: string, metricDefinition: string) {
return this.getResource(
`${this.resourcePath}/${subscriptionId}/resourceGroups/${resourceGroup}/resources?api-version=${this.apiVersion}`
`${this.resourcePath}/${subscriptionId}/resourceGroups/${resourceGroup}/resources?$filter=resourceType eq '${metricDefinition}'&api-version=${this.listByResourceGroupApiVersion}`
).then((result: any) => {
if (!startsWith(metricDefinition, 'Microsoft.Storage/storageAccounts/')) {
return ResponseParser.parseResourceNames(result, metricDefinition);