From 271b33eaf7ed06e1946d6b254e1632129a6d350e Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Fri, 3 Feb 2023 18:13:54 -0500 Subject: [PATCH] fix(striker-ui-api): filter /host with host type(s) --- striker-ui-api/src/lib/buildCondition.ts | 2 +- .../src/lib/request_handlers/host/getHost.ts | 21 ++++++++++++++++--- striker-ui-api/src/types/HostOverview.d.ts | 1 + 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/striker-ui-api/src/lib/buildCondition.ts b/striker-ui-api/src/lib/buildCondition.ts index 3226a80d..b128322e 100644 --- a/striker-ui-api/src/lib/buildCondition.ts +++ b/striker-ui-api/src/lib/buildCondition.ts @@ -22,7 +22,7 @@ const buildIDCondition = ( export const buildUnknownIDCondition = ( keys: unknown, conditionPrefix: string, - { onFallback }: { onFallback?: () => string }, + { onFallback }: { onFallback?: () => string } = {}, ): { after: string; before: string[] } => { const before = sanitize(keys, 'string[]', { modifierType: 'sql', diff --git a/striker-ui-api/src/lib/request_handlers/host/getHost.ts b/striker-ui-api/src/lib/request_handlers/host/getHost.ts index f1173610..8e5b2000 100644 --- a/striker-ui-api/src/lib/request_handlers/host/getHost.ts +++ b/striker-ui-api/src/lib/request_handlers/host/getHost.ts @@ -1,4 +1,5 @@ import { getLocalHostUUID } from '../../accessModule'; +import { buildUnknownIDCondition } from '../../buildCondition'; import buildGetRequestHandler from '../buildGetRequestHandler'; import { buildQueryHostDetail } from './buildQueryHostDetail'; import { buildQueryResultReducer } from '../../buildQueryResultModifier'; @@ -7,22 +8,36 @@ import { getShortHostName } from '../../getShortHostName'; import { sanitize } from '../../sanitize'; export const getHost = buildGetRequestHandler((request, buildQueryOptions) => { - const { hostUUIDs } = request.query; + const { hostUUIDs, types: hostTypes } = request.query; const localHostUUID: string = getLocalHostUUID(); + const { after: typeCondition } = buildUnknownIDCondition( + hostTypes, + 'hos.host_type', + ); + + let condition = ''; + + if (typeCondition.length > 0) { + condition += `WHERE ${typeCondition}`; + } + let query = ` SELECT hos.host_name, + hos.host_type, hos.host_uuid - FROM hosts AS hos;`; + FROM hosts AS hos + ${condition};`; let afterQueryReturn: QueryResultModifierFunction | undefined = buildQueryResultReducer<{ [hostUUID: string]: HostOverview }>( - (previous, [hostName, hostUUID]) => { + (previous, [hostName, hostType, hostUUID]) => { const key = toLocal(hostUUID, localHostUUID); previous[key] = { hostName, + hostType, hostUUID, shortHostName: getShortHostName(hostName), }; diff --git a/striker-ui-api/src/types/HostOverview.d.ts b/striker-ui-api/src/types/HostOverview.d.ts index 660798bd..8e991dd3 100644 --- a/striker-ui-api/src/types/HostOverview.d.ts +++ b/striker-ui-api/src/types/HostOverview.d.ts @@ -1,5 +1,6 @@ type HostOverview = { hostName: string; + hostType: string; hostUUID: string; shortHostName: string; };