From d3a33de5031be518d63e71f366acd857193170e0 Mon Sep 17 00:00:00 2001 From: Isabella Siu Date: Wed, 1 Feb 2023 14:53:13 -0500 Subject: [PATCH] Cloudwatch: Fix log group variable interpolation (#62640) (#62722) (cherry picked from commit 1f09508d8c0ea7ef4725aab2089417dc6e883f4e) --- .../datasource/cloudwatch/variables.test.ts | 27 +++++++++++++++++-- .../datasource/cloudwatch/variables.ts | 3 ++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/public/app/plugins/datasource/cloudwatch/variables.test.ts b/public/app/plugins/datasource/cloudwatch/variables.test.ts index 0fc0f282d83..17e3143c56c 100644 --- a/public/app/plugins/datasource/cloudwatch/variables.test.ts +++ b/public/app/plugins/datasource/cloudwatch/variables.test.ts @@ -1,7 +1,12 @@ import { toOption } from '@grafana/data'; import { setupMockedAPI } from './__mocks__/API'; -import { dimensionVariable, labelsVariable, setupMockedDataSource } from './__mocks__/CloudWatchDataSource'; +import { + dimensionVariable, + fieldsVariable, + labelsVariable, + setupMockedDataSource, +} from './__mocks__/CloudWatchDataSource'; import { VariableQuery, VariableQueryType } from './types'; import { CloudWatchVariableSupport } from './variables'; @@ -17,7 +22,7 @@ const defaultQuery: VariableQuery = { refId: '', }; -const mock = setupMockedDataSource({ variables: [labelsVariable, dimensionVariable] }); +const mock = setupMockedDataSource({ variables: [labelsVariable, dimensionVariable, fieldsVariable] }); mock.datasource.api.getRegions = jest.fn().mockResolvedValue([{ label: 'a', value: 'a' }]); mock.datasource.api.getNamespaces = jest.fn().mockResolvedValue([{ label: 'b', value: 'b' }]); mock.datasource.api.getMetrics = jest.fn().mockResolvedValue([{ label: 'c', value: 'c' }]); @@ -28,6 +33,7 @@ const getDimensionValues = jest.fn().mockResolvedValue([{ label: 'e', value: 'e' const getEbsVolumeIds = jest.fn().mockResolvedValue([{ label: 'f', value: 'f' }]); const getEc2InstanceAttribute = jest.fn().mockResolvedValue([{ label: 'g', value: 'g' }]); const getResourceARNs = jest.fn().mockResolvedValue([{ label: 'h', value: 'h' }]); +const describeAllLogGroups = jest.fn().mockResolvedValue(['a', 'b'].map(toOption)); const variables = new CloudWatchVariableSupport(mock.datasource.api); @@ -196,6 +202,11 @@ describe('variables', () => { }); describe('log groups', () => { + beforeEach(() => { + mock.datasource.api.describeAllLogGroups = describeAllLogGroups; + describeAllLogGroups.mockClear(); + }); + it('should call describe log groups', async () => { const result = await variables.execute({ ...defaultQuery, queryType: VariableQueryType.LogGroups }); expect(result).toEqual([ @@ -203,5 +214,17 @@ describe('variables', () => { { text: 'b', value: 'b', expandable: true }, ]); }); + it('should replace variables', async () => { + const query = { + ...defaultQuery, + queryType: VariableQueryType.LogGroups, + logGroupPrefix: '$fields', + }; + await variables.execute(query); + expect(describeAllLogGroups).toBeCalledWith({ + region: query.region, + logGroupNamePrefix: 'templatedField', + }); + }); }); }); diff --git a/public/app/plugins/datasource/cloudwatch/variables.ts b/public/app/plugins/datasource/cloudwatch/variables.ts index 25629c87ff3..a3b88cfc6e7 100644 --- a/public/app/plugins/datasource/cloudwatch/variables.ts +++ b/public/app/plugins/datasource/cloudwatch/variables.ts @@ -62,10 +62,11 @@ export class CloudWatchVariableSupport extends CustomVariableSupport logGroups.map(selectableValueToMetricFindOption)); }