From ce495d4e0588859b4ee26fba1da73c075f3f1dba Mon Sep 17 00:00:00 2001 From: Isabella Siu Date: Thu, 21 Apr 2022 13:25:59 -0400 Subject: [PATCH] migration between 8.5 and this --- .../datasource/cloudwatch/migration.test.ts | 24 +++++++++++++ .../datasource/cloudwatch/migrations.ts | 36 +++++++++++++++++-- .../plugins/datasource/cloudwatch/types.ts | 14 ++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/public/app/plugins/datasource/cloudwatch/migration.test.ts b/public/app/plugins/datasource/cloudwatch/migration.test.ts index cfa3ede6c6c..290f62aec14 100644 --- a/public/app/plugins/datasource/cloudwatch/migration.test.ts +++ b/public/app/plugins/datasource/cloudwatch/migration.test.ts @@ -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'] }); + }); + }); }); diff --git a/public/app/plugins/datasource/cloudwatch/migrations.ts b/public/app/plugins/datasource/cloudwatch/migrations.ts index 0f43968f9ce..95a5fb3f7fc 100644 --- a/public/app/plugins/datasource/cloudwatch/migrations.ts +++ b/public/app/plugins/datasource/cloudwatch/migrations.ts @@ -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, diff --git a/public/app/plugins/datasource/cloudwatch/types.ts b/public/app/plugins/datasource/cloudwatch/types.ts index b032bca9a44..b5f1fcd3be7 100644 --- a/public/app/plugins/datasource/cloudwatch/types.ts +++ b/public/app/plugins/datasource/cloudwatch/types.ts @@ -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;