mirror of
https://github.com/ansible/awx.git
synced 2026-01-18 13:11:19 -03:30
Allow space on host filter for smart inventory
Allow space on host filter for smart inventory Fix: https://github.com/ansible/tower/issues/4874
This commit is contained in:
parent
51abbb9464
commit
a60abe38a8
@ -17,13 +17,19 @@ export function toSearchParams(string = '') {
|
||||
});
|
||||
}
|
||||
|
||||
const unescapeString = v => {
|
||||
// This is necessary when editing a string that was initially
|
||||
// escaped to allow white space
|
||||
return v.replace(/"/g, '');
|
||||
};
|
||||
|
||||
return orArr
|
||||
.join(' and ')
|
||||
.split(/ and | or /)
|
||||
.map(s => s.split('='))
|
||||
.reduce((searchParams, [k, v]) => {
|
||||
const key = decodeURIComponent(k);
|
||||
const value = decodeURIComponent(v);
|
||||
const value = decodeURIComponent(unescapeString(v));
|
||||
if (searchParams[key] === undefined) {
|
||||
searchParams[key] = value;
|
||||
} else if (Array.isArray(searchParams[key])) {
|
||||
@ -61,6 +67,27 @@ export function toQueryString(config, searchParams = {}) {
|
||||
.join('&');
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape a string with double quote in case there was a white space
|
||||
* @param {string} value A string to be parsed
|
||||
* @return {string} string
|
||||
*/
|
||||
const escapeString = value => {
|
||||
if (verifySpace(value)) {
|
||||
return `"${value}"`;
|
||||
}
|
||||
return value;
|
||||
};
|
||||
|
||||
/**
|
||||
* Verify whether a string has white spaces
|
||||
* @param {string} value A string to be parsed
|
||||
* @return {bool} true if a string has white spaces
|
||||
*/
|
||||
const verifySpace = value => {
|
||||
return value.trim().indexOf(' ') >= 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert params object to host filter string
|
||||
* @param {object} searchParams A string or array of strings keyed by query param key
|
||||
@ -71,9 +98,9 @@ export function toHostFilter(searchParams = {}) {
|
||||
.sort()
|
||||
.flatMap(key => {
|
||||
if (Array.isArray(searchParams[key])) {
|
||||
return searchParams[key].map(val => `${key}=${val}`);
|
||||
return searchParams[key].map(val => `${key}=${escapeString(val)}`);
|
||||
}
|
||||
return `${key}=${searchParams[key]}`;
|
||||
return `${key}=${escapeString(searchParams[key])}`;
|
||||
});
|
||||
|
||||
const filteredSearchParams = flattenSearchParams.filter(
|
||||
|
||||
@ -104,6 +104,18 @@ describe('toHostFilter', () => {
|
||||
);
|
||||
});
|
||||
|
||||
test('should return a host filter with escaped string', () => {
|
||||
const object = {
|
||||
or__description__contains: 'bar biz',
|
||||
enabled: 'true',
|
||||
name__contains: 'x',
|
||||
or__name: 'foo',
|
||||
};
|
||||
expect(toHostFilter(object)).toEqual(
|
||||
'enabled=true and name__contains=x or description__contains="bar biz" or name=foo'
|
||||
);
|
||||
});
|
||||
|
||||
test('should return a host filter with and conditional', () => {
|
||||
const object = {
|
||||
enabled: 'true',
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user