migration between 8.5 and this
This commit is contained in:
@@ -12,6 +12,7 @@ import {
|
||||
MetricQueryType,
|
||||
MetricEditorMode,
|
||||
VariableQueryType,
|
||||
OldVariableQuery,
|
||||
} from './types';
|
||||
|
||||
describe('migration', () => {
|
||||
@@ -245,4 +246,27 @@ describe('migration', () => {
|
||||
expect(query.ec2Filters).toStrictEqual({ environment: ['$environment'] });
|
||||
});
|
||||
});
|
||||
describe('when OldVariableQuery is used', () => {
|
||||
it('should parse the query', () => {
|
||||
const oldQuery: OldVariableQuery = {
|
||||
queryType: VariableQueryType.EC2InstanceAttributes,
|
||||
namespace: '',
|
||||
region: 'us-east-1',
|
||||
metricName: '',
|
||||
dimensionKey: '',
|
||||
ec2Filters: '{"environment":["$environment"]}',
|
||||
instanceID: '',
|
||||
attributeName: 'rds:db',
|
||||
resourceType: 'elasticloadbalancing:loadbalancer',
|
||||
tags: '{"elasticbeanstalk:environment-name":["myApp-dev","myApp-prod"]}',
|
||||
refId: '',
|
||||
};
|
||||
const query = migrateVariableQuery(oldQuery);
|
||||
expect(query.region).toBe('us-east-1');
|
||||
expect(query.attributeName).toBe('rds:db');
|
||||
expect(query.ec2Filters).toStrictEqual({ environment: ['$environment'] });
|
||||
expect(query.resourceType).toBe('elasticloadbalancing:loadbalancer');
|
||||
expect(query.tags).toStrictEqual({ 'elasticbeanstalk:environment-name': ['myApp-dev', 'myApp-prod'] });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { omit } from 'lodash';
|
||||
|
||||
import { AnnotationQuery, DataQuery } from '@grafana/data';
|
||||
import { getNextRefIdChar } from 'app/core/utils/query';
|
||||
|
||||
|
||||
import {
|
||||
MetricEditorMode,
|
||||
CloudWatchAnnotationQuery,
|
||||
@@ -8,6 +11,7 @@ import {
|
||||
MetricQueryType,
|
||||
VariableQuery,
|
||||
VariableQueryType,
|
||||
OldVariableQuery,
|
||||
} from './types';
|
||||
|
||||
// Migrates a metric query that use more than one statistic into multiple queries
|
||||
@@ -70,10 +74,38 @@ export function migrateCloudWatchQuery(query: CloudWatchMetricsQuery) {
|
||||
}
|
||||
}
|
||||
|
||||
export function migrateVariableQuery(rawQuery: string | VariableQuery): VariableQuery {
|
||||
if (typeof rawQuery !== 'string') {
|
||||
function isVariableQuery(rawQuery: string | VariableQuery | OldVariableQuery): rawQuery is VariableQuery {
|
||||
return typeof rawQuery !== 'string' && typeof rawQuery.ec2Filters !== 'string' && typeof rawQuery.tags !== 'string';
|
||||
}
|
||||
|
||||
export function migrateVariableQuery(rawQuery: string | VariableQuery | OldVariableQuery): VariableQuery {
|
||||
if (isVariableQuery(rawQuery)) {
|
||||
return rawQuery;
|
||||
}
|
||||
|
||||
// rawQuery is OldVariableQuery
|
||||
if (typeof rawQuery !== 'string') {
|
||||
const newQuery: VariableQuery = omit(rawQuery, ['ec2Filters', 'tags']);
|
||||
newQuery.ec2Filters = {};
|
||||
newQuery.tags = {};
|
||||
|
||||
if (rawQuery.ec2Filters !== '') {
|
||||
try {
|
||||
newQuery.ec2Filters = JSON.parse(rawQuery.ec2Filters);
|
||||
} catch {
|
||||
throw new Error(`unable to migrate poorly formed filters: ${rawQuery.ec2Filters}`);
|
||||
}
|
||||
}
|
||||
if (rawQuery.tags !== '') {
|
||||
try {
|
||||
newQuery.tags = JSON.parse(rawQuery.tags);
|
||||
} catch {
|
||||
throw new Error(`unable to migrate poorly formed filters: ${rawQuery.tags}`);
|
||||
}
|
||||
}
|
||||
return newQuery;
|
||||
}
|
||||
|
||||
const newQuery: VariableQuery = {
|
||||
refId: 'CloudWatchVariableQueryEditor-VariableQuery',
|
||||
queryType: VariableQueryType.Regions,
|
||||
|
||||
@@ -391,6 +391,20 @@ export enum VariableQueryType {
|
||||
Statistics = 'statistics',
|
||||
}
|
||||
|
||||
export interface OldVariableQuery extends DataQuery {
|
||||
queryType: VariableQueryType;
|
||||
namespace: string;
|
||||
region: string;
|
||||
metricName: string;
|
||||
dimensionKey: string;
|
||||
dimensionFilters?: Dimensions;
|
||||
ec2Filters: string;
|
||||
instanceID: string;
|
||||
attributeName: string;
|
||||
resourceType: string;
|
||||
tags: string;
|
||||
}
|
||||
|
||||
export interface VariableQuery extends DataQuery {
|
||||
queryType: VariableQueryType;
|
||||
namespace: string;
|
||||
|
||||
Reference in New Issue
Block a user