Commit b2187b70 by Daniel Lee Committed by GitHub

Merge pull request #11990 from mtanda/issue_11925

fix "Cloudwatch panel does not allow arbitrary variable values in query"
parents eaf5b1c1 e65cf5cb
......@@ -374,23 +374,33 @@ export default class CloudWatchDatasource {
getExpandedVariables(target, dimensionKey, variable, templateSrv) {
/* if the all checkbox is marked we should add all values to the targets */
var allSelected = _.find(variable.options, { selected: true, text: 'All' });
return _.chain(variable.options)
.filter(v => {
if (allSelected) {
return v.text !== 'All';
} else {
return v.selected;
}
})
.map(v => {
var t = angular.copy(target);
var scopedVar = {};
scopedVar[variable.name] = v;
t.refId = target.refId + '_' + v.value;
t.dimensions[dimensionKey] = templateSrv.replace(t.dimensions[dimensionKey], scopedVar);
return t;
})
.value();
var selectedVariables = _.filter(variable.options, v => {
if (allSelected) {
return v.text !== 'All';
} else {
return v.selected;
}
});
var currentVariables = !_.isArray(variable.current.value)
? [variable.current]
: variable.current.value.map(v => {
return {
text: v,
value: v,
};
});
let useSelectedVariables =
selectedVariables.some(s => {
return s.value === currentVariables[0].value;
}) || currentVariables[0].value === '$__all';
return (useSelectedVariables ? selectedVariables : currentVariables).map(v => {
var t = angular.copy(target);
var scopedVar = {};
scopedVar[variable.name] = v;
t.refId = target.refId + '_' + v.value;
t.dimensions[dimensionKey] = templateSrv.replace(t.dimensions[dimensionKey], scopedVar);
return t;
});
}
expandTemplateVariable(targets, scopedVars, templateSrv) {
......
......@@ -135,6 +135,10 @@ describe('CloudWatchDatasource', function() {
{ text: 'i-23456789', value: 'i-23456789', selected: false },
{ text: 'i-34567890', value: 'i-34567890', selected: true },
],
current: {
text: 'i-34567890',
value: 'i-34567890',
},
},
],
replace: function(target, scopedVars) {
......@@ -171,6 +175,53 @@ describe('CloudWatchDatasource', function() {
var result = ctx.ds.expandTemplateVariable(targets, {}, templateSrv);
expect(result[0].dimensions.InstanceId).to.be('i-34567890');
});
it('should generate the correct targets by expanding template variables from url', function() {
var templateSrv = {
variables: [
{
name: 'instance_id',
options: [
{ text: 'i-23456789', value: 'i-23456789', selected: false },
{ text: 'i-34567890', value: 'i-34567890', selected: false },
],
current: 'i-45678901',
},
],
replace: function(target, scopedVars) {
if (target === '$instance_id') {
return 'i-45678901';
} else {
return '';
}
},
getVariableName: function(e) {
return 'instance_id';
},
variableExists: function(e) {
return true;
},
containsVariable: function(str, variableName) {
return str.indexOf('$' + variableName) !== -1;
},
};
var targets = [
{
region: 'us-east-1',
namespace: 'AWS/EC2',
metricName: 'CPUUtilization',
dimensions: {
InstanceId: '$instance_id',
},
statistics: ['Average'],
period: 300,
},
];
var result = ctx.ds.expandTemplateVariable(targets, {}, templateSrv);
expect(result[0].dimensions.InstanceId).to.be('i-45678901');
});
});
describe('When query region is "default"', function() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment