Azure: Support more complex variable interpolation (#99284)

Support more complex variable interpolation

- Update test
pull/99592/head
Andreas Christou 5 months ago committed by GitHub
parent 1f19fc8e0f
commit 763f0bac90
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 79
      public/app/plugins/datasource/azuremonitor/azure_monitor/azure_monitor_datasource.test.ts
  2. 25
      public/app/plugins/datasource/azuremonitor/azure_monitor/azure_monitor_datasource.ts

@ -1,5 +1,8 @@
import { get, set } from 'lodash'; import { get, set } from 'lodash';
import { ScopedVars } from '@grafana/data';
import { VariableInterpolation } from '@grafana/runtime';
import createMockQuery from '../__mocks__/query'; import createMockQuery from '../__mocks__/query';
import { createTemplateVariables } from '../__mocks__/utils'; import { createTemplateVariables } from '../__mocks__/utils';
import { multiVariable } from '../__mocks__/variables'; import { multiVariable } from '../__mocks__/variables';
@ -138,11 +141,22 @@ describe('AzureMonitorDatasource', () => {
it('expand template variables in resource groups and names', () => { it('expand template variables in resource groups and names', () => {
const resourceGroup = '$rg'; const resourceGroup = '$rg';
const resourceName = '$rn'; const resourceName = '$rn';
replace = (target?: string) => { replace = (
target?: string,
_scopedVars?: ScopedVars,
_format?: string | Function,
interpolated?: VariableInterpolation[]
) => {
if (target?.includes('$rg')) { if (target?.includes('$rg')) {
if (interpolated) {
interpolated.push({ value: 'rg1,rg2', match: '$rg', variableName: 'rg' });
}
return 'rg1,rg2'; return 'rg1,rg2';
} }
if (target?.includes('$rn')) { if (target?.includes('$rn')) {
if (interpolated) {
interpolated.push({ value: 'rn1,rn2', match: '$rn', variableName: 'rn' });
}
return 'rn1,rn2'; return 'rn1,rn2';
} }
return target || ''; return target || '';
@ -166,6 +180,48 @@ describe('AzureMonitorDatasource', () => {
}); });
}); });
it('expand template variables in more complex resource groups and names', () => {
const resourceGroup = 'test-$rg-testGroup';
const resourceName = 'test-$rn-testResource';
replace = (
target?: string,
_scopedVars?: ScopedVars,
_format?: string | Function,
interpolated?: VariableInterpolation[]
) => {
if (target?.includes('$rg')) {
if (interpolated) {
interpolated.push({ value: 'rg1,rg2', match: '$rg', variableName: 'rg' });
}
return 'rg1,rg2';
}
if (target?.includes('$rn')) {
if (interpolated) {
interpolated.push({ value: 'rn1,rn2', match: '$rn', variableName: 'rn' });
}
return 'rn1,rn2';
}
return target || '';
};
ctx.ds = new AzureMonitorDatasource(ctx.instanceSettings);
const query = createMockQuery({
azureMonitor: {
resources: [{ resourceGroup, resourceName }],
},
});
const templatedQuery = ctx.ds.azureMonitorDatasource.applyTemplateVariables(query, {});
expect(templatedQuery).toMatchObject({
azureMonitor: {
resources: [
{ resourceGroup: 'test-rg1-testGroup', resourceName: 'test-rn1-testResource' },
{ resourceGroup: 'test-rg2-testGroup', resourceName: 'test-rn1-testResource' },
{ resourceGroup: 'test-rg1-testGroup', resourceName: 'test-rn2-testResource' },
{ resourceGroup: 'test-rg2-testGroup', resourceName: 'test-rn2-testResource' },
],
},
});
});
it('expand template variables for a region', () => { it('expand template variables for a region', () => {
const region = '$reg'; const region = '$reg';
replace = (target?: string) => { replace = (target?: string) => {
@ -787,10 +843,29 @@ describe('AzureMonitorDatasource', () => {
}); });
it('should return multiple resources from a template variable', () => { it('should return multiple resources from a template variable', () => {
replace = (target?: string) => { replace = (
target?: string,
_scopedVars?: ScopedVars,
_format?: string | Function,
interpolated?: VariableInterpolation[]
) => {
if (target?.includes('$reg')) { if (target?.includes('$reg')) {
if (interpolated) {
interpolated.push({ value: 'eastus', match: '$reg', variableName: 'reg' });
}
return 'eastus'; return 'eastus';
} }
if (target === `$${multiVariable.id}`) {
if (interpolated) {
interpolated.push({ value: 'foo,bar', match: `$${multiVariable.id}`!, variableName: 'target' });
}
return 'foo,bar';
}
if (interpolated) {
interpolated.push({ value: target ?? '', match: `$${target}`!, variableName: 'target' });
}
return target === `$${multiVariable.id}` ? 'foo,bar' : (target ?? ''); return target === `$${multiVariable.id}` ? 'foo,bar' : (target ?? '');
}; };
const ds = new AzureMonitorDatasource(ctx.instanceSettings); const ds = new AzureMonitorDatasource(ctx.instanceSettings);

@ -2,7 +2,7 @@ import { find, startsWith } from 'lodash';
import { AzureCredentials } from '@grafana/azure-sdk'; import { AzureCredentials } from '@grafana/azure-sdk';
import { ScopedVars } from '@grafana/data'; import { ScopedVars } from '@grafana/data';
import { DataSourceWithBackend, getTemplateSrv, TemplateSrv } from '@grafana/runtime'; import { DataSourceWithBackend, getTemplateSrv, TemplateSrv, VariableInterpolation } from '@grafana/runtime';
import { getCredentials } from '../credentials'; import { getCredentials } from '../credentials';
import TimegrainConverter from '../time_grain_converter'; import TimegrainConverter from '../time_grain_converter';
@ -355,16 +355,23 @@ export default class AzureMonitorDatasource extends DataSourceWithBackend<
const workingQueries: Array<{ [K in keyof T]: string }> = [{ ...query }]; const workingQueries: Array<{ [K in keyof T]: string }> = [{ ...query }];
const keys = Object.keys(query) as Array<keyof T>; const keys = Object.keys(query) as Array<keyof T>;
keys.forEach((key) => { keys.forEach((key) => {
const replaced = this.templateSrv.replace(workingQueries[0][key], scopedVars, 'raw'); const rawValue = workingQueries[0][key];
if (replaced.includes(',')) { let interpolated: VariableInterpolation[] = [];
const multiple = replaced.split(','); const replaced = this.templateSrv.replace(rawValue, scopedVars, 'raw', interpolated);
if (interpolated.length > 0) {
for (const variable of interpolated) {
if (variable.found === false) {
continue;
}
if (variable.value.includes(',')) {
const multiple = variable.value.split(',');
const currentQueries = [...workingQueries]; const currentQueries = [...workingQueries];
multiple.forEach((value, i) => { multiple.forEach((value, i) => {
currentQueries.forEach((q) => { currentQueries.forEach((q) => {
if (i === 0) { if (i === 0) {
q[key] = value; q[key] = rawValue.replace(variable.match, value);
} else { } else {
workingQueries.push({ ...q, [key]: value }); workingQueries.push({ ...q, [key]: rawValue.replace(variable.match, value) });
} }
}); });
}); });
@ -373,6 +380,12 @@ export default class AzureMonitorDatasource extends DataSourceWithBackend<
q[key] = replaced; q[key] = replaced;
}); });
} }
}
} else {
workingQueries.forEach((q) => {
q[key] = replaced;
});
}
}); });
return workingQueries; return workingQueries;

Loading…
Cancel
Save