CloudMonitoring: Avoid to escape regexps in filters (#41961) (#42417)

(cherry picked from commit 9cbc872f22)

Co-authored-by: Andres Martinez Gotor <andres.martinez@grafana.com>
pull/42413/head
Grot (@grafanabot) 4 years ago committed by GitHub
parent 03f54577a5
commit 4044cc9aeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      public/app/plugins/datasource/cloud-monitoring/datasource.ts
  2. 16
      public/app/plugins/datasource/cloud-monitoring/specs/datasource.test.ts

@ -1,4 +1,4 @@
import { chunk, flatten, isString } from 'lodash';
import { chunk, flatten, isString, isArray } from 'lodash';
import { from, lastValueFrom, Observable, of } from 'rxjs';
import { map, mergeMap } from 'rxjs/operators';
import {
@ -296,7 +296,9 @@ export default class CloudMonitoringDatasource extends DataSourceWithBackend<
completeFilter.map(({ key, operator, value, condition }: Filter) => [
this.templateSrv.replace(key, scopedVars || {}),
operator,
this.templateSrv.replace(value, scopedVars || {}, 'regex'),
this.templateSrv.replace(value, scopedVars || {}, (value: string | string[]) => {
return isArray(value) && value.length ? `(${value.join('|')})` : value;
}),
...(condition ? [condition] : []),
])
);

@ -113,6 +113,22 @@ describe('CloudMonitoringDataSource', () => {
expect(interpolated[2]).toBe('(filtervalue1|filtervalue2)');
});
it('should not escape a regex', () => {
const templateSrv = initTemplateSrv('/[a-Z]*.html', true);
const { ds } = getTestcontext({ templateSrv });
const interpolated = ds.interpolateFilters(['resource.label.zone', '=~', '[[test]]'], {});
expect(interpolated[2]).toBe('/[a-Z]*.html');
});
it('should not escape an array of regexes but join them as a regex', () => {
const templateSrv = initTemplateSrv(['/[a-Z]*.html', '/foo.html'], true);
const { ds } = getTestcontext({ templateSrv });
const interpolated = ds.interpolateFilters(['resource.label.zone', '=~', '[[test]]'], {});
expect(interpolated[2]).toBe('(/[a-Z]*.html|/foo.html)');
});
});
});

Loading…
Cancel
Save