mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
AzureMonitor: Filter list of resources by resourceType (#43522)
This commit is contained in:
committed by
GitHub
parent
1a0f5595c3
commit
de4ac8ae59
@@ -1,10 +1,10 @@
|
|||||||
import AzureMonitorDatasource from '../datasource';
|
|
||||||
|
|
||||||
import { TemplateSrv } from 'app/features/templating/template_srv';
|
|
||||||
import { DataSourceInstanceSettings } from '@grafana/data';
|
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 createMockQuery from '../__mocks__/query';
|
||||||
import { singleVariable, subscriptionsVariable } from '../__mocks__/variables';
|
import { singleVariable, subscriptionsVariable } from '../__mocks__/variables';
|
||||||
|
import AzureMonitorDatasource from '../datasource';
|
||||||
|
import { AzureDataSourceJsonData, AzureQueryType, DatasourceValidationResult } from '../types';
|
||||||
|
|
||||||
const templateSrv = new TemplateSrv();
|
const templateSrv = new TemplateSrv();
|
||||||
|
|
||||||
@@ -163,7 +163,7 @@ describe('AzureMonitorDatasource', () => {
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
ctx.ds.azureMonitorDatasource.getResource = jest.fn().mockImplementation((path: string) => {
|
ctx.ds.azureMonitorDatasource.getResource = jest.fn().mockImplementation((path: string) => {
|
||||||
const basePath = 'azuremonitor/subscriptions/9935389e-9122-4ef9-95f9-1513dd24753f/resourceGroups';
|
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);
|
return Promise.resolve(response);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -192,6 +192,16 @@ describe('AzureMonitorDatasource', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('When performing getResourceNames', () => {
|
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', () => {
|
describe('and there are no special cases', () => {
|
||||||
const response = {
|
const response = {
|
||||||
value: [
|
value: [
|
||||||
@@ -200,23 +210,25 @@ describe('AzureMonitorDatasource', () => {
|
|||||||
type: 'microsoft.insights/alertrules',
|
type: 'microsoft.insights/alertrules',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'nodeapp',
|
name: resourceGroup,
|
||||||
type: 'microsoft.insights/components',
|
type: metricDefinition,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
ctx.ds.azureMonitorDatasource.getResource = jest.fn().mockImplementation((path: string) => {
|
ctx.ds.azureMonitorDatasource.getResource = jest.fn().mockImplementation((path: string) => {
|
||||||
const basePath = 'azuremonitor/subscriptions/9935389e-9122-4ef9-95f9-1513dd24753f/resourceGroups';
|
const basePath = `azuremonitor/subscriptions/${subscription}/resourceGroups`;
|
||||||
expect(path).toBe(basePath + '/nodeapp/resources?api-version=2018-01-01');
|
expect(path).toBe(
|
||||||
|
`${basePath}/${resourceGroup}/resources?$filter=resourceType eq '${metricDefinition}'&api-version=2021-04-01`
|
||||||
|
);
|
||||||
return Promise.resolve(response);
|
return Promise.resolve(response);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return list of Resource Names', () => {
|
it('should return list of Resource Names', () => {
|
||||||
return ctx.ds
|
return ctx.ds
|
||||||
.getResourceNames('9935389e-9122-4ef9-95f9-1513dd24753f', 'nodeapp', 'microsoft.insights/components')
|
.getResourceNames(subscription, resourceGroup, metricDefinition)
|
||||||
.then((results: Array<{ text: string; value: string }>) => {
|
.then((results: Array<{ text: string; value: string }>) => {
|
||||||
expect(results.length).toEqual(1);
|
expect(results.length).toEqual(1);
|
||||||
expect(results[0].text).toEqual('nodeapp');
|
expect(results[0].text).toEqual('nodeapp');
|
||||||
@@ -225,8 +237,9 @@ describe('AzureMonitorDatasource', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should return ignore letter case', () => {
|
it('should return ignore letter case', () => {
|
||||||
|
metricDefinition = 'microsoft.insights/Components';
|
||||||
return ctx.ds
|
return ctx.ds
|
||||||
.getResourceNames('9935389e-9122-4ef9-95f9-1513dd24753f', 'nodeapp', 'microsoft.insights/Components')
|
.getResourceNames(subscription, resourceGroup, metricDefinition)
|
||||||
.then((results: Array<{ text: string; value: string }>) => {
|
.then((results: Array<{ text: string; value: string }>) => {
|
||||||
expect(results.length).toEqual(1);
|
expect(results.length).toEqual(1);
|
||||||
expect(results[0].text).toEqual('nodeapp');
|
expect(results[0].text).toEqual('nodeapp');
|
||||||
@@ -251,19 +264,19 @@ describe('AzureMonitorDatasource', () => {
|
|||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
ctx.ds.azureMonitorDatasource.getResource = jest.fn().mockImplementation((path: string) => {
|
ctx.ds.azureMonitorDatasource.getResource = jest.fn().mockImplementation((path: string) => {
|
||||||
const basePath = 'azuremonitor/subscriptions/9935389e-9122-4ef9-95f9-1513dd24753f/resourceGroups';
|
const basePath = `azuremonitor/subscriptions/${subscription}/resourceGroups`;
|
||||||
expect(path).toBe(basePath + '/nodeapp/resources?api-version=2018-01-01');
|
expect(path).toBe(
|
||||||
|
basePath +
|
||||||
|
`/${resourceGroup}/resources?$filter=resourceType eq '${metricDefinition}'&api-version=2021-04-01`
|
||||||
|
);
|
||||||
return Promise.resolve(response);
|
return Promise.resolve(response);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return list of Resource Names', () => {
|
it('should return list of Resource Names', () => {
|
||||||
|
metricDefinition = 'Microsoft.Storage/storageAccounts/blobServices';
|
||||||
return ctx.ds
|
return ctx.ds
|
||||||
.getResourceNames(
|
.getResourceNames(subscription, resourceGroup, metricDefinition)
|
||||||
'9935389e-9122-4ef9-95f9-1513dd24753f',
|
|
||||||
'nodeapp',
|
|
||||||
'Microsoft.Storage/storageAccounts/blobServices'
|
|
||||||
)
|
|
||||||
.then((results: Array<{ text: string; value: string }>) => {
|
.then((results: Array<{ text: string; value: string }>) => {
|
||||||
expect(results.length).toEqual(1);
|
expect(results.length).toEqual(1);
|
||||||
expect(results[0].text).toEqual('storagetest/default');
|
expect(results[0].text).toEqual('storagetest/default');
|
||||||
|
|||||||
@@ -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 { filter, startsWith } from 'lodash';
|
||||||
import UrlBuilder from './url_builder';
|
|
||||||
import ResponseParser from './response_parser';
|
import { resourceTypeDisplayNames } from '../azureMetadata';
|
||||||
import SupportedNamespaces from './supported_namespaces';
|
import { getAuthType, getAzureCloud, getAzurePortalUrl } from '../credentials';
|
||||||
import TimegrainConverter from '../time_grain_converter';
|
import TimegrainConverter from '../time_grain_converter';
|
||||||
import {
|
import {
|
||||||
AzureMonitorQuery,
|
|
||||||
AzureDataSourceJsonData,
|
AzureDataSourceJsonData,
|
||||||
AzureMonitorMetricDefinitionsResponse,
|
AzureMonitorMetricDefinitionsResponse,
|
||||||
|
AzureMonitorQuery,
|
||||||
AzureMonitorResourceGroupsResponse,
|
AzureMonitorResourceGroupsResponse,
|
||||||
AzureQueryType,
|
AzureQueryType,
|
||||||
DatasourceValidationResult,
|
DatasourceValidationResult,
|
||||||
} from '../types';
|
} 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 { routeNames } from '../utils/common';
|
||||||
|
import ResponseParser from './response_parser';
|
||||||
|
import SupportedNamespaces from './supported_namespaces';
|
||||||
|
import UrlBuilder from './url_builder';
|
||||||
|
|
||||||
const defaultDropdownValue = 'select';
|
const defaultDropdownValue = 'select';
|
||||||
|
|
||||||
export default class AzureMonitorDatasource extends DataSourceWithBackend<AzureMonitorQuery, AzureDataSourceJsonData> {
|
export default class AzureMonitorDatasource extends DataSourceWithBackend<AzureMonitorQuery, AzureDataSourceJsonData> {
|
||||||
apiVersion = '2018-01-01';
|
apiVersion = '2018-01-01';
|
||||||
apiPreviewVersion = '2017-12-01-preview';
|
apiPreviewVersion = '2017-12-01-preview';
|
||||||
|
listByResourceGroupApiVersion = '2021-04-01';
|
||||||
defaultSubscriptionId?: string;
|
defaultSubscriptionId?: string;
|
||||||
resourcePath: string;
|
resourcePath: string;
|
||||||
azurePortalUrl: string;
|
azurePortalUrl: string;
|
||||||
@@ -134,7 +135,7 @@ export default class AzureMonitorDatasource extends DataSourceWithBackend<AzureM
|
|||||||
|
|
||||||
getResourceGroups(subscriptionId: string) {
|
getResourceGroups(subscriptionId: string) {
|
||||||
return this.getResource(
|
return this.getResource(
|
||||||
`${this.resourcePath}/${subscriptionId}/resourceGroups?api-version=${this.apiVersion}`
|
`${this.resourcePath}/${subscriptionId}/resourceGroups?api-version=${this.listByResourceGroupApiVersion}`
|
||||||
).then((result: AzureMonitorResourceGroupsResponse) => {
|
).then((result: AzureMonitorResourceGroupsResponse) => {
|
||||||
return ResponseParser.parseResponseValues(result, 'name', 'name');
|
return ResponseParser.parseResponseValues(result, 'name', 'name');
|
||||||
});
|
});
|
||||||
@@ -142,7 +143,7 @@ export default class AzureMonitorDatasource extends DataSourceWithBackend<AzureM
|
|||||||
|
|
||||||
getMetricDefinitions(subscriptionId: string, resourceGroup: string) {
|
getMetricDefinitions(subscriptionId: string, resourceGroup: string) {
|
||||||
return this.getResource(
|
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) => {
|
.then((result: AzureMonitorMetricDefinitionsResponse) => {
|
||||||
return ResponseParser.parseResponseValues(result, 'type', 'type');
|
return ResponseParser.parseResponseValues(result, 'type', 'type');
|
||||||
@@ -195,7 +196,7 @@ export default class AzureMonitorDatasource extends DataSourceWithBackend<AzureM
|
|||||||
|
|
||||||
getResourceNames(subscriptionId: string, resourceGroup: string, metricDefinition: string) {
|
getResourceNames(subscriptionId: string, resourceGroup: string, metricDefinition: string) {
|
||||||
return this.getResource(
|
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) => {
|
).then((result: any) => {
|
||||||
if (!startsWith(metricDefinition, 'Microsoft.Storage/storageAccounts/')) {
|
if (!startsWith(metricDefinition, 'Microsoft.Storage/storageAccounts/')) {
|
||||||
return ResponseParser.parseResourceNames(result, metricDefinition);
|
return ResponseParser.parseResourceNames(result, metricDefinition);
|
||||||
|
|||||||
Reference in New Issue
Block a user