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

(cherry picked from commit 9cbc872f22)

Co-authored-by: Andres Martinez Gotor <andres.martinez@grafana.com>
This commit is contained in:
Grot (@grafanabot) 2021-11-29 04:25:43 -05:00 committed by GitHub
parent 03f54577a5
commit 4044cc9aeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 2 deletions

View File

@ -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 { from, lastValueFrom, Observable, of } from 'rxjs';
import { map, mergeMap } from 'rxjs/operators'; import { map, mergeMap } from 'rxjs/operators';
import { import {
@ -296,7 +296,9 @@ export default class CloudMonitoringDatasource extends DataSourceWithBackend<
completeFilter.map(({ key, operator, value, condition }: Filter) => [ completeFilter.map(({ key, operator, value, condition }: Filter) => [
this.templateSrv.replace(key, scopedVars || {}), this.templateSrv.replace(key, scopedVars || {}),
operator, 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] : []), ...(condition ? [condition] : []),
]) ])
); );

View File

@ -113,6 +113,22 @@ describe('CloudMonitoringDataSource', () => {
expect(interpolated[2]).toBe('(filtervalue1|filtervalue2)'); 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)');
});
}); });
}); });