grafanaTemplateVariables.test.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import createMockDatasource from './__mocks__/datasource';
  2. import { migrateStringQueriesToObjectQueries } from './grafanaTemplateVariableFns';
  3. import { AzureMonitorQuery, AzureQueryType } from './types';
  4. describe('migrateStringQueriesToObjectQueries', () => {
  5. const expectedMigrations: Array<{ input: string; output: AzureMonitorQuery }> = [
  6. {
  7. input: 'Subscriptions()',
  8. output: {
  9. refId: 'A',
  10. queryType: AzureQueryType.GrafanaTemplateVariableFn,
  11. grafanaTemplateVariableFn: { kind: 'SubscriptionsQuery', rawQuery: 'Subscriptions()' },
  12. subscription: 'defaultSubscriptionId',
  13. },
  14. },
  15. {
  16. input: 'ResourceGroups()',
  17. output: {
  18. refId: 'A',
  19. queryType: AzureQueryType.GrafanaTemplateVariableFn,
  20. grafanaTemplateVariableFn: {
  21. kind: 'ResourceGroupsQuery',
  22. rawQuery: 'ResourceGroups()',
  23. subscription: 'defaultSubscriptionId',
  24. },
  25. subscription: 'defaultSubscriptionId',
  26. },
  27. },
  28. {
  29. input: 'ResourceGroups(subId)',
  30. output: {
  31. refId: 'A',
  32. queryType: AzureQueryType.GrafanaTemplateVariableFn,
  33. grafanaTemplateVariableFn: {
  34. kind: 'ResourceGroupsQuery',
  35. rawQuery: 'ResourceGroups(subId)',
  36. subscription: 'subId',
  37. },
  38. subscription: 'defaultSubscriptionId',
  39. },
  40. },
  41. {
  42. input: 'Namespaces(rg)',
  43. output: {
  44. refId: 'A',
  45. queryType: AzureQueryType.GrafanaTemplateVariableFn,
  46. grafanaTemplateVariableFn: {
  47. kind: 'MetricDefinitionsQuery',
  48. rawQuery: 'Namespaces(rg)',
  49. subscription: 'defaultSubscriptionId',
  50. resourceGroup: 'rg',
  51. },
  52. subscription: 'defaultSubscriptionId',
  53. },
  54. },
  55. {
  56. input: 'Namespaces(subId, rg)',
  57. output: {
  58. refId: 'A',
  59. queryType: AzureQueryType.GrafanaTemplateVariableFn,
  60. grafanaTemplateVariableFn: {
  61. kind: 'MetricDefinitionsQuery',
  62. rawQuery: 'Namespaces(subId, rg)',
  63. subscription: 'subId',
  64. resourceGroup: 'rg',
  65. },
  66. subscription: 'defaultSubscriptionId',
  67. },
  68. },
  69. {
  70. input: 'ResourceNames(rg, md)',
  71. output: {
  72. refId: 'A',
  73. queryType: AzureQueryType.GrafanaTemplateVariableFn,
  74. grafanaTemplateVariableFn: {
  75. kind: 'ResourceNamesQuery',
  76. rawQuery: 'ResourceNames(rg, md)',
  77. subscription: 'defaultSubscriptionId',
  78. resourceGroup: 'rg',
  79. metricDefinition: 'md',
  80. },
  81. subscription: 'defaultSubscriptionId',
  82. },
  83. },
  84. {
  85. input: 'ResourceNames(subId, rg, md)',
  86. output: {
  87. refId: 'A',
  88. queryType: AzureQueryType.GrafanaTemplateVariableFn,
  89. grafanaTemplateVariableFn: {
  90. kind: 'ResourceNamesQuery',
  91. rawQuery: 'ResourceNames(subId, rg, md)',
  92. subscription: 'subId',
  93. resourceGroup: 'rg',
  94. metricDefinition: 'md',
  95. },
  96. subscription: 'defaultSubscriptionId',
  97. },
  98. },
  99. {
  100. input: 'MetricNamespace(rg, md, rn)',
  101. output: {
  102. refId: 'A',
  103. queryType: AzureQueryType.GrafanaTemplateVariableFn,
  104. grafanaTemplateVariableFn: {
  105. kind: 'MetricNamespaceQuery',
  106. rawQuery: 'MetricNamespace(rg, md, rn)',
  107. subscription: 'defaultSubscriptionId',
  108. resourceGroup: 'rg',
  109. metricDefinition: 'md',
  110. resourceName: 'rn',
  111. },
  112. subscription: 'defaultSubscriptionId',
  113. },
  114. },
  115. {
  116. input: 'MetricNamespace(subId, rg, md, rn)',
  117. output: {
  118. refId: 'A',
  119. queryType: AzureQueryType.GrafanaTemplateVariableFn,
  120. grafanaTemplateVariableFn: {
  121. kind: 'MetricNamespaceQuery',
  122. rawQuery: 'MetricNamespace(subId, rg, md, rn)',
  123. subscription: 'subId',
  124. resourceGroup: 'rg',
  125. metricDefinition: 'md',
  126. resourceName: 'rn',
  127. },
  128. subscription: 'defaultSubscriptionId',
  129. },
  130. },
  131. {
  132. input: 'MetricNames(rg, md, rn, mn)',
  133. output: {
  134. refId: 'A',
  135. queryType: AzureQueryType.GrafanaTemplateVariableFn,
  136. grafanaTemplateVariableFn: {
  137. kind: 'MetricNamesQuery',
  138. rawQuery: 'MetricNames(rg, md, rn, mn)',
  139. subscription: 'defaultSubscriptionId',
  140. resourceGroup: 'rg',
  141. metricDefinition: 'md',
  142. resourceName: 'rn',
  143. metricNamespace: 'mn',
  144. },
  145. subscription: 'defaultSubscriptionId',
  146. },
  147. },
  148. {
  149. input: 'MetricNames(subId, rg, md, rn, mn)',
  150. output: {
  151. refId: 'A',
  152. queryType: AzureQueryType.GrafanaTemplateVariableFn,
  153. grafanaTemplateVariableFn: {
  154. kind: 'MetricNamesQuery',
  155. rawQuery: 'MetricNames(subId, rg, md, rn, mn)',
  156. subscription: 'subId',
  157. resourceGroup: 'rg',
  158. metricDefinition: 'md',
  159. resourceName: 'rn',
  160. metricNamespace: 'mn',
  161. },
  162. subscription: 'defaultSubscriptionId',
  163. },
  164. },
  165. {
  166. input: 'AppInsightsMetricNames()',
  167. output: {
  168. refId: 'A',
  169. queryType: AzureQueryType.GrafanaTemplateVariableFn,
  170. grafanaTemplateVariableFn: {
  171. kind: 'AppInsightsMetricNameQuery',
  172. rawQuery: 'AppInsightsMetricNames()',
  173. },
  174. subscription: 'defaultSubscriptionId',
  175. },
  176. },
  177. {
  178. input: 'AppInsightsGroupBys(mn)',
  179. output: {
  180. refId: 'A',
  181. queryType: AzureQueryType.GrafanaTemplateVariableFn,
  182. grafanaTemplateVariableFn: {
  183. kind: 'AppInsightsGroupByQuery',
  184. rawQuery: 'AppInsightsGroupBys(mn)',
  185. metricName: 'mn',
  186. },
  187. subscription: 'defaultSubscriptionId',
  188. },
  189. },
  190. {
  191. input: 'workspaces()',
  192. output: {
  193. refId: 'A',
  194. queryType: AzureQueryType.GrafanaTemplateVariableFn,
  195. grafanaTemplateVariableFn: {
  196. kind: 'WorkspacesQuery',
  197. rawQuery: 'workspaces()',
  198. subscription: 'defaultSubscriptionId',
  199. },
  200. subscription: 'defaultSubscriptionId',
  201. },
  202. },
  203. {
  204. input: 'workspaces(subId)',
  205. output: {
  206. refId: 'A',
  207. queryType: AzureQueryType.GrafanaTemplateVariableFn,
  208. grafanaTemplateVariableFn: {
  209. kind: 'WorkspacesQuery',
  210. rawQuery: 'workspaces(subId)',
  211. subscription: 'subId',
  212. },
  213. subscription: 'defaultSubscriptionId',
  214. },
  215. },
  216. {
  217. input: 'some kind of kql query',
  218. output: {
  219. refId: 'A',
  220. queryType: AzureQueryType.LogAnalytics,
  221. azureLogAnalytics: {
  222. query: 'some kind of kql query',
  223. resource: '',
  224. },
  225. subscription: 'defaultSubscriptionId',
  226. },
  227. },
  228. ];
  229. it('successfully converts all old string queries into formatted query objects', async () => {
  230. return expectedMigrations.map(async ({ input, output }) => {
  231. const datasource = createMockDatasource({
  232. azureMonitorDatasource: {
  233. defaultSubscriptionId: 'defaultSubscriptionId',
  234. },
  235. });
  236. const actual = await migrateStringQueriesToObjectQueries(input, { datasource });
  237. expect(actual).toEqual(output);
  238. });
  239. });
  240. });