Cloudwatch: Fix log group variable interpolation (#62640) (#62722)

(cherry picked from commit 1f09508d8c)
This commit is contained in:
Isabella Siu
2023-02-01 14:53:13 -05:00
committed by GitHub
parent e08bfdfcf6
commit d3a33de503
2 changed files with 27 additions and 3 deletions
@@ -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',
});
});
});
});
@@ -62,10 +62,11 @@ export class CloudWatchVariableSupport extends CustomVariableSupport<CloudWatchD
}
}
async handleLogGroupsQuery({ region, logGroupPrefix }: VariableQuery) {
const interpolatedPrefix = this.api.templateSrv.replace(logGroupPrefix);
return this.api
.describeAllLogGroups({
region,
logGroupNamePrefix: logGroupPrefix,
logGroupNamePrefix: interpolatedPrefix,
})
.then((logGroups) => logGroups.map(selectableValueToMetricFindOption));
}