mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
AzureMonitor: Fix metric namespace list (#54826)
This commit is contained in:
parent
d88f1c8e35
commit
366129c14e
@ -125,7 +125,7 @@ describe('AzureMonitorDatasource', () => {
|
||||
const expected =
|
||||
basePath +
|
||||
'/providers/microsoft.insights/components/resource1' +
|
||||
'/providers/microsoft.insights/metricNamespaces?region=global&api-version=2017-12-01-preview';
|
||||
'/providers/microsoft.insights/metricNamespaces?api-version=2017-12-01-preview®ion=global';
|
||||
expect(path).toBe(expected);
|
||||
return Promise.resolve(response);
|
||||
});
|
||||
@ -133,10 +133,13 @@ describe('AzureMonitorDatasource', () => {
|
||||
|
||||
it('should return list of Metric Namspaces', () => {
|
||||
return ctx.ds.azureMonitorDatasource
|
||||
.getMetricNamespaces({
|
||||
resourceUri:
|
||||
'/subscriptions/mock-subscription-id/resourceGroups/nodeapp/providers/microsoft.insights/components/resource1',
|
||||
})
|
||||
.getMetricNamespaces(
|
||||
{
|
||||
resourceUri:
|
||||
'/subscriptions/mock-subscription-id/resourceGroups/nodeapp/providers/microsoft.insights/components/resource1',
|
||||
},
|
||||
true
|
||||
)
|
||||
.then((results: Array<{ text: string; value: string }>) => {
|
||||
expect(results.length).toEqual(2);
|
||||
expect(results[0].text).toEqual('Azure.ApplicationInsights');
|
||||
|
@ -196,11 +196,12 @@ export default class AzureMonitorDatasource extends DataSourceWithBackend<AzureM
|
||||
});
|
||||
}
|
||||
|
||||
getMetricNamespaces(query: GetMetricNamespacesQuery) {
|
||||
getMetricNamespaces(query: GetMetricNamespacesQuery, globalRegion: boolean) {
|
||||
const url = UrlBuilder.buildAzureMonitorGetMetricNamespacesUrl(
|
||||
this.resourcePath,
|
||||
this.apiPreviewVersion,
|
||||
this.replaceTemplateVariables(query),
|
||||
globalRegion,
|
||||
this.templateSrv
|
||||
);
|
||||
return this.getResource(url)
|
||||
|
@ -135,10 +135,11 @@ describe('AzureMonitorUrlBuilder', () => {
|
||||
{
|
||||
resourceUri: '/subscriptions/sub/resource-uri/resource',
|
||||
},
|
||||
true,
|
||||
templateSrv
|
||||
);
|
||||
expect(url).toBe(
|
||||
'/subscriptions/sub/resource-uri/resource/providers/microsoft.insights/metricNamespaces?region=global&api-version=2017-05-01-preview'
|
||||
'/subscriptions/sub/resource-uri/resource/providers/microsoft.insights/metricNamespaces?api-version=2017-05-01-preview®ion=global'
|
||||
);
|
||||
});
|
||||
});
|
||||
@ -172,11 +173,12 @@ describe('AzureMonitorUrlBuilder', () => {
|
||||
metricNamespace: 'Microsoft.NetApp/netAppAccounts/capacityPools/volumes',
|
||||
resourceName: 'rn1/rn2/rn3',
|
||||
},
|
||||
true,
|
||||
templateSrv
|
||||
);
|
||||
expect(url).toBe(
|
||||
'/subscriptions/sub1/resourceGroups/rg/providers/Microsoft.NetApp/netAppAccounts/rn1/capacityPools/rn2/volumes/rn3/' +
|
||||
'providers/microsoft.insights/metricNamespaces?region=global&api-version=2017-05-01-preview'
|
||||
'providers/microsoft.insights/metricNamespaces?api-version=2017-05-01-preview®ion=global'
|
||||
);
|
||||
});
|
||||
});
|
||||
@ -192,11 +194,31 @@ describe('AzureMonitorUrlBuilder', () => {
|
||||
metricNamespace: 'Microsoft.Sql/servers/databases',
|
||||
resourceName: 'rn1/rn2',
|
||||
},
|
||||
true,
|
||||
templateSrv
|
||||
);
|
||||
expect(url).toBe(
|
||||
'/subscriptions/sub1/resourceGroups/rg/providers/Microsoft.Sql/servers/rn1/databases/rn2/' +
|
||||
'providers/microsoft.insights/metricNamespaces?region=global&api-version=2017-05-01-preview'
|
||||
'providers/microsoft.insights/metricNamespaces?api-version=2017-05-01-preview®ion=global'
|
||||
);
|
||||
});
|
||||
|
||||
it('should omit global region if specified', () => {
|
||||
const url = UrlBuilder.buildAzureMonitorGetMetricNamespacesUrl(
|
||||
'',
|
||||
'2017-05-01-preview',
|
||||
{
|
||||
subscription: 'sub1',
|
||||
resourceGroup: 'rg',
|
||||
metricNamespace: 'Microsoft.Sql/servers/databases',
|
||||
resourceName: 'rn1/rn2',
|
||||
},
|
||||
false,
|
||||
templateSrv
|
||||
);
|
||||
expect(url).toBe(
|
||||
'/subscriptions/sub1/resourceGroups/rg/providers/Microsoft.Sql/servers/rn1/databases/rn2/' +
|
||||
'providers/microsoft.insights/metricNamespaces?api-version=2017-05-01-preview'
|
||||
);
|
||||
});
|
||||
});
|
||||
@ -212,11 +234,12 @@ describe('AzureMonitorUrlBuilder', () => {
|
||||
metricNamespace: 'Microsoft.Sql/servers',
|
||||
resourceName: 'rn',
|
||||
},
|
||||
true,
|
||||
templateSrv
|
||||
);
|
||||
expect(url).toBe(
|
||||
'/subscriptions/sub1/resourceGroups/rg/providers/Microsoft.Sql/servers/rn/' +
|
||||
'providers/microsoft.insights/metricNamespaces?region=global&api-version=2017-05-01-preview'
|
||||
'providers/microsoft.insights/metricNamespaces?api-version=2017-05-01-preview®ion=global'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
@ -52,6 +52,7 @@ export default class UrlBuilder {
|
||||
baseUrl: string,
|
||||
apiVersion: string,
|
||||
query: GetMetricNamespacesQuery,
|
||||
globalRegion: boolean,
|
||||
templateSrv: TemplateSrv
|
||||
) {
|
||||
let resourceUri: string;
|
||||
@ -68,7 +69,9 @@ export default class UrlBuilder {
|
||||
});
|
||||
}
|
||||
|
||||
return `${baseUrl}${resourceUri}/providers/microsoft.insights/metricNamespaces?region=global&api-version=${apiVersion}`;
|
||||
return `${baseUrl}${resourceUri}/providers/microsoft.insights/metricNamespaces?api-version=${apiVersion}${
|
||||
globalRegion ? '®ion=global' : ''
|
||||
}`;
|
||||
}
|
||||
|
||||
static buildAzureMonitorGetMetricNamesUrl(
|
||||
|
@ -234,4 +234,40 @@ describe('AzureMonitor: metrics dataHooks', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('useMetricNamespaces', () => {
|
||||
const metricNamespacesConfig = {
|
||||
name: 'useMetricNamespaces',
|
||||
hook: useMetricNamespaces,
|
||||
emptyQueryPartial: {
|
||||
resourceGroup: 'rg',
|
||||
resourceName: 'rn',
|
||||
metricNamespace: 'azure/vm',
|
||||
},
|
||||
customProperties: {},
|
||||
expectedOptions: [
|
||||
{ label: 'Compute Virtual Machine', value: 'azure/vmc' },
|
||||
{ label: 'Database NS', value: 'azure/dbns' },
|
||||
{ label: 'azure/vm', value: 'azure/vm' },
|
||||
],
|
||||
};
|
||||
|
||||
it('call getMetricNamespaces without global region', async () => {
|
||||
const query = {
|
||||
...bareQuery,
|
||||
azureMonitor: metricNamespacesConfig.emptyQueryPartial,
|
||||
};
|
||||
const { result, waitForNextUpdate } = renderHook(() =>
|
||||
metricNamespacesConfig.hook(query, datasource, onChange, jest.fn())
|
||||
);
|
||||
await waitForNextUpdate(WAIT_OPTIONS);
|
||||
|
||||
expect(result.current).toEqual(metricNamespacesConfig.expectedOptions);
|
||||
expect(datasource.azureMonitorDatasource.getMetricNamespaces).toHaveBeenCalledWith(
|
||||
expect.objectContaining(metricNamespacesConfig.emptyQueryPartial),
|
||||
// Here, "global" should be false
|
||||
false
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -48,12 +48,15 @@ export const useMetricNamespaces: DataHook = (query, datasource, onChange, setEr
|
||||
return;
|
||||
}
|
||||
|
||||
const results = await datasource.azureMonitorDatasource.getMetricNamespaces({
|
||||
subscription,
|
||||
metricNamespace,
|
||||
resourceGroup,
|
||||
resourceName,
|
||||
});
|
||||
const results = await datasource.azureMonitorDatasource.getMetricNamespaces(
|
||||
{
|
||||
subscription,
|
||||
metricNamespace,
|
||||
resourceGroup,
|
||||
resourceName,
|
||||
},
|
||||
false
|
||||
);
|
||||
const options = formatOptions(results, metricNamespace);
|
||||
|
||||
// Do some cleanup of the query state if need be
|
||||
|
@ -152,7 +152,7 @@ export default class Datasource extends DataSourceWithBackend<AzureMonitorQuery,
|
||||
if (resourceGroup) {
|
||||
url += `/resourceGroups/${resourceGroup};`;
|
||||
}
|
||||
return this.azureMonitorDatasource.getMetricNamespaces({ resourceUri: url });
|
||||
return this.azureMonitorDatasource.getMetricNamespaces({ resourceUri: url }, true);
|
||||
}
|
||||
|
||||
getResourceNames(subscriptionId: string, resourceGroup?: string, metricNamespace?: string) {
|
||||
|
@ -123,7 +123,7 @@ export class VariableSupport extends CustomVariableSupport<DataSource, AzureMoni
|
||||
}
|
||||
|
||||
if (query.kind === 'MetricNamespaceQuery') {
|
||||
return this.datasource.azureMonitorDatasource.getMetricNamespaces(query);
|
||||
return this.datasource.azureMonitorDatasource.getMetricNamespaces(query, true);
|
||||
}
|
||||
|
||||
if (query.kind === 'MetricNamesQuery') {
|
||||
|
Loading…
Reference in New Issue
Block a user