variableQueryMigrations.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { omit } from 'lodash';
  2. import { Dimensions, VariableQuery, VariableQueryType, OldVariableQuery, MultiFilters } from '../types';
  3. const jsonVariable = /\${(\w+):json}/g;
  4. function isVariableQuery(rawQuery: string | VariableQuery | OldVariableQuery): rawQuery is VariableQuery {
  5. return typeof rawQuery !== 'string' && typeof rawQuery.ec2Filters !== 'string' && typeof rawQuery.tags !== 'string';
  6. }
  7. function migrateMultiFilters(oldFilters: string): MultiFilters {
  8. const tempFilters = oldFilters.replace(jsonVariable, '"$$$1"');
  9. const parsedFilters: Dimensions = JSON.parse(tempFilters);
  10. const newFilters: MultiFilters = {};
  11. // if the old filter was {key:value} transform it to {key:[value]}
  12. Object.keys(parsedFilters).forEach((key) => {
  13. const value = parsedFilters[key];
  14. if (typeof value === 'string') {
  15. newFilters[key] = [value];
  16. } else if (value !== undefined) {
  17. newFilters[key] = value;
  18. }
  19. });
  20. return newFilters;
  21. }
  22. export function migrateVariableQuery(rawQuery: string | VariableQuery | OldVariableQuery): VariableQuery {
  23. if (isVariableQuery(rawQuery)) {
  24. return rawQuery;
  25. }
  26. // rawQuery is OldVariableQuery
  27. if (typeof rawQuery !== 'string') {
  28. const newQuery: VariableQuery = omit(rawQuery, ['dimensionFilters', 'ec2Filters', 'tags']);
  29. newQuery.dimensionFilters = {};
  30. newQuery.ec2Filters = {};
  31. newQuery.tags = {};
  32. if (rawQuery.dimensionFilters !== '' && rawQuery.ec2Filters !== '[]') {
  33. const tempFilters = rawQuery.dimensionFilters.replace(jsonVariable, '"$$$1"');
  34. try {
  35. newQuery.dimensionFilters = JSON.parse(tempFilters);
  36. } catch {
  37. throw new Error(`unable to migrate poorly formed filters: ${rawQuery.dimensionFilters}`);
  38. }
  39. }
  40. if (rawQuery.ec2Filters !== '' && rawQuery.ec2Filters !== '[]') {
  41. try {
  42. newQuery.ec2Filters = migrateMultiFilters(rawQuery.ec2Filters);
  43. } catch {
  44. throw new Error(`unable to migrate poorly formed filters: ${rawQuery.ec2Filters}`);
  45. }
  46. }
  47. if (rawQuery.tags !== '' && rawQuery.tags !== '[]') {
  48. try {
  49. newQuery.tags = migrateMultiFilters(rawQuery.tags);
  50. } catch {
  51. throw new Error(`unable to migrate poorly formed filters: ${rawQuery.tags}`);
  52. }
  53. }
  54. return newQuery;
  55. }
  56. const newQuery: VariableQuery = {
  57. refId: 'CloudWatchVariableQueryEditor-VariableQuery',
  58. queryType: VariableQueryType.Regions,
  59. namespace: '',
  60. region: '',
  61. metricName: '',
  62. dimensionKey: '',
  63. dimensionFilters: {},
  64. ec2Filters: {},
  65. instanceID: '',
  66. attributeName: '',
  67. resourceType: '',
  68. tags: {},
  69. };
  70. if (rawQuery === '') {
  71. return newQuery;
  72. }
  73. if (rawQuery.match(/^regions\(\)/)) {
  74. return newQuery;
  75. }
  76. if (rawQuery.match(/^namespaces\(\)/)) {
  77. newQuery.queryType = VariableQueryType.Namespaces;
  78. return newQuery;
  79. }
  80. const metricNameQuery = rawQuery.match(/^metrics\(([^\)]+?)(,\s?([^,]+?))?\)/);
  81. if (metricNameQuery) {
  82. newQuery.queryType = VariableQueryType.Metrics;
  83. newQuery.namespace = metricNameQuery[1];
  84. newQuery.region = metricNameQuery[3] || '';
  85. return newQuery;
  86. }
  87. const dimensionKeysQuery = rawQuery.match(/^dimension_keys\(([^\)]+?)(,\s?([^,]+?))?\)/);
  88. if (dimensionKeysQuery) {
  89. newQuery.queryType = VariableQueryType.DimensionKeys;
  90. newQuery.namespace = dimensionKeysQuery[1];
  91. newQuery.region = dimensionKeysQuery[3] || '';
  92. return newQuery;
  93. }
  94. const dimensionValuesQuery = rawQuery.match(
  95. /^dimension_values\(([^,]+?),\s?([^,]+?),\s?([^,]+?),\s?([^,]+?)(,\s?(.+))?\)/
  96. );
  97. if (dimensionValuesQuery) {
  98. newQuery.queryType = VariableQueryType.DimensionValues;
  99. newQuery.region = dimensionValuesQuery[1];
  100. newQuery.namespace = dimensionValuesQuery[2];
  101. newQuery.metricName = dimensionValuesQuery[3];
  102. newQuery.dimensionKey = dimensionValuesQuery[4];
  103. newQuery.dimensionFilters = {};
  104. if (!!dimensionValuesQuery[6] && dimensionValuesQuery[6] !== '[]') {
  105. const tempFilters = dimensionValuesQuery[6].replace(jsonVariable, '"$$$1"');
  106. try {
  107. newQuery.dimensionFilters = JSON.parse(tempFilters);
  108. } catch {
  109. throw new Error(`unable to migrate poorly formed filters: ${dimensionValuesQuery[6]}`);
  110. }
  111. }
  112. return newQuery;
  113. }
  114. const ebsVolumeIdsQuery = rawQuery.match(/^ebs_volume_ids\(([^,]+?),\s?([^,]+?)\)/);
  115. if (ebsVolumeIdsQuery) {
  116. newQuery.queryType = VariableQueryType.EBSVolumeIDs;
  117. newQuery.region = ebsVolumeIdsQuery[1];
  118. newQuery.instanceID = ebsVolumeIdsQuery[2];
  119. return newQuery;
  120. }
  121. const ec2InstanceAttributeQuery = rawQuery.match(/^ec2_instance_attribute\(([^,]+?),\s?([^,]+?),\s?(.+?)\)/);
  122. if (ec2InstanceAttributeQuery) {
  123. newQuery.queryType = VariableQueryType.EC2InstanceAttributes;
  124. newQuery.region = ec2InstanceAttributeQuery[1];
  125. newQuery.attributeName = ec2InstanceAttributeQuery[2];
  126. if (ec2InstanceAttributeQuery[3] && ec2InstanceAttributeQuery[3] !== '[]') {
  127. try {
  128. newQuery.ec2Filters = migrateMultiFilters(ec2InstanceAttributeQuery[3]);
  129. } catch {
  130. throw new Error(`unable to migrate poorly formed filters: ${ec2InstanceAttributeQuery[3]}`);
  131. }
  132. }
  133. return newQuery;
  134. }
  135. const resourceARNsQuery = rawQuery.match(/^resource_arns\(([^,]+?),\s?([^,]+?),\s?(.+?)\)/);
  136. if (resourceARNsQuery) {
  137. newQuery.queryType = VariableQueryType.ResourceArns;
  138. newQuery.region = resourceARNsQuery[1];
  139. newQuery.resourceType = resourceARNsQuery[2];
  140. if (resourceARNsQuery[3] && resourceARNsQuery[3] !== '[]') {
  141. try {
  142. newQuery.tags = migrateMultiFilters(resourceARNsQuery[3]);
  143. } catch {
  144. throw new Error(`unable to migrate poorly formed filters: ${resourceARNsQuery[3]}`);
  145. }
  146. }
  147. return newQuery;
  148. }
  149. const statsQuery = rawQuery.match(/^statistics\(\)/);
  150. if (statsQuery) {
  151. newQuery.queryType = VariableQueryType.Statistics;
  152. return newQuery;
  153. }
  154. throw new Error('unable to parse old variable query');
  155. }