mirror of
https://github.com/ansible/awx.git
synced 2026-05-10 19:07:36 -02: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:
@@ -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
|
return orArr
|
||||||
.join(' and ')
|
.join(' and ')
|
||||||
.split(/ and | or /)
|
.split(/ and | or /)
|
||||||
.map(s => s.split('='))
|
.map(s => s.split('='))
|
||||||
.reduce((searchParams, [k, v]) => {
|
.reduce((searchParams, [k, v]) => {
|
||||||
const key = decodeURIComponent(k);
|
const key = decodeURIComponent(k);
|
||||||
const value = decodeURIComponent(v);
|
const value = decodeURIComponent(unescapeString(v));
|
||||||
if (searchParams[key] === undefined) {
|
if (searchParams[key] === undefined) {
|
||||||
searchParams[key] = value;
|
searchParams[key] = value;
|
||||||
} else if (Array.isArray(searchParams[key])) {
|
} else if (Array.isArray(searchParams[key])) {
|
||||||
@@ -61,6 +67,27 @@ export function toQueryString(config, searchParams = {}) {
|
|||||||
.join('&');
|
.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
|
* Convert params object to host filter string
|
||||||
* @param {object} searchParams A string or array of strings keyed by query param key
|
* @param {object} searchParams A string or array of strings keyed by query param key
|
||||||
@@ -71,9 +98,9 @@ export function toHostFilter(searchParams = {}) {
|
|||||||
.sort()
|
.sort()
|
||||||
.flatMap(key => {
|
.flatMap(key => {
|
||||||
if (Array.isArray(searchParams[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(
|
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', () => {
|
test('should return a host filter with and conditional', () => {
|
||||||
const object = {
|
const object = {
|
||||||
enabled: 'true',
|
enabled: 'true',
|
||||||
|
|||||||
Reference in New Issue
Block a user