From 4e731409637a1bd04278fb137ce3e7e5b080a627 Mon Sep 17 00:00:00 2001 From: Erik Sundell Date: Wed, 8 Jan 2020 12:24:10 +0100 Subject: [PATCH] CloudWatch: dimension_values templating fix (#21401) * Handle dimension value if passed as array * Break out dimension value loading into its own func (cherry picked from commit cf2dd5182796cf4b7d38701b32153b7d02445b9b) --- pkg/tsdb/cloudwatch/metric_find_query.go | 7 ++++++ .../cloudwatch/components/QueryEditor.tsx | 23 +++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/pkg/tsdb/cloudwatch/metric_find_query.go b/pkg/tsdb/cloudwatch/metric_find_query.go index 13f00fda941..8064d699f47 100644 --- a/pkg/tsdb/cloudwatch/metric_find_query.go +++ b/pkg/tsdb/cloudwatch/metric_find_query.go @@ -417,6 +417,13 @@ func (e *CloudWatchExecutor) handleGetDimensionValues(ctx context.Context, param Name: aws.String(k), Value: aws.String(vv), }) + } else if vv, ok := v.([]interface{}); ok { + for _, v := range vv { + dimensions = append(dimensions, &cloudwatch.DimensionFilter{ + Name: aws.String(k), + Value: aws.String(v.(string)), + }) + } } } diff --git a/public/app/plugins/datasource/cloudwatch/components/QueryEditor.tsx b/public/app/plugins/datasource/cloudwatch/components/QueryEditor.tsx index ced336219b0..64a9d713525 100644 --- a/public/app/plugins/datasource/cloudwatch/components/QueryEditor.tsx +++ b/public/app/plugins/datasource/cloudwatch/components/QueryEditor.tsx @@ -106,6 +106,21 @@ export class QueryEditor extends PureComponent { onRunQuery(); } + // Load dimension values based on current selected dimensions. + // Remove the new dimension key and all dimensions that has a wildcard as selected value + loadDimensionValues = (newKey: string) => { + const { datasource, query } = this.props; + const { [newKey]: value, ...dim } = query.dimensions; + const newDimensions = Object.entries(dim).reduce( + (result, [key, value]) => (value === '*' ? result : { ...result, [key]: value }), + {} + ); + return datasource + .getDimensionValues(query.region, query.namespace, query.metricName, newKey, newDimensions) + .then(values => (values.length ? [{ value: '*', text: '*', label: '*' }, ...values] : values)) + .then(this.appendTemplateVariables); + }; + render() { const { query, datasource, onChange, onRunQuery, data } = this.props; const { regions, namespaces, variableOptionGroup: variableOptionGroup, showMeta } = this.state; @@ -157,13 +172,7 @@ export class QueryEditor extends PureComponent { loadKeys={() => datasource.getDimensionKeys(query.namespace, query.region).then(this.appendTemplateVariables) } - loadValues={newKey => { - const { [newKey]: value, ...newDimensions } = query.dimensions; - return datasource - .getDimensionValues(query.region, query.namespace, query.metricName, newKey, newDimensions) - .then(values => (values.length ? [{ value: '*', text: '*', label: '*' }, ...values] : values)) - .then(this.appendTemplateVariables); - }} + loadValues={this.loadDimensionValues} />