diff --git a/public/app/plugins/datasource/cloudwatch/datasource.js b/public/app/plugins/datasource/cloudwatch/datasource.js index dc57c41b94d..60c7e167a06 100644 --- a/public/app/plugins/datasource/cloudwatch/datasource.js +++ b/public/app/plugins/datasource/cloudwatch/datasource.js @@ -4,9 +4,10 @@ define([ 'moment', 'app/core/utils/datemath', 'app/core/utils/kbn', + 'app/features/templating/variable', './annotation_query', ], -function (angular, _, moment, dateMath, kbn, CloudWatchAnnotationQuery) { +function (angular, _, moment, dateMath, kbn, templatingVariable, CloudWatchAnnotationQuery) { 'use strict'; /** @ngInject */ @@ -390,7 +391,7 @@ function (angular, _, moment, dateMath, kbn, CloudWatchAnnotationQuery) { }); } - this.getExpandedVariables = function(target, dimensionKey, variable) { + this.getExpandedVariables = function(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) @@ -403,15 +404,13 @@ function (angular, _, moment, dateMath, kbn, CloudWatchAnnotationQuery) { }) .map(function(v) { var t = angular.copy(target); - t.dimensions[dimensionKey] = v.value; + var scopedVar = {}; + scopedVar[variable.name] = v; + t.dimensions[dimensionKey] = templateSrv.replace(t.dimensions[dimensionKey], scopedVar); return t; }).value(); }; - this.containsVariable = function (str, variableName) { - return str.indexOf('$' + variableName) !== -1; - }; - this.expandTemplateVariable = function(targets, scopedVars, templateSrv) { var self = this; return _.chain(targets) @@ -421,10 +420,13 @@ function (angular, _, moment, dateMath, kbn, CloudWatchAnnotationQuery) { }); if (dimensionKey) { - var variable = _.find(templateSrv.variables, function(variable) { - return self.containsVariable(target.dimensions[dimensionKey], variable.name); + var multiVariable = _.find(templateSrv.variables, function(variable) { + return templatingVariable.containsVariable(target.dimensions[dimensionKey], variable.name) && variable.multi; }); - return self.getExpandedVariables(target, dimensionKey, variable); + var variable = _.find(templateSrv.variables, function(variable) { + return templatingVariable.containsVariable(target.dimensions[dimensionKey], variable.name); + }); + return self.getExpandedVariables(target, dimensionKey, multiVariable || variable, templateSrv); } else { return [target]; } diff --git a/public/app/plugins/datasource/cloudwatch/specs/datasource_specs.ts b/public/app/plugins/datasource/cloudwatch/specs/datasource_specs.ts index 93dfa24036d..afc0f4a5962 100644 --- a/public/app/plugins/datasource/cloudwatch/specs/datasource_specs.ts +++ b/public/app/plugins/datasource/cloudwatch/specs/datasource_specs.ts @@ -134,11 +134,18 @@ describe('CloudWatchDatasource', function() { { name: 'instance_id', options: [ - { value: 'i-23456789', selected: false }, - { value: 'i-34567890', selected: true } + { text: 'i-23456789', value: 'i-23456789', selected: false }, + { text: 'i-34567890', value: 'i-34567890', selected: true } ] } ], + replace: function (target, scopedVars) { + if (target === '$instance_id' && scopedVars['instance_id']['text'] === 'i-34567890') { + return 'i-34567890'; + } else { + return ''; + } + }, getVariableName: function (e) { return 'instance_id'; }, variableExists: function (e) { return true; }, containsVariable: function (str, variableName) { return str.indexOf('$' + variableName) !== -1; }