@ -1,5 +1,8 @@
import { get , set } from 'lodash' ;
import { ScopedVars } from '@grafana/data' ;
import { VariableInterpolation } from '@grafana/runtime' ;
import createMockQuery from '../__mocks__/query' ;
import { createTemplateVariables } from '../__mocks__/utils' ;
import { multiVariable } from '../__mocks__/variables' ;
@ -138,11 +141,22 @@ describe('AzureMonitorDatasource', () => {
it ( 'expand template variables in resource groups and names' , ( ) = > {
const resourceGroup = '$rg' ;
const resourceName = '$rn' ;
replace = ( target? : string ) = > {
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 || '' ;
@ -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' , ( ) = > {
const region = '$reg' ;
replace = ( target? : string ) = > {
@ -787,10 +843,29 @@ describe('AzureMonitorDatasource', () => {
} ) ;
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 ( interpolated ) {
interpolated . push ( { value : 'eastus' , match : '$reg' , variableName : 'reg' } ) ;
}
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 ? ? '' ) ;
} ;
const ds = new AzureMonitorDatasource ( ctx . instanceSettings ) ;