fix(striker-ui-api): filter /host with host type(s)

main
Tsu-ba-me 2 years ago
parent 11aa7f548f
commit 271b33eaf7
  1. 2
      striker-ui-api/src/lib/buildCondition.ts
  2. 21
      striker-ui-api/src/lib/request_handlers/host/getHost.ts
  3. 1
      striker-ui-api/src/types/HostOverview.d.ts

@ -22,7 +22,7 @@ const buildIDCondition = (
export const buildUnknownIDCondition = ( export const buildUnknownIDCondition = (
keys: unknown, keys: unknown,
conditionPrefix: string, conditionPrefix: string,
{ onFallback }: { onFallback?: () => string }, { onFallback }: { onFallback?: () => string } = {},
): { after: string; before: string[] } => { ): { after: string; before: string[] } => {
const before = sanitize(keys, 'string[]', { const before = sanitize(keys, 'string[]', {
modifierType: 'sql', modifierType: 'sql',

@ -1,4 +1,5 @@
import { getLocalHostUUID } from '../../accessModule'; import { getLocalHostUUID } from '../../accessModule';
import { buildUnknownIDCondition } from '../../buildCondition';
import buildGetRequestHandler from '../buildGetRequestHandler'; import buildGetRequestHandler from '../buildGetRequestHandler';
import { buildQueryHostDetail } from './buildQueryHostDetail'; import { buildQueryHostDetail } from './buildQueryHostDetail';
import { buildQueryResultReducer } from '../../buildQueryResultModifier'; import { buildQueryResultReducer } from '../../buildQueryResultModifier';
@ -7,22 +8,36 @@ import { getShortHostName } from '../../getShortHostName';
import { sanitize } from '../../sanitize'; import { sanitize } from '../../sanitize';
export const getHost = buildGetRequestHandler((request, buildQueryOptions) => { export const getHost = buildGetRequestHandler((request, buildQueryOptions) => {
const { hostUUIDs } = request.query; const { hostUUIDs, types: hostTypes } = request.query;
const localHostUUID: string = getLocalHostUUID(); const localHostUUID: string = getLocalHostUUID();
const { after: typeCondition } = buildUnknownIDCondition(
hostTypes,
'hos.host_type',
);
let condition = '';
if (typeCondition.length > 0) {
condition += `WHERE ${typeCondition}`;
}
let query = ` let query = `
SELECT SELECT
hos.host_name, hos.host_name,
hos.host_type,
hos.host_uuid hos.host_uuid
FROM hosts AS hos;`; FROM hosts AS hos
${condition};`;
let afterQueryReturn: QueryResultModifierFunction | undefined = let afterQueryReturn: QueryResultModifierFunction | undefined =
buildQueryResultReducer<{ [hostUUID: string]: HostOverview }>( buildQueryResultReducer<{ [hostUUID: string]: HostOverview }>(
(previous, [hostName, hostUUID]) => { (previous, [hostName, hostType, hostUUID]) => {
const key = toLocal(hostUUID, localHostUUID); const key = toLocal(hostUUID, localHostUUID);
previous[key] = { previous[key] = {
hostName, hostName,
hostType,
hostUUID, hostUUID,
shortHostName: getShortHostName(hostName), shortHostName: getShortHostName(hostName),
}; };

@ -1,5 +1,6 @@
type HostOverview = { type HostOverview = {
hostName: string; hostName: string;
hostType: string;
hostUUID: string; hostUUID: string;
shortHostName: string; shortHostName: string;
}; };

Loading…
Cancel
Save